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

Comparing ray/src/cal/cnt.c (file contents):
Revision 1.4 by greg, Wed Apr 20 19:13:12 2022 UTC vs.
Revision 1.8 by greg, Fri Apr 22 15:52:50 2022 UTC

# Line 128 | Line 128 | tree_alloc(long alen)
128                  exit(1);
129          }
130          *troot = ht;                    /* first byte is tree height */
131 <        for (i = 256; i--; ) {          /* assign bit count table */
131 >        for (i = 256; i--; ) {          /* assign 0-bit count table */
132                  int     b;
133 <                buf[i] = 0;
133 >                buf[i] = 8;
134                  for (b = i; b; b >>= 1)
135 <                        buf[i] += b&1;
135 >                        buf[i] -= b&1;
136          }
137          return(troot);
138   }
# Line 161 | Line 161 | incr_counter(uby8 *ctrp, int n)
161   {
162          n = tree_br[n].cntr_siz;
163  
164 <        while (n-- > 0)                 /* LSB first */
165 <                if (++(*ctrp++))
166 <                        break;
164 >        while (! ++(*ctrp++))           /* LSB first */
165 >                if (--n <= 0) {
166 >                        fputs("Shuffle occupancy overflow!\n", stderr);
167 >                        exit(1);        /* means we sized something wrong */
168 >                }
169   }
170  
171  
# Line 190 | Line 192 | eat_nth_leaf(uby8 *brp, long ski)
192                  incr_counter(brp, lvl);         /* we intend to eat one */
193                  brp += tree_br[lvl--].cntr_siz; /* drop a level */
194          }
195 <        b = 0;                                  /* browse the leaves */
194 <        while (ski >= 8-buf[*brp]) {            /* buf contains bit counts */
195 >        while (ski >= buf[*brp]) {              /* browse the leaves */
196                  pos += 8;
197 <                ski -= 8-buf[*brp++];
197 >                ski -= buf[*brp++];             /* buf contains 0-bit counts */
198          }
199 <        while (ski >= 0) {                      /* target zero leaf bit */
199 >        b = 0;                                  /* find target bit in byte */
200 >        while ((ski -= !(*brp & 1<<b)) >= 0) {
201                  pos++;
202 <                ski -= !(*brp & 1<<b++);
202 >                b++;
203          }
204 <        *brp |= 1 << --b;                       /* eat it */
205 <        return(--pos);                          /* & return leaf's slot# */
204 >        *brp |= 1<<b;                           /* eat it */
205 >        return(pos);                            /* & return leaf's slot# */
206   }
207  
208  
# Line 213 | Line 215 | big_shuffle(long *n, long alen)
215          tree_root = tree_alloc(alen);
216  
217          while (alen > 0)                /* allocate and print random array entries */
218 <                print_shuf(n, eat_nth_leaf(tree_root, random() % alen--));
218 >                print_shuf(n, eat_nth_leaf(tree_root, irandom(alen--)));
219  
220          free(tree_root);                /* all done */
221   }
# Line 251 | Line 253 | shuffle(long *n)
253                  myshuf[i] = i;
254                                  /* perform Fisher-Yates shuffle */
255          for (i = 0; i < alen-1; i++) {
256 <                int     ix = random()%(alen-i) + i;
256 >                int     ix = irandom(alen-i) + i;
257                  int     ndx = myshuf[i];
258                  myshuf[i] = myshuf[ix];
259                  myshuf[ix] = ndx;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines