mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Replaces the inflate API from `inflateStream(reader: anytype, window_slice: []u8)` to `decompressor(allocator: mem.Allocator, reader: anytype, dictionary: ?[]const u8)` and `compressor(allocator: mem.Allocator, writer: anytype, options: CompressorOptions)`
34 lines
1,013 B
Zig
34 lines
1,013 B
Zig
const math = @import("std").math;
|
|
|
|
// Reverse bit-by-bit a N-bit code.
|
|
pub fn bitReverse(comptime T: type, value: T, N: usize) T {
|
|
const r = @bitReverse(T, value);
|
|
return r >> @intCast(math.Log2Int(T), @typeInfo(T).Int.bits - N);
|
|
}
|
|
|
|
test "bitReverse" {
|
|
const std = @import("std");
|
|
const expect = std.testing.expect;
|
|
|
|
const ReverseBitsTest = struct {
|
|
in: u16,
|
|
bit_count: u5,
|
|
out: u16,
|
|
};
|
|
|
|
var reverse_bits_tests = [_]ReverseBitsTest{
|
|
.{ .in = 1, .bit_count = 1, .out = 1 },
|
|
.{ .in = 1, .bit_count = 2, .out = 2 },
|
|
.{ .in = 1, .bit_count = 3, .out = 4 },
|
|
.{ .in = 1, .bit_count = 4, .out = 8 },
|
|
.{ .in = 1, .bit_count = 5, .out = 16 },
|
|
.{ .in = 17, .bit_count = 5, .out = 17 },
|
|
.{ .in = 257, .bit_count = 9, .out = 257 },
|
|
.{ .in = 29, .bit_count = 5, .out = 23 },
|
|
};
|
|
|
|
for (reverse_bits_tests) |h| {
|
|
var v = bitReverse(u16, h.in, h.bit_count);
|
|
try expect(v == h.out);
|
|
}
|
|
}
|