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.7 by greg, Mon Aug 12 13:07:02 1991 UTC vs.
Revision 2.10 by greg, Wed Apr 5 06:22:56 2006 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  <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 */
18 < int  urmask;            /* bits used in permutation */
19 > static unsigned short  empty_tab = 0;
20  
21 + unsigned short  *urperm = &empty_tab;   /* urand() permutation */
22 + int  urmask = 0;                        /* bits used in permutation */
23  
24 + int
25   initurand(size)         /* initialize urand() for size entries */
26   int  size;
27   {
28          int  order, n;
29          register int  i, offset;
30  
31 <        size--;
32 <        for (i = 1; size >>= 1; i++)
33 <                ;
31 >        if ((urperm != NULL) & (urperm != &empty_tab))
32 >                free((void *)urperm);
33 >        if (--size <= 0) {
34 >                empty_tab = 0;
35 >                urperm = &empty_tab;
36 >                urmask = 0;
37 >                return(0);
38 >        }
39 >        for (i = 1; (size >>= 1); i++)
40 >                if (i == MAXORDER)
41 >                        break;
42          order = i;
43 <        urmask = (1<<i) - 1;
44 <        urperm = (short *)malloc((urmask+1)*sizeof(short));
43 >        urmask = (1<<order) - 1;
44 >        urperm = (unsigned short *)malloc((urmask+1)*sizeof(unsigned short));
45          if (urperm == NULL) {
46                  eputs("out of memory in initurand\n");
47                  quit(1);
# Line 37 | Line 49 | int  size;
49          urperm[0] = 0;
50          for (n = 1, offset = 1; n <= order; n++, offset <<= 1)
51                  for (i = offset; i--; ) {
52 <                        urperm[i] =
41 <                        urperm[i+offset] = 2*urperm[i];
52 >                        urperm[i+offset] = urperm[i] <<= 1;
53                          if (random() & 0x4000)
54                                  urperm[i]++;
55                          else
56                                  urperm[i+offset]++;
57                  }
58 +        return(urmask+1);
59   }
60  
61  
# Line 52 | Line 64 | ilhash(d, n)                   /* hash a set of integer values */
64   register int  *d;
65   register int  n;
66   {
67 <        static int  tab[8] = {13623,353,1637,5831,2314,3887,5832,8737};
67 >        static int  tab[8] = {103699,96289,73771,65203,81119,87037,92051,98899};
68          register int  hval;
69  
70          hval = 0;
71          while (n-- > 0)
72 <                hval += *d++ * tab[n&7];
73 <        return(hval);
72 >                hval ^= *d++ * tab[n&7];
73 >        return(hval & 0x7fffffff);
74   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines