示例#1
0
文件: decode.c 项目: Distrotech/dirac
int
main (int argc, char *argv[])
{
  int x;
  int i;

  schro_init();

  x = 100;
  for(i=0;i<N;i++){
    in_data[i] = rand_u8() < x;
  }

  dumpbits(in_data + 9900, 100);

  //n = encode_arith_dirac (out_data, in_data, N);
  //dumpbits(out_data, n);

  encode_arith_exp (out_data, in_data, N);
  //dumpbits(out_data, n);

  decode_arith_exp (c_data, out_data, N);
  dumpbits(c_data + 9900, 100);

  return 0;
}
示例#2
0
int diehard_count_1s_byte(Test **test, int irun)
{

 uint i,j,k,index5=0,index4,letter,t;
 uint boffset;
 uint count5[3125],count4[625];
 Vtest vtest4,vtest5;
 Xtest ptest;

 /*
  * count_1s in specific bytes is straightforward after looking over
  * count_1s in a byte.  The statistic is identical; we just have to
  * cycle the offset of the bytes selected and generate 1 random uint
  * per digit.
  */

 /*
  * I'm leaving this in so the chronically bored can validate that
  * the table exists and is correctly loaded and addressable.
  */
 if(verbose == -1){
   for(i=0;i<256;i++){
     printf("%u, ",b5b[i]);
     /* dumpbits(&i,8); */
     if((i+1)%16 == 0){
       printf("\n");
     }
   }
   exit(0);
 }

 /*
  * for display only.  0 means "ignored".
  */
 test[0]->ntuple = 0;

 /*
  * This is basically a pair of parallel vtests, with a final test
  * statistic generated from their difference (somehow).  We therefore
  * create two vtests, one for four digit base 5 integers and one for
  * five digit base 5 integers, and generate their expected values for
  * test[0]->tsamples trials.
  */

 ptest.y = 2500.0;
 ptest.sigma = sqrt(5000.0);

 Vtest_create(&vtest4,625);
 vtest4.cutoff = 5.0;
 for(i=0;i<625;i++){
   j = i;
   vtest4.y[i] = test[0]->tsamples;
   vtest4.x[i] = 0.0;
   /*
    * Digitize base 5, compute expected value for THIS integer i.
    */
   /* printf("%u:  ",j); */
   for(k=0;k<4;k++){
     /*
      * Take the least significant "letter" of j in range 0-4
      */
     letter = j%5;
     /*
      * multiply by the probability of getting this letter
      */
     vtest4.y[i] *= pb[letter];
     /*
      * Right shift j to get next digit.
      */
     /* printf("%1u",letter); */
     j /= 5;
   }
   /* printf(" = %f\n",vtest4.y[i]); */
 }

 Vtest_create(&vtest5,3125);
 vtest5.cutoff = 5.0;
 for(i=0;i<3125;i++){
   j = i;
   vtest5.y[i] = test[0]->tsamples;
   vtest5.x[i] = 0.0;
   /*
    * Digitize base 5, compute expected value for THIS integer i.
    */
   for(k=0;k<5;k++){
     /*
      * Take the least significant "letter" of j in range 0-4
      */
     letter = j%5;
     /*
      * multiply by the probability of getting this letter
      */
     vtest5.y[i] *= pb[letter];
     /*
      * Right shift j to get next digit.
      */
     j /= 5;
   }
 }

 /*
  * Here is the test.  We cycle boffset through test[0]->tsamples
  */
 boffset = 0;
 for(t=0;t<test[0]->tsamples;t++){

   boffset = t%32;  /* Remember that get_bit_ntuple periodic wraps the uint */
   /*
    * Get the next five bytes and make an index5 out of them, no
    * overlap.
    */
   for(k=0;k<5;k++){
     i = get_rand_bits_uint(32, 0xFFFFFFFF, rng);
     if(verbose == D_DIEHARD_COUNT_1S_STREAM || verbose == D_ALL){
       dumpbits(&i,32);
     }
     /*
      * get next byte from the last rand we generated.
      * Bauer fix - 
      *   Cruft: j = get_bit_ntuple_from_uint(i,8,0x000000FF,boffset);
      */
     j = get_bit_ntuple_from_whole_uint(i,8,0x000000FF,boffset);
     index5 = LSHIFT5(index5,b5b[j]);
     if(verbose == D_DIEHARD_COUNT_1S_STREAM || verbose == D_ALL){
       printf("b5b[%u] = %u, index5 = %u\n",j,b5b[j],index5);
       dumpbits(&j,8);
     }
   }
   /*
    * We use modulus to throw away the sixth digit in the left-shifted
    * base 5 index value, keeping the value of the 5-digit base 5 number
    * in the range 0 to 5^5-1 or 0 to 3124 decimal.  We repeat for the
    * four digit index.  At this point we increment the counts for index4
    * and index5.  Tres simple, no?
    */
   index5 = index5%3125;
   index4 = index5%625;
   vtest4.x[index4]++;
   vtest5.x[index5]++;
 }
 /*
  * OK, all that is left now is to figure out the statistic.
  */
 if(verbose == D_DIEHARD_COUNT_1S_BYTE || verbose == D_ALL){
   for(i = 0;i<625;i++){
     printf("%u:  %f    %f\n",i,vtest4.y[i],vtest4.x[i]);
   }
   for(i = 0;i<3125;i++){
     printf("%u:  %f    %f\n",i,vtest5.y[i],vtest5.x[i]);
   }
 }
 Vtest_eval(&vtest4);
 Vtest_eval(&vtest5);
 if(verbose == D_DIEHARD_COUNT_1S_BYTE || verbose == D_ALL){
   printf("vtest4.chisq = %f   vtest5.chisq = %f\n",vtest4.chisq,vtest5.chisq);
 }
 ptest.x = vtest5.chisq - vtest4.chisq;

 Xtest_eval(&ptest);
 test[0]->pvalues[irun] = ptest.pvalue;

 MYDEBUG(D_DIEHARD_COUNT_1S_BYTE) {
   printf("# diehard_count_1s_byte(): test[0]->pvalues[%u] = %10.5f\n",irun,test[0]->pvalues[irun]);
 }


 Vtest_destroy(&vtest4);
 Vtest_destroy(&vtest5);

 return(0);
}
示例#3
0
int binary_rank(uint **mtx,int mrows,int ncols)
{

 int i,j,k,m,n,s_uint,rank;
 int col_ind,uint_col_max;
 uint mask,colchk;
 uint *rowp;

 /*
  * mtx is a vector of unsigned integers, e.g.:
  *
  * mtx[0]       = 0110...110
  * mtx[1]       = 1010...001
  * ...
  * mtx[ncols-1] = 0010...100
  *
  * We go through this vector a row at a time, searching each
  * column for a pivot (1).  When we find a pivot, we swap rows
  * and eliminate the column bitwise.
  */

 /*
  * size of an unsigned int
  */
 s_uint = 8*sizeof(uint);
 /*
  * row size in uint columns.  Note that we have to remember
  * which uint ** column we are in and therefore have to
  * convert bit column into uint column regularly.
  * Subtract 1, because it is zero-basd.
  */
 uint_col_max = (ncols-1)/s_uint;

 if(verbose == D_BRANK || verbose == D_ALL){
   printf("Starting bitmatrix:\n");
   for(m=0;m<mrows;m++){
     printf("# br: ");
     dumpbits(&mtx[m][0],32);
   }
 }

 rank = 0;
 i = 0;
 mask = 1;
 /*
  * j is the column BIT index, which can be
  * larger than s_uint (or rmax_bits).
  */
 for(j = 0;j < ncols && i < mrows;j++){
   /*
    * This is the mxt[i][j] index of the
    * current bit column.
    */
   col_ind = j/s_uint; 

   /*
    * This handles the transition to the next
    * uint when the bit index j passes the uint
    * boundary.  mask picks out the correct bit
    * column from right to left (why not from
    * left to right?).
    */
   j%s_uint ? (mask <<=1) : (mask = 1);

   /*
    * Find a pivot element (a 1) if there is one
    * in this column (column fixed by mask).
    */
   if(verbose == D_BRANK || verbose == D_ALL){
     printf("Checking column mask ");
     dumpbits(&mask,32);
   }
   for(k=i;k<mrows;k++){
     colchk = mtx[k][col_ind]&mask;
     if(verbose == D_BRANK || verbose == D_ALL){
       printf("row %d = ",k);
       dumpbits(&colchk,32);
     }
     if(colchk) break;
   }

   /*
    * OK, k either points to a row with a "1" in
    * the mask column or it equals mrows.  In the latter
    * case, the entire column from i on down is zero
    * and we move on without incrementing rank.
    *
    * Otherwise, we bring the pivot ROW to the ith position
    * (which may involve doing nothing).  k is set to
    * point to the first location that could still be
    * a 1, which is always k+1;
    */
   if(k < mrows){
     if(verbose == D_BRANK || verbose == D_ALL){
       printf("Swapping %d and %d rows. before bitmatrix:\n",i,k);
       for(m=0;m<mrows;m++){
         printf("# br: ");
         dumpbits(&mtx[m][col_ind],32);
       }
     }
     if(i != k){
       if(verbose == D_BRANK || verbose == D_ALL){
         printf("before: mtx[%d] = %p  mtx[%d = %p\n",i,(void*) mtx[i],k,(void*) mtx[k]);
       }
       rowp = mtx[i]; /* rowp is the ADDRESS of the ith row */
       mtx[i] = mtx[k];
       mtx[k] = rowp;
       if(verbose == D_BRANK || verbose == D_ALL){
         printf("after mtx[%d] = %p  mtx[%d = %p\n",i,(void*) mtx[i],k,(void*) mtx[k]);
       }
     }
     if(verbose == D_BRANK || verbose == D_ALL){
       printf("Swapped %d and %d rows. after bitmatrix:\n",i,k);
       for(m=0;m<mrows;m++){
         printf("# br: ");
         dumpbits(&mtx[m][col_ind],32);
       }
     }
     k++;  /* First row that could have a 1 in this column after i */

     /*
      * Now we eliminate the rest of the column, by rows, starting
      * at k.
      */
     while(k<mrows){
       /*
        * if the row also has a 1 in this column...
        */
       if(mtx[k][col_ind] & mask){
         /*
          * we use exclusive or to eliminate the
          * rest of the column.
          */
         n = uint_col_max - col_ind;
         if(verbose == D_BRANK || verbose == D_ALL){
           printf("eliminating against row %2d = ",i);
           dumpbits(&mtx[i][col_ind],32);
           printf("eliminating row %2d, before = ",k);
           dumpbits(&mtx[k][col_ind],32);
         }
         while (n >= 0){
           if(verbose == D_BRANK || verbose == D_ALL){
             printf("xoring column = %2d\n",n);
	   }
           mtx[k][n] ^= mtx[i][n];
	   n--;
         }
         if(verbose == D_BRANK || verbose == D_ALL){
           printf("eliminating row %2d, after  = ",k);
           dumpbits(&mtx[k][col_ind],32);
           printf("\n");
         }
       }
       k++;
     }
     if(verbose == D_BRANK || verbose == D_ALL){
       printf("Eliminated. New bitmatrix:\n");
       for(m=0;m<mrows;m++){
         printf("# br: ");
         dumpbits(&mtx[m][col_ind],32);
       }
     }
     i++;
     if(verbose == D_BRANK || verbose == D_ALL){
       printf("NEW RANK = %d\n",i);
     }
   }
 }

 return(i);

}
void run_rgb_persist()
{

 /*
  * Declare the results struct.
  */
 Rgb_Persist persist;
 Test **rgb_persist_test;

 /*
  * First we create the test (to set some values displayed in test header
  * correctly).
  */
 rgb_persist_test = create_test(&rgb_persist_dtest,tsamples,psamples,&rgb_persist);

 /*
  * Set any GLOBAL data used by the test.
  *
  * We arbitrarily choose 256 successive random numbers as enough.
  * This should be plenty -- the probability of any bit slot not
  * changing by CHANCE in 256 tries is 2^-256 or almost 10^-26,
  * so even with 32 bit slots, repeatedly, we aren't horribly likely
  * to see it in our lifetime unless we run this test continuously for
  * months at a time (yes, a dumb idea).
  */
 rgb_persist_rand_uint = (unsigned int*)malloc(256 * sizeof(unsigned int));

 /*
  * Show the standard test header for this test.
  */
 show_test_header(&rgb_persist_dtest,rgb_persist_test);

 /*
  * Call the actual test that fills in the results struct.
  */
 rgb_persist(rgb_persist_test,&persist);

 if(strncmp("file_input",gsl_rng_name(rng),10) == 0){
   printf("# %u rands were used in this test\n",file_input_get_rtot(rng));
   printf("# The file %s was rewound %u times\n",gsl_rng_name(rng),file_input_get_rewind_cnt(rng));
 }
 printf("#==================================================================\n");
 printf("#                          Results\n");
 printf("# Results for %s rng, using its %d valid bits:\n",gsl_rng_name(rng),rmax_bits);
 printf("# (Cumulated mask of zero is good.)\n");
 printf("# cumulated_mask = %10u = ",persist.cumulative_mask);
 dumpbits(&persist.cumulative_mask,persist.nbits);
 printf("\n# randm_mask     = %10u = ",rmax_mask);
 dumpbits(&rmax_mask,persist.nbits);
 printf("\n# random_max     = %10u = ",random_max);
 dumpbits(&random_max,persist.nbits);
 if(persist.cumulative_mask){
   printf("\n# rgb_persist test FAILED (bits repeat)\n");
 } else {
   printf("\n# rgb_persist test PASSED (no bits repeat)\n");
 }
 printf("#==================================================================\n");

 free(rgb_persist_rand_uint);

}