Created
October 12, 2021 19:33
-
-
Save flysand7/9ca388b87685ecba93aa8f2cfad3cc2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define LO32(x) (x&((UINT64_C(1)<<32)-1)) | |
#define HI32(x) (x >> 32) | |
// Returns the high 64-bits of the 128-bit result. | |
// the `result` variable specifies the address of the low 64-bits | |
static uint64_t add_u64(uint64_t *result, uint64_t a, uint64_t b) | |
{ | |
uint64_t al = LO32(a); | |
uint64_t ah = HI32(a); | |
uint64_t bl = LO32(b); | |
uint64_t bh = HI32(b); | |
uint64_t r0 = LO32(al+bl); | |
uint64_t r1 = HI32(al+bl) + LO32(ah+bh); | |
*result = r0 + (r1 << 32); | |
return HI32(ah+bh); | |
} | |
// Returns the high 64-bits of the 128-bit result | |
// the `result` is the address of the variable that receives the low 64 bits | |
static uint64_t mul_u64(uint64_t *result, uint64_t a, uint64_t b) | |
{ | |
uint64_t al = LO32(a); | |
uint64_t bl = LO32(b); | |
uint64_t tmp = (al * bl); | |
uint64_t r3 = LO32(tmp); | |
uint64_t k = HI32(tmp); | |
uint64_t ah = HI32(a); | |
uint64_t bh = HI32(b); | |
tmp = (ah * bl) + k; | |
k = LO32(tmp); | |
uint64_t r1 = HI32(tmp); | |
tmp = (al * bh) + k; | |
k = HI32(tmp); | |
*result = (tmp << 32) + r3; | |
return (ah * bh) + r1 + k; | |
} | |
#undef LO32 | |
#undef HI32 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment