4 ms·
int64_t a, b, c, r; r = (a * b) / c; /* multiplication step could overflow so use 128bits */
by PaulDavisThe1st 9mo ago
int64_t a, b, c, r;
r = (a * b) / c; /* multiplication step could overflow so use 128bits */
- cmovq 9mo agoLast time I checked LLVM had surprisingly bad codegen for this using int128. On x86 you only need two instructions: __asm ( "mulq %[multiplier]\n" "divq %[divisor]\n" : "=a"(result) : "a"(num), [multiplier]"r"(multiplier), [divisor]"r"(divisor) : "rdx" ); The intermediate 128bit number is in rdx:rax.
- bonzini 9mo agoThat only works if you are sure to have a 64-bit result. If you can have divisor < multiplier and need to detect overflow, it's more complicated.