ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/urind.c
Revision: 2.4
Committed: Thu Jul 17 09:21:29 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R2, rad4R2P2, rad5R0, rad5R1, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1, rad5R3, HEAD
Changes since 2.3: +2 -1 lines
Log Message:
Added prototypes and includes from patch by Randolph Fritz.
Added more required includes and reduced other compile warnings.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.4 static const char RCSid[] = "$Id: urind.c,v 2.3 2003/02/25 02:47:22 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Compute pseudo-asyncronous entry point for urand(3)
6 greg 2.2 */
7    
8 greg 2.3 #include "copyright.h"
9 schorsch 2.4 #include "random.h"
10 greg 1.1
11     #define NBITS 32 /* number of bits in an integer */
12    
13     static char bctab[256] = {
14     0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
15     1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
16     1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
17     2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
18     1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
19     2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
20     2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
21     3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
22     1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
23     2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
24     2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
25     3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
26     2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
27     3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
28     3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
29     4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
30     };
31    
32     #if NBITS==32
33     #define bitcount(i) (bctab[(i)>>24&0xff]+bctab[(i)>>16&0xff]+ \
34     bctab[(i)>>8&0xff]+bctab[(i)&0xff])
35     #endif
36     #if NBITS==16
37     #define bitcount(i) (bctab[(i)>>8&0xff]+bctab[(i)&0xff])
38     #endif
39    
40    
41     int
42     urind(s, i) /* compute i'th index from seed s */
43     int s, i;
44     {
45     register int ss, k;
46     int left;
47    
48     ss = s*1103515245 + 12345;
49     left = 0;
50     for (k = i/NBITS; k--; ) {
51     left += bitcount(ss);
52     ss = ss*1103515245 + 12345;
53     }
54     for (k = i&(NBITS-1); k--; ss >>= 1)
55     left += ss & 1;
56     if (ss & 1)
57     return(s-left-1);
58     return(s-left+i);
59     }