bits.bend checks
raw source on the hub · import bend-kit-bits@0.3.0.0/bits.bend as Bits
Bit readers and writers over Bytes, LSB-first (DEFLATE) or MSB-first (JPEG), and canonical Huffman codes and decode tables. Source: https://github.com/paymog/bend-kit/tree/main/bits
2 imports
import Base import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes
Types
type Reader source · line 61 · raw
Type
A reader keeps its total and remaining bit counts as Nat.
Reader@buf:Array<U32> -> @total:Nat -> @left:Nat -> Reader
type Writer source · line 135 · raw
Type
A writer's buf has 2^dep words, all 0 past bit pos; msb is the order of the last write.
Writer@buf:Array<U32> -> @dep:U32 -> @pos:Nat -> @msb:Bool -> Writer
type Huff source · line 205 · raw
Type
A root of up to nine bits; longer codes use fixed-width secondary tables.
Huff@msb:Bool -> @max:U32 -> @table:Array<U32> -> @sub:Array<U32> -> Huff
type Code source · line 209 · raw
Data
One symbol and the length of its code.
Code@sym:U32 -> @len:U32 -> Code
type Encoding source · line 415 · raw
Data
A symbol's canonical value, with its first stream bit at bit len - 1.
Encoding@sym:U32 -> @len:U32 -> @value:U32 -> Encoding
Definitions
def pos.index source · line 8 · raw
@+p:Nat -> U32
Bytes packs byte i at bits 8*(i%4) of word i/4, so LSB-first stream bit p is bit p%32 of word p/32, and MSB-first it is bit 31 - p%32 of that word byte-swapped. A U32 shift by 32 or more gives 0; the code below relies on it.
def pos.offset source · line 11 · raw
@+p:Nat -> U32
def pos.bytes source · line 14 · raw
@+p:Nat -> U32
def pos.align source · line 17 · raw
@+p:Nat -> Nat
def pos.words source · line 20 · raw
@+p:Nat -> U32
def pos.left source · line 23 · raw
@+len:U32 -> @+p:Nat -> Nat
def mask source · line 26 · raw
@+n:U32 -> U32
def win.lsb.join source · line 29 · raw
@+s:U32 -> @+lo:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)
def win.msb.join source · line 33 · raw
@+s:U32 -> @+lo:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)
def win.lsb.at source · line 37 · raw
@+k:U32 -> @s:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)
def win.msb.at source · line 41 · raw
@+k:U32 -> @s:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)
def win.at source · line 45 · raw
@msb:Bool -> @a:Array<U32> -> @+k:U32 -> @s:U32 -> Pair(Array<U32>, U32)
def win source · line 53 · raw
@msb:Bool -> @a:Array<U32> -> @+p:Nat -> Pair(Array<U32>, U32)
Two words hold the next 32 stream bits; callers mask bits past the end.
def field source · line 57 · raw
@+msb:Bool -> @+n:U32 -> @+w:U32 -> U32
The first n (at most 32) bits of a window as a number, first stream bit most significant MSB-first.
def Reader.at source · line 64 · raw
@len:U32 -> @buf:Array<U32> -> @+pos:Nat -> Reader
def Reader.new source · line 68 · raw
@b:0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes -> Reader
def Reader.finish source · line 74 · raw
@r:Reader -> Pair(0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes, Nat)
The buffer back, and the bit position.
def Reader.left source · line 78 · raw
@r:Reader -> Pair(Reader, Nat)
def read.got source · line 82 · raw
@msb:Bool -> @total:Nat -> @+left:Nat -> @+n:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Reader, Maybe<&2, U32>)
def read.if source · line 86 · raw
@ok:Bool -> @+msb:Bool -> @buf:Array<U32> -> @+total:Nat -> @+left:Nat -> @+n:U32 -> Pair(Reader, Maybe<&2, U32>)
def read source · line 93 · raw
@+msb:Bool -> @r:Reader -> @+n:U32 -> Pair(Reader, Maybe<&2, U32>)
def Reader.lsb source · line 99 · raw
@r:Reader -> @+n:U32 -> Pair(Reader, Maybe<&2, U32>)
The next n bits (0 to 32) as a number, and the reader past them. None, with the reader unchanged, when n > 32 or fewer than n bits are left.
def Reader.msb source · line 102 · raw
@r:Reader -> @+n:U32 -> Pair(Reader, Maybe<&2, U32>)
def peek.got source · line 106 · raw
@+msb:Bool -> @total:Nat -> @left:Nat -> @+n:U32 -> @+avail:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Reader, U32)
Bits past the end read as 0: the low avail bits stay LSB-first, the high ones MSB-first.
def peek source · line 111 · raw
@+msb:Bool -> @r:Reader -> @+n0:U32 -> Pair(Reader, U32)
def Reader.peek.lsb source · line 117 · raw
@r:Reader -> @+n:U32 -> Pair(Reader, U32)
The next n bits (at most 32) without moving. Bits past the end read as 0.
def Reader.peek.msb source · line 120 · raw
@r:Reader -> @+n:U32 -> Pair(Reader, U32)
def Reader.skip source · line 124 · raw
@r:Reader -> @+n:Nat -> Pair(Reader, Bool)
Moves n bits on. False, with the reader unchanged, when fewer than n are left.
def Reader.align source · line 130 · raw
@r:Reader -> Reader
Moves to the next byte boundary, as a DEFLATE stored block or a JPEG marker needs.
def Writer.new source · line 138 · raw
Writer
def room source · line 142 · raw
@full:Bool -> @w:Writer -> Writer
A write adds at most 32 bits, so one doubling always makes room.
def or.of source · line 150 · raw
@+k:U32 -> @+x:U32 -> @r:Pair(Array<U32>, U32) -> Array<U32>
def or.if source · line 154 · raw
@some:Bool -> @a:Array<U32> -> @+k:U32 -> @+x:U32 -> Array<U32>
def parts source · line 162 · raw
@msb:Bool -> @+s:U32 -> @+n:U32 -> @+v:U32 -> Pair(U32, U32)
The parts of an n-bit v that land in word k and word k + 1, as stored words.
def write.put source · line 171 · raw
@+k:U32 -> @x:Pair(U32, U32) -> @a:Array<U32> -> Array<U32>
def write.at source · line 175 · raw
@+msb:Bool -> @w:Writer -> @+n:U32 -> @+v:U32 -> Writer
def write source · line 180 · raw
@+msb:Bool -> @w:Writer -> @+n0:U32 -> @+v:U32 -> Writer
The two orders put a stream bit at different places in its byte, so an order switch starts a new byte.
def Writer.lsb source · line 188 · raw
@w:Writer -> @+n:U32 -> @+v:U32 -> Writer
Appends the low n bits of v (n is cut to 32). LSB-first sends v's lowest bit first; MSB-first sends bit n - 1 first. A write in the other order than the last one first pads to a byte boundary.
def Writer.msb source · line 191 · raw
@w:Writer -> @+n:U32 -> @+v:U32 -> Writer
def Writer.align source · line 195 · raw
@w:Writer -> Writer
Pads with 0 bits to the next byte boundary.
def Writer.finish source · line 200 · raw
@w:Writer -> 0x632652e3c1c0183f12728faf3870bb68/bytes.Bytes
The bytes written; 0 bits fill the last byte.
def lens.max source · line 212 · raw
@xs:List<&2, Code> -> @+m:U32 -> U32
def count.of source · line 219 · raw
@+len:U32 -> @r:Pair(Array<U32>, U32) -> Array<U32>
def count.if source · line 223 · raw
@zero:Bool -> @c:Array<U32> -> @+len:U32 -> Array<U32>
def count source · line 230 · raw
@xs:List<&2, Code> -> @c:Array<U32> -> Array<U32>
def next.go source · line 239 · raw
@k:Nat -> @+l:U32 -> @+code:U32 -> @+left:U32 -> @+ok:Bool -> @n:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Bool)
RFC 1951 §3.2.2: next[l] = (next[l-1] + count[l-1]) << 1. r holds count[l-1]; left counts the codes of length l - 1 still free, and a negative left means the lengths over-subscribe.
def fill source · line 249 · raw
@f:Nat -> @t:Array<U32> -> @+i:U32 -> @+step:U32 -> @+e:U32 -> Array<U32>
def rev source · line 256 · raw
@k:Nat -> @+v:U32 -> @+acc:U32 -> U32
def assign.put source · line 264 · raw
@+msb:Bool -> @+max:U32 -> @+sym:U32 -> @+len:U32 -> @r:Pair(Array<U32>, U32) -> @t:Array<U32> -> Pair(Array<U32>, Array<U32>)
MSB-first, a code fills a run of consecutive entries; LSB-first, its bits reversed fill every 2^len-th.
def assign.one source · line 271 · raw
@zero:Bool -> @+msb:Bool -> @+max:U32 -> @+sym:U32 -> @+len:U32 -> @st:Pair(Array<U32>, Array<U32>) -> Pair(Array<U32>, Array<U32>)
def prefix source · line 280 · raw
@+msb:Bool -> @+root:U32 -> @+len:U32 -> @+code:U32 -> U32
def reserve.hit source · line 284 · raw
@empty:Bool -> @+i:U32 -> @+width:U32 -> @+used:U32 -> @t:Array<U32> -> Pair(Array<U32>, U32)
Root length 31 marks a secondary-table offset; valid code lengths never exceed 16.
def reserve.entry source · line 291 · raw
@+i:U32 -> @+width:U32 -> @used:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)
def reserve.long source · line 295 · raw
@long:Bool -> @+msb:Bool -> @+root:U32 -> @+max:U32 -> @+len:U32 -> @+code:U32 -> @t:Array<U32> -> @+used:U32 -> Pair(Array<U32>, U32)
def reserve.state source · line 303 · raw
@n:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Pair(Array<U32>, U32))
def reserve.one source · line 307 · raw
@+msb:Bool -> @+root:U32 -> @+max:U32 -> @+len:U32 -> @t:Array<U32> -> @used:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Pair(Array<U32>, U32))
def reserve source · line 311 · raw
@xs:List<&2, Code> -> @+msb:Bool -> @+root:U32 -> @+max:U32 -> @st:Pair(Array<U32>, Pair(Array<U32>, U32)) -> Pair(Array<U32>, U32)
def depth source · line 320 · raw
@k:Nat -> @+size:U32 -> @+d:Nat -> Nat
def assign.short source · line 327 · raw
@sub:Array<U32> -> @r:Pair(Array<U32>, Array<U32>) -> Pair(Array<U32>, Pair(Array<U32>, Array<U32>))
def assign.tail source · line 331 · raw
@+msb:Bool -> @+root:U32 -> @+max:U32 -> @+sym:U32 -> @+len:U32 -> @+code:U32 -> @n:Array<U32> -> @sub:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Pair(Array<U32>, Array<U32>))
def assign.long source · line 339 · raw
@+msb:Bool -> @+root:U32 -> @+max:U32 -> @sym:U32 -> @+len:U32 -> @t:Array<U32> -> @sub:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Pair(Array<U32>, Array<U32>))
def assign.split source · line 343 · raw
@long:Bool -> @+msb:Bool -> @+root:U32 -> @+max:U32 -> @+sym:U32 -> @+len:U32 -> @n:Array<U32> -> @t:Array<U32> -> @sub:Array<U32> -> Pair(Array<U32>, Pair(Array<U32>, Array<U32>))
def assign.two source · line 350 · raw
@xs:List<&2, Code> -> @+msb:Bool -> @+root:U32 -> @+max:U32 -> @st:Pair(Array<U32>, Pair(Array<U32>, Array<U32>)) -> Pair(Array<U32>, Array<U32>)
def build.done source · line 359 · raw
@msb:Bool -> @max:U32 -> @r:Pair(Array<U32>, Array<U32>) -> Huff
def build.reserved source · line 363 · raw
@+msb:Bool -> @+max:U32 -> @+root:U32 -> @xs:List<&2, Code> -> @n:Array<U32> -> @r:Pair(Array<U32>, U32) -> Huff
def build.cloned source · line 367 · raw
@+msb:Bool -> @+max:U32 -> @+root:U32 -> @+xs:List<&2, Code> -> @r:Pair(Array<U32>, Array<U32>) -> Huff
def build.two source · line 371 · raw
@+msb:Bool -> @+max:U32 -> @+xs:List<&2, Code> -> @n:Array<U32> -> Huff
def build.choose source · line 374 · raw
@small:Bool -> @+msb:Bool -> @+max:U32 -> @xs:List<&2, Code> -> @n:Array<U32> -> Huff
def build.next source · line 381 · raw
@+msb:Bool -> @+max:U32 -> @xs:List<&2, Code> -> @r:Pair(Array<U32>, Bool) -> Maybe<&1, Huff>
def build.short source · line 389 · raw
@ok:Bool -> @+msb:Bool -> @+max:U32 -> @+xs:List<&2, Code> -> Maybe<&1, Huff>
def Huff.from_codes source · line 399 · raw
@+msb:Bool -> @+xs:List<&2, Code> -> Maybe<&1, Huff>
A table from codes. Codes go shortest first, ties in list order; length 0 means no code. None when a length exceeds 16 or the lengths over-subscribe. An incomplete set is allowed, and decode answers None for its unused codes. Symbols must be below 2^27.
def codes source · line 403 · raw
@xs:List<&2, U32> -> @+i:U32 -> List<&2, Code>
def Huff.from_lens source · line 411 · raw
@+msb:Bool -> @+lens:List<&2, U32> -> Maybe<&1, Huff>
Symbol i has length lens[i], as DEFLATE sends them (RFC 1951 §3.2.2).
def encode.put source · line 418 · raw
@+sym:U32 -> @+len:U32 -> @acc:List<&2, Encoding> -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, List<&2, Encoding>)
def encode.one source · line 422 · raw
@zero:Bool -> @+sym:U32 -> @+len:U32 -> @st:Pair(Array<U32>, List<&2, Encoding>) -> Pair(Array<U32>, List<&2, Encoding>)
def encode.go source · line 430 · raw
@xs:List<&2, Code> -> @st:Pair(Array<U32>, List<&2, Encoding>) -> List<&2, Encoding>
def encode.next source · line 438 · raw
@xs:List<&2, Code> -> @r:Pair(Array<U32>, Bool) -> Maybe<&2, List<&2, Encoding>>
def encode.short source · line 446 · raw
@ok:Bool -> @+xs:List<&2, Code> -> Maybe<&2, List<&2, Encoding>>
def Huff.encode_codes source · line 454 · raw
@+xs:List<&2, Code> -> Maybe<&2, List<&2, Encoding>>
Input order, without zero lengths; validation and canonical assignment match Huff.from_codes.
def Huff.encode_lens source · line 457 · raw
@lens:List<&2, U32> -> Maybe<&2, List<&2, Encoding>>
def Writer.code source · line 461 · raw
@msb:Bool -> @w:Writer -> @e:Encoding -> Writer
Huffman codes send their most significant bit first in either stream order.
def decode.hit source · line 472 · raw
@+msb:Bool -> @+max:U32 -> @+total:Nat -> @+left:Nat -> @buf:Array<U32> -> @sub:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Huff, Pair(Reader, Maybe<&2, U32>))
Bits past the end may be garbage in w: a code's entry repeats over every suffix, so only its own len bits pick it, and len <= left keeps them inside the stream.
def decode.sub source · line 478 · raw
@long:Bool -> @+msb:Bool -> @+max:U32 -> @+w:U32 -> @+e:U32 -> @sub:Array<U32> -> Pair(Array<U32>, U32)
def decode.found source · line 488 · raw
@+msb:Bool -> @+max:U32 -> @+total:Nat -> @+left:Nat -> @buf:Array<U32> -> @t:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Huff, Pair(Reader, Maybe<&2, U32>))
def decode.root source · line 492 · raw
@+msb:Bool -> @+max:U32 -> @+total:Nat -> @+left:Nat -> @w:U32 -> @buf:Array<U32> -> @sub:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Huff, Pair(Reader, Maybe<&2, U32>))
def decode.look source · line 496 · raw
@+msb:Bool -> @+max:U32 -> @+total:Nat -> @+left:Nat -> @t:Array<U32> -> @sub:Array<U32> -> @r:Pair(Array<U32>, U32) -> Pair(Huff, Pair(Reader, Maybe<&2, U32>))
def Huff.decode source · line 502 · raw
@h:Huff -> @r:Reader -> Pair(Huff, Pair(Reader, Maybe<&2, U32>))
One symbol, and the reader past its code. None, with the reader unchanged, when the next bits start no code or the input ends inside one.