ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/urand.c
Revision: 2.6
Committed: Tue Feb 25 02:47:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.5: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Anticorrelated random function due to Christophe Schlick
6 */
7
8 #include "copyright.h"
9
10 #include <stdlib.h>
11 #include "random.h"
12
13 #undef initurand
14
15 #define MAXORDER (8*sizeof(unsigned short))
16
17 unsigned short *urperm = NULL; /* urand() permutation */
18 int urmask; /* bits used in permutation */
19
20 int
21 initurand(size) /* initialize urand() for size entries */
22 int size;
23 {
24 int order, n;
25 register int i, offset;
26
27 if (urperm != NULL)
28 free((void *)urperm);
29 if (--size <= 0) {
30 urperm = NULL;
31 urmask = 0;
32 return(0);
33 }
34 for (i = 1; size >>= 1; i++)
35 ;
36 order = i>MAXORDER ? MAXORDER : i;
37 urmask = (1<<i) - 1;
38 urperm = (unsigned short *)malloc((urmask+1)*sizeof(unsigned short));
39 if (urperm == NULL) {
40 eputs("out of memory in initurand\n");
41 quit(1);
42 }
43 urperm[0] = 0;
44 for (n = 1, offset = 1; n <= order; n++, offset <<= 1)
45 for (i = offset; i--; ) {
46 urperm[i] =
47 urperm[i+offset] = 2*urperm[i];
48 if (random() & 0x4000)
49 urperm[i]++;
50 else
51 urperm[i+offset]++;
52 }
53 return(1<<order);
54 }
55
56
57 int
58 ilhash(d, n) /* hash a set of integer values */
59 register int *d;
60 register int n;
61 {
62 static int tab[8] = {13623,353,1637,5831,2314,3887,5832,8737};
63 register int hval;
64
65 hval = 0;
66 while (n-- > 0)
67 hval += *d++ * tab[n&7];
68 return(hval);
69 }