Static Arithmetic Coding

hard · compression, arithmetic-coding, entropy

Static Arithmetic (Range) Coding

Implement a static arithmetic coder using fixed frequencies.

Functions

func BuildFrequencies(data []byte) []int
func ArithmeticEncode(data []byte, freqs []int) []byte
func ArithmeticDecode(encoded []byte, freqs []int) []byte

Behavior

  • freqs has length 257 (symbols 0..255 plus EOF at index 256).
  • BuildFrequencies counts bytes in data and sets freqs[256] = 1.
  • ArithmeticEncode encodes data followed by an explicit EOF symbol.
  • ArithmeticDecode decodes until EOF is seen.

Constraints

  • Total frequency sum must fit within 20 bits (sum <= 1<<20).
  • Input length <= 4096 (to keep arithmetic ranges safe).

Notes

  • Use a fixed-precision arithmetic coder with renormalization and underflow handling.
  • Bitstream is MSB-first; trailing zero padding is allowed.
Run tests to see results
No issues detected