K
Kevin Bracey
In message <[email protected]>
Yes. Because the compiler knows that b2 and b1 must be pointing to elements
of the same array, it knows that the difference must be a multiple of 527,
so the division is exact. It exploits this knowledge to turn the division
into a (faster) modulo multiplication, as described in a paper by Granlund &
Montgomery (1994).
In this case, it is performing a modulo-2^32 multiplication by 0x83A4ACEF
(-2086359825), which is equivalent to dividing by 527, but ONLY if the
division is exact.
An excellent example of a compiler optimisation taking advantage of being
allowed to produce undefined behaviour for incorrect code.
"Ralmin said:I would have expected the implicit division of 1 by 527 to always round
down to zero in practise. It appears to produce zero on most of my
implementations to hand (lcc-win32, microsoft vc++, borland bcc32), but
surprisingly not on cygwin gcc, where it produces a ptrdiff_t value of
-2086359825. Weird.
<OT> Any explanations? </OT>
Yes. Because the compiler knows that b2 and b1 must be pointing to elements
of the same array, it knows that the difference must be a multiple of 527,
so the division is exact. It exploits this knowledge to turn the division
into a (faster) modulo multiplication, as described in a paper by Granlund &
Montgomery (1994).
In this case, it is performing a modulo-2^32 multiplication by 0x83A4ACEF
(-2086359825), which is equivalent to dividing by 527, but ONLY if the
division is exact.
An excellent example of a compiler optimisation taking advantage of being
allowed to produce undefined behaviour for incorrect code.