File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change 1
- __all__ = [ "log2_int" , "bits_for" ]
1
+ import operator
2
2
3
+ __all__ = ["ceil_log2" , "exact_log2" , "log2_int" , "bits_for" ]
4
+
5
+
6
+ def ceil_log2 (n ):
7
+ """Returns the integer log2 of the smallest power-of-2 greater than or equal to `n`.
8
+
9
+ Raises a `ValueError` for negative inputs."""
10
+ n = operator .index (n )
11
+ if n < 0 :
12
+ raise ValueError ("{n} is negative" )
13
+ if n == 0 :
14
+ return 0
15
+ return (n - 1 ).bit_length ()
16
+
17
+
18
+ def exact_log2 (n ):
19
+ """Returns the integer log2 of `n`, which must be an exact power of two.
20
+
21
+ Raises a `ValueError` if `n` is not a power of two."""
22
+ n = operator .index (n )
23
+ if n <= 0 or (n & (n - 1 )):
24
+ raise ValueError ("{n} is not a power of 2" )
25
+ return (n - 1 ).bit_length ()
3
26
4
27
def log2_int (n , need_pow2 = True ):
5
28
if n == 0 :
You can’t perform that action at this time.
0 commit comments