xref: /plan9-contrib/sys/src/9/port/logpow.c (revision 22402af72e824b13a03306b380e882c0b67c2e73)
1 /*
2  * log2 and pow2 routines.
3  */
4 #include "u.h"
5 #include "../port/lib.h"
6 #include "mem.h"
7 #include "dat.h"
8 #include "fns.h"
9 
10 /*
11  * these routines should be cheap enough that there will
12  * be no hesitation to use them.
13  *
14  * once 5c in-lines vlong ops, just use the vlong versions.
15  */
16 
17 int
ispow2(uvlong uvl)18 ispow2(uvlong uvl)
19 {
20 	return ISPOW2(uvl);
21 }
22 
23 static int
isulpow2(ulong ul)24 isulpow2(ulong ul)				/* temporary speed hack */
25 {
26 	return ISPOW2(ul);
27 }
28 
29 /*
30  * return exponent of smallest power of 2 ≥ n
31  */
32 int
log2(ulong n)33 log2(ulong n)
34 {
35 	int i;
36 
37 	i = BI2BY*BY2WD - 1 - clz(n);
38 	if (n == 0 || !ISPOW2(n))
39 		i++;
40 	return i;
41 }
42