Skip to content
Snippets Groups Projects
Commit a632b0df authored by Romain Vimont's avatar Romain Vimont Committed by Steve Lhomme
Browse files

compat: fix jrand48()

According to the manpage, jrand48() must return a signed long integer
uniformly distributed over the interval [-2^31, 2^31).

The old implementation first computed iterate48(), which returns a
positive 48-bit integer (as 'uint64_t', then cast to 'int64_t'). Once
right-shifted by 16 bits, the result is guaranteed to be a 32-bit
positive integer as 'int64_t'.

It is then (implicitly) cast to 'long' to match the return type.

When the 32th bit is 0 (i.e. the value fits in 31-bit), then everything
is ok. However, when the 32nd bit is 1, then there are two cases (which
are both incorrect):
 - if the 'long' type is 32-bit on the platform, then conversion from
   'int64_t' to 'long' is undefined;
 - if the 'long' type is more than 32-bit, then the resulting value will
   be a positive integer in the interval [0, 2^32), whereas jrand48()
   must return a value in the interval [-2^31, 2^31).
parent 01d73811
No related branches found
No related tags found
Loading
Pipeline #306183 passed with stage
in 23 minutes and 25 seconds
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment