ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/urand.c
Revision: 2.4
Committed: Mon Jan 5 16:47:37 1998 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.3: +6 -4 lines
Log Message:
added check for too many urand values

File Contents

# User Rev Content
1 gregl 2.4 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2 greg 1.1
3     #ifndef lint
4 gregl 2.3 static char SCCSid[] = "$SunId$ SGI";
5 greg 1.1 #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 gregl 2.4 #define MAXORDER (8*sizeof(unsigned short))
16    
17 greg 1.1 extern char *malloc();
18    
19 gregl 2.4 unsigned short *urperm = NULL; /* urand() permutation */
20 greg 1.1 int urmask; /* bits used in permutation */
21    
22    
23 gregl 2.3 int
24 greg 1.1 initurand(size) /* initialize urand() for size entries */
25     int size;
26     {
27     int order, n;
28     register int i, offset;
29    
30 greg 2.2 if (urperm != NULL)
31     free((char *)urperm);
32 greg 1.1 size--;
33     for (i = 1; size >>= 1; i++)
34     ;
35 gregl 2.4 order = i>MAXORDER ? MAXORDER : i;
36 greg 1.1 urmask = (1<<i) - 1;
37 gregl 2.4 urperm = (unsigned short *)malloc((urmask+1)*sizeof(unsigned short));
38 greg 1.1 if (urperm == NULL) {
39     eputs("out of memory in initurand\n");
40     quit(1);
41     }
42     urperm[0] = 0;
43     for (n = 1, offset = 1; n <= order; n++, offset <<= 1)
44     for (i = offset; i--; ) {
45     urperm[i] =
46     urperm[i+offset] = 2*urperm[i];
47     if (random() & 0x4000)
48     urperm[i]++;
49     else
50     urperm[i+offset]++;
51     }
52 gregl 2.3 return(1<<order);
53 greg 1.2 }
54    
55    
56     int
57 greg 1.3 ilhash(d, n) /* hash a set of integer values */
58 greg 1.2 register int *d;
59     register int n;
60     {
61     static int tab[8] = {13623,353,1637,5831,2314,3887,5832,8737};
62 greg 1.4 register int hval;
63 greg 1.2
64     hval = 0;
65     while (n-- > 0)
66     hval += *d++ * tab[n&7];
67 greg 1.3 return(hval);
68 greg 1.1 }