Skip to content

Commit

Permalink
pp_sexpr cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmuth committed May 12, 2024
1 parent 6a261fc commit c6fc122
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 246 deletions.
112 changes: 49 additions & 63 deletions FrontEnd/Lib/bitstream.cw
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(module bitstream [] :


@doc """supports retrieval of bitfields up to 32 bit wide from underlying slice

not thread-safe"""
Expand All @@ -18,87 +17,74 @@ the exact number is bits_count"""
@doc """n must be from [0, 32]
may set eos
"""

@pub (fun Stream32GetBits [(param bs (ptr! Stream32))
(param bits_requested u8)] u32 :
(let! new_bits u32)
(let! bits_count u8 (^. bs bits_count))
(let! bits_cache u32 (as (^. bs bits_cache) u32))

@doc """when the while loop exits and bits_count > 32, new_bits contains
@pub (fun Stream32GetBits [(param bs (ptr! Stream32)) (param bits_requested u8)] u32 :
(let! new_bits u32)
(let! bits_count u8 (^. bs bits_count))
(let! bits_cache u32 (as (^. bs bits_cache) u32))
@doc """when the while loop exits and bits_count > 32, new_bits contains
(bits_count - 32) bits we still need to put into the cache"""
(while (< bits_count bits_requested) :
(if (== (^. bs offset) (len (^. bs buf))) :
(= (^. bs eos) true)
(return 0)
:)
(= new_bits (as (at (^. bs buf) (^. bs offset)) u32))
(+= (^. bs offset) 1)
(or= bits_cache (<< new_bits (as bits_count u32)))
(+= bits_count 8)
)

(while (< bits_count bits_requested) :
(if (== (^. bs offset) (len (^. bs buf))) :
(= (^. bs eos) true)
(return 0)
:)
(= new_bits (as (at (^. bs buf) (^. bs offset)) u32))
(+= (^. bs offset) 1)
(or= bits_cache (<< new_bits (as bits_count u32)))
(+= bits_count 8))
(let! out u32)
(if (< bits_requested 32) :
(= out (and bits_cache (- (<< 1_u32 (as bits_requested u32)) 1)))
(>>= bits_cache (as bits_requested u32))
:
@doc "bits_requested == 32"
(= out bits_cache)
(= bits_cache 0)
)

(= out (and bits_cache (- (<< 1_u32 (as bits_requested u32)) 1)))
(>>= bits_cache (as bits_requested u32))
:
@doc "bits_requested == 32"
(= out bits_cache)
(= bits_cache 0))
(if (>= bits_count 32) :
(>>= new_bits (- 40_u32 (as bits_count u32)))
(<<= new_bits (- 32_u32 (as bits_requested u32)))
(or= bits_cache new_bits)
:)

(-= bits_count bits_requested)
(= (^. bs bits_count) bits_count)
(= (^. bs bits_cache) (as bits_cache u8))
(>>= new_bits (- 40_u32 (as bits_count u32)))
(<<= new_bits (- 32_u32 (as bits_requested u32)))
(or= bits_cache new_bits)
:)
(-= bits_count bits_requested)
(= (^. bs bits_count) bits_count)
(= (^. bs bits_cache) (as bits_cache u8))
(return out))

(return out)
)

@doc "Resume bit retrieval at the next byte boundary"
@pub (fun Stream32SkipToNextByte [(param bs (ptr! Stream32))] void :
@doc "If there are any bits in the cache throw them away"
(= (^. bs bits_count) 0)
)
@doc "If there are any bits in the cache throw them away"
(= (^. bs bits_count) 0))


@pub (fun Stream32GetBool [(param bs (ptr! Stream32))] bool :
(return (as (Stream32GetBits [bs 1]) bool))
)
(return (as (Stream32GetBits [bs 1]) bool)))


@doc "may set eos bit"
@pub (fun Stream32GetByteSlice [(param bs (ptr! Stream32))
(param n uint)] (slice u8) :
(let! l uint (len (^. bs buf)))
(let! f auto (front (^. bs buf)))
(let offset uint (^. bs offset))

(if (> n (- l offset)) :
(= (^. bs eos) true)
(return (slice_val f 0))
:
(= (^. bs offset) (+ offset n))
(return (slice_val (pinc f offset) n))
)
@pub (fun Stream32GetByteSlice [(param bs (ptr! Stream32)) (param n uint)] (slice u8) :
(let! l uint (len (^. bs buf)))
(let! f auto (front (^. bs buf)))
(let offset uint (^. bs offset))
(if (> n (- l offset)) :
(= (^. bs eos) true)
(return (slice_val f 0))
:
(= (^. bs offset) (+ offset n))
(return (slice_val (pinc f offset) n))))

)

@doc "rounds down - bits_cache treated as consumed/empty"
@pub (fun Stream32BytesLeft [(param bs (ptr Stream32))] uint :
(return (- (len (^. bs buf)) (^. bs offset)))
)
(return (- (len (^. bs buf)) (^. bs offset))))


@doc "rounds up - bits_cache treated as consumed/empty"
@pub (fun Stream32BytesConsumed [(param bs (ptr Stream32))] uint :
(return (^. bs offset))
)
(return (^. bs offset)))


@pub (fun Stream32Eos [(param bs (ptr Stream32))] bool :
(return (^. bs eos))
(return (^. bs eos)))
)
)

97 changes: 72 additions & 25 deletions FrontEnd/Lib/bitstream_test.cw
Original file line number Diff line number Diff line change
@@ -1,71 +1,118 @@
(module main [] :
(import test)

(import fmt)

(import bitstream)

(global DataFF auto (array_val 1024 u8 [ 0xff ]))

(global DataFF auto (array_val 1024 u8 [0xff]))


(fun test1 [] void :
(@ref let! bs auto (rec_val bitstream::Stream32 [(field_val DataFF)]))
(test::AssertEq# (bitstream::Stream32GetBits [(&! bs) 1]) 1_u32)
(test::AssertEq# (bitstream::Stream32GetBits [(&! bs) 2]) 3_u32)
(test::AssertEq# (bitstream::Stream32GetBits [(&! bs) 3]) 7_u32)

(test::AssertEq# (bitstream::Stream32GetBits [(&! bs) 32]) 0xffffffff_u32)
(test::AssertEq# (. bs bits_count) 2_u8)
(test::AssertEq# (. bs bits_cache) 3_u8)
(test::AssertEq# (. bs offset) 5_uint)
(test::AssertFalse# (. bs eos))

(test::AssertEq# (bitstream::Stream32GetBits [(&! bs) 31]) 0x7fffffff_u32)
(test::AssertEq# (. bs bits_count) 3_u8)
(test::AssertEq# (. bs bits_cache) 7_u8)
(test::AssertEq# (. bs offset) 9_uint)
(test::AssertFalse# (. bs eos))

(test::AssertEq# (bitstream::Stream32GetBits [(&! bs) 30]) 0x3fffffff_u32)
(test::AssertEq# (. bs bits_count) 5_u8)
(test::AssertEq# (. bs bits_cache) 0x1f_u8)
(test::AssertEq# (. bs offset) 13_uint)
(test::AssertFalse# (. bs eos))

(test::AssertEq# (bitstream::Stream32BytesLeft [(& bs)]) 1011_uint)
(test::AssertEq# (front (bitstream::Stream32GetByteSlice [(&! bs) 1000_uint]))
(pinc (front DataFF) 13))
(test::AssertEq# (front (bitstream::Stream32GetByteSlice [(&! bs) 1000_uint])) (pinc (front DataFF) 13))
(test::AssertEq# (bitstream::Stream32BytesLeft [(& bs)]) 11_uint)
(test::AssertFalse# (. bs eos)))

(test::AssertFalse# (. bs eos))
)

(global Data123 auto (array_val 1024 u8 [
0x12 0x34 0x56 0x78 0x12 0x34 0x56 0x78
0x12 0x34 0x56 0x78 0x12 0x34 0x56 0x78
0x12 0x34 0x56 0x78 0x12 0x34 0x56 0x78
0x12 0x34 0x56 0x78 0x12 0x34 0x56 0x78
0x12 0x34 0x56 0x78 0x12 0x34 0x56 0x78
0x12 0x34 0x56 0x78 0x12 0x34 0x56 0x78
0x12 0x34 0x56 0x78 0x12 0x34 0x56 0x78
0x12 0x34 0x56 0x78 0x12 0x34 0x56 0x78
]))
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78
0x12
0x34
0x56
0x78]))


(fun test2 [] void :
(@ref let! bs auto (rec_val bitstream::Stream32 [(field_val Data123)]))
(test::AssertEq# (bitstream::Stream32GetBits [(&! bs) 4]) 2_u32)
(test::AssertEq# (bitstream::Stream32GetBits [(&! bs) 32])
0x27856341_u32)
(test::AssertEq# (bitstream::Stream32GetBits [(&! bs) 32]) 0x27856341_u32))

)

@cdecl (fun main [(param argc s32) (param argv (ptr (ptr u8)))] s32 :


(shed (test1 []))
(shed (test2 []))

@doc "test end"
(test::Success#)
(return 0))
)

)
Loading

0 comments on commit c6fc122

Please sign in to comment.