/ reference / nim / proof_input / src / misc.nim
misc.nim
 1  
 2  #
 3  # helper functions
 4  #
 5  
 6  import std/math
 7  
 8  #-------------------------------------------------------------------------------
 9  
10  func floorLog2* (x: int): int = 
11    var k = -1
12    var y = x
13    while (y > 0):
14      k += 1
15      y = y shr 1
16    return k
17  
18  func ceilingLog2* (x: int): int = 
19    if (x==0):
20      return -1
21    else:
22      return (floorLog2(x-1) + 1)
23  
24  func exactLog2*(x: int): int = 
25    let k = ceilingLog2(x)
26    assert( x == 2^k, "exactLog2: not a power of two" )
27    return k
28  
29  func checkPowerOfTwo*(x: int , what: string): int = 
30    let k = ceilingLog2(x)
31    assert( x == 2^k, ("`" & what & "` is expected to be a power of 2") )
32    return x
33  
34  # wtf Nim, serously
35  func pow2*(k: int): int = 2^k
36  
37  #-------------------------------------------------------------------------------