ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/urand.c
Revision: 2.11
Committed: Wed May 27 08:42:06 2015 UTC (8 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad5R0, rad5R1, rad5R3
Changes since 2.10: +11 -10 lines
Log Message:
Fixed includes and prototypes

File Contents

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