authorgravatar for tgschultz@gmail.comtgschultz <tgschultz@gmail.com> 2021-10-08 16:22:32+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-09 03:15:34-04:00
log526191bfafe722700323df30647eed0c03fc2403
tree3b9b1c31d0a2a53ba8014da076bd6b04e1df5fca
parent73403d897caec40eff16226abb54098fa6623954

Better documentation, use of `len` field instead of function, @bitSizeOf instead of meta.bitCout


1 files changed, 109 insertions(+), 116 deletions(-)

lib/std/packed_int_array.zig+109-116
......@@ -1,3 +1,7 @@
1//! An set of array and slice types that bit-pack integer elements. A normal [12]u3
2//! takes up 12 bytes of memory since u3's alignment is 1. PackedArray(u3, 12) only
3//! takes up 4 bytes of memory.
4
15const std = @import("std");
26const builtin = @import("builtin");
37const debug = std.debug;
......@@ -5,8 +9,10 @@ const testing = std.testing;
59const native_endian = builtin.target.cpu.arch.endian();
610const Endian = std.builtin.Endian;
711
12/// Provides a set of functions for reading and writing packed integers from a
13/// slice of bytes.
814pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
9 //The general technique employed here is to cast bytes in the array to a container
15 // The general technique employed here is to cast bytes in the array to a container
1016 // integer (having bits % 8 == 0) large enough to contain the number of bits we want,
1117 // then we can retrieve or store the new value with a relative minimum of masking
1218 // and shifting. In this worst case, this means that we'll need an integer that's
......@@ -18,13 +24,13 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
1824 // mean the OS fatally kills the program. Thus, we use a larger container (MaxIo)
1925 // most of the time, but a smaller container (MinIo) when touching the last byte
2026 // of the memory.
21 const int_bits = comptime std.meta.bitCount(Int);
27 const int_bits = @bitSizeOf(Int);
2228
23 //in the best case, this is the number of bytes we need to touch
24 // to read or write a value, as bits
29 // In the best case, this is the number of bytes we need to touch
30 // to read or write a value, as bits.
2531 const min_io_bits = ((int_bits + 7) / 8) * 8;
2632
27 //in the worst case, this is the number of bytes we need to touch
33 // In the worst case, this is the number of bytes we need to touch
2834 // to read or write a value, as bits. To calculate for int_bits > 1,
2935 // set aside 2 bits to touch the first and last bytes, then divide
3036 // by 8 to see how many bytes can be filled up inbetween.
......@@ -34,30 +40,32 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
3440 else => ((int_bits - 2) / 8 + 2) * 8,
3541 };
3642
37 //we bitcast the desired Int type to an unsigned version of itself
43 // We bitcast the desired Int type to an unsigned version of itself
3844 // to avoid issues with shifting signed ints.
3945 const UnInt = std.meta.Int(.unsigned, int_bits);
4046
41 //The maximum container int type
47 // The maximum container int type
4248 const MinIo = std.meta.Int(.unsigned, min_io_bits);
4349
44 //The minimum container int type
50 // The minimum container int type
4551 const MaxIo = std.meta.Int(.unsigned, max_io_bits);
4652
4753 return struct {
54 /// Retrieves the integer at `index` from the packed data beginning at `bit_offset`
55 /// within `bytes`.
4856 pub fn get(bytes: []const u8, index: usize, bit_offset: u7) Int {
4957 if (int_bits == 0) return 0;
5058
5159 const bit_index = (index * int_bits) + bit_offset;
5260 const max_end_byte = (bit_index + max_io_bits) / 8;
5361
54 //Using the larger container size will potentially read out of bounds
62 //using the larger container size will potentially read out of bounds
5563 if (max_end_byte > bytes.len) return getBits(bytes, MinIo, bit_index);
5664 return getBits(bytes, MaxIo, bit_index);
5765 }
5866
5967 fn getBits(bytes: []const u8, comptime Container: type, bit_index: usize) Int {
60 const container_bits = comptime std.meta.bitCount(Container);
68 const container_bits = @bitSizeOf(Container);
6169 const Shift = std.math.Log2Int(Container);
6270
6371 const start_byte = bit_index / 8;
......@@ -86,19 +94,21 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
8694 return @bitCast(Int, @truncate(UnInt, value));
8795 }
8896
97 /// Sets the integer at `index` to `val` within the packed data beginning
98 /// at `bit_offset` into `bytes`.
8999 pub fn set(bytes: []u8, index: usize, bit_offset: u3, int: Int) void {
90100 if (int_bits == 0) return;
91101
92102 const bit_index = (index * int_bits) + bit_offset;
93103 const max_end_byte = (bit_index + max_io_bits) / 8;
94104
95 //Using the larger container size will potentially write out of bounds
105 //using the larger container size will potentially write out of bounds
96106 if (max_end_byte > bytes.len) return setBits(bytes, MinIo, bit_index, int);
97107 setBits(bytes, MaxIo, bit_index, int);
98108 }
99109
100110 fn setBits(bytes: []u8, comptime Container: type, bit_index: usize, int: Int) void {
101 const container_bits = comptime std.meta.bitCount(Container);
111 const container_bits = @bitSizeOf(Container);
102112 const Shift = std.math.Log2Int(Container);
103113
104114 const start_byte = bit_index / 8;
......@@ -132,7 +142,9 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
132142 target_ptr.* = target;
133143 }
134144
135 fn slice(bytes: []u8, bit_offset: u3, start: usize, end: usize) PackedIntSliceEndian(Int, endian) {
145 /// Provides a PackedIntSlice of the packed integers in `bytes` (which begins at `bit_offset`)
146 /// from the element specified by `start` to the element specified by `end`.
147 pub fn slice(bytes: []u8, bit_offset: u3, start: usize, end: usize) PackedIntSliceEndian(Int, endian) {
136148 debug.assert(end >= start);
137149
138150 const length = end - start;
......@@ -148,8 +160,11 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
148160 return new_slice;
149161 }
150162
151 fn sliceCast(bytes: []u8, comptime NewInt: type, comptime new_endian: Endian, bit_offset: u3, old_len: usize) PackedIntSliceEndian(NewInt, new_endian) {
152 const new_int_bits = comptime std.meta.bitCount(NewInt);
163 /// Recasts a packed slice to a version with elements of type `NewInt` and endianness `new_endian`.
164 /// Slice will begin at `bit_offset` within `bytes` and the new length will be automatically
165 /// calculated from `old_len` using the sizes of the current integer type and `NewInt`.
166 pub fn sliceCast(bytes: []u8, comptime NewInt: type, comptime new_endian: Endian, bit_offset: u3, old_len: usize) PackedIntSliceEndian(NewInt, new_endian) {
167 const new_int_bits = @bitSizeOf(NewInt);
153168 const New = PackedIntSliceEndian(NewInt, new_endian);
154169
155170 const total_bits = (old_len * int_bits);
......@@ -165,18 +180,21 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type {
165180 };
166181}
167182
168///Creates a bit-packed array of integers of type Int. Bits
169/// are packed using native endianess and without storing any meta
170/// data. PackedIntArray(i3, 8) will occupy exactly 3 bytes of memory.
183/// Creates a bit-packed array of `Int`. Non-byte-multiple integers
184/// will take up less memory in PackedIntArray than in a normal array.
185/// Elements are packed using native endianess and without storing any
186/// meta data. PackedArray(i3, 8) will occupy exactly 3 bytes
187/// of memory.
171188pub fn PackedIntArray(comptime Int: type, comptime int_count: usize) type {
172189 return PackedIntArrayEndian(Int, native_endian, int_count);
173190}
174191
175///Creates a bit-packed array of integers of type Int. Bits
176/// are packed using specified endianess and without storing any meta
177/// data.
192/// Creates a bit-packed array of `Int` with bit order specified by `endian`.
193/// Non-byte-multiple integers will take up less memory in PackedIntArrayEndian
194/// than in a normal array. Elements are packed without storing any meta data.
195/// PackedIntArrayEndian(i3, 8) will occupy exactly 3 bytes of memory.
178196pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptime int_count: usize) type {
179 const int_bits = comptime std.meta.bitCount(Int);
197 const int_bits = @bitSizeOf(Int);
180198 const total_bits = int_bits * int_count;
181199 const total_bytes = (total_bits + 7) / 8;
182200
......@@ -185,15 +203,12 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptim
185203 return struct {
186204 const Self = @This();
187205
206 /// The byte buffer containing the packed data.
188207 bytes: [total_bytes]u8,
208 /// The number of elements in the packed array.
209 comptime len: usize = int_count,
189210
190 ///Returns the number of elements in the packed array
191 pub fn len(self: Self) usize {
192 _ = self;
193 return int_count;
194 }
195
196 ///Initialize a packed array using an unpacked array
211 /// Initialize a packed array using an unpacked array
197212 /// or, more likely, an array literal.
198213 pub fn init(ints: [int_count]Int) Self {
199214 var self = @as(Self, undefined);
......@@ -201,27 +216,27 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptim
201216 return self;
202217 }
203218
204 ///Initialize all entries of a packed array to the same value
219 /// Initialize all entries of a packed array to the same value.
205220 pub fn initAllTo(int: Int) Self {
206221 // TODO: use `var self = @as(Self, undefined);` https://github.com/ziglang/zig/issues/7635
207 var self = Self{ .bytes = [_]u8{0} ** total_bytes };
222 var self = Self{ .bytes = [_]u8{0} ** total_bytes, .len = int_count };
208223 self.setAll(int);
209224 return self;
210225 }
211226
212 ///Return the Int stored at index
227 /// Return the integer stored at `index`.
213228 pub fn get(self: Self, index: usize) Int {
214229 debug.assert(index < int_count);
215230 return Io.get(&self.bytes, index, 0);
216231 }
217232
218 ///Copy int into the array at index
233 ///Copy the value of `int` into the array at `index`.
219234 pub fn set(self: *Self, index: usize, int: Int) void {
220235 debug.assert(index < int_count);
221236 return Io.set(&self.bytes, index, 0, int);
222237 }
223238
224 ///Set all entries of a packed array to the same value
239 /// Set all entries of a packed array to the value of `int`.
225240 pub fn setAll(self: *Self, int: Int) void {
226241 var i: usize = 0;
227242 while (i < int_count) : (i += 1) {
......@@ -229,105 +244,96 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptim
229244 }
230245 }
231246
232 ///Create a PackedIntSlice of the array from given start to given end
247 /// Create a PackedIntSlice of the array from `start` to `end`.
233248 pub fn slice(self: *Self, start: usize, end: usize) PackedIntSliceEndian(Int, endian) {
234249 debug.assert(start < int_count);
235250 debug.assert(end <= int_count);
236251 return Io.slice(&self.bytes, 0, start, end);
237252 }
238253
239 ///Create a PackedIntSlice of the array using NewInt as the bit width integer.
240 /// NewInt's bit width must fit evenly within the array's Int's total bits.
254 /// Create a PackedIntSlice of the array using `NewInt` as the integer type.
255 /// `NewInt`'s bit width must fit evenly within the array's `Int`'s total bits.
241256 pub fn sliceCast(self: *Self, comptime NewInt: type) PackedIntSlice(NewInt) {
242257 return self.sliceCastEndian(NewInt, endian);
243258 }
244259
245 ///Create a PackedIntSlice of the array using NewInt as the bit width integer
246 /// and new_endian as the new endianess. NewInt's bit width must fit evenly within
247 /// the array's Int's total bits.
260 /// Create a PackedIntSliceEndian of the array using `NewInt` as the integer type
261 /// and `new_endian` as the new endianess. `NewInt`'s bit width must fit evenly
262 /// within the array's `Int`'s total bits.
248263 pub fn sliceCastEndian(self: *Self, comptime NewInt: type, comptime new_endian: Endian) PackedIntSliceEndian(NewInt, new_endian) {
249264 return Io.sliceCast(&self.bytes, NewInt, new_endian, 0, int_count);
250265 }
251266 };
252267}
253268
254///Uses a slice as a bit-packed block of int_count integers of type Int.
255/// Bits are packed using native endianess and without storing any meta
256/// data.
269/// A type representing a sub range of a PackedIntArray.
257270pub fn PackedIntSlice(comptime Int: type) type {
258271 return PackedIntSliceEndian(Int, native_endian);
259272}
260273
261///Uses a slice as a bit-packed block of int_count integers of type Int.
262/// Bits are packed using specified endianess and without storing any meta
263/// data.
274/// A type representing a sub range of a PackedIntArrayEndian.
264275pub fn PackedIntSliceEndian(comptime Int: type, comptime endian: Endian) type {
265 const int_bits = comptime std.meta.bitCount(Int);
276 const int_bits = @bitSizeOf(Int);
266277 const Io = PackedIntIo(Int, endian);
267278
268279 return struct {
269280 const Self = @This();
270281
271282 bytes: []u8,
272 int_count: usize,
273283 bit_offset: u3,
284 len: usize,
274285
275 ///Returns the number of elements in the packed slice
276 pub fn len(self: Self) usize {
277 return self.int_count;
278 }
279
280 ///Calculates the number of bytes required to store a desired count
281 /// of Ints
286 /// Calculates the number of bytes required to store a desired count
287 /// of `Int`s.
282288 pub fn bytesRequired(int_count: usize) usize {
283289 const total_bits = int_bits * int_count;
284290 const total_bytes = (total_bits + 7) / 8;
285291 return total_bytes;
286292 }
287293
288 ///Initialize a packed slice using the memory at bytes, with int_count
289 /// elements. bytes must be large enough to accomodate the requested
294 /// Initialize a packed slice using the memory at `bytes`, with `int_count`
295 /// elements. `bytes` must be large enough to accomodate the requested
290296 /// count.
291297 pub fn init(bytes: []u8, int_count: usize) Self {
292298 debug.assert(bytes.len >= bytesRequired(int_count));
293299
294300 return Self{
295301 .bytes = bytes,
296 .int_count = int_count,
302 .len = int_count,
297303 .bit_offset = 0,
298304 };
299305 }
300306
301 ///Return the Int stored at index
307 /// Return the integer stored at `index`.
302308 pub fn get(self: Self, index: usize) Int {
303 debug.assert(index < self.int_count);
309 debug.assert(index < self.len);
304310 return Io.get(self.bytes, index, self.bit_offset);
305311 }
306312
307 ///Copy int into the array at index
313 /// Copy `int` into the slice at `index`.
308314 pub fn set(self: *Self, index: usize, int: Int) void {
309 debug.assert(index < self.int_count);
315 debug.assert(index < self.len);
310316 return Io.set(self.bytes, index, self.bit_offset, int);
311317 }
312318
313 ///Create a PackedIntSlice of this slice from given start to given end
319 /// Create a PackedIntSlice of this slice from `start` to `end`.
314320 pub fn slice(self: Self, start: usize, end: usize) PackedIntSliceEndian(Int, endian) {
315 debug.assert(start < self.int_count);
316 debug.assert(end <= self.int_count);
321 debug.assert(start < self.len);
322 debug.assert(end <= self.len);
317323 return Io.slice(self.bytes, self.bit_offset, start, end);
318324 }
319325
320 ///Create a PackedIntSlice of this slice using NewInt as the bit width integer.
321 /// NewInt's bit width must fit evenly within this slice's Int's total bits.
326 /// Create a PackedIntSlice of the sclice using `NewInt` as the integer type.
327 /// `NewInt`'s bit width must fit evenly within the slice's `Int`'s total bits.
322328 pub fn sliceCast(self: Self, comptime NewInt: type) PackedIntSliceEndian(NewInt, endian) {
323329 return self.sliceCastEndian(NewInt, endian);
324330 }
325331
326 ///Create a PackedIntSlice of this slice using NewInt as the bit width integer
327 /// and new_endian as the new endianess. NewInt's bit width must fit evenly within
328 /// this slice's Int's total bits.
332 /// Create a PackedIntSliceEndian of the slice using `NewInt` as the integer type
333 /// and `new_endian` as the new endianess. `NewInt`'s bit width must fit evenly
334 /// within the slice's `Int`'s total bits.
329335 pub fn sliceCastEndian(self: Self, comptime NewInt: type, comptime new_endian: Endian) PackedIntSliceEndian(NewInt, new_endian) {
330 return Io.sliceCast(self.bytes, NewInt, new_endian, self.bit_offset, self.int_count);
336 return Io.sliceCast(self.bytes, NewInt, new_endian, self.bit_offset, self.len);
331337 }
332338 };
333339}
......@@ -358,7 +364,7 @@ test "PackedIntArray" {
358364 //write values, counting up
359365 var i = @as(usize, 0);
360366 var count = @as(I, 0);
361 while (i < data.len()) : (i += 1) {
367 while (i < data.len) : (i += 1) {
362368 data.set(i, count);
363369 if (bits > 0) count +%= 1;
364370 }
......@@ -366,7 +372,7 @@ test "PackedIntArray" {
366372 //read and verify values
367373 i = 0;
368374 count = 0;
369 while (i < data.len()) : (i += 1) {
375 while (i < data.len) : (i += 1) {
370376 const val = data.get(i);
371377 try testing.expect(val == count);
372378 if (bits > 0) count +%= 1;
......@@ -383,19 +389,17 @@ test "PackedIntIo" {
383389}
384390
385391test "PackedIntArray init" {
386 if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest;
387392 const PackedArray = PackedIntArray(u3, 8);
388393 var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 });
389394 var i = @as(usize, 0);
390 while (i < packed_array.len()) : (i += 1) try testing.expectEqual(@intCast(u3, i), packed_array.get(i));
395 while (i < packed_array.len) : (i += 1) try testing.expectEqual(@intCast(u3, i), packed_array.get(i));
391396}
392397
393398test "PackedIntArray initAllTo" {
394 if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest;
395399 const PackedArray = PackedIntArray(u3, 8);
396400 var packed_array = PackedArray.initAllTo(5);
397401 var i = @as(usize, 0);
398 while (i < packed_array.len()) : (i += 1) try 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));
399403}
400404
401405test "PackedIntSlice" {
......@@ -423,7 +427,7 @@ test "PackedIntSlice" {
423427 //write values, counting up
424428 var i = @as(usize, 0);
425429 var count = @as(I, 0);
426 while (i < data.len()) : (i += 1) {
430 while (i < data.len) : (i += 1) {
427431 data.set(i, count);
428432 if (bits > 0) count +%= 1;
429433 }
......@@ -431,7 +435,7 @@ test "PackedIntSlice" {
431435 //read and verify values
432436 i = 0;
433437 count = 0;
434 while (i < data.len()) : (i += 1) {
438 while (i < data.len) : (i += 1) {
435439 const val = data.get(i);
436440 try testing.expect(val == count);
437441 if (bits > 0) count +%= 1;
......@@ -454,14 +458,14 @@ test "PackedIntSlice of PackedInt(Array/Slice)" {
454458 const limit = (1 << bits);
455459
456460 var i = @as(usize, 0);
457 while (i < packed_array.len()) : (i += 1) {
461 while (i < packed_array.len) : (i += 1) {
458462 packed_array.set(i, @intCast(Int, i % limit));
459463 }
460464
461465 //slice of array
462466 var packed_slice = packed_array.slice(2, 5);
463 try testing.expect(packed_slice.len() == 3);
464 const ps_bit_count = (bits * packed_slice.len()) + packed_slice.bit_offset;
467 try testing.expect(packed_slice.len == 3);
468 const ps_bit_count = (bits * packed_slice.len) + packed_slice.bit_offset;
465469 const ps_expected_bytes = (ps_bit_count + 7) / 8;
466470 try testing.expect(packed_slice.bytes.len == ps_expected_bytes);
467471 try testing.expect(packed_slice.get(0) == 2 % limit);
......@@ -475,8 +479,8 @@ test "PackedIntSlice of PackedInt(Array/Slice)" {
475479
476480 //slice of a slice
477481 const packed_slice_two = packed_slice.slice(0, 3);
478 try testing.expect(packed_slice_two.len() == 3);
479 const ps2_bit_count = (bits * packed_slice_two.len()) + packed_slice_two.bit_offset;
482 try testing.expect(packed_slice_two.len == 3);
483 const ps2_bit_count = (bits * packed_slice_two.len) + packed_slice_two.bit_offset;
480484 const ps2_expected_bytes = (ps2_bit_count + 7) / 8;
481485 try testing.expect(packed_slice_two.bytes.len == ps2_expected_bytes);
482486 try testing.expect(packed_slice_two.get(1) == 7 % limit);
......@@ -484,21 +488,21 @@ test "PackedIntSlice of PackedInt(Array/Slice)" {
484488
485489 //size one case
486490 const packed_slice_three = packed_slice_two.slice(1, 2);
487 try testing.expect(packed_slice_three.len() == 1);
488 const ps3_bit_count = (bits * packed_slice_three.len()) + packed_slice_three.bit_offset;
491 try testing.expect(packed_slice_three.len == 1);
492 const ps3_bit_count = (bits * packed_slice_three.len) + packed_slice_three.bit_offset;
489493 const ps3_expected_bytes = (ps3_bit_count + 7) / 8;
490494 try testing.expect(packed_slice_three.bytes.len == ps3_expected_bytes);
491495 try testing.expect(packed_slice_three.get(0) == 7 % limit);
492496
493497 //empty slice case
494498 const packed_slice_empty = packed_slice.slice(0, 0);
495 try testing.expect(packed_slice_empty.len() == 0);
499 try testing.expect(packed_slice_empty.len == 0);
496500 try testing.expect(packed_slice_empty.bytes.len == 0);
497501
498502 //slicing at byte boundaries
499503 const packed_slice_edge = packed_array.slice(8, 16);
500 try testing.expect(packed_slice_edge.len() == 8);
501 const pse_bit_count = (bits * packed_slice_edge.len()) + packed_slice_edge.bit_offset;
504 try testing.expect(packed_slice_edge.len == 8);
505 const pse_bit_count = (bits * packed_slice_edge.len) + packed_slice_edge.bit_offset;
502506 const pse_expected_bytes = (pse_bit_count + 7) / 8;
503507 try testing.expect(packed_slice_edge.bytes.len == pse_expected_bytes);
504508 try testing.expect(packed_slice_edge.bit_offset == 0);
......@@ -506,45 +510,40 @@ test "PackedIntSlice of PackedInt(Array/Slice)" {
506510}
507511
508512test "PackedIntSlice accumulating bit offsets" {
509 if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest;
510513 //bit_offset is u3, so standard debugging asserts should catch
511514 // anything
512515 {
513516 const PackedArray = PackedIntArray(u3, 16);
514517 var packed_array = @as(PackedArray, undefined);
515518
516 var packed_slice = packed_array.slice(0, packed_array.len());
519 var packed_slice = packed_array.slice(0, packed_array.len);
517520 var i = @as(usize, 0);
518 while (i < packed_array.len() - 1) : (i += 1) {
519 packed_slice = packed_slice.slice(1, packed_slice.len());
521 while (i < packed_array.len - 1) : (i += 1) {
522 packed_slice = packed_slice.slice(1, packed_slice.len);
520523 }
521524 }
522525 {
523526 const PackedArray = PackedIntArray(u11, 88);
524527 var packed_array = @as(PackedArray, undefined);
525528
526 var packed_slice = packed_array.slice(0, packed_array.len());
529 var packed_slice = packed_array.slice(0, packed_array.len);
527530 var i = @as(usize, 0);
528 while (i < packed_array.len() - 1) : (i += 1) {
529 packed_slice = packed_slice.slice(1, packed_slice.len());
531 while (i < packed_array.len - 1) : (i += 1) {
532 packed_slice = packed_slice.slice(1, packed_slice.len);
530533 }
531534 }
532535}
533536
534//@NOTE: As I do not have a big endian system to test this on,
535// big endian values were not tested
536537test "PackedInt(Array/Slice) sliceCast" {
537 if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest;
538
539538 const PackedArray = PackedIntArray(u1, 16);
540539 var packed_array = PackedArray.init([_]u1{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 });
541540 const packed_slice_cast_2 = packed_array.sliceCast(u2);
542541 const packed_slice_cast_4 = packed_slice_cast_2.sliceCast(u4);
543 var packed_slice_cast_9 = packed_array.slice(0, (packed_array.len() / 9) * 9).sliceCast(u9);
542 var packed_slice_cast_9 = packed_array.slice(0, (packed_array.len / 9) * 9).sliceCast(u9);
544543 const packed_slice_cast_3 = packed_slice_cast_9.sliceCast(u3);
545544
546545 var i = @as(usize, 0);
547 while (i < packed_slice_cast_2.len()) : (i += 1) {
546 while (i < packed_slice_cast_2.len) : (i += 1) {
548547 const val = switch (native_endian) {
549548 .Big => 0b01,
550549 .Little => 0b10,
......@@ -552,7 +551,7 @@ test "PackedInt(Array/Slice) sliceCast" {
552551 try testing.expect(packed_slice_cast_2.get(i) == val);
553552 }
554553 i = 0;
555 while (i < packed_slice_cast_4.len()) : (i += 1) {
554 while (i < packed_slice_cast_4.len) : (i += 1) {
556555 const val = switch (native_endian) {
557556 .Big => 0b0101,
558557 .Little => 0b1010,
......@@ -560,13 +559,13 @@ test "PackedInt(Array/Slice) sliceCast" {
560559 try testing.expect(packed_slice_cast_4.get(i) == val);
561560 }
562561 i = 0;
563 while (i < packed_slice_cast_9.len()) : (i += 1) {
562 while (i < packed_slice_cast_9.len) : (i += 1) {
564563 const val = 0b010101010;
565564 try testing.expect(packed_slice_cast_9.get(i) == val);
566565 packed_slice_cast_9.set(i, 0b111000111);
567566 }
568567 i = 0;
569 while (i < packed_slice_cast_3.len()) : (i += 1) {
568 while (i < packed_slice_cast_3.len) : (i += 1) {
570569 const val = switch (native_endian) {
571570 .Big => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000),
572571 .Little => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000),
......@@ -576,8 +575,6 @@ test "PackedInt(Array/Slice) sliceCast" {
576575}
577576
578577test "PackedInt(Array/Slice)Endian" {
579 if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest;
580
581578 {
582579 const PackedArrayBe = PackedIntArrayEndian(u4, .Big, 8);
583580 var packed_array_be = PackedArrayBe.init([_]u4{ 0, 1, 2, 3, 4, 5, 6, 7 });
......@@ -585,20 +582,20 @@ test "PackedInt(Array/Slice)Endian" {
585582 try testing.expect(packed_array_be.bytes[1] == 0b00100011);
586583
587584 var i = @as(usize, 0);
588 while (i < packed_array_be.len()) : (i += 1) {
585 while (i < packed_array_be.len) : (i += 1) {
589586 try testing.expect(packed_array_be.get(i) == i);
590587 }
591588
592589 var packed_slice_le = packed_array_be.sliceCastEndian(u4, .Little);
593590 i = 0;
594 while (i < packed_slice_le.len()) : (i += 1) {
591 while (i < packed_slice_le.len) : (i += 1) {
595592 const val = if (i % 2 == 0) i + 1 else i - 1;
596593 try testing.expect(packed_slice_le.get(i) == val);
597594 }
598595
599596 var packed_slice_le_shift = packed_array_be.slice(1, 5).sliceCastEndian(u4, .Little);
600597 i = 0;
601 while (i < packed_slice_le_shift.len()) : (i += 1) {
598 while (i < packed_slice_le_shift.len) : (i += 1) {
602599 const val = if (i % 2 == 0) i else i + 2;
603600 try testing.expect(packed_slice_le_shift.get(i) == val);
604601 }
......@@ -614,7 +611,7 @@ test "PackedInt(Array/Slice)Endian" {
614611 try testing.expect(packed_array_be.bytes[4] == 0b00000000);
615612
616613 var i = @as(usize, 0);
617 while (i < packed_array_be.len()) : (i += 1) {
614 while (i < packed_array_be.len) : (i += 1) {
618615 try testing.expect(packed_array_be.get(i) == i);
619616 }
620617
......@@ -639,14 +636,12 @@ test "PackedInt(Array/Slice)Endian" {
639636//@NOTE: Need to manually update this list as more posix os's get
640637// added to DirectAllocator.
641638
642//These tests prove we aren't accidentally accessing memory past
639// These tests prove we aren't accidentally accessing memory past
643640// the end of the array/slice by placing it at the end of a page
644641// and reading the last element. The assumption is that the page
645642// after this one is not mapped and will cause a segfault if we
646643// don't account for the bounds.
647644test "PackedIntArray at end of available memory" {
648 if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest;
649
650645 switch (builtin.target.os.tag) {
651646 .linux, .macos, .ios, .freebsd, .netbsd, .openbsd, .windows => {},
652647 else => return,
......@@ -666,8 +661,6 @@ test "PackedIntArray at end of available memory" {
666661}
667662
668663test "PackedIntSlice at end of available memory" {
669 if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest;
670
671664 switch (builtin.target.os.tag) {
672665 .linux, .macos, .ios, .freebsd, .netbsd, .openbsd, .windows => {},
673666 else => return,