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 2.8 by greg, Fri May 14 20:49:13 2004 UTC vs.
Revision 2.13 by greg, Thu Apr 21 02:55:19 2022 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
7  
8   #include "copyright.h"
9  
10 < #include  <stdlib.h>
11 <
12 < #include  "standard.h"
10 > #include  "rterror.h"
11   #include  "random.h"
12  
13   #undef initurand
14  
15   #define  MAXORDER       (8*sizeof(unsigned short))
16  
17 < unsigned short  *urperm = NULL; /* urand() permutation */
20 < 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 +
23 + long
24 + irandom(                        /* better than using random() % modulus */
25 +        long modulus
26 + )
27 + {
28 +        if ((sizeof(long) == 8) | (sizeof(int) == 8))
29 +                return((random()*modulus)>>31);
30 +
31 +        if (sizeof(long long) == 8)
32 +                return((random()*(long long)modulus)>>31);
33 +
34 +        return(random() % modulus);
35 + }
36 +
37   int
38 < initurand(size)         /* initialize urand() for size entries */
39 < int  size;
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 <        if (urperm != NULL)
45 >        if ((urperm != NULL) & (urperm != &empty_tab))
46                  free((void *)urperm);
47          if (--size <= 0) {
48 <                urperm = NULL;
48 >                empty_tab = 0;
49 >                urperm = &empty_tab;
50                  urmask = 0;
51                  return(0);
52          }
# Line 43 | Line 60 | int  size;
60                  eputs("out of memory in initurand\n");
61                  quit(1);
62          }
63 <        urperm[0] = (random() & 0x4000) != 0;
63 >        urperm[0] = 0;
64          for (n = 1, offset = 1; n <= order; n++, offset <<= 1)
65                  for (i = offset; i--; ) {
66                          urperm[i+offset] = urperm[i] <<= 1;
# Line 57 | Line 74 | int  size;
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);
87 >                hval ^= *d++ * tab[n&7];
88 >        return(hval & 0x7fffffff);
89   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines