ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/urand.c
Revision: 1.2
Committed: Fri May 17 13:51:14 1991 UTC (32 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +16 -0 lines
Log Message:
added urhash() function

File Contents

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