Subject: bug#32605: [w64] (random) never returns negative

From: Eli Zaretskii <[email-masked]>
Date: Thu, 12 Aug 2021 16:42:35 +0300

> From: Lars Ingebrigtsen <[email-masked]>
> Cc: [email-masked], Eli Zaretskii <[email-masked]>
> Date: Thu, 12 Aug 2021 15:17:46 +0200
> 
> Andy Moreton <[email-masked]> writes:
> 
> >> This bug seems specific to 64 bit Windows builds.
> >
> > ON 64bit Windows, sysdep.c sets RAND_BITS to 31, but random (in w32.c)
> > only provides 30 bits. It looks like the mixing in get_random does not
> > result in the top fixnum bit being set.
> 
> So it's this:
> 
> int
> random (void)
> {
>   /* rand_as183 () gives us 15 random bits...hack together 30 bits.  */
>   return ((rand_as183 () << 15) | rand_as183 ());
> }
> 
> So running rand_as183 and taking another bit from that might do the
> trick?  Eli, do you have any comments here?

The 'random' emulation in w32.c was never adapted to w64.

Instead of calling rand_as183 one more time, perhaps it's better to
trivially transform the value we have?  Something like

    int val = ((rand_as183 () << 15) | rand_as183 ());
  #ifdef __x86_64__
    return 2 * val - 0x3FFFFFFF;
  #else
    return val;
  #endif

Andy, can you test this, please?