0 | module Data.HashMap.Bits
 1 |
 2 | import System.Info
 3 |
 4 | export %inline
 5 | oneBits : Bits64
 6 | oneBits = 0xffff_ffff_ffff_ffff
 7 |
 8 | infixl 5 .|.
 9 |
10 | export %inline
11 | (.|.) : Bits64 -> Bits64 -> Bits64
12 | (.|.) = prim__or_Bits64
13 |
14 | infixl 7 .&.
15 |
16 | export %inline
17 | (.&.) : Bits64 -> Bits64 -> Bits64
18 | (.&.) = prim__and_Bits64
19 |
20 | export %inline
21 | not : Bits64 -> Bits64
22 | not = prim__xor_Bits64 oneBits
23 |
24 | export %inline
25 | shiftR : Bits64 -> Bits64 -> Bits64
26 | shiftR = prim__shr_Bits64
27 |
28 | export %inline
29 | shiftL : Bits64 -> Bits64 -> Bits64
30 | shiftL = prim__shl_Bits64
31 |
32 | export %inline
33 | bit : Bits32 -> Bits64
34 | bit k = 1 `shiftL` cast k
35 |
36 | export %inline
37 | setBit : Bits64 -> Bits32 -> Bits64
38 | setBit x k = bit k .|. x
39 |
40 | export %inline
41 | clearBit : Bits64 -> Bits32 -> Bits64
42 | clearBit x k = not (bit k) .&. x
43 |
44 | export %inline
45 | testBit : Bits64 -> Bits32 -> Bool
46 | testBit x k = (bit k .&. x) /= 0
47 |
48 | export %inline
49 | popCount : Bits64 -> Bits64
50 | popCount x0 =
51 |     -- see https://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-64-bit-Bits32eger
52 |     let x1 = (x0 .&. 0x5555555555555555) +
53 |              ((x0 `shiftR` 1) .&. 0x5555555555555555)
54 |         x2 = (x1 .&. 0x3333333333333333)
55 |              + ((x1 `shiftR` 2) .&. 0x3333333333333333)
56 |         x3 = ((x2 + (x2 `shiftR` 4)) .&. 0x0F0F0F0F0F0F0F0F)
57 |         x4 = (x3 * 0x0101010101010101) `shiftR` 56
58 |      in x4
59 |
60 | export
61 | isScheme : Bool
62 | isScheme = (codegen == "chez") || (codegen == "racket")
63 |
64 | export %inline
65 | unsafeIncr : Bits32 -> Bits32
66 | unsafeIncr x = if isScheme
67 |      then believe_me $ the Nat $ believe_me x + 1
68 |      else x + 1
69 |
70 | export %inline
71 | unsafeCast : Cast a b => a -> b
72 | unsafeCast x = if isScheme
73 |     then believe_me x
74 |     else cast x
75 |