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 |
< |
* Uncorrelated (anticorrelated) random function |
5 |
> |
* Anticorrelated random function due to Christophe Schlick |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "copyright.h" |
9 |
+ |
|
10 |
+ |
#include <stdlib.h> |
11 |
+ |
|
12 |
+ |
#include "standard.h" |
13 |
|
#include "random.h" |
14 |
|
|
15 |
< |
#define NULL 0 |
15 |
> |
#undef initurand |
16 |
|
|
17 |
< |
extern char *malloc(); |
17 |
> |
#define MAXORDER (8*sizeof(unsigned short)) |
18 |
|
|
19 |
< |
short *urperm; /* urand() permutation */ |
19 |
> |
unsigned short *urperm = NULL; /* urand() permutation */ |
20 |
|
int urmask; /* bits used in permutation */ |
19 |
– |
static int urorder; /* number of bits */ |
21 |
|
|
22 |
< |
|
22 |
> |
int |
23 |
|
initurand(size) /* initialize urand() for size entries */ |
24 |
|
int size; |
25 |
|
{ |
26 |
|
int order, n; |
27 |
|
register int i, offset; |
28 |
|
|
29 |
< |
size--; |
30 |
< |
for (i = 1; size >>= 1; i++) |
31 |
< |
; |
29 |
> |
if (urperm != NULL) |
30 |
> |
free((void *)urperm); |
31 |
> |
if (--size <= 0) { |
32 |
> |
urperm = NULL; |
33 |
> |
urmask = 0; |
34 |
> |
return(0); |
35 |
> |
} |
36 |
> |
for (i = 1; (size >>= 1); i++) |
37 |
> |
if (i == MAXORDER) |
38 |
> |
break; |
39 |
|
order = i; |
40 |
< |
urmask = (1<<i) - 1; |
41 |
< |
urperm = (short *)malloc((urmask+1)*sizeof(short)); |
40 |
> |
urmask = (1<<order) - 1; |
41 |
> |
urperm = (unsigned short *)malloc((urmask+1)*sizeof(unsigned short)); |
42 |
|
if (urperm == NULL) { |
43 |
|
eputs("out of memory in initurand\n"); |
44 |
|
quit(1); |
45 |
|
} |
46 |
< |
urperm[0] = 0; |
46 |
> |
urperm[0] = (random() & 0x4000) != 0; |
47 |
|
for (n = 1, offset = 1; n <= order; n++, offset <<= 1) |
48 |
|
for (i = offset; i--; ) { |
49 |
< |
urperm[i] = |
42 |
< |
urperm[i+offset] = 2*urperm[i]; |
49 |
> |
urperm[i+offset] = urperm[i] <<= 1; |
50 |
|
if (random() & 0x4000) |
51 |
|
urperm[i]++; |
52 |
|
else |
53 |
|
urperm[i+offset]++; |
54 |
|
} |
55 |
+ |
return(urmask+1); |
56 |
|
} |
57 |
|
|
58 |
|
|
59 |
|
int |
60 |
< |
urhash(d, n) /* hash a set of integer values */ |
60 |
> |
ilhash(d, n) /* hash a set of integer values */ |
61 |
|
register int *d; |
62 |
|
register int n; |
63 |
|
{ |
64 |
|
static int tab[8] = {13623,353,1637,5831,2314,3887,5832,8737}; |
65 |
< |
register unsigned hval; |
65 |
> |
register int hval; |
66 |
|
|
67 |
|
hval = 0; |
68 |
|
while (n-- > 0) |
69 |
|
hval += *d++ * tab[n&7]; |
70 |
< |
return(hval & urmask); |
70 |
> |
return(hval); |
71 |
|
} |