On 2010-10-13, at 4:16 PM, chevalma@iro.umontreal.ca wrote:
The machine multiplication instruction typically produces a result that
is twice the word width, in 2 registers (the hi 32 bits and the lo 32 bits of the result).
div and idiv seem to use two registers, one for the quotient, and one for the remainder, is that what you mean? The quotient here is the only thing that we care about,
No I mean the x86 "mul" instruction, as in
movl $3435973837,%edx mull %edx
which multiplies the 32 bit %eax by the 32 bit 3435973837 (which was put in %edx). The result of the multiplication is always stored in %edx:%eax (a total of 64 bits). So if after the multiplication %edx is right shifted by 2 bits, it will contain the initial content of %eax divided by 5.
So if a 34 bit right shift is needed (for example when dividing by 5),
it is really a right shift of the hi 32 bit register by 2 bits.
So you ignore the remainder and shift the quotient register by 2 bits?
Does this work just as well for signed divide? Some of the values you printed are outside the positive range of 32 bit signed integers, do you then just treat these as negative values and use a signed multiply and signed shift?
I only know that it works for unsigned numbers. You'll have to check what happens with negative integers.
Marc