Created
January 23, 2017 18:50
-
-
Save elbeno/da19e7f1716fee9dc55e1055c3d3942e to your computer and use it in GitHub Desktop.
constexpr u64 mul/add, avoiding overflow warnings on VC++
This file contains hidden or 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
constexpr uint64_t lo(uint64_t x) { return x & UINT64_C(0xffffffff); } | |
constexpr uint64_t hi(uint64_t x) { return x >> 32; } | |
constexpr uint64_t mulu64(uint64_t a, uint64_t b) | |
{ | |
return lo(lo(a) * lo(b)) | |
+ (lo(hi(lo(a) * lo(b)) | |
+ lo(a) * hi(b) | |
+ hi(a) * lo(b)) << 32); | |
} | |
constexpr uint64_t addu64(uint64_t a, uint64_t b) | |
{ | |
return lo(lo(a) + lo(b)) | |
+ (lo(hi(lo(a) + lo(b)) + (hi(a) + hi(b))) << 32); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment