4 ms·
> If you have a full 32-bit number and you need to divide, you can simply do a multiply and take the top 32-bit half as the result. Can someone explain how thi
by ynoxinul 2y ago
> If you have a full 32-bit number and you need to divide, you can simply do a multiply and take the top 32-bit half as the result.
Can someone explain how this can work? Obviously, you can't just multiply the same numbers instead of dividing.
- Findecanor 2y agoOf course not. It is multiplication with a reciprocal in fixed-point representation. You'd first have to compute the reciprocal as 2**32 / divisor. Therefore it is most often done with constant divisors. A longer tutorial that goes into more depth: https://homepage.cs.uiowa.edu/~jones/bcd/divide.html https://homepage.cs.uiowa.edu/~jones/bcd/divide.html
- 0xf00ff00f 2y agoAlso, x86 has an instruction that multiplies two 32-bit registers and stores the 64-bit result in two 32-bit registers. So you get the result of the division in the register with the high part of the multiplication result, and don't need to do a shift.