Is there any general (not ISA specific) way, how to get a reciprocal value of any positive number in assembly, not using div
function?
I am using reciprocal values for division of an unknown number x
by a constant y
. Say, I want to divide 256 by a constant 3 (which is same as multiply 256 by 1/3)
- Calculate
1/3
by hand - Convert
0.3333...
from floating point to Q32 fixed point (32 fraction bits unsigned)1/3 * 2^32 = 0x55555555
by hand - Save
0x0x55555555
constant to a register in assembly as an immediate value - Multiply
0x100 * 0x55555555 = 0x5555555500
in assembly - Convert back from Q64 fixed point to 32bit integer
0x5555555500 >> 32 = 0x55 = 85
in assembly
This is working fine, but now, I want to divide 2 unknown number by each other. In order to do this, using the algorithm above, first I must calculate a reciprocal value of the y
in assembly, not by hand.
Or is there another general method, which I can use for division? I don't want to use subtraction in a cycle to calculate the division because of speed performance of my code.