| ... | @@ -30,13 +30,13 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { | ... | @@ -30,13 +30,13 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 30 | const min_io_bits = ((int_bits + 7) / 8) * 8; | 30 | const min_io_bits = ((int_bits + 7) / 8) * 8; |
| 31 | | 31 | |
| 32 | //in the worst case, this is the number of bytes we need to touch | 32 | //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. |
| 34 | const max_io_bits = switch (int_bits) { | 36 | const max_io_bits = switch (int_bits) { |
| 35 | 0 => 0, | 37 | 0 => 0, |
| 36 | 1 => 8, | 38 | 1 => 8, |
| 37 | 2...9 => 16, | 39 | else => ((int_bits - 2) / 8 + 2) * 8, |
| 38 | 10...65535 => ((int_bits / 8) + 2) * 8, | | |
| 39 | else => unreachable, | | |
| 40 | }; | 40 | }; |
| 41 | | 41 | |
| 42 | //we bitcast the desired Int type to an unsigned version of itself | 42 | //we bitcast the desired Int type to an unsigned version of itself |
| ... | @@ -378,12 +378,20 @@ test "PackedIntArray" { | ... | @@ -378,12 +378,20 @@ test "PackedIntArray" { |
| 378 | } | 378 | } |
| 379 | } | 379 | } |
| 380 | | 380 | |
| | 381 | test "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 | |
| 381 | test "PackedIntArray init" { | 389 | test "PackedIntArray init" { |
| 382 | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; | 390 | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; |
| 383 | const PackedArray = PackedIntArray(u3, 8); | 391 | const PackedArray = PackedIntArray(u3, 8); |
| 384 | var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); | 392 | var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| 385 | var i = @as(usize, 0); | 393 | 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)); |
| 387 | } | 395 | } |
| 388 | | 396 | |
| 389 | test "PackedIntArray initAllTo" { | 397 | test "PackedIntArray initAllTo" { |
| ... | @@ -391,7 +399,7 @@ test "PackedIntArray initAllTo" { | ... | @@ -391,7 +399,7 @@ test "PackedIntArray initAllTo" { |
| 391 | const PackedArray = PackedIntArray(u3, 8); | 399 | const PackedArray = PackedIntArray(u3, 8); |
| 392 | var packed_array = PackedArray.initAllTo(5); | 400 | var packed_array = PackedArray.initAllTo(5); |
| 393 | var i = @as(usize, 0); | 401 | 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)); |
| 395 | } | 403 | } |
| 396 | | 404 | |
| 397 | test "PackedIntSlice" { | 405 | test "PackedIntSlice" { |