Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9e7be3
commit d8cbd09
Showing
3 changed files
with
28 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
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; | ||
assert(ac <= *product_hi); | ||
|
||
return product_lo; | ||
} |