Mail Archives: djgpp/1999/02/17/14:40:29
George Marsaglia <geo AT stat DOT fsu DOT edu> writes:
The method is Multiply-with-Carry for 32-bit integers.
This is the idea. Suppose you have
a current 32-bit integer x
a current 32-bit carry c
a fixed multiplier, say, a=2083801278
Now form a*x+c in 64 bits.
Return the bottom 32 bits as the new x,
and keep the top 32 bits as the new carry, c.
If you allow use of long long, which IIRC will be in C9x, then you can
implement this cleanly in C:
const unsigned long long a = 2083801278ull;
unsigned long long x, c;
[...]
x = a * x + c;
c = x >> 32;
x &= 0xffffffffull;
c &= 0xffffffffull;
(You only need the final AND operation if you're worried about the
target machine having an unsigned long long that's bigger than 64
bits.)
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Please: do not email me copies of your posts to comp.lang.c
do not ask me C questions via email; post them instead
- Raw text -