ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/urand.c
Revision: 2.7
Committed: Sat Jun 7 12:50:21 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.6: +3 -1 lines
Log Message:
Various small changes to reduce compile warnings/errors on Windows.

File Contents

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