authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-03-28 14:03:45+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-03-28 15:24:01+02:00
log0588595128c4534e3a6bc207d446b66032d5720c
tree52319167c55c4de7e86e281ed02673744ba30ac6
parent2cdc48a6326f61c17947867ed9bf61cba606ce97

std.PackedInt: remove workaround for stage1 bug

Closes #7635

1 files changed, 53 insertions(+), 43 deletions(-)

lib/std/packed_int_array.zig+53-43
......@@ -66,32 +66,31 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
6666
6767 fn getBits(bytes: []const u8, comptime Container: type, bit_index: usize) Int {
6868 const container_bits = @bitSizeOf(Container);
69 const Shift = std.math.Log2Int(Container);
7069
7170 const start_byte = bit_index / 8;
7271 const head_keep_bits = bit_index - (start_byte * 8);
7372 const tail_keep_bits = container_bits - (int_bits + head_keep_bits);
7473
7574 //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]);
7776 var value = value_ptr.*;
7877
7978 if (endian != native_endian) value = @byteSwap(value);
8079
8180 switch (endian) {
8281 .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);
8685 },
8786 .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);
9190 },
9291 }
9392
94 return @as(Int, @bitCast(@as(UnInt, @truncate(value))));
93 return @bitCast(@as(UnInt, @truncate(value)));
9594 }
9695
9796 /// 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 {
114113 const start_byte = bit_index / 8;
115114 const head_keep_bits = bit_index - (start_byte * 8);
116115 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),
120119 };
121120
122121 //position the bits where they need to be in the container
123122 const value = @as(Container, @intCast(@as(UnInt, @bitCast(int)))) << keep_shift;
124123
125124 //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]);
127126 var target = target_ptr.*;
128127
129128 if (endian != native_endian) target = @byteSwap(target);
......@@ -156,7 +155,7 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
156155 if (length == 0) return PackedIntSliceEndian(Int, endian).init(new_bytes[0..0], 0);
157156
158157 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)));
160159 return new_slice;
161160 }
162161
......@@ -214,15 +213,14 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptim
214213 /// Initialize a packed array using an unpacked array
215214 /// or, more likely, an array literal.
216215 pub fn init(ints: [int_count]Int) Self {
217 var self = @as(Self, undefined);
216 var self: Self = undefined;
218217 for (ints, 0..) |int, i| self.set(i, int);
219218 return self;
220219 }
221220
222221 /// Initialize all entries of a packed array to the same value.
223222 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;
226224 self.setAll(int);
227225 return self;
228226 }
......@@ -365,11 +363,11 @@ test "PackedIntArray" {
365363 const expected_bytes = ((bits * int_count) + 7) / 8;
366364 try testing.expect(@sizeOf(PackedArray) == expected_bytes);
367365
368 var data = @as(PackedArray, undefined);
366 var data: PackedArray = undefined;
369367
370368 //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;
373371 while (i < data.len) : (i += 1) {
374372 data.set(i, count);
375373 if (bits > 0) count +%= 1;
......@@ -395,17 +393,29 @@ test "PackedIntIo" {
395393}
396394
397395test "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();
402406}
403407
404408test "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();
409419}
410420
411421test "PackedIntSlice" {
......@@ -433,8 +443,8 @@ test "PackedIntSlice" {
433443 var data = P.init(&buffer, int_count);
434444
435445 //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;
438448 while (i < data.len) : (i += 1) {
439449 data.set(i, count);
440450 if (bits > 0) count +%= 1;
......@@ -463,13 +473,13 @@ test "PackedIntSlice of PackedInt(Array/Slice)" {
463473 const Int = std.meta.Int(.unsigned, bits);
464474
465475 const PackedArray = PackedIntArray(Int, int_count);
466 var packed_array = @as(PackedArray, undefined);
476 var packed_array: PackedArray = undefined;
467477
468478 const limit = (1 << bits);
469479
470 var i = @as(usize, 0);
480 var i: usize = 0;
471481 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));
473483 }
474484
475485 //slice of array
......@@ -524,20 +534,20 @@ test "PackedIntSlice accumulating bit offsets" {
524534 // anything
525535 {
526536 const PackedArray = PackedIntArray(u3, 16);
527 var packed_array = @as(PackedArray, undefined);
537 var packed_array: PackedArray = undefined;
528538
529539 var packed_slice = packed_array.slice(0, packed_array.len);
530 var i = @as(usize, 0);
540 var i: usize = 0;
531541 while (i < packed_array.len - 1) : (i += 1) {
532542 packed_slice = packed_slice.slice(1, packed_slice.len);
533543 }
534544 }
535545 {
536546 const PackedArray = PackedIntArray(u11, 88);
537 var packed_array = @as(PackedArray, undefined);
547 var packed_array: PackedArray = undefined;
538548
539549 var packed_slice = packed_array.slice(0, packed_array.len);
540 var i = @as(usize, 0);
550 var i: usize = 0;
541551 while (i < packed_array.len - 1) : (i += 1) {
542552 packed_slice = packed_slice.slice(1, packed_slice.len);
543553 }
......@@ -552,7 +562,7 @@ test "PackedInt(Array/Slice) sliceCast" {
552562 var packed_slice_cast_9 = packed_array.slice(0, (packed_array.len / 9) * 9).sliceCast(u9);
553563 const packed_slice_cast_3 = packed_slice_cast_9.sliceCast(u3);
554564
555 var i = @as(usize, 0);
565 var i: usize = 0;
556566 while (i < packed_slice_cast_2.len) : (i += 1) {
557567 const val = switch (native_endian) {
558568 .big => 0b01,
......@@ -576,9 +586,9 @@ test "PackedInt(Array/Slice) sliceCast" {
576586 }
577587 i = 0;
578588 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,
582592 };
583593 try testing.expect(packed_slice_cast_3.get(i) == val);
584594 }
......@@ -591,7 +601,7 @@ test "PackedInt(Array/Slice)Endian" {
591601 try testing.expect(packed_array_be.bytes[0] == 0b00000001);
592602 try testing.expect(packed_array_be.bytes[1] == 0b00100011);
593603
594 var i = @as(usize, 0);
604 var i: usize = 0;
595605 while (i < packed_array_be.len) : (i += 1) {
596606 try testing.expect(packed_array_be.get(i) == i);
597607 }
......@@ -620,7 +630,7 @@ test "PackedInt(Array/Slice)Endian" {
620630 try testing.expect(packed_array_be.bytes[3] == 0b00000001);
621631 try testing.expect(packed_array_be.bytes[4] == 0b00000000);
622632
623 var i = @as(usize, 0);
633 var i: usize = 0;
624634 while (i < packed_array_be.len) : (i += 1) {
625635 try testing.expect(packed_array_be.get(i) == i);
626636 }