Skip to content

Instantly share code, notes, and snippets.

@elbeno
Created January 23, 2017 18:50
Show Gist options
  • Save elbeno/da19e7f1716fee9dc55e1055c3d3942e to your computer and use it in GitHub Desktop.
Save elbeno/da19e7f1716fee9dc55e1055c3d3942e to your computer and use it in GitHub Desktop.
constexpr u64 mul/add, avoiding overflow warnings on VC++
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