Improve random bignum generation

* src/bignum.c (get_random_limb, get_random_limb_lim)
(get_random_bignum): New functions, for more-efficient
generation of random bignums without using Frem etc.
* src/fns.c (get_random_fixnum): New function.
(Frandom): Use it, and get_random_bignum.
Be consistent about signalling nonpositive integer arguments;
since zero is invalid, Qnatnump is not quite right here.
* src/sysdep.c (get_random_ulong): New function.
This commit is contained in:
Paul Eggert 2022-03-16 17:21:55 -07:00
parent 31a2428d6f
commit 2ef037c0dd
5 changed files with 132 additions and 50 deletions

View file

@ -2200,6 +2200,16 @@ get_random (void)
return val & INTMASK;
}
/* Return a random unsigned long. */
unsigned long int
get_random_ulong (void)
{
unsigned long int r = 0;
for (int i = 0; i < (ULONG_WIDTH + RAND_BITS - 1) / RAND_BITS; i++)
r = random () ^ (r << RAND_BITS) ^ (r >> (ULONG_WIDTH - RAND_BITS));
return r;
}
#ifndef HAVE_SNPRINTF
/* Approximate snprintf as best we can on ancient hosts that lack it. */
int