Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Commit

Permalink
Fix mul128 for x86
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbandi committed Jan 17, 2017
1 parent 770c633 commit 41a4798
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions algorithm/cryptonight.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,40 @@ void CNKeccak(uint64_t *output, uint64_t *input)
memcpy(output, st, 200);
}

#if !defined(_WIN64) && !defined(__amd64__)
static inline uint64_t hi_dword(uint64_t val) {
return val >> 32;
}

static inline uint64_t lo_dword(uint64_t val) {
return val & 0xFFFFFFFF;
}

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
// ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
uint64_t a = hi_dword(multiplier);
uint64_t b = lo_dword(multiplier);
uint64_t c = hi_dword(multiplicand);
uint64_t d = lo_dword(multiplicand);

uint64_t ac = a * c;
uint64_t ad = a * d;
uint64_t bc = b * c;
uint64_t bd = b * d;

uint64_t adbc = ad + bc;
uint64_t adbc_carry = adbc < ad ? 1 : 0;

// multiplier * multiplicand = product_hi * 2^64 + product_lo
uint64_t product_lo = bd + (adbc << 32);
uint64_t product_lo_carry = product_lo < bd ? 1 : 0;
*product_hi = ac + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry;

return product_lo;
}
#else
static inline uint64_t mul128(uint64_t a, uint64_t b, uint64_t* product_hi)
{
uint64_t lo, hi;
Expand All @@ -138,6 +172,7 @@ static inline uint64_t mul128(uint64_t a, uint64_t b, uint64_t* product_hi)

return lo;
}
#endif

#define BYTE(x, y) (((x) >> ((y) << 3)) & 0xFF)
#define ROTL32(x, y) (((x) << (y)) | ((x) >> (32 - (y))))
Expand Down

0 comments on commit 41a4798

Please sign in to comment.