Skip to content

Instantly share code, notes, and snippets.

View jorangreef's full-sized avatar

Joran Dirk Greef jorangreef

View GitHub Profile
const M = 12; // Probability scale for rANS state. Symbol frequencies in this log range. Usually 8-12.
const L = 23; // Renormalization factor to control dumping rANS state to bitstream. From rans_byte.h.
const m_min = 8 - 2 - (std.math.divCeil(u32, M, 4) catch unreachable); // Small-size-opt limit when compressing frequencies.
const m_max = [_]u16{m_min, m_min+16, m_min+16+256, m_min+16+256+4096, 1<<M}; // Size ranges for frequencies after small size limit.
fn compress(dst: anytype, src: []const u8) !void {
// Histogram for the frequency of each byte in input.
var hist = [_]u32{0} ** 256;
for (src) |byte| hist[byte] += 1;