Add VLQ builders for Word32 and Word64

This commit is contained in:
Andrew Martin 2023-04-17 12:01:30 -04:00
parent 41855c4911
commit 2a6f03725f
5 changed files with 93 additions and 1 deletions

View file

@ -242,6 +242,10 @@ tests = testGroup "Tests"
in runConcat 1 (foldMap word256BE xs)
===
runConcat 1 (word256ArrayBE ys 0 (Prelude.length xs))
, TQC.testProperty "word64Vlq" $ \(x :: Word64) ->
runConcat 1 (word64Vlq x)
===
naiveVlq (fromIntegral x)
, TQC.testProperty "word64LEB128" $ \(x :: Word64) ->
runConcat 1 (word64LEB128 x)
===
@ -416,3 +420,18 @@ naiveLeb128 x =
in if q == 0
then L.reverse xs'
else go xs' q
naiveVlq :: Natural -> ByteArray
naiveVlq x =
Bytes.toByteArray (Bytes.unsafeDrop 1 (Exts.fromList (0xFF : go [] x)))
where
go !xs !n =
let (q,r) = quotRem n 128
r' = fromIntegral @Natural @Word8 r
w = case xs of
[] -> r'
_ -> Bits.setBit r' 7
xs' = w : xs
in if q == 0
then xs'
else go xs' q