@@ -4,7 +4,7 @@ import Data.Fin
5%default total
5%default total
8nextPow2 : Nat -> Nat
8nextPow2 : Nat -> Nat
9nextPow2 Z = Z
9nextPow2 Z = Z
10nextPow2 (S x) = if (S x) == (2 `power` l2x)
10nextPow2 (S x) = if (S x) == (2 `power` l2x)
@@ -13,11 +13,11 @@ nextPow2 (S x) = if (S x) == (2 `power` l2x)
14 l2x = log2NZ (S x) SIsNotZ
14 l2x = log2NZ (S x) SIsNotZ
16−public
16+public export
17nextBytes : Nat -> Nat
17nextBytes : Nat -> Nat
18nextBytes bits = (nextPow2 (divCeilNZ bits 8 SIsNotZ))
18nextBytes bits = (nextPow2 (divCeilNZ bits 8 SIsNotZ))
20−public
20+public export
21machineTy : Nat -> Type
21machineTy : Nat -> Type
22machineTy Z = Bits8
22machineTy Z = Bits8
23machineTy (S Z) = Bits16
23machineTy (S Z) = Bits16
@@ -47,7 +47,7 @@ natToBits {n=n} x with (n)
47getPad : Nat -> machineTy n
47getPad : Nat -> machineTy n
48getPad n = natToBits (minus (bitsUsed (nextBytes n)) n)
48getPad n = natToBits (minus (bitsUsed (nextBytes n)) n)
50−public
50+public export
51data Bits : Nat -> Type where
51data Bits : Nat -> Type where
52 MkBits : machineTy (nextBytes n) -> Bits n
52 MkBits : machineTy (nextBytes n) -> Bits n
@@ -99,7 +99,7 @@ shiftLeft' {n=n} x c with (nextBytes n)
99 | S (S Z) = pad32' n prim__shlB32 x c
99 | S (S Z) = pad32' n prim__shlB32 x c
100 | S (S (S _)) = pad64' n prim__shlB64 x c
100 | S (S (S _)) = pad64' n prim__shlB64 x c
102−public
102+public export
103shiftLeft : Bits n -> Bits n -> Bits n
103shiftLeft : Bits n -> Bits n -> Bits n
104shiftLeft (MkBits x) (MkBits y) = MkBits (shiftLeft' x y)
104shiftLeft (MkBits x) (MkBits y) = MkBits (shiftLeft' x y)
@@ -110,7 +110,7 @@ shiftRightLogical' {n=n} x c with (n)
110 | S (S Z) = prim__lshrB32 x c
110 | S (S Z) = prim__lshrB32 x c
111 | S (S (S _)) = prim__lshrB64 x c
111 | S (S (S _)) = prim__lshrB64 x c
113−public
113+public export
114shiftRightLogical : Bits n -> Bits n -> Bits n
114shiftRightLogical : Bits n -> Bits n -> Bits n
115shiftRightLogical {n} (MkBits x) (MkBits y)
115shiftRightLogical {n} (MkBits x) (MkBits y)
116 = MkBits {n} (shiftRightLogical' {n=nextBytes n} x y)
116 = MkBits {n} (shiftRightLogical' {n=nextBytes n} x y)
@@ -122,7 +122,7 @@ shiftRightArithmetic' {n=n} x c with (nextBytes n)
122 | S (S Z) = pad32' n prim__ashrB32 x c
122 | S (S Z) = pad32' n prim__ashrB32 x c
123 | S (S (S _)) = pad64' n prim__ashrB64 x c
123 | S (S (S _)) = pad64' n prim__ashrB64 x c
125−public
125+public export
126shiftRightArithmetic : Bits n -> Bits n -> Bits n
126shiftRightArithmetic : Bits n -> Bits n -> Bits n
127shiftRightArithmetic (MkBits x) (MkBits y) = MkBits (shiftRightArithmetic' x y)
127shiftRightArithmetic (MkBits x) (MkBits y) = MkBits (shiftRightArithmetic' x y)
@@ -133,7 +133,7 @@ and' {n=n} x y with (n)
133 | S (S Z) = prim__andB32 x y
133 | S (S Z) = prim__andB32 x y
134 | S (S (S _)) = prim__andB64 x y
134 | S (S (S _)) = prim__andB64 x y
136−public
136+public export
137and : Bits n -> Bits n -> Bits n
137and : Bits n -> Bits n -> Bits n
138and {n} (MkBits x) (MkBits y) = MkBits (and' {n=nextBytes n} x y)
138and {n} (MkBits x) (MkBits y) = MkBits (and' {n=nextBytes n} x y)
@@ -144,7 +144,7 @@ or' {n=n} x y with (n)
144 | S (S Z) = prim__orB32 x y
144 | S (S Z) = prim__orB32 x y
145 | S (S (S _)) = prim__orB64 x y
145 | S (S (S _)) = prim__orB64 x y
147−public
147+public export
148or : Bits n -> Bits n -> Bits n
148or : Bits n -> Bits n -> Bits n
149or {n} (MkBits x) (MkBits y) = MkBits (or' {n=nextBytes n} x y)
149or {n} (MkBits x) (MkBits y) = MkBits (or' {n=nextBytes n} x y)
@@ -155,7 +155,7 @@ xor' {n=n} x y with (n)
155 | S (S Z) = prim__xorB32 x y
155 | S (S Z) = prim__xorB32 x y
156 | S (S (S _)) = prim__xorB64 x y
156 | S (S (S _)) = prim__xorB64 x y
158−public
158+public export
159xor : Bits n -> Bits n -> Bits n
159xor : Bits n -> Bits n -> Bits n
160xor {n} (MkBits x) (MkBits y) = MkBits {n} (xor' {n=nextBytes n} x y)
160xor {n} (MkBits x) (MkBits y) = MkBits {n} (xor' {n=nextBytes n} x y)
@@ -166,7 +166,7 @@ plus' {n=n} x y with (nextBytes n)
166 | S (S Z) = pad32 n prim__addB32 x y
166 | S (S Z) = pad32 n prim__addB32 x y
167 | S (S (S _)) = pad64 n prim__addB64 x y
167 | S (S (S _)) = pad64 n prim__addB64 x y
169−public
169+public export
170plus : Bits n -> Bits n -> Bits n
170plus : Bits n -> Bits n -> Bits n
171plus (MkBits x) (MkBits y) = MkBits (plus' x y)
171plus (MkBits x) (MkBits y) = MkBits (plus' x y)
@@ -177,7 +177,7 @@ minus' {n=n} x y with (nextBytes n)
177 | S (S Z) = pad32 n prim__subB32 x y
177 | S (S Z) = pad32 n prim__subB32 x y
178 | S (S (S _)) = pad64 n prim__subB64 x y
178 | S (S (S _)) = pad64 n prim__subB64 x y
180−public
180+public export
181minus : Bits n -> Bits n -> Bits n
181minus : Bits n -> Bits n -> Bits n
182minus (MkBits x) (MkBits y) = MkBits (minus' x y)
182minus (MkBits x) (MkBits y) = MkBits (minus' x y)
@@ -188,7 +188,7 @@ times' {n=n} x y with (nextBytes n)
188 | S (S Z) = pad32 n prim__mulB32 x y
188 | S (S Z) = pad32 n prim__mulB32 x y
189 | S (S (S _)) = pad64 n prim__mulB64 x y
189 | S (S (S _)) = pad64 n prim__mulB64 x y
191−public
191+public export
192times : Bits n -> Bits n -> Bits n
192times : Bits n -> Bits n -> Bits n
193times (MkBits x) (MkBits y) = MkBits (times' x y)
193times (MkBits x) (MkBits y) = MkBits (times' x y)
@@ -200,7 +200,7 @@ sdiv' {n=n} x y with (nextBytes n)
200 | S (S Z) = prim__sdivB32 x y
200 | S (S Z) = prim__sdivB32 x y
201 | S (S (S _)) = prim__sdivB64 x y
201 | S (S (S _)) = prim__sdivB64 x y
203−public partial
203+public export partial
204sdiv : Bits n -> Bits n -> Bits n
204sdiv : Bits n -> Bits n -> Bits n
205sdiv (MkBits x) (MkBits y) = MkBits (sdiv' x y)
205sdiv (MkBits x) (MkBits y) = MkBits (sdiv' x y)
@@ -212,7 +212,7 @@ udiv' {n=n} x y with (nextBytes n)
212 | S (S Z) = prim__udivB32 x y
212 | S (S Z) = prim__udivB32 x y
213 | S (S (S _)) = prim__udivB64 x y
213 | S (S (S _)) = prim__udivB64 x y
215−public partial
215+public export partial
216udiv : Bits n -> Bits n -> Bits n
216udiv : Bits n -> Bits n -> Bits n
217udiv (MkBits x) (MkBits y) = MkBits (udiv' x y)
217udiv (MkBits x) (MkBits y) = MkBits (udiv' x y)
@@ -224,7 +224,7 @@ srem' {n=n} x y with (nextBytes n)
224 | S (S Z) = prim__sremB32 x y
224 | S (S Z) = prim__sremB32 x y
225 | S (S (S _)) = prim__sremB64 x y
225 | S (S (S _)) = prim__sremB64 x y
227−public partial
227+public export partial
228srem : Bits n -> Bits n -> Bits n
228srem : Bits n -> Bits n -> Bits n
229srem (MkBits x) (MkBits y) = MkBits (srem' x y)
229srem (MkBits x) (MkBits y) = MkBits (srem' x y)
@@ -236,7 +236,7 @@ urem' {n=n} x y with (nextBytes n)
236 | S (S Z) = prim__uremB32 x y
236 | S (S Z) = prim__uremB32 x y
237 | S (S (S _)) = prim__uremB64 x y
237 | S (S (S _)) = prim__uremB64 x y
239−public partial
239+public export partial
240urem : Bits n -> Bits n -> Bits n
240urem : Bits n -> Bits n -> Bits n
241urem (MkBits x) (MkBits y) = MkBits (urem' x y)
241urem (MkBits x) (MkBits y) = MkBits (urem' x y)
@@ -302,7 +302,7 @@ complement' {n=n} x with (nextBytes n)
302 | S (S (S _)) = let pad = getPad {n=3} n in
302 | S (S (S _)) = let pad = getPad {n=3} n in
303 prim__complB64 (x `prim__shlB64` pad) `prim__lshrB64` pad
303 prim__complB64 (x `prim__shlB64` pad) `prim__lshrB64` pad
305−public
305+public export
306complement : Bits n -> Bits n
306complement : Bits n -> Bits n
307complement (MkBits x) = MkBits (complement' x)
307complement (MkBits x) = MkBits (complement' x)
@@ -321,7 +321,7 @@ zext' {n=n} {m=m} x with (nextBytes n, nextBytes (n+m))
321 | (S (S Z), S (S (S _))) = believe_me (prim__zextB32_B64 (believe_me x))
321 | (S (S Z), S (S (S _))) = believe_me (prim__zextB32_B64 (believe_me x))
322 | (S (S (S _)), S (S (S _))) = believe_me x
322 | (S (S (S _)), S (S (S _))) = believe_me x
324−public
324+public export
325zeroExtend : Bits n -> Bits (n+m)
325zeroExtend : Bits n -> Bits (n+m)
326zeroExtend (MkBits x) = MkBits (zext' x)
326zeroExtend (MkBits x) = MkBits (zext' x)
@@ -337,7 +337,7 @@ intToBits' {n=n} x with (nextBytes n)
337 | S (S (S _)) = let pad = getPad {n=3} n in
337 | S (S (S _)) = let pad = getPad {n=3} n in
338 prim__lshrB64 (prim__shlB64 (prim__truncBigInt_B64 x) pad) pad
338 prim__lshrB64 (prim__shlB64 (prim__truncBigInt_B64 x) pad) pad
340−public
340+public export
341intToBits : Integer -> Bits n
341intToBits : Integer -> Bits n
342intToBits n = MkBits (intToBits' n)
342intToBits n = MkBits (intToBits' n)
@@ -351,7 +351,7 @@ bitsToInt' {n=n} x with (nextBytes n)
351 | S (S Z) = prim__zextB32_BigInt x
351 | S (S Z) = prim__zextB32_BigInt x
352 | S (S (S _)) = prim__zextB64_BigInt x
352 | S (S (S _)) = prim__zextB64_BigInt x
354−public
354+public export
355bitsToInt : Bits n -> Integer
355bitsToInt : Bits n -> Integer
356bitsToInt (MkBits x) = bitsToInt' x
356bitsToInt (MkBits x) = bitsToInt' x
@@ -393,7 +393,7 @@ sext' {n=n} {m=m} x with (nextBytes n, nextBytes (n+m))
393 | (S (S (S _)), S (S (S _))) = let pad = getPad {n=3} n in
393 | (S (S (S _)), S (S (S _))) = let pad = getPad {n=3} n in
394 believe_me (prim__ashrB64 (prim__shlB64 (believe_me x) pad) pad)
394 believe_me (prim__ashrB64 (prim__shlB64 (believe_me x) pad) pad)
396−--public
396+--public export
397--signExtend : Bits n -> Bits (n+m)
397--signExtend : Bits n -> Bits (n+m)
398--signExtend {m=m} (MkBits x) = MkBits (zeroUnused (sext' x))
398--signExtend {m=m} (MkBits x) = MkBits (zeroUnused (sext' x))
@@ -412,23 +412,23 @@ trunc' {m=m} {n=n} x with (nextBytes n, nextBytes (m+n))
412 | (S (S Z), S (S (S _))) = believe_me (prim__truncB64_B32 (believe_me x))
412 | (S (S Z), S (S (S _))) = believe_me (prim__truncB64_B32 (believe_me x))
413 | (S (S (S _)), S (S (S _))) = believe_me x
413 | (S (S (S _)), S (S (S _))) = believe_me x
415−public
415+public export
416truncate : Bits (m+n) -> Bits n
416truncate : Bits (m+n) -> Bits n
417truncate (MkBits x) = MkBits (zeroUnused (trunc' x))
417truncate (MkBits x) = MkBits (zeroUnused (trunc' x))
419−public
419+public export
420bitAt : Fin n -> Bits n
420bitAt : Fin n -> Bits n
421bitAt n = intToBits 1 `shiftLeft` intToBits (cast n)
421bitAt n = intToBits 1 `shiftLeft` intToBits (cast n)
423−public
423+public export
424getBit : Fin n -> Bits n -> Bool
424getBit : Fin n -> Bits n -> Bool
425getBit n x = (x `and` (bitAt n)) /= intToBits 0
425getBit n x = (x `and` (bitAt n)) /= intToBits 0
427−public
427+public export
428setBit : Fin n -> Bits n -> Bits n
428setBit : Fin n -> Bits n -> Bits n
429setBit n x = x `or` (bitAt n)
429setBit n x = x `or` (bitAt n)
431−public
431+public export
432unsetBit : Fin n -> Bits n -> Bits n
432unsetBit : Fin n -> Bits n -> Bits n
433unsetBit n x = x `and` complement (bitAt n)
433unsetBit n x = x `and` complement (bitAt n)