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