1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Anticorrelated random function due to Christophe Schlick |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "copyright.h" |
9 |
+ |
|
10 |
+ |
#include "rterror.h" |
11 |
|
#include "random.h" |
12 |
|
|
13 |
< |
#define NULL 0 |
13 |
> |
#undef initurand |
14 |
|
|
15 |
< |
extern char *malloc(); |
15 |
> |
#define MAXORDER (8*sizeof(unsigned short)) |
16 |
|
|
17 |
< |
short *urperm; /* urand() permutation */ |
18 |
< |
int urmask; /* bits used in permutation */ |
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 |
< |
initurand(size) /* initialize urand() for size entries */ |
23 |
< |
int size; |
22 |
> |
|
23 |
> |
long |
24 |
> |
irandom( /* better than using random() % modulus */ |
25 |
> |
long modulus |
26 |
> |
) |
27 |
|
{ |
28 |
+ |
if (sizeof(long) >= 8) |
29 |
+ |
return((random()*modulus)>>31); |
30 |
+ |
|
31 |
+ |
if (sizeof(long long) >= 8) |
32 |
+ |
return((random()*(long long)modulus)>>31); |
33 |
+ |
|
34 |
+ |
return(frandom()*modulus); |
35 |
+ |
} |
36 |
+ |
|
37 |
+ |
int |
38 |
+ |
initurand( /* initialize urand() for size entries */ |
39 |
+ |
int size |
40 |
+ |
) |
41 |
+ |
{ |
42 |
|
int order, n; |
43 |
< |
register int i, offset; |
43 |
> |
int i, offset; |
44 |
|
|
45 |
< |
size--; |
46 |
< |
for (i = 1; size >>= 1; i++) |
47 |
< |
; |
45 |
> |
if ((urperm != NULL) & (urperm != &empty_tab)) |
46 |
> |
free((void *)urperm); |
47 |
> |
if (--size <= 0) { |
48 |
> |
empty_tab = 0; |
49 |
> |
urperm = &empty_tab; |
50 |
> |
urmask = 0; |
51 |
> |
return(0); |
52 |
> |
} |
53 |
> |
for (i = 1; (size >>= 1); i++) |
54 |
> |
if (i == MAXORDER) |
55 |
> |
break; |
56 |
|
order = i; |
57 |
< |
urmask = (1<<i) - 1; |
58 |
< |
urperm = (short *)malloc((urmask+1)*sizeof(short)); |
57 |
> |
urmask = (1<<order) - 1; |
58 |
> |
urperm = (unsigned short *)malloc((urmask+1)*sizeof(unsigned short)); |
59 |
|
if (urperm == NULL) { |
60 |
|
eputs("out of memory in initurand\n"); |
61 |
|
quit(1); |
63 |
|
urperm[0] = 0; |
64 |
|
for (n = 1, offset = 1; n <= order; n++, offset <<= 1) |
65 |
|
for (i = offset; i--; ) { |
66 |
< |
urperm[i] = |
41 |
< |
urperm[i+offset] = 2*urperm[i]; |
66 |
> |
urperm[i+offset] = urperm[i] <<= 1; |
67 |
|
if (random() & 0x4000) |
68 |
|
urperm[i]++; |
69 |
|
else |
70 |
|
urperm[i+offset]++; |
71 |
|
} |
72 |
+ |
return(urmask+1); |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
|
int |
77 |
< |
ilhash(d, n) /* hash a set of integer values */ |
78 |
< |
register int *d; |
79 |
< |
register int n; |
77 |
> |
ilhash( /* hash a set of integer values */ |
78 |
> |
int *d, |
79 |
> |
int n |
80 |
> |
) |
81 |
|
{ |
82 |
< |
static int tab[8] = {13623,353,1637,5831,2314,3887,5832,8737}; |
83 |
< |
register int hval; |
82 |
> |
static int tab[8] = {103699,96289,73771,65203,81119,87037,92051,98899}; |
83 |
> |
int hval; |
84 |
|
|
85 |
|
hval = 0; |
86 |
|
while (n-- > 0) |
87 |
< |
hval += *d++ * tab[n&7]; |
88 |
< |
return(hval); |
62 |
< |
} |
63 |
< |
|
64 |
< |
|
65 |
< |
static char bctab[256] = { |
66 |
< |
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, |
67 |
< |
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
68 |
< |
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
69 |
< |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
70 |
< |
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
71 |
< |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
72 |
< |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
73 |
< |
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
74 |
< |
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
75 |
< |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
76 |
< |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
77 |
< |
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
78 |
< |
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
79 |
< |
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
80 |
< |
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
81 |
< |
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, |
82 |
< |
}; |
83 |
< |
|
84 |
< |
|
85 |
< |
#define NBITS 32 |
86 |
< |
|
87 |
< |
#if NBITS==32 |
88 |
< |
#define bitcount(i) (bctab[(i)>>24&0xff]+bctab[(i)>>16&0xff]+ \ |
89 |
< |
bctab[(i)>>8&0xff]+bctab[(i)&0xff]) |
90 |
< |
#endif |
91 |
< |
#if NBITS==16 |
92 |
< |
#define bitcount(i) (bctab[(i)>>8&0xff]+bctab[(i)&0xff]) |
93 |
< |
#endif |
94 |
< |
|
95 |
< |
|
96 |
< |
int |
97 |
< |
urind(s, i) /* compute i'th index from seed s */ |
98 |
< |
int s, i; |
99 |
< |
{ |
100 |
< |
register int ss, k; |
101 |
< |
int left; |
102 |
< |
|
103 |
< |
ss = s*1103515245 + 12345; |
104 |
< |
left = 0; |
105 |
< |
for (k = i/NBITS; k--; ) { |
106 |
< |
left += bitcount(ss); |
107 |
< |
ss = ss*1103515245 + 12345; |
108 |
< |
} |
109 |
< |
for (k = i&(NBITS-1); k--; ss >>= 1) |
110 |
< |
left += ss & 1; |
111 |
< |
if (ss & 1) |
112 |
< |
return(s-left-1); |
113 |
< |
return(s-left+i); |
87 |
> |
hval ^= *d++ * tab[n&7]; |
88 |
> |
return(hval & 0x7fffffff); |
89 |
|
} |