1/*
2 *========================================================================
3 * $Id: bits.c 245 2006-10-05 16:34:42Z rgb $
4 *
5 * See copyright in copyright.h and the accompanying file COPYING
6 *========================================================================
7 */
8
9/*
10 *========================================================================
11 * This should be a nice, big case switch where we add EACH test
12 * we might want to do and either just configure and do it or
13 * prompt for input (if absolutely necessary) and then do it.
14 *========================================================================
15 */
16
17#include <dieharder/libdieharder.h>
18
19/*
20 * This should be the only tool we use to access bit substrings
21 * from now on.  Note that we write our own bitstring access tools
22 * instead of using ldap's LBER (Basic Encoding Rules) library call
23 * to both retain control and because it would introduce a slightly
24 * exotic dependency in the resulting application.
25 *
26 * bstring is a pointer to the uint string to be parsed.  It is a uint
27 * pointer to make it easy to pass arbitrary strings which will generally
28 * be e.g. unsigned ints in dieharder but might be other data types
29 * in other applications (might as well make this semi-portable while I'm
30 * writing it at all).  bslen is the length of bitstring in uints.  blen is
31 * the length of the bitstring to be returned (as an unsigned int) and has
32 * to be less than the length, in bits, of bitstring.  Finally, boffset
33 * is the bit index of the point in the bitstring from which the result
34 * will be returned.
35 *
36 * The only other thing that has to be defined is the direction from which
37 * the bit offset and bit length are counted.  We will make the
38 * LEAST significant bit offset 0, and take the string from the direction
39 * of increasing signicance.  Examples:
40 *
41 *   bitstring:  10010110, length 1 (byte, char).
42 * for offset 0, length 4 this should return: 0110
43 *     offset 1, length 4: 1011
44 *     offset 5, length 4: 0100 (note periodic wraparound)
45 *     offset 6, length 4: 1010 (ditto)
46 *
47 * where of course the strings are actually returned as e.g.
48 *     00000000000000000000000000000110  (unsigned int).
49 */
50uint get_bit_ntuple(uint *bitstring,uint bslen,uint blen,uint boffset)
51{
52
53 unsigned int b,rlen;
54 int ioffset;
55 unsigned int result,carry,nmask;
56
57 /*
58  * Some tests for failure or out of bounds.  8*blen has to be less than
59  * sizeof(uint).
60  */
61 if(blen > 8*sizeof(uint)) blen = 8*sizeof(uint);
62 /*
63  * Now that blen is sane, we can make a mask for the eventual return
64  * value of length blen.
65  */
66 nmask = 1;
67 /* dumpbits(&nmask,32); */
68 for(b=0;b<blen-1;b++) {
69   nmask = nmask <<1;
70   nmask++;
71   /* dumpbits(&nmask,32); */
72 }
73 /* dumpbits(&nmask,32); */
74
75 if(verbose == D_BITS || verbose == D_ALL){
76   printf("# get_bit_ntuple(): bslen = %u, blen = %u, boffset = %u\n",bslen,blen,boffset);
77   printf("# get_bit_ntuple(): bitstring (uint) = %u\n",*bitstring);
78   printf("# get_bit_ntuple(): bitstring = ");
79   dumpbits(&bitstring[0],32);
80   printf("# get_bit_ntuple(): nmask     = ");
81   dumpbits(&nmask,32);
82 }
83
84 /*
85  * This is the index of bitstring[] containing the first bit to
86  * be returned, for example if boffset is 30, rmax_bits is 24,
87  * and bslen (the length of the uint bitstring) is 4 (indices
88  * run from 0-3) then ioffset is 4 - 1 -1 = 2 and the 30th bit
89  * from the RIGHT is to be found in bitstring[2]. Put this uint
90  * into result to work on further.
91  */
92 ioffset = bslen - (uint) boffset/rmax_bits - 1;
93 result = bitstring[ioffset];
94 if(verbose == D_BITS || verbose == D_ALL){
95   printf("bitstring[%d] = %u\n",ioffset,result);
96   printf("Initial result =          ");
97   dumpbits(&result,32);
98 }
99 /*
100  * Now, WHICH bit in result is the boffset relative to ITS
101  * rightmost bit?  We have to find the modulus boffset%rmax_bits.
102  * For example 30%24 = 6, so in the continuing example it would
103  * be bit 6 in result.
104  */
105 boffset = boffset%rmax_bits;
106 if(verbose == D_BITS || verbose == D_ALL){
107   printf("Shifting to bit offset %u\n",boffset);
108 }
109
110 /*
111  * Now for the easy part.  We shift right boffset bits.
112  */
113 for(b=0;b<boffset;b++) result = result >> 1;
114 if(verbose == D_BITS || verbose == D_ALL){
115   printf("Shifted result =          ");
116   dumpbits(&result,32);
117 }
118
119 /*
120  * rlen is the cumulated length of result.  Initially, it is set to
121  * rmax_bits - boffset, the number of bits result can now contribute from
122  * boffset.  We now have to loop, adding bits from uints up the list
123  * (cyclic) until we get enough to return blen.  Note that we have to
124  * loop because (for example) blen = 32, rmax_bits = 16, boffset = 30
125  * would start in bitstring[2] and get 2 bits (30 and 31), get all 16 bits
126  * from bitstring[1], and still need 12 bits of bitstring[0] to return.
127  */
128 rlen = rmax_bits - boffset;
129 if(verbose == D_BITS || verbose == D_ALL){
130   printf("Cumulated %u signifcant bits\n",rlen);
131 }
132
133 while(blen > rlen){
134   /*
135    * If we get here, we have to use either bitstring[ioffset-1] or
136    * bitstring[bslen-1], shifted and filled into result starting
137    * at the correct slot.  Put this into carry to work on.
138    */
139   ioffset--;
140   if(ioffset < 0) ioffset = bslen-1;
141   carry = bitstring[ioffset];
142   if(verbose == D_BITS || verbose == D_ALL){
143     printf("bitstring[%d] = %u\n",ioffset,carry);
144     printf("Next carry =              ");
145     dumpbits(&carry,32);
146   }
147
148   /*
149    * This is tricky!  Shift carry left until the first bit lines
150    * up with the first empty bit in result, which we'll hope is
151    * the current rlen bit.
152    */
153   for(b=0;b<rlen;b++){
154     carry = carry << 1;
155   }
156   if(verbose == D_BITS || verbose == D_ALL){
157     printf("Shifted carry =           ");
158     dumpbits(&carry,32);
159   }
160
161   /*
162    * Now we simply add result and carry AND increment rlen by
163    * rmax_bit (since this is the exact number of bits it adds
164    * to rlen).
165    */
166   result += carry;
167   rlen += rmax_bits;
168   if(verbose == D_BITS || verbose == D_ALL){
169     printf("Cumulated %u signifcant bits\n",rlen);
170     printf("result+carry =            ");
171     dumpbits(&result,32);
172   }
173 }
174
175 result = result & nmask;
176   if(verbose == D_BITS || verbose == D_ALL){
177     printf("Returning Result =        ");
178     dumpbits(&result,32);
179     printf("==========================================================\n");
180   }
181 return(result);
182
183}
184
185/*
186 * dumpbits only can dump 8*sizeof(unsigned int) bits at a time.
187 */
188void dumpbits(uint *data, unsigned int nbits)
189{
190
191 uint i,j;
192 uint mask;
193
194 if(nbits > 8*sizeof(unsigned int)) {
195   nbits = 8*sizeof(unsigned int);
196 }
197
198 mask = (uint)pow(2,nbits-1);
199 for(i=0;i<nbits;i++){
200   if(verbose == -1){
201     printf("\nmask = %u = %04x :",mask,mask);
202   }
203   j = (mask & *data)?1:0;
204   printf("%1u",j);
205   mask = mask >> 1;
206 }
207 printf("\n");
208
209}
210
211void cycle(unsigned int *data, unsigned int nbits)
212{
213 unsigned int i;
214 unsigned int result,rbit,lmask,rmask;
215 /*
216  * We need two masks.  One is a mask of all the significant bits
217  * in the rng, which may be as few as 24 and can easily be only
218  * 31.
219  *
220  * The other is lmask, with a single bit in the position of the
221  * leftmost significant bit.  We can make them both at once.
222  */
223 rmask = 1;
224 lmask = 1;
225 for(i=0;i<nbits-1;i++) {
226  rmask = rmask << 1;
227  rmask++;
228  lmask = lmask << 1;
229 }
230 if(verbose){
231   printf("%u bit rmask = ",nbits);
232   dumpbits(&rmask,32);
233   printf("%u bit lmask = ",nbits);
234   dumpbits(&lmask,32);
235 }
236 /*
237  * Next, we create mask the int being bit cycled into an internal
238  * register.
239  */
240 result = *data & rmask;
241 if(verbose){
242   printf("Original int: ");
243   dumpbits(data,32);
244   printf("  Masked int: ");
245   dumpbits(&result,32);
246 }
247 rbit = 1 & result;
248 result = result >> 1;
249 result += rbit*lmask;
250 *data = result;
251 if(verbose){
252   printf(" Rotated int: ");
253   dumpbits(data,32);
254 }
255
256}
257
258/*
259 * This is still a good idea, but we have to modify it so that it ONLY
260 * gets VALID bits by their absolute index.
261 */
262int get_bit(uint *rand_uint, unsigned int n)
263{
264
265 int i;
266 unsigned int index,offset,mask;
267 static unsigned last_rand;
268
269 /*
270  * This routine is designed to get the nth VALID bit of an input uint
271  * *rand_int.  The indexing is a bit tricky.  index tells us which vector
272  * element contains the bit being sought:
273  */
274 index = (int) (n/rmax_bits);
275
276 /*
277  * Then we have to compute the offset of the bit desired, starting from
278  * the first significant/valid bit in the unsigned int.
279  */
280 offset = (8*sizeof(unsigned int) - rmax_bits) + n%rmax_bits;
281 mask = (int)pow(2,8*sizeof(unsigned int) - 1);
282 mask = mask>>offset;
283 if(mask & rand_uint[index]){
284   return(1);
285 } else {
286   return(0);
287 }
288
289}
290
291int get_int_bit(uint i, uint n)
292{
293
294 unsigned int mask;
295
296 /*
297  * This routine gets the nth bit of the unsigned int i.  It does very
298  * limited checking to ensure that n is in the range 0-sizeof(uint)
299  * Note
300  */
301 if(n < 0 || n > 8*sizeof(uint)){
302   fprintf(stderr,"Error: bit offset %d exceeds length %d of uint.\n",n,8*sizeof(uint));
303   exit(0);
304 }
305
306
307 /*
308  * Then we have make a mask and shift it over from the first (least
309  * significant) bit in the unsigned int.  AND the result with i and
310  * we're done.
311  */
312 mask = 1;
313 mask = mask<<n;
314 /* dumpbits(&mask,32); */
315 /* dumpbits(&i,32); */
316 if(mask & i){
317   return(1);
318 } else {
319   return(0);
320 }
321
322}
323
324/*
325 * dumpbits only can dump 8*sizeof(unsigned int) bits at a time.
326 */
327void dumpbits_left(unsigned int *data, unsigned int nbits)
328{
329
330 int i,j;
331 unsigned int mask;
332
333 if(nbits > 8*sizeof(unsigned int)) {
334   nbits = 8*sizeof(unsigned int);
335 }
336
337 mask = 1;
338 for(i=0;i<nbits;i++){
339   if(mask & *data){
340     printf("1");
341   } else {
342     printf("0");
343   }
344   mask = mask << 1;
345 }
346 printf("\n");
347}
348
349
350unsigned int bit2uint(char *abit,unsigned int blen)
351{
352
353 int i,bit;
354 unsigned int result;
355
356 /* Debugging
357 if(verbose == D_BITS || verbose == D_ALL){
358   printf("# bit2uint(): converting %s\n",abit);
359 }
360 */
361
362 result = 0;
363 for(i = 0; i < blen; i++){
364   result = result << 1;
365   bit = abit[i] - '0';
366   result += bit;
367   /* Debugging
368   if(verbose == D_BITS || verbose == D_ALL){
369     printf("# bit2uint(): bit[%d] = %d, result = %u\n",i,bit,result);
370   }
371   */
372 }
373
374 /* Debugging
375 if(verbose == D_BITS || verbose == D_ALL){
376   printf("# bit2uint(): returning %0X\n",result);
377 }
378 */
379 return(result);
380
381}
382
383void fill_uint_buffer(uint *data,uint buflength)
384{
385
386 /*
387  * This routine fills *data with random bits from the current
388  * random number generator.  Note that MANY generators return
389  * fewer than 32 bits, making this a bit of a pain in the ass.
390  * We need buffers like this for several tests, though, so it
391  * is worth it to create a routine to do this once and for all.
392  */
393
394 uint bufbits,bcnt,bdelta;
395 uint i,tmp1,tmp2,mask;
396
397 /*
398  * Number of bits we must generate.
399  */
400 bufbits = buflength*sizeof(uint)*CHAR_BIT;
401 bdelta = sizeof(uint)*CHAR_BIT - rmax_bits;
402 mask = 0;
403 for(i=0;i<bdelta;i++) {
404  mask = mask<<1;
405  mask++;
406 }
407 if(verbose == D_BITS || verbose == D_ALL){
408   printf("rmax_bits = %d  bdelta = %d\n",rmax_bits,bdelta);
409 }
410
411 for(i=0;i<buflength;i++){
412
413   /* put rmax_bits into tmp1 */
414   tmp1 = gsl_rng_get(rng);
415   /* Cruft
416   printf("tmp1 = %10u = ",tmp1);
417   dumpbits(&tmp1,32);
418   */
419
420   /* Shift it over to the left */
421   tmp1 = tmp1 << bdelta;
422   /*
423   printf("tmp1 = %10u = ",tmp1);
424   dumpbits(&tmp1,32);
425   */
426
427   /* put rmax_bits into tmp2 */
428   tmp2 = gsl_rng_get(rng);
429   /*
430   printf("tmp2 = %10u = ",tmp2);
431   printf("mask = %10u = ",mask);
432   dumpbits(&mask,32);
433   */
434
435   /* mask the second rand */
436   tmp2 = tmp2&mask;
437   /*
438   printf("tmp2 = %10u = ",tmp2);
439   dumpbits(&tmp2,32);
440   */
441
442   /* Fill in the rest of the uint */
443   tmp1 = tmp1 + tmp2;
444   /* Cruft
445   printf("tmp1 = %10u = ",tmp1);
446   dumpbits(&tmp1,32);
447   */
448
449   data[i] = tmp1;
450 }
451
452}
453
454/*
455 * OK, the routines above work but they suck.  We need a set of SMALL
456 * bitlevel routines for manipulating, aggregating, windowing and so
457 * on, especially if we want to write extended versions of the bit
458 * distribution test.
459 *
460 * On that note, let's generate SMALL routines for:
461 *
462 *  a) creating a uint mask() to select bits from uints
463 *
464 *  b) grabbing a masked window of bits and left/right shifting it
465 *     to an arbitrary offset within the uint (no wraparound).
466 *
467 *  c) conjoining two such masked objects to produce a new object.
468 *
469 *  d) filling a buffer of arbitrary length with sequential bits from
470 *     the selected rng.
471 *
472 *  e) Selecting a given window from that buffer with a given bitwise offset
473 *     and with periodic wraparound.
474 *
475 *  f).... (we'll see what we need)
476 */
477
478
479/*
480 * This generates a uint-sized mask of 1's starting at bit position
481 * bstart FROM THE LEFT (with 0 being the most significant/sign bit)
482 * and ending at bit position bstop.
483 *
484 * That is:
485 *
486 * umask(3,9) generates a uint containing (first line indicates bit
487 * positions only):
488 *  01234567890123456789012345678901
489 *  00011111110000000000000000000000
490 */
491uint b_umask(uint bstart,uint bstop)
492{
493
494 uint b,mask,blen;
495
496 if(bstart < 0 || bstop > 31 || bstop < bstart){
497   printf("b_umask() error: bstart <= bstop must be in range 0-31.\n");
498   exit(0);
499 }
500 blen = bstop-bstart+1;
501
502 /*
503  * Create blen 1's on right
504  */
505 mask = 1;
506 for(b=1;b<blen;b++) {
507   mask = mask <<1;
508   mask++;
509   /* dumpbits(&mask,sizeof(uint)*CHAR_BIT); */
510 }
511
512 /*
513  * Now shift them over to the correct starting point.
514  */
515 mask = mask << (32-blen-bstart);
516 /* dumpbits(&mask,sizeof(uint)*CHAR_BIT); */
517
518 return mask;
519
520}
521
522/*
523 * This uses b_umask to grab a particular window's worth of bits
524 * from an arbitrary uint and THEN shift it to the new desired offset.
525 * bstart FROM THE LEFT (with 0 being the most significant/sign bit)
526 * and ending at bit position bstop.
527 *
528 * That is:
529 *
530 * b_window(input,2,5,0) generates a uint containing (first line indicates bit
531 * positions only):
532 *  input 01101011010000111010101001110110
533 *   mask 00111100000000000000000000000000
534 *      & 00101000000000000000000000000000
535 *  shift 10100000000000000000000000000000
536 */
537uint b_window(uint input,uint bstart,uint bstop,uint boffset)
538{
539
540 uint b,mask,output;
541 int shift;
542
543 if(bstart < 0 || bstop > 31 || bstop < bstart){
544   printf("b_umask() error: bstart <= bstop must be in range 0-31.\n");
545   exit(0);
546 }
547 if(boffset < 0 || boffset > 31){
548   printf("b_window() error: boffset must be in range 0-31.\n");
549   exit(0);
550 }
551 shift = bstart - boffset;
552
553 /* dumpbits(&input,sizeof(uint)*CHAR_BIT); */
554 mask = b_umask(bstart,bstop);
555 /* dumpbits(&mask,sizeof(uint)*CHAR_BIT); */
556 output = input & mask;
557 /* dumpbits(&output,sizeof(uint)*CHAR_BIT); */
558 if(shift>0){
559   output = output << shift;
560 } else {
561   output = output >> (-shift);
562 }
563 /* dumpbits(&output,sizeof(uint)*CHAR_BIT); */
564
565 return output;
566
567}
568
569/*
570 * Rotate the uint left (with periodic BCs)
571 */
572uint b_rotate_left(uint input,uint shift)
573{
574
575 uint tmp;
576
577 dumpbits(&input,sizeof(uint)*CHAR_BIT);
578 tmp = b_window(input,0,shift-1,32-shift);
579 dumpbits(&tmp,sizeof(uint)*CHAR_BIT);
580 input = input << shift;
581 dumpbits(&input,sizeof(uint)*CHAR_BIT);
582 input += tmp;
583 dumpbits(&input,sizeof(uint)*CHAR_BIT);
584
585 return input;
586
587}
588/*
589 * Rotate the uint right (with periodic BCs)
590 */
591uint b_rotate_right(uint input, uint shift)
592{
593
594 uint tmp;
595
596 dumpbits(&input,sizeof(uint)*CHAR_BIT);
597 tmp = b_window(input,32-shift,31,0);
598 dumpbits(&tmp,sizeof(uint)*CHAR_BIT);
599 input = input >> shift;
600 dumpbits(&input,sizeof(uint)*CHAR_BIT);
601 input += tmp;
602 dumpbits(&input,sizeof(uint)*CHAR_BIT);
603
604 return input;
605
606}
607
608/*
609 * OK, with this all in hand, we can NOW write routines to return
610 * pretty much any sort of string of bits from the prevailing rng
611 * without too much effort. Let's get an ntuple from a uint vector
612 * of arbitrary length and offset, with cyclic boundary conditions.
613 *
614 * We have to pack the answer into the LEAST significant bits in the
615 * output vector, BTW, not the MOST.  That is, we have to fill the
616 * output window all the way to the rightmost bit.  Tricky.
617 */
618void get_ntuple_cyclic(uint *input,uint ilen,
619    uint *output,uint olen,uint ntuple,uint offset)
620{
621
622 int istart,iend,istartbit,iendbit,ostart,oend,ostartbit,oendbit;
623 int i,j,bcnt,bdelta,uintlen;
624
625 uintlen = sizeof(uint)*CHAR_BIT;
626
627 /*
628  * We start somewhere in input[istart] and end in input[iend].
629  * Ditto output[ostart], output[oend].  We also need the BIT offsets
630  * in these uints.
631  *
632  * SO, suppose offset = 40, ntuple = 40.  Then istart = 40/32 = 1,
633  * iend = 80/32 = 2, istartbit = 40 - 32 = 8, iendbit = 96 - 80 = 16
634  * (where this is one bit PAST the end of the window!)
635  * The slice thus runs from:
636  *   index[1], bits 8-31 (total of 24 bits)
637  * to:
638  *   index[2], bits 0-15 (total of 16 bits)
639  *
640  * for a total of 40 bits starting at offset 1*32+8 = 40.  We can deal
641  * with periodic wraparound as we work though index by using i = i%ilen
642  * and trying to reach iend = iend%ilen;
643  */
644 istart = offset/uintlen;
645 iend = (offset+ntuple)/uintlen;
646 istartbit = offset - istart*uintlen;
647 iendbit = (istartbit + ntuple)%uintlen;
648 /* We want to point at the bit PAST the end */
649 if(iendbit == 0){
650   iend--;
651   iendbit = uintlen;
652 }
653 /*
654  * To accomplish periodic wraparound, we mod the
655  * istart and iend markers (and later, i itself).
656  */
657 istart = istart%ilen;
658 iend = iend%ilen;
659 printf("istart = %u, iend = %u, istartbit = %u, iendbit = %u\n",
660   istart,iend,istartbit,iendbit);
661 /*
662  * Now we have to do the same thing for the output vector.  Here life
663  * is a BIT better, as we can just copy the windows above straight over
664  * and shift things at the end.  So what we really have to do is figure
665  * out ostart.  Assuming that olen = 4, ostart in our example should be:
666  *  ostart = 4 - 40/32 - 1 = 2,
667  * so the answer will go in output[2] and output[3].
668  *
669  *
670  */
671 if((ntuple%uintlen) == 0){
672   ostart = olen - ntuple/uintlen;
673 } else {
674   ostart = olen - ntuple/uintlen - 1;
675 }
676 oend = olen - 1;
677 printf("ostart = %u, oend = %u,\n",ostart,oend);
678
679 /*
680  * Now we do a double loop, iterating BACKWARDS.
681  */
682 i = iend;
683 j = oend;
684
685 /*
686  * The only sane way to do this with periodic wraparound is to just
687  * count the bits returned and the chunk sizes.  There are only four
688  * (possible) chunks we might grab:
689  */
690 left = iendbit;
691 right = uintlen - iendbit;  /* May be zero */
692 /*
693  * This is a possible chunk, depending on whether or not it is
694  * less than right chunk.
695  */
696 if(istartbit > iendbit) {
697   topright = uintlen - istartbit;
698   topleft = 0;
699 } else {
700   topright = right;
701   topleft = iendbit - istartbit;
702 }
703
704 bcnt = 0;
705 while(bcnt < ntuple){
706   /*
707    * Second (least significant) part of output[j].
708    */
709   printf("Starting Fill Right: i = %d j = %d bcnt = %d\n",i,j,bcnt);
710
711   /* Fill right to terminate */
712   if((uintlen-istartbit) == (ntuple-bcnt)){
713     bdelta = uintlen-istartbit;
714     printf("3 Fill right from %d: output[%d] = ",i,j);
715     output[j] = b_window(input[i],istartbit,uintlen-1,bdelta);
716     bcnt += bdelta;
717     dumpbits(&output[j],sizeof(uint)*CHAR_BIT);
718     break;
719   }
720
721   /* Fill right to terminate */
722   if((ntuple-bcnt) == (iendbit-istartbit)){
723     printf("2 Fill right from %d: output[%d] = ",i,j);
724     output[j] = b_window(input[i],istartbit,iendbit-1,uintlen-iendbit+istartbit);
725     bcnt += iendbit-istartbit;
726     dumpbits(&output[j],sizeof(uint)*CHAR_BIT);
727     break;
728   }
729
730   /* Plain old fill right (all other cases) */
731   printf("1 Fill right from %d: output[%d] = ",i,j);
732   output[j] = b_window(input[i],0,iendbit-1,uintlen-iendbit);
733   bcnt += iendbit;
734   dumpbits(&output[j],sizeof(uint)*CHAR_BIT);
735   printf("Finished Fill Right: i = %d j = %d bcnt = %d\n",i,j,bcnt);
736   if(bcnt == ntuple) break;
737
738   /*
739    * Now we check for the First (most significant) part of output[j].
740    * It MUST come from the previous input line if any, so we decrement
741    * i.
742    */
743   i--;
744   if(i == -1) i = ilen-1;
745   printf("Starting Fill Left: i = %d j = %d bcnt = %d\n",i,j,bcnt);
746
747   if((ntuple-bcnt) > iendbit){
748     printf("1 Fill left from %d: output[%d] = ",i,j);
749     output[j] += b_window(input[i],iendbit,uintlen-1,0);
750     bcnt += uintlen-iendbit;
751     dumpbits(&output[j],sizeof(uint)*CHAR_BIT);
752   }
753
754   /* Fill left to terminate */
755   if((ntuple-bcnt) == (uintlen-iendbit)){
756      printf("2 Fill left from %d: output[%d] = ",i,j);
757      output[j] += b_window(input[i],iendbit,uintlen-1,0);
758      bcnt += uintlen-iendbit;
759      dumpbits(&output[j],sizeof(uint)*CHAR_BIT);
760      break;
761   }
762
763   /* Fill left to terminate */
764   if((ntuple-bcnt) == (uintlen-istartbit)){
765     printf("3 Fill left from %d: output[%d] = ",i,j);
766     output[j] += b_window(input[i],istartbit,uintlen-1,istartbit-iendbit);
767     bcnt += uintlen-istartbit;
768     dumpbits(&output[j],sizeof(uint)*CHAR_BIT);
769     break;
770   }
771
772   printf("Finished Fill Left: i = %d j = %d bcnt = %d\n",i,j,bcnt);
773   j--;
774
775 }
776
777}
778