ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/urand.c
(Generate patch)

Comparing ray/src/common/urand.c (file contents):
Revision 1.6 by greg, Thu Jul 11 16:25:58 1991 UTC vs.
Revision 2.12 by greg, Thu Apr 21 02:52:40 2022 UTC

# Line 1 | Line 1
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 +        static const int        isize = sizeof(int);
29 +        static const int        lsize = sizeof(long);
30 +        static const int        llsize = sizeof(long long);
31 +
32 +        if ((lsize == 8) | (isize == 8))
33 +                return((random()*modulus)>>31);
34 +
35 +        if (llsize == 8)
36 +                return((random()*(long long)modulus)>>31);
37 +
38 +        return(random() % modulus);
39 + }
40 +
41 + int
42 + initurand(                      /* initialize urand() for size entries */
43 +        int  size
44 + )
45 + {
46          int  order, n;
47 <        register int  i, offset;
47 >        int  i, offset;
48  
49 <        size--;
50 <        for (i = 1; size >>= 1; i++)
51 <                ;
49 >        if ((urperm != NULL) & (urperm != &empty_tab))
50 >                free((void *)urperm);
51 >        if (--size <= 0) {
52 >                empty_tab = 0;
53 >                urperm = &empty_tab;
54 >                urmask = 0;
55 >                return(0);
56 >        }
57 >        for (i = 1; (size >>= 1); i++)
58 >                if (i == MAXORDER)
59 >                        break;
60          order = i;
61 <        urmask = (1<<i) - 1;
62 <        urperm = (short *)malloc((urmask+1)*sizeof(short));
61 >        urmask = (1<<order) - 1;
62 >        urperm = (unsigned short *)malloc((urmask+1)*sizeof(unsigned short));
63          if (urperm == NULL) {
64                  eputs("out of memory in initurand\n");
65                  quit(1);
# Line 37 | Line 67 | int  size;
67          urperm[0] = 0;
68          for (n = 1, offset = 1; n <= order; n++, offset <<= 1)
69                  for (i = offset; i--; ) {
70 <                        urperm[i] =
41 <                        urperm[i+offset] = 2*urperm[i];
70 >                        urperm[i+offset] = urperm[i] <<= 1;
71                          if (random() & 0x4000)
72                                  urperm[i]++;
73                          else
74                                  urperm[i+offset]++;
75                  }
76 +        return(urmask+1);
77   }
78  
79  
80   int
81 < ilhash(d, n)                    /* hash a set of integer values */
82 < register int  *d;
83 < register int  n;
81 > ilhash(                         /* hash a set of integer values */
82 >        int  *d,
83 >        int  n
84 > )
85   {
86 <        static int  tab[8] = {13623,353,1637,5831,2314,3887,5832,8737};
87 <        register int  hval;
86 >        static int  tab[8] = {103699,96289,73771,65203,81119,87037,92051,98899};
87 >        int  hval;
88  
89          hval = 0;
90          while (n-- > 0)
91 <                hval += *d++ * tab[n&7];
92 <        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);
91 >                hval ^= *d++ * tab[n&7];
92 >        return(hval & 0x7fffffff);
93   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines