| ... | ... | @@ -66,32 +66,31 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 66 | 66 | |
| 67 | 67 | fn getBits(bytes: []const u8, comptime Container: type, bit_index: usize) Int { |
| 68 | 68 | const container_bits = @bitSizeOf(Container); |
| 69 | | const Shift = std.math.Log2Int(Container); |
| 70 | 69 | |
| 71 | 70 | const start_byte = bit_index / 8; |
| 72 | 71 | const head_keep_bits = bit_index - (start_byte * 8); |
| 73 | 72 | const tail_keep_bits = container_bits - (int_bits + head_keep_bits); |
| 74 | 73 | |
| 75 | 74 | //read bytes as container |
| 76 | | const value_ptr = @as(*align(1) const Container, @ptrCast(&bytes[start_byte])); |
| 75 | const value_ptr: *align(1) const Container = @ptrCast(&bytes[start_byte]); |
| 77 | 76 | var value = value_ptr.*; |
| 78 | 77 | |
| 79 | 78 | if (endian != native_endian) value = @byteSwap(value); |
| 80 | 79 | |
| 81 | 80 | switch (endian) { |
| 82 | 81 | .big => { |
| 83 | | value <<= @as(Shift, @intCast(head_keep_bits)); |
| 84 | | value >>= @as(Shift, @intCast(head_keep_bits)); |
| 85 | | value >>= @as(Shift, @intCast(tail_keep_bits)); |
| 82 | value <<= @intCast(head_keep_bits); |
| 83 | value >>= @intCast(head_keep_bits); |
| 84 | value >>= @intCast(tail_keep_bits); |
| 86 | 85 | }, |
| 87 | 86 | .little => { |
| 88 | | value <<= @as(Shift, @intCast(tail_keep_bits)); |
| 89 | | value >>= @as(Shift, @intCast(tail_keep_bits)); |
| 90 | | value >>= @as(Shift, @intCast(head_keep_bits)); |
| 87 | value <<= @intCast(tail_keep_bits); |
| 88 | value >>= @intCast(tail_keep_bits); |
| 89 | value >>= @intCast(head_keep_bits); |
| 91 | 90 | }, |
| 92 | 91 | } |
| 93 | 92 | |
| 94 | | return @as(Int, @bitCast(@as(UnInt, @truncate(value)))); |
| 93 | return @bitCast(@as(UnInt, @truncate(value))); |
| 95 | 94 | } |
| 96 | 95 | |
| 97 | 96 | /// Sets the integer at `index` to `val` within the packed data beginning |
| ... | ... | @@ -114,16 +113,16 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 114 | 113 | const start_byte = bit_index / 8; |
| 115 | 114 | const head_keep_bits = bit_index - (start_byte * 8); |
| 116 | 115 | const tail_keep_bits = container_bits - (int_bits + head_keep_bits); |
| 117 | | const keep_shift = switch (endian) { |
| 118 | | .big => @as(Shift, @intCast(tail_keep_bits)), |
| 119 | | .little => @as(Shift, @intCast(head_keep_bits)), |
| 116 | const keep_shift: Shift = switch (endian) { |
| 117 | .big => @intCast(tail_keep_bits), |
| 118 | .little => @intCast(head_keep_bits), |
| 120 | 119 | }; |
| 121 | 120 | |
| 122 | 121 | //position the bits where they need to be in the container |
| 123 | 122 | const value = @as(Container, @intCast(@as(UnInt, @bitCast(int)))) << keep_shift; |
| 124 | 123 | |
| 125 | 124 | //read existing bytes |
| 126 | | const target_ptr = @as(*align(1) Container, @ptrCast(&bytes[start_byte])); |
| 125 | const target_ptr: *align(1) Container = @ptrCast(&bytes[start_byte]); |
| 127 | 126 | var target = target_ptr.*; |
| 128 | 127 | |
| 129 | 128 | if (endian != native_endian) target = @byteSwap(target); |
| ... | ... | @@ -156,7 +155,7 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 156 | 155 | if (length == 0) return PackedIntSliceEndian(Int, endian).init(new_bytes[0..0], 0); |
| 157 | 156 | |
| 158 | 157 | var new_slice = PackedIntSliceEndian(Int, endian).init(new_bytes, length); |
| 159 | | new_slice.bit_offset = @as(u3, @intCast((bit_index - (start_byte * 8)))); |
| 158 | new_slice.bit_offset = @intCast((bit_index - (start_byte * 8))); |
| 160 | 159 | return new_slice; |
| 161 | 160 | } |
| 162 | 161 | |
| ... | ... | @@ -214,15 +213,14 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptim |
| 214 | 213 | /// Initialize a packed array using an unpacked array |
| 215 | 214 | /// or, more likely, an array literal. |
| 216 | 215 | pub fn init(ints: [int_count]Int) Self { |
| 217 | | var self = @as(Self, undefined); |
| 216 | var self: Self = undefined; |
| 218 | 217 | for (ints, 0..) |int, i| self.set(i, int); |
| 219 | 218 | return self; |
| 220 | 219 | } |
| 221 | 220 | |
| 222 | 221 | /// Initialize all entries of a packed array to the same value. |
| 223 | 222 | pub fn initAllTo(int: Int) Self { |
| 224 | | // TODO: use `var self = @as(Self, undefined);` https://github.com/ziglang/zig/issues/7635 |
| 225 | | var self = Self{ .bytes = [_]u8{0} ** total_bytes, .len = int_count }; |
| 223 | var self: Self = undefined; |
| 226 | 224 | self.setAll(int); |
| 227 | 225 | return self; |
| 228 | 226 | } |
| ... | ... | @@ -365,11 +363,11 @@ test "PackedIntArray" { |
| 365 | 363 | const expected_bytes = ((bits * int_count) + 7) / 8; |
| 366 | 364 | try testing.expect(@sizeOf(PackedArray) == expected_bytes); |
| 367 | 365 | |
| 368 | | var data = @as(PackedArray, undefined); |
| 366 | var data: PackedArray = undefined; |
| 369 | 367 | |
| 370 | 368 | //write values, counting up |
| 371 | | var i = @as(usize, 0); |
| 372 | | var count = @as(I, 0); |
| 369 | var i: usize = 0; |
| 370 | var count: I = 0; |
| 373 | 371 | while (i < data.len) : (i += 1) { |
| 374 | 372 | data.set(i, count); |
| 375 | 373 | if (bits > 0) count +%= 1; |
| ... | ... | @@ -395,17 +393,29 @@ test "PackedIntIo" { |
| 395 | 393 | } |
| 396 | 394 | |
| 397 | 395 | test "PackedIntArray init" { |
| 398 | | const PackedArray = PackedIntArray(u3, 8); |
| 399 | | var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| 400 | | var i = @as(usize, 0); |
| 401 | | while (i < packed_array.len) : (i += 1) try testing.expectEqual(@as(u3, @intCast(i)), packed_array.get(i)); |
| 396 | const S = struct { |
| 397 | fn doTheTest() !void { |
| 398 | const PackedArray = PackedIntArray(u3, 8); |
| 399 | var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| 400 | var i: usize = 0; |
| 401 | while (i < packed_array.len) : (i += 1) try testing.expectEqual(@as(u3, @intCast(i)), packed_array.get(i)); |
| 402 | } |
| 403 | }; |
| 404 | try S.doTheTest(); |
| 405 | try comptime S.doTheTest(); |
| 402 | 406 | } |
| 403 | 407 | |
| 404 | 408 | test "PackedIntArray initAllTo" { |
| 405 | | const PackedArray = PackedIntArray(u3, 8); |
| 406 | | var packed_array = PackedArray.initAllTo(5); |
| 407 | | var i = @as(usize, 0); |
| 408 | | while (i < packed_array.len) : (i += 1) try testing.expectEqual(@as(u3, 5), packed_array.get(i)); |
| 409 | const S = struct { |
| 410 | fn doTheTest() !void { |
| 411 | const PackedArray = PackedIntArray(u3, 8); |
| 412 | var packed_array = PackedArray.initAllTo(5); |
| 413 | var i: usize = 0; |
| 414 | while (i < packed_array.len) : (i += 1) try testing.expectEqual(@as(u3, 5), packed_array.get(i)); |
| 415 | } |
| 416 | }; |
| 417 | try S.doTheTest(); |
| 418 | try comptime S.doTheTest(); |
| 409 | 419 | } |
| 410 | 420 | |
| 411 | 421 | test "PackedIntSlice" { |
| ... | ... | @@ -433,8 +443,8 @@ test "PackedIntSlice" { |
| 433 | 443 | var data = P.init(&buffer, int_count); |
| 434 | 444 | |
| 435 | 445 | //write values, counting up |
| 436 | | var i = @as(usize, 0); |
| 437 | | var count = @as(I, 0); |
| 446 | var i: usize = 0; |
| 447 | var count: I = 0; |
| 438 | 448 | while (i < data.len) : (i += 1) { |
| 439 | 449 | data.set(i, count); |
| 440 | 450 | if (bits > 0) count +%= 1; |
| ... | ... | @@ -463,13 +473,13 @@ test "PackedIntSlice of PackedInt(Array/Slice)" { |
| 463 | 473 | const Int = std.meta.Int(.unsigned, bits); |
| 464 | 474 | |
| 465 | 475 | const PackedArray = PackedIntArray(Int, int_count); |
| 466 | | var packed_array = @as(PackedArray, undefined); |
| 476 | var packed_array: PackedArray = undefined; |
| 467 | 477 | |
| 468 | 478 | const limit = (1 << bits); |
| 469 | 479 | |
| 470 | | var i = @as(usize, 0); |
| 480 | var i: usize = 0; |
| 471 | 481 | while (i < packed_array.len) : (i += 1) { |
| 472 | | packed_array.set(i, @as(Int, @intCast(i % limit))); |
| 482 | packed_array.set(i, @intCast(i % limit)); |
| 473 | 483 | } |
| 474 | 484 | |
| 475 | 485 | //slice of array |
| ... | ... | @@ -524,20 +534,20 @@ test "PackedIntSlice accumulating bit offsets" { |
| 524 | 534 | // anything |
| 525 | 535 | { |
| 526 | 536 | const PackedArray = PackedIntArray(u3, 16); |
| 527 | | var packed_array = @as(PackedArray, undefined); |
| 537 | var packed_array: PackedArray = undefined; |
| 528 | 538 | |
| 529 | 539 | var packed_slice = packed_array.slice(0, packed_array.len); |
| 530 | | var i = @as(usize, 0); |
| 540 | var i: usize = 0; |
| 531 | 541 | while (i < packed_array.len - 1) : (i += 1) { |
| 532 | 542 | packed_slice = packed_slice.slice(1, packed_slice.len); |
| 533 | 543 | } |
| 534 | 544 | } |
| 535 | 545 | { |
| 536 | 546 | const PackedArray = PackedIntArray(u11, 88); |
| 537 | | var packed_array = @as(PackedArray, undefined); |
| 547 | var packed_array: PackedArray = undefined; |
| 538 | 548 | |
| 539 | 549 | var packed_slice = packed_array.slice(0, packed_array.len); |
| 540 | | var i = @as(usize, 0); |
| 550 | var i: usize = 0; |
| 541 | 551 | while (i < packed_array.len - 1) : (i += 1) { |
| 542 | 552 | packed_slice = packed_slice.slice(1, packed_slice.len); |
| 543 | 553 | } |
| ... | ... | @@ -552,7 +562,7 @@ test "PackedInt(Array/Slice) sliceCast" { |
| 552 | 562 | var packed_slice_cast_9 = packed_array.slice(0, (packed_array.len / 9) * 9).sliceCast(u9); |
| 553 | 563 | const packed_slice_cast_3 = packed_slice_cast_9.sliceCast(u3); |
| 554 | 564 | |
| 555 | | var i = @as(usize, 0); |
| 565 | var i: usize = 0; |
| 556 | 566 | while (i < packed_slice_cast_2.len) : (i += 1) { |
| 557 | 567 | const val = switch (native_endian) { |
| 558 | 568 | .big => 0b01, |
| ... | ... | @@ -576,9 +586,9 @@ test "PackedInt(Array/Slice) sliceCast" { |
| 576 | 586 | } |
| 577 | 587 | i = 0; |
| 578 | 588 | while (i < packed_slice_cast_3.len) : (i += 1) { |
| 579 | | const val = switch (native_endian) { |
| 580 | | .big => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), |
| 581 | | .little => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), |
| 589 | const val: u3 = switch (native_endian) { |
| 590 | .big => if (i % 2 == 0) 0b111 else 0b000, |
| 591 | .little => if (i % 2 == 0) 0b111 else 0b000, |
| 582 | 592 | }; |
| 583 | 593 | try testing.expect(packed_slice_cast_3.get(i) == val); |
| 584 | 594 | } |
| ... | ... | @@ -591,7 +601,7 @@ test "PackedInt(Array/Slice)Endian" { |
| 591 | 601 | try testing.expect(packed_array_be.bytes[0] == 0b00000001); |
| 592 | 602 | try testing.expect(packed_array_be.bytes[1] == 0b00100011); |
| 593 | 603 | |
| 594 | | var i = @as(usize, 0); |
| 604 | var i: usize = 0; |
| 595 | 605 | while (i < packed_array_be.len) : (i += 1) { |
| 596 | 606 | try testing.expect(packed_array_be.get(i) == i); |
| 597 | 607 | } |
| ... | ... | @@ -620,7 +630,7 @@ test "PackedInt(Array/Slice)Endian" { |
| 620 | 630 | try testing.expect(packed_array_be.bytes[3] == 0b00000001); |
| 621 | 631 | try testing.expect(packed_array_be.bytes[4] == 0b00000000); |
| 622 | 632 | |
| 623 | | var i = @as(usize, 0); |
| 633 | var i: usize = 0; |
| 624 | 634 | while (i < packed_array_be.len) : (i += 1) { |
| 625 | 635 | try testing.expect(packed_array_be.get(i) == i); |
| 626 | 636 | } |