--- ray/src/common/urand.c 1991/07/11 16:25:58 1.6 +++ ray/src/common/urand.c 2006/04/05 06:22:56 2.10 @@ -1,35 +1,47 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: urand.c,v 2.10 2006/04/05 06:22:56 greg Exp $"; #endif - /* * Anticorrelated random function due to Christophe Schlick */ +#include "copyright.h" + +#include + +#include "standard.h" #include "random.h" -#define NULL 0 +#undef initurand -extern char *malloc(); +#define MAXORDER (8*sizeof(unsigned short)) -short *urperm; /* 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 */ +int initurand(size) /* initialize urand() for size entries */ int size; { int order, n; register int i, offset; - size--; - for (i = 1; size >>= 1; i++) - ; + if ((urperm != NULL) & (urperm != &empty_tab)) + free((void *)urperm); + if (--size <= 0) { + empty_tab = 0; + urperm = &empty_tab; + urmask = 0; + return(0); + } + for (i = 1; (size >>= 1); i++) + if (i == MAXORDER) + break; order = i; - urmask = (1< 0) - hval += *d++ * tab[n&7]; - return(hval); -} - - -static char bctab[256] = { - 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, - }; - - -#define NBITS 32 - -#if NBITS==32 -#define bitcount(i) (bctab[(i)>>24&0xff]+bctab[(i)>>16&0xff]+ \ - bctab[(i)>>8&0xff]+bctab[(i)&0xff]) -#endif -#if NBITS==16 -#define bitcount(i) (bctab[(i)>>8&0xff]+bctab[(i)&0xff]) -#endif - - -int -urind(s, i) /* compute i'th index from seed s */ -int s, i; -{ - register int ss, k; - int left; - - ss = s*1103515245 + 12345; - left = 0; - for (k = i/NBITS; k--; ) { - left += bitcount(ss); - ss = ss*1103515245 + 12345; - } - for (k = i&(NBITS-1); k--; ss >>= 1) - left += ss & 1; - if (ss & 1) - return(s-left-1); - return(s-left+i); + hval ^= *d++ * tab[n&7]; + return(hval & 0x7fffffff); }