clean up portable 128-bit multiply

This commit is contained in:
Javier Smooth
2015-07-27 15:47:26 -07:00
parent 25990016d5
commit b5d19efd6f
2 changed files with 12 additions and 15 deletions
+12
View File
@@ -68,6 +68,17 @@ static inline uint64_t lo_dword(uint64_t val) {
return val & 0xFFFFFFFF;
}
#if defined(__GNUC__) && defined(__x86_64__)
static inline uint64_t mul128(uint64_t a, uint64_t b, uint64_t *high) {
typedef unsigned __int128 uint128_t;
uint128_t res = (uint128_t) a * (uint128_t) b;
*high = (uint64_t) (res >> 64);
return (uint64_t) res;
}
#else
static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* product_hi) {
// multiplier = ab = a * 2^32 + b
// multiplicand = cd = c * 2^32 + d
@@ -93,6 +104,7 @@ static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64
return product_lo;
}
#endif
static inline uint64_t div_with_reminder(uint64_t dividend, uint32_t divisor, uint32_t* remainder) {
dividend |= ((uint64_t)*remainder) << 32;
-15
View File
@@ -45,25 +45,10 @@ namespace cryptonote {
using std::uint64_t;
using std::vector;
#if defined(_MSC_VER)
#include <windows.h>
#include <winnt.h>
static inline void mul(uint64_t a, uint64_t b, uint64_t &low, uint64_t &high) {
low = mul128(a, b, &high);
}
#else
static inline void mul(uint64_t a, uint64_t b, uint64_t &low, uint64_t &high) {
typedef unsigned __int128 uint128_t;
uint128_t res = (uint128_t) a * (uint128_t) b;
low = (uint64_t) res;
high = (uint64_t) (res >> 64);
}
#endif
static inline bool cadd(uint64_t a, uint64_t b) {
return a + b < a;
}