]> git.ozlabs.org Git - ccan/blob - ccan/ilog/_info
9a92f4e70e98f6ec87161dd18b6ccfd16df49bad
[ccan] / ccan / ilog / _info
1 /**
2  * ilog - Integer logarithm.
3  *
4  * ilog_32() and ilog_64() compute the minimum number of bits required to store
5  *  an unsigned 32-bit or 64-bit value without any leading zero bits.
6  * This can also be thought of as the location of the highest set bit, with
7  *  counting starting from one (so that 0 returns 0, 1 returns 1, and 2**31
8  *  returns 32).
9  * When the value is known to be non-zero ilog32_nz() and ilog64_nz() can
10  *  compile into as few as two instructions, one of which may get optimized out
11  *  later.
12  * STATIC_ILOG_32 and STATIC_ILOG_64 allow computation on compile-time
13  *  constants, so other compile-time constants can be derived from them.
14  *
15  * Example:
16  *  #include <stdio.h>
17  *  #include <limits.h>
18  *  #include <ccan/ilog/ilog.h>
19  *
20  *  int main(void){
21  *    int i;
22  *    printf("ilog32(0x%08X)=%i\n",0,ilog32(0));
23  *    for(i=1;i<=STATIC_ILOG_32(USHRT_MAX);i++){
24  *      uint32_t v;
25  *      v=(uint32_t)1U<<(i-1);
26  *      //Here we know v is non-zero, so we can use ilog32_nz().
27  *      printf("ilog32(0x%08X)=%i\n",v,ilog32_nz(v));
28  *    }
29  *    return 0;
30  *  }
31  *
32  * License: LGPL (v2.1 or any later version)
33  * Author: Timothy B. Terriberry <tterribe@xiph.org>
34  */
35 #include <string.h>
36 #include <stdio.h>
37 #include "config.h"
38
39 int main(int _argc,const char *_argv[]){
40   /*Expect exactly one argument.*/
41   if(_argc!=2)return 1;
42   if(strcmp(_argv[1],"depends")==0){
43     printf("ccan/compiler\n");
44     return 0;
45   }
46   return 1;
47 }