@@ -1,5 +1,7 @@
1module Prelude.Bits
1module Prelude.Bits
3%default total
5%default total
5log2ceil : Nat -> Nat
7log2ceil : Nat -> Nat
@@ -67,7 +69,7 @@ pow x y = if y > 0
69%assert_total
71%assert_total
70−intToBits' : {n: Nat} -> Int -> nextBits n
72+intToBits' : {n : Nat} -> Int -> nextBits n
71intToBits' {n=n} x with (nextBits n)
73intToBits' {n=n} x with (nextBits n)
72 | Bits8 = let pad = (prim__intToB8 (cast (8-n))) in
74 | Bits8 = let pad = (prim__intToB8 (cast (8-n))) in
73 prim__lshrB8 (prim__shlB8 (prim__intToB8 x) pad) pad
75 prim__lshrB8 (prim__shlB8 (prim__intToB8 x) pad) pad
@@ -85,6 +87,17 @@ intToBits n = MkBits (intToBits' n)
85instance Cast Int (Bits n) where
87instance Cast Int (Bits n) where
86 cast = intToBits
88 cast = intToBits
90+unsafeBitsToInt' : {n : Nat} -> nextBits n -> Int
91+unsafeBitsToInt' {n=n} x with (nextBits n)
92+ | Bits8 = prim__B32ToInt (prim__zextB8_32 x)
93+ | Bits16 = prim__B32ToInt (prim__zextB16_32 x)
94+ | Bits32 = prim__B32ToInt x
95+ | Bits64 = prim__B32ToInt (prim__truncB64_32 x)
98+unsafeBitsToInt : Bits n -> Int
99+unsafeBitsToInt (MkBits x) = unsafeBitsToInt' x
88bitsShl' : {n: Nat} -> nextBits n -> nextBits n -> nextBits n
101bitsShl' : {n: Nat} -> nextBits n -> nextBits n -> nextBits n
89bitsShl' {n=n} x c with (nextBits n)
102bitsShl' {n=n} x c with (nextBits n)
90 | Bits8 = pad8' n prim__shlB8 x c
103 | Bits8 = pad8' n prim__shlB8 x c
@@ -308,11 +321,9 @@ bitSet n x = bitsAnd (intToBits 1 `bitsShl` intToBits (cast (finToNat n))) x /=
310bitsToStr : Bits n -> String
323bitsToStr : Bits n -> String
311−bitsToStr x = helper last x
324+bitsToStr x = pack (helper last x)
313 %assert_total
326 %assert_total
314− helper : Fin (S n) -> Bits n -> String
327+ helper : Fin (S n) -> Bits n -> List Char
315− helper fO _ = ""
328+ helper fO _ = []
317− (if bitSet x b then "1" else "0")
329+ helper (fS x) b = (if bitSet x b then '1' else '0') :: helper (weaken x) b
318− ++ helper (weaken x) b