authorgravatar for matthew.h.borkowski@gmail.comMatthew Borkowski <matthew.h.borkowski@gmail.com> 2021-05-14 23:18:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-20 18:47:16-04:00
log510f858143d12da029b5ea1f8fda13af411aff0b
tree5295bddd2cc837869910e9ffa733d385e70ae124
parent3845c264a6880a810a381ab4f7d0c03514350200

fix calculation of max_io_bits in PackedIntIo


1 files changed, 14 insertions(+), 6 deletions(-)

lib/std/packed_int_array.zig+14-6
......@@ -30,13 +30,13 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
3030 const min_io_bits = ((int_bits + 7) / 8) * 8;
3131
3232 //in the worst case, this is the number of bytes we need to touch
33 // to read or write a value, as bits
33 // to read or write a value, as bits. To calculate for int_bits > 1,
34 // set aside 2 bits to touch the first and last bytes, then divide
35 // by 8 to see how many bytes can be filled up inbetween.
3436 const max_io_bits = switch (int_bits) {
3537 0 => 0,
3638 1 => 8,
37 2...9 => 16,
38 10...65535 => ((int_bits / 8) + 2) * 8,
39 else => unreachable,
39 else => ((int_bits - 2) / 8 + 2) * 8,
4040 };
4141
4242 //we bitcast the desired Int type to an unsigned version of itself
......@@ -378,12 +378,20 @@ test "PackedIntArray" {
378378 }
379379}
380380
381test "PackedIntIo" {
382 const bytes = [_]u8 { 0b01101_000, 0b01011_110, 0b00011_101 };
383 try testing.expectEqual(@as(u15, 0x2bcd), PackedIntIo(u15, .Little).get(&bytes, 0, 3));
384 try testing.expectEqual(@as(u16, 0xabcd), PackedIntIo(u16, .Little).get(&bytes, 0, 3));
385 try testing.expectEqual(@as(u17, 0x1abcd), PackedIntIo(u17, .Little).get(&bytes, 0, 3));
386 try testing.expectEqual(@as(u18, 0x3abcd), PackedIntIo(u18, .Little).get(&bytes, 0, 3));
387}
388
381389test "PackedIntArray init" {
382390 if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest;
383391 const PackedArray = PackedIntArray(u3, 8);
384392 var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 });
385393 var i = @as(usize, 0);
386 while (i < packed_array.len()) : (i += 1) testing.expectEqual(@intCast(u3, i), packed_array.get(i));
394 while (i < packed_array.len()) : (i += 1) try testing.expectEqual(@intCast(u3, i), packed_array.get(i));
387395}
388396
389397test "PackedIntArray initAllTo" {
......@@ -391,7 +399,7 @@ test "PackedIntArray initAllTo" {
391399 const PackedArray = PackedIntArray(u3, 8);
392400 var packed_array = PackedArray.initAllTo(5);
393401 var i = @as(usize, 0);
394 while (i < packed_array.len()) : (i += 1) testing.expectEqual(@as(u3, 5), packed_array.get(i));
402 while (i < packed_array.len()) : (i += 1) try testing.expectEqual(@as(u3, 5), packed_array.get(i));
395403}
396404
397405test "PackedIntSlice" {