1 #include <ccan/ilog/ilog.h>
2 #include <ccan/ilog/ilog.c>
4 #include <ccan/tap/tap.h>
6 /*Dead simple (but slow) versions to compare against.*/
8 static int test_ilog32(uint32_t _v){
10 for(ret=0;_v;ret++)_v>>=1;
14 static int test_ilog64(uint64_t _v){
16 for(ret=0;_v;ret++)_v>>=1;
22 int main(int _argc,const char *_argv[]){
26 /*This is how many tests you plan to run.*/
31 /*Test each bit in turn (and 0).*/
32 v=i?(uint32_t)1U<<(i-1):0;
33 for(j=0;j<NTRIALS;j++){
37 fprintf(stderr,"ILOG_32(0x%08lX): %i != %i\n",(long)v,ILOG_32(v),l);
41 fprintf(stderr,"ilog32(0x%08lX): %i != %i\n",(long)v,ilog32(v),l);
44 if(STATIC_ILOG_32(v)!=l){
45 fprintf(stderr,"STATIC_ILOG_32(0x%08lX): %i != %i\n",
46 (long)v,STATIC_ILOG_32(v),l);
49 /*Also try a few more pseudo-random values with at most the same number
51 v=(1103515245U*v+12345U)&0xFFFFFFFFU>>((33-i)>>1)>>((32-i)>>1);
54 ok1(nmatches==3*(32+1)*NTRIALS);
58 /*Test each bit in turn (and 0).*/
59 v=i?(uint64_t)1U<<(i-1):0;
60 for(j=0;j<NTRIALS;j++){
64 fprintf(stderr,"ILOG_64(0x%016llX): %i != %i\n",
65 (long long)v,ILOG_64(v),l);
69 fprintf(stderr,"ilog64(0x%016llX): %i != %i\n",
70 (long long)v,ilog64(v),l);
73 if(STATIC_ILOG_64(v)!=l){
74 fprintf(stderr,"STATIC_ILOG_64(0x%016llX): %i != %i\n",
75 (long long)v,STATIC_ILOG_64(v),l);
78 /*Also try a few more pseudo-random values with at most the same number
80 v=(uint64_t)((2862933555777941757ULL*v+3037000493ULL)
81 &0xFFFFFFFFFFFFFFFFULL>>((65-i)>>1)>>((64-i)>>1));
84 ok1(nmatches==3*(64+1)*NTRIALS);