Skip to content

Commit

Permalink
Rewrite bit_reader and bit_writer to take advantage of current zig se…
Browse files Browse the repository at this point in the history
…mantics and enhance readability (#21689)

Co-authored-by: Tanner Schultz <tgschultz@tgschultz-dl.tail7ba92.ts.net>
  • Loading branch information
tgschultz and Tanner Schultz authored Oct 14, 2024
1 parent e2e7996 commit ba569bb
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 256 deletions.
2 changes: 1 addition & 1 deletion lib/std/compress/zstandard/decode/block.zig
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ pub const DecodeState = struct {
};
fn readLiteralsBits(
self: *DecodeState,
bit_count_to_read: usize,
bit_count_to_read: u16,
) LiteralBitsError!u16 {
return self.literal_stream_reader.readBitsNoEof(u16, bit_count_to_read) catch bits: {
if (self.literal_streams == .four and self.literal_stream_index < 3) {
Expand Down
4 changes: 2 additions & 2 deletions lib/std/compress/zstandard/decode/huffman.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn decodeFseHuffmanTreeSlice(src: []const u8, compressed_size: usize, weights: *

fn assignWeights(
huff_bits: *readers.ReverseBitReader,
accuracy_log: usize,
accuracy_log: u16,
entries: *[1 << 6]Table.Fse,
weights: *[256]u4,
) !usize {
Expand All @@ -73,7 +73,7 @@ fn assignWeights(

while (i < 254) {
const even_data = entries[even_state];
var read_bits: usize = 0;
var read_bits: u16 = 0;
const even_bits = huff_bits.readBits(u32, even_data.bits, &read_bits) catch unreachable;
weights[i] = std.math.cast(u4, even_data.symbol) orelse return error.MalformedHuffmanTree;
i += 1;
Expand Down
10 changes: 5 additions & 5 deletions lib/std/compress/zstandard/readers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ pub const ReverseBitReader = struct {
if (i == 8) return error.BitStreamHasNoStartBit;
}

pub fn readBitsNoEof(self: *@This(), comptime U: type, num_bits: usize) error{EndOfStream}!U {
pub fn readBitsNoEof(self: *@This(), comptime U: type, num_bits: u16) error{EndOfStream}!U {
return self.bit_reader.readBitsNoEof(U, num_bits);
}

pub fn readBits(self: *@This(), comptime U: type, num_bits: usize, out_bits: *usize) error{}!U {
pub fn readBits(self: *@This(), comptime U: type, num_bits: u16, out_bits: *u16) error{}!U {
return try self.bit_reader.readBits(U, num_bits, out_bits);
}

Expand All @@ -55,19 +55,19 @@ pub const ReverseBitReader = struct {
}

pub fn isEmpty(self: ReverseBitReader) bool {
return self.byte_reader.remaining_bytes == 0 and self.bit_reader.bit_count == 0;
return self.byte_reader.remaining_bytes == 0 and self.bit_reader.count == 0;
}
};

pub fn BitReader(comptime Reader: type) type {
return struct {
underlying: std.io.BitReader(.little, Reader),

pub fn readBitsNoEof(self: *@This(), comptime U: type, num_bits: usize) !U {
pub fn readBitsNoEof(self: *@This(), comptime U: type, num_bits: u16) !U {
return self.underlying.readBitsNoEof(U, num_bits);
}

pub fn readBits(self: *@This(), comptime U: type, num_bits: usize, out_bits: *usize) !U {
pub fn readBits(self: *@This(), comptime U: type, num_bits: u16, out_bits: *u16) !U {
return self.underlying.readBits(U, num_bits, out_bits);
}

Expand Down
Loading

0 comments on commit ba569bb

Please sign in to comment.