ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/urand.c
Revision: 2.2
Committed: Sat Sep 5 13:40:55 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +4 -2 lines
Log Message:
added free call for multiple calls to initurand()

File Contents

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