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
[]){
25 int (*il32
)(uint32_t) = ilog32
;
26 int (*il64
)(uint64_t) = ilog64
;
27 int (*il32_nz
)(uint32_t) = ilog32_nz
;
28 int (*il64_nz
)(uint64_t) = ilog64_nz
;
30 /*This is how many tests you plan to run.*/
31 plan_tests(33 * NTRIALS
* 3 + 65 * NTRIALS
* 3);
34 /*Test each bit in turn (and 0).*/
35 v
=i
?(uint32_t)1U<<(i
-1):0;
36 for(j
=0;j
<NTRIALS
;j
++){
39 ok1(STATIC_ILOG_32(v
)==l
);
41 ok1(il32_nz(v
) == l
|| v
== 0);
42 /*Also try a few more pseudo-random values with at most the same number
44 v
=(1103515245U*v
+12345U)&0xFFFFFFFFU
>>((33-i
)>>1)>>((32-i
)>>1);
50 /*Test each bit in turn (and 0).*/
51 v
=i
?(uint64_t)1U<<(i
-1):0;
52 for(j
=0;j
<NTRIALS
;j
++){
55 ok1(STATIC_ILOG_64(v
)==l
);
57 ok1(il64_nz(v
) == l
|| v
== 0);
58 /*Also try a few more pseudo-random values with at most the same number
60 v
=(uint64_t)((2862933555777941757ULL*v
+3037000493ULL)
61 &0xFFFFFFFFFFFFFFFFULL
>>((65-i
)>>1)>>((64-i
)>>1));