--- ray/src/common/urand.c 2004/05/14 20:49:13 2.8 +++ ray/src/common/urand.c 2022/04/21 15:22:30 2.14 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: urand.c,v 2.8 2004/05/14 20:49:13 greg Exp $"; +static const char RCSid[] = "$Id: urand.c,v 2.14 2022/04/21 15:22:30 greg Exp $"; #endif /* * Anticorrelated random function due to Christophe Schlick @@ -7,29 +7,46 @@ static const char RCSid[] = "$Id: urand.c,v 2.8 2004/0 #include "copyright.h" -#include - -#include "standard.h" +#include "rterror.h" #include "random.h" #undef initurand #define MAXORDER (8*sizeof(unsigned short)) -unsigned short *urperm = NULL; /* urand() permutation */ -int urmask; /* bits used in permutation */ +static unsigned short empty_tab = 0; +unsigned short *urperm = &empty_tab; /* urand() permutation */ +int urmask = 0; /* bits used in permutation */ + + +long +irandom( /* better than using random() % modulus */ + long modulus +) +{ + if (sizeof(long) >= 8) + return((random()*modulus)>>31); + + if (sizeof(long long) >= 8) + return((random()*(long long)modulus)>>31); + + return(frandom()*modulus); +} + int -initurand(size) /* initialize urand() for size entries */ -int size; +initurand( /* initialize urand() for size entries */ + int size +) { int order, n; - register int i, offset; + int i, offset; - if (urperm != NULL) + if ((urperm != NULL) & (urperm != &empty_tab)) free((void *)urperm); if (--size <= 0) { - urperm = NULL; + empty_tab = 0; + urperm = &empty_tab; urmask = 0; return(0); } @@ -43,7 +60,7 @@ int size; eputs("out of memory in initurand\n"); quit(1); } - urperm[0] = (random() & 0x4000) != 0; + urperm[0] = 0; for (n = 1, offset = 1; n <= order; n++, offset <<= 1) for (i = offset; i--; ) { urperm[i+offset] = urperm[i] <<= 1; @@ -57,15 +74,16 @@ int size; int -ilhash(d, n) /* hash a set of integer values */ -register int *d; -register int n; +ilhash( /* hash a set of integer values */ + int *d, + int n +) { - static int tab[8] = {13623,353,1637,5831,2314,3887,5832,8737}; - register int hval; + static int tab[8] = {103699,96289,73771,65203,81119,87037,92051,98899}; + int hval; hval = 0; while (n-- > 0) - hval += *d++ * tab[n&7]; - return(hval); + hval ^= *d++ * tab[n&7]; + return(hval & 0x7fffffff); }