| ... | ... | @@ -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 | |
| 1 | 5 | const std = @import("std"); |
| 2 | 6 | const builtin = @import("builtin"); |
| 3 | 7 | const debug = std.debug; |
| ... | ... | @@ -5,8 +9,10 @@ const testing = std.testing; |
| 5 | 9 | const native_endian = builtin.target.cpu.arch.endian(); |
| 6 | 10 | const Endian = std.builtin.Endian; |
| 7 | 11 | |
| 12 | /// Provides a set of functions for reading and writing packed integers from a |
| 13 | /// slice of bytes. |
| 8 | 14 | pub 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 |
| 10 | 16 | // integer (having bits % 8 == 0) large enough to contain the number of bits we want, |
| 11 | 17 | // then we can retrieve or store the new value with a relative minimum of masking |
| 12 | 18 | // 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 { |
| 18 | 24 | // mean the OS fatally kills the program. Thus, we use a larger container (MaxIo) |
| 19 | 25 | // most of the time, but a smaller container (MinIo) when touching the last byte |
| 20 | 26 | // of the memory. |
| 21 | | const int_bits = comptime std.meta.bitCount(Int); |
| 27 | const int_bits = @bitSizeOf(Int); |
| 22 | 28 | |
| 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. |
| 25 | 31 | const min_io_bits = ((int_bits + 7) / 8) * 8; |
| 26 | 32 | |
| 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 |
| 28 | 34 | // to read or write a value, as bits. To calculate for int_bits > 1, |
| 29 | 35 | // set aside 2 bits to touch the first and last bytes, then divide |
| 30 | 36 | // 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 { |
| 34 | 40 | else => ((int_bits - 2) / 8 + 2) * 8, |
| 35 | 41 | }; |
| 36 | 42 | |
| 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 |
| 38 | 44 | // to avoid issues with shifting signed ints. |
| 39 | 45 | const UnInt = std.meta.Int(.unsigned, int_bits); |
| 40 | 46 | |
| 41 | | //The maximum container int type |
| 47 | // The maximum container int type |
| 42 | 48 | const MinIo = std.meta.Int(.unsigned, min_io_bits); |
| 43 | 49 | |
| 44 | | //The minimum container int type |
| 50 | // The minimum container int type |
| 45 | 51 | const MaxIo = std.meta.Int(.unsigned, max_io_bits); |
| 46 | 52 | |
| 47 | 53 | return struct { |
| 54 | /// Retrieves the integer at `index` from the packed data beginning at `bit_offset` |
| 55 | /// within `bytes`. |
| 48 | 56 | pub fn get(bytes: []const u8, index: usize, bit_offset: u7) Int { |
| 49 | 57 | if (int_bits == 0) return 0; |
| 50 | 58 | |
| 51 | 59 | const bit_index = (index * int_bits) + bit_offset; |
| 52 | 60 | const max_end_byte = (bit_index + max_io_bits) / 8; |
| 53 | 61 | |
| 54 | | //Using the larger container size will potentially read out of bounds |
| 62 | //using the larger container size will potentially read out of bounds |
| 55 | 63 | if (max_end_byte > bytes.len) return getBits(bytes, MinIo, bit_index); |
| 56 | 64 | return getBits(bytes, MaxIo, bit_index); |
| 57 | 65 | } |
| 58 | 66 | |
| 59 | 67 | 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); |
| 61 | 69 | const Shift = std.math.Log2Int(Container); |
| 62 | 70 | |
| 63 | 71 | const start_byte = bit_index / 8; |
| ... | ... | @@ -86,19 +94,21 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 86 | 94 | return @bitCast(Int, @truncate(UnInt, value)); |
| 87 | 95 | } |
| 88 | 96 | |
| 97 | /// Sets the integer at `index` to `val` within the packed data beginning |
| 98 | /// at `bit_offset` into `bytes`. |
| 89 | 99 | pub fn set(bytes: []u8, index: usize, bit_offset: u3, int: Int) void { |
| 90 | 100 | if (int_bits == 0) return; |
| 91 | 101 | |
| 92 | 102 | const bit_index = (index * int_bits) + bit_offset; |
| 93 | 103 | const max_end_byte = (bit_index + max_io_bits) / 8; |
| 94 | 104 | |
| 95 | | //Using the larger container size will potentially write out of bounds |
| 105 | //using the larger container size will potentially write out of bounds |
| 96 | 106 | if (max_end_byte > bytes.len) return setBits(bytes, MinIo, bit_index, int); |
| 97 | 107 | setBits(bytes, MaxIo, bit_index, int); |
| 98 | 108 | } |
| 99 | 109 | |
| 100 | 110 | 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); |
| 102 | 112 | const Shift = std.math.Log2Int(Container); |
| 103 | 113 | |
| 104 | 114 | const start_byte = bit_index / 8; |
| ... | ... | @@ -132,7 +142,9 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 132 | 142 | target_ptr.* = target; |
| 133 | 143 | } |
| 134 | 144 | |
| 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) { |
| 136 | 148 | debug.assert(end >= start); |
| 137 | 149 | |
| 138 | 150 | const length = end - start; |
| ... | ... | @@ -148,8 +160,11 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 148 | 160 | return new_slice; |
| 149 | 161 | } |
| 150 | 162 | |
| 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); |
| 153 | 168 | const New = PackedIntSliceEndian(NewInt, new_endian); |
| 154 | 169 | |
| 155 | 170 | const total_bits = (old_len * int_bits); |
| ... | ... | @@ -165,18 +180,21 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: Endian) type { |
| 165 | 180 | }; |
| 166 | 181 | } |
| 167 | 182 | |
| 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. |
| 171 | 188 | pub fn PackedIntArray(comptime Int: type, comptime int_count: usize) type { |
| 172 | 189 | return PackedIntArrayEndian(Int, native_endian, int_count); |
| 173 | 190 | } |
| 174 | 191 | |
| 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. |
| 178 | 196 | pub 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); |
| 180 | 198 | const total_bits = int_bits * int_count; |
| 181 | 199 | const total_bytes = (total_bits + 7) / 8; |
| 182 | 200 | |
| ... | ... | @@ -185,15 +203,12 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptim |
| 185 | 203 | return struct { |
| 186 | 204 | const Self = @This(); |
| 187 | 205 | |
| 206 | /// The byte buffer containing the packed data. |
| 188 | 207 | bytes: [total_bytes]u8, |
| 208 | /// The number of elements in the packed array. |
| 209 | comptime len: usize = int_count, |
| 189 | 210 | |
| 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 |
| 197 | 212 | /// or, more likely, an array literal. |
| 198 | 213 | pub fn init(ints: [int_count]Int) Self { |
| 199 | 214 | var self = @as(Self, undefined); |
| ... | ... | @@ -201,27 +216,27 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptim |
| 201 | 216 | return self; |
| 202 | 217 | } |
| 203 | 218 | |
| 204 | | ///Initialize all entries of a packed array to the same value |
| 219 | /// Initialize all entries of a packed array to the same value. |
| 205 | 220 | pub fn initAllTo(int: Int) Self { |
| 206 | 221 | // 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 }; |
| 208 | 223 | self.setAll(int); |
| 209 | 224 | return self; |
| 210 | 225 | } |
| 211 | 226 | |
| 212 | | ///Return the Int stored at index |
| 227 | /// Return the integer stored at `index`. |
| 213 | 228 | pub fn get(self: Self, index: usize) Int { |
| 214 | 229 | debug.assert(index < int_count); |
| 215 | 230 | return Io.get(&self.bytes, index, 0); |
| 216 | 231 | } |
| 217 | 232 | |
| 218 | | ///Copy int into the array at index |
| 233 | ///Copy the value of `int` into the array at `index`. |
| 219 | 234 | pub fn set(self: *Self, index: usize, int: Int) void { |
| 220 | 235 | debug.assert(index < int_count); |
| 221 | 236 | return Io.set(&self.bytes, index, 0, int); |
| 222 | 237 | } |
| 223 | 238 | |
| 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`. |
| 225 | 240 | pub fn setAll(self: *Self, int: Int) void { |
| 226 | 241 | var i: usize = 0; |
| 227 | 242 | while (i < int_count) : (i += 1) { |
| ... | ... | @@ -229,105 +244,96 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: Endian, comptim |
| 229 | 244 | } |
| 230 | 245 | } |
| 231 | 246 | |
| 232 | | ///Create a PackedIntSlice of the array from given start to given end |
| 247 | /// Create a PackedIntSlice of the array from `start` to `end`. |
| 233 | 248 | pub fn slice(self: *Self, start: usize, end: usize) PackedIntSliceEndian(Int, endian) { |
| 234 | 249 | debug.assert(start < int_count); |
| 235 | 250 | debug.assert(end <= int_count); |
| 236 | 251 | return Io.slice(&self.bytes, 0, start, end); |
| 237 | 252 | } |
| 238 | 253 | |
| 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. |
| 241 | 256 | pub fn sliceCast(self: *Self, comptime NewInt: type) PackedIntSlice(NewInt) { |
| 242 | 257 | return self.sliceCastEndian(NewInt, endian); |
| 243 | 258 | } |
| 244 | 259 | |
| 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. |
| 248 | 263 | pub fn sliceCastEndian(self: *Self, comptime NewInt: type, comptime new_endian: Endian) PackedIntSliceEndian(NewInt, new_endian) { |
| 249 | 264 | return Io.sliceCast(&self.bytes, NewInt, new_endian, 0, int_count); |
| 250 | 265 | } |
| 251 | 266 | }; |
| 252 | 267 | } |
| 253 | 268 | |
| 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. |
| 257 | 270 | pub fn PackedIntSlice(comptime Int: type) type { |
| 258 | 271 | return PackedIntSliceEndian(Int, native_endian); |
| 259 | 272 | } |
| 260 | 273 | |
| 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. |
| 264 | 275 | pub fn PackedIntSliceEndian(comptime Int: type, comptime endian: Endian) type { |
| 265 | | const int_bits = comptime std.meta.bitCount(Int); |
| 276 | const int_bits = @bitSizeOf(Int); |
| 266 | 277 | const Io = PackedIntIo(Int, endian); |
| 267 | 278 | |
| 268 | 279 | return struct { |
| 269 | 280 | const Self = @This(); |
| 270 | 281 | |
| 271 | 282 | bytes: []u8, |
| 272 | | int_count: usize, |
| 273 | 283 | bit_offset: u3, |
| 284 | len: usize, |
| 274 | 285 | |
| 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. |
| 282 | 288 | pub fn bytesRequired(int_count: usize) usize { |
| 283 | 289 | const total_bits = int_bits * int_count; |
| 284 | 290 | const total_bytes = (total_bits + 7) / 8; |
| 285 | 291 | return total_bytes; |
| 286 | 292 | } |
| 287 | 293 | |
| 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 |
| 290 | 296 | /// count. |
| 291 | 297 | pub fn init(bytes: []u8, int_count: usize) Self { |
| 292 | 298 | debug.assert(bytes.len >= bytesRequired(int_count)); |
| 293 | 299 | |
| 294 | 300 | return Self{ |
| 295 | 301 | .bytes = bytes, |
| 296 | | .int_count = int_count, |
| 302 | .len = int_count, |
| 297 | 303 | .bit_offset = 0, |
| 298 | 304 | }; |
| 299 | 305 | } |
| 300 | 306 | |
| 301 | | ///Return the Int stored at index |
| 307 | /// Return the integer stored at `index`. |
| 302 | 308 | pub fn get(self: Self, index: usize) Int { |
| 303 | | debug.assert(index < self.int_count); |
| 309 | debug.assert(index < self.len); |
| 304 | 310 | return Io.get(self.bytes, index, self.bit_offset); |
| 305 | 311 | } |
| 306 | 312 | |
| 307 | | ///Copy int into the array at index |
| 313 | /// Copy `int` into the slice at `index`. |
| 308 | 314 | pub fn set(self: *Self, index: usize, int: Int) void { |
| 309 | | debug.assert(index < self.int_count); |
| 315 | debug.assert(index < self.len); |
| 310 | 316 | return Io.set(self.bytes, index, self.bit_offset, int); |
| 311 | 317 | } |
| 312 | 318 | |
| 313 | | ///Create a PackedIntSlice of this slice from given start to given end |
| 319 | /// Create a PackedIntSlice of this slice from `start` to `end`. |
| 314 | 320 | 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); |
| 317 | 323 | return Io.slice(self.bytes, self.bit_offset, start, end); |
| 318 | 324 | } |
| 319 | 325 | |
| 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. |
| 322 | 328 | pub fn sliceCast(self: Self, comptime NewInt: type) PackedIntSliceEndian(NewInt, endian) { |
| 323 | 329 | return self.sliceCastEndian(NewInt, endian); |
| 324 | 330 | } |
| 325 | 331 | |
| 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. |
| 329 | 335 | 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); |
| 331 | 337 | } |
| 332 | 338 | }; |
| 333 | 339 | } |
| ... | ... | @@ -358,7 +364,7 @@ test "PackedIntArray" { |
| 358 | 364 | //write values, counting up |
| 359 | 365 | var i = @as(usize, 0); |
| 360 | 366 | var count = @as(I, 0); |
| 361 | | while (i < data.len()) : (i += 1) { |
| 367 | while (i < data.len) : (i += 1) { |
| 362 | 368 | data.set(i, count); |
| 363 | 369 | if (bits > 0) count +%= 1; |
| 364 | 370 | } |
| ... | ... | @@ -366,7 +372,7 @@ test "PackedIntArray" { |
| 366 | 372 | //read and verify values |
| 367 | 373 | i = 0; |
| 368 | 374 | count = 0; |
| 369 | | while (i < data.len()) : (i += 1) { |
| 375 | while (i < data.len) : (i += 1) { |
| 370 | 376 | const val = data.get(i); |
| 371 | 377 | try testing.expect(val == count); |
| 372 | 378 | if (bits > 0) count +%= 1; |
| ... | ... | @@ -383,19 +389,17 @@ test "PackedIntIo" { |
| 383 | 389 | } |
| 384 | 390 | |
| 385 | 391 | test "PackedIntArray init" { |
| 386 | | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; |
| 387 | 392 | const PackedArray = PackedIntArray(u3, 8); |
| 388 | 393 | var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| 389 | 394 | 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)); |
| 391 | 396 | } |
| 392 | 397 | |
| 393 | 398 | test "PackedIntArray initAllTo" { |
| 394 | | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; |
| 395 | 399 | const PackedArray = PackedIntArray(u3, 8); |
| 396 | 400 | var packed_array = PackedArray.initAllTo(5); |
| 397 | 401 | 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)); |
| 399 | 403 | } |
| 400 | 404 | |
| 401 | 405 | test "PackedIntSlice" { |
| ... | ... | @@ -423,7 +427,7 @@ test "PackedIntSlice" { |
| 423 | 427 | //write values, counting up |
| 424 | 428 | var i = @as(usize, 0); |
| 425 | 429 | var count = @as(I, 0); |
| 426 | | while (i < data.len()) : (i += 1) { |
| 430 | while (i < data.len) : (i += 1) { |
| 427 | 431 | data.set(i, count); |
| 428 | 432 | if (bits > 0) count +%= 1; |
| 429 | 433 | } |
| ... | ... | @@ -431,7 +435,7 @@ test "PackedIntSlice" { |
| 431 | 435 | //read and verify values |
| 432 | 436 | i = 0; |
| 433 | 437 | count = 0; |
| 434 | | while (i < data.len()) : (i += 1) { |
| 438 | while (i < data.len) : (i += 1) { |
| 435 | 439 | const val = data.get(i); |
| 436 | 440 | try testing.expect(val == count); |
| 437 | 441 | if (bits > 0) count +%= 1; |
| ... | ... | @@ -454,14 +458,14 @@ test "PackedIntSlice of PackedInt(Array/Slice)" { |
| 454 | 458 | const limit = (1 << bits); |
| 455 | 459 | |
| 456 | 460 | var i = @as(usize, 0); |
| 457 | | while (i < packed_array.len()) : (i += 1) { |
| 461 | while (i < packed_array.len) : (i += 1) { |
| 458 | 462 | packed_array.set(i, @intCast(Int, i % limit)); |
| 459 | 463 | } |
| 460 | 464 | |
| 461 | 465 | //slice of array |
| 462 | 466 | 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; |
| 465 | 469 | const ps_expected_bytes = (ps_bit_count + 7) / 8; |
| 466 | 470 | try testing.expect(packed_slice.bytes.len == ps_expected_bytes); |
| 467 | 471 | try testing.expect(packed_slice.get(0) == 2 % limit); |
| ... | ... | @@ -475,8 +479,8 @@ test "PackedIntSlice of PackedInt(Array/Slice)" { |
| 475 | 479 | |
| 476 | 480 | //slice of a slice |
| 477 | 481 | 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; |
| 480 | 484 | const ps2_expected_bytes = (ps2_bit_count + 7) / 8; |
| 481 | 485 | try testing.expect(packed_slice_two.bytes.len == ps2_expected_bytes); |
| 482 | 486 | try testing.expect(packed_slice_two.get(1) == 7 % limit); |
| ... | ... | @@ -484,21 +488,21 @@ test "PackedIntSlice of PackedInt(Array/Slice)" { |
| 484 | 488 | |
| 485 | 489 | //size one case |
| 486 | 490 | 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; |
| 489 | 493 | const ps3_expected_bytes = (ps3_bit_count + 7) / 8; |
| 490 | 494 | try testing.expect(packed_slice_three.bytes.len == ps3_expected_bytes); |
| 491 | 495 | try testing.expect(packed_slice_three.get(0) == 7 % limit); |
| 492 | 496 | |
| 493 | 497 | //empty slice case |
| 494 | 498 | 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); |
| 496 | 500 | try testing.expect(packed_slice_empty.bytes.len == 0); |
| 497 | 501 | |
| 498 | 502 | //slicing at byte boundaries |
| 499 | 503 | 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; |
| 502 | 506 | const pse_expected_bytes = (pse_bit_count + 7) / 8; |
| 503 | 507 | try testing.expect(packed_slice_edge.bytes.len == pse_expected_bytes); |
| 504 | 508 | try testing.expect(packed_slice_edge.bit_offset == 0); |
| ... | ... | @@ -506,45 +510,40 @@ test "PackedIntSlice of PackedInt(Array/Slice)" { |
| 506 | 510 | } |
| 507 | 511 | |
| 508 | 512 | test "PackedIntSlice accumulating bit offsets" { |
| 509 | | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; |
| 510 | 513 | //bit_offset is u3, so standard debugging asserts should catch |
| 511 | 514 | // anything |
| 512 | 515 | { |
| 513 | 516 | const PackedArray = PackedIntArray(u3, 16); |
| 514 | 517 | var packed_array = @as(PackedArray, undefined); |
| 515 | 518 | |
| 516 | | var packed_slice = packed_array.slice(0, packed_array.len()); |
| 519 | var packed_slice = packed_array.slice(0, packed_array.len); |
| 517 | 520 | 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); |
| 520 | 523 | } |
| 521 | 524 | } |
| 522 | 525 | { |
| 523 | 526 | const PackedArray = PackedIntArray(u11, 88); |
| 524 | 527 | var packed_array = @as(PackedArray, undefined); |
| 525 | 528 | |
| 526 | | var packed_slice = packed_array.slice(0, packed_array.len()); |
| 529 | var packed_slice = packed_array.slice(0, packed_array.len); |
| 527 | 530 | 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); |
| 530 | 533 | } |
| 531 | 534 | } |
| 532 | 535 | } |
| 533 | 536 | |
| 534 | | //@NOTE: As I do not have a big endian system to test this on, |
| 535 | | // big endian values were not tested |
| 536 | 537 | test "PackedInt(Array/Slice) sliceCast" { |
| 537 | | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; |
| 538 | | |
| 539 | 538 | const PackedArray = PackedIntArray(u1, 16); |
| 540 | 539 | var packed_array = PackedArray.init([_]u1{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }); |
| 541 | 540 | const packed_slice_cast_2 = packed_array.sliceCast(u2); |
| 542 | 541 | 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); |
| 544 | 543 | const packed_slice_cast_3 = packed_slice_cast_9.sliceCast(u3); |
| 545 | 544 | |
| 546 | 545 | 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) { |
| 548 | 547 | const val = switch (native_endian) { |
| 549 | 548 | .Big => 0b01, |
| 550 | 549 | .Little => 0b10, |
| ... | ... | @@ -552,7 +551,7 @@ test "PackedInt(Array/Slice) sliceCast" { |
| 552 | 551 | try testing.expect(packed_slice_cast_2.get(i) == val); |
| 553 | 552 | } |
| 554 | 553 | i = 0; |
| 555 | | while (i < packed_slice_cast_4.len()) : (i += 1) { |
| 554 | while (i < packed_slice_cast_4.len) : (i += 1) { |
| 556 | 555 | const val = switch (native_endian) { |
| 557 | 556 | .Big => 0b0101, |
| 558 | 557 | .Little => 0b1010, |
| ... | ... | @@ -560,13 +559,13 @@ test "PackedInt(Array/Slice) sliceCast" { |
| 560 | 559 | try testing.expect(packed_slice_cast_4.get(i) == val); |
| 561 | 560 | } |
| 562 | 561 | i = 0; |
| 563 | | while (i < packed_slice_cast_9.len()) : (i += 1) { |
| 562 | while (i < packed_slice_cast_9.len) : (i += 1) { |
| 564 | 563 | const val = 0b010101010; |
| 565 | 564 | try testing.expect(packed_slice_cast_9.get(i) == val); |
| 566 | 565 | packed_slice_cast_9.set(i, 0b111000111); |
| 567 | 566 | } |
| 568 | 567 | i = 0; |
| 569 | | while (i < packed_slice_cast_3.len()) : (i += 1) { |
| 568 | while (i < packed_slice_cast_3.len) : (i += 1) { |
| 570 | 569 | const val = switch (native_endian) { |
| 571 | 570 | .Big => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), |
| 572 | 571 | .Little => if (i % 2 == 0) @as(u3, 0b111) else @as(u3, 0b000), |
| ... | ... | @@ -576,8 +575,6 @@ test "PackedInt(Array/Slice) sliceCast" { |
| 576 | 575 | } |
| 577 | 576 | |
| 578 | 577 | test "PackedInt(Array/Slice)Endian" { |
| 579 | | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; |
| 580 | | |
| 581 | 578 | { |
| 582 | 579 | const PackedArrayBe = PackedIntArrayEndian(u4, .Big, 8); |
| 583 | 580 | var packed_array_be = PackedArrayBe.init([_]u4{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| ... | ... | @@ -585,20 +582,20 @@ test "PackedInt(Array/Slice)Endian" { |
| 585 | 582 | try testing.expect(packed_array_be.bytes[1] == 0b00100011); |
| 586 | 583 | |
| 587 | 584 | var i = @as(usize, 0); |
| 588 | | while (i < packed_array_be.len()) : (i += 1) { |
| 585 | while (i < packed_array_be.len) : (i += 1) { |
| 589 | 586 | try testing.expect(packed_array_be.get(i) == i); |
| 590 | 587 | } |
| 591 | 588 | |
| 592 | 589 | var packed_slice_le = packed_array_be.sliceCastEndian(u4, .Little); |
| 593 | 590 | i = 0; |
| 594 | | while (i < packed_slice_le.len()) : (i += 1) { |
| 591 | while (i < packed_slice_le.len) : (i += 1) { |
| 595 | 592 | const val = if (i % 2 == 0) i + 1 else i - 1; |
| 596 | 593 | try testing.expect(packed_slice_le.get(i) == val); |
| 597 | 594 | } |
| 598 | 595 | |
| 599 | 596 | var packed_slice_le_shift = packed_array_be.slice(1, 5).sliceCastEndian(u4, .Little); |
| 600 | 597 | i = 0; |
| 601 | | while (i < packed_slice_le_shift.len()) : (i += 1) { |
| 598 | while (i < packed_slice_le_shift.len) : (i += 1) { |
| 602 | 599 | const val = if (i % 2 == 0) i else i + 2; |
| 603 | 600 | try testing.expect(packed_slice_le_shift.get(i) == val); |
| 604 | 601 | } |
| ... | ... | @@ -614,7 +611,7 @@ test "PackedInt(Array/Slice)Endian" { |
| 614 | 611 | try testing.expect(packed_array_be.bytes[4] == 0b00000000); |
| 615 | 612 | |
| 616 | 613 | var i = @as(usize, 0); |
| 617 | | while (i < packed_array_be.len()) : (i += 1) { |
| 614 | while (i < packed_array_be.len) : (i += 1) { |
| 618 | 615 | try testing.expect(packed_array_be.get(i) == i); |
| 619 | 616 | } |
| 620 | 617 | |
| ... | ... | @@ -639,14 +636,12 @@ test "PackedInt(Array/Slice)Endian" { |
| 639 | 636 | //@NOTE: Need to manually update this list as more posix os's get |
| 640 | 637 | // added to DirectAllocator. |
| 641 | 638 | |
| 642 | | //These tests prove we aren't accidentally accessing memory past |
| 639 | // These tests prove we aren't accidentally accessing memory past |
| 643 | 640 | // the end of the array/slice by placing it at the end of a page |
| 644 | 641 | // and reading the last element. The assumption is that the page |
| 645 | 642 | // after this one is not mapped and will cause a segfault if we |
| 646 | 643 | // don't account for the bounds. |
| 647 | 644 | test "PackedIntArray at end of available memory" { |
| 648 | | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; |
| 649 | | |
| 650 | 645 | switch (builtin.target.os.tag) { |
| 651 | 646 | .linux, .macos, .ios, .freebsd, .netbsd, .openbsd, .windows => {}, |
| 652 | 647 | else => return, |
| ... | ... | @@ -666,8 +661,6 @@ test "PackedIntArray at end of available memory" { |
| 666 | 661 | } |
| 667 | 662 | |
| 668 | 663 | test "PackedIntSlice at end of available memory" { |
| 669 | | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; |
| 670 | | |
| 671 | 664 | switch (builtin.target.os.tag) { |
| 672 | 665 | .linux, .macos, .ios, .freebsd, .netbsd, .openbsd, .windows => {}, |
| 673 | 666 | else => return, |