--- ray/src/common/urand.c 1991/05/17 12:55:27 1.1 +++ ray/src/common/urand.c 2015/05/27 08:42:06 2.11 @@ -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.11 2015/05/27 08:42:06 greg Exp $"; #endif - /* - * Uncorrelated (anticorrelated) random function + * Anticorrelated random function due to Christophe Schlick */ +#include "copyright.h" + +#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 */ -initurand(size) /* initialize urand() for size entries */ -int size; + +int +initurand( /* initialize urand() for size entries */ + int size +) { int order, n; - register int i, offset; + 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 & 0x7fffffff); }