| ... | ... | @@ -3,141 +3,128 @@ const builtin = @import("builtin"); |
| 3 | 3 | const debug = std.debug; |
| 4 | 4 | const testing = std.testing; |
| 5 | 5 | |
| 6 | | pub fn PackedIntIo(comptime Int: type) type |
| 7 | | { |
| 6 | pub fn PackedIntIo(comptime Int: type) type { |
| 8 | 7 | //The general technique employed here is to cast bytes in the array to a container |
| 9 | 8 | // integer (having bits % 8 == 0) large enough to contain the number of bits we want, |
| 10 | 9 | // then we can retrieve or store the new value with a relative minimum of masking |
| 11 | 10 | // and shifting. In this worst case, this means that we'll need an integer that's |
| 12 | 11 | // actually 1 byte larger than the minimum required to store the bits, because it |
| 13 | | // is possible that the bits start at the end of the first byte, continue through |
| 12 | // is possible that the bits start at the end of the first byte, continue through |
| 14 | 13 | // zero or more, then end in the beginning of the last. But, if we try to access |
| 15 | 14 | // a value in the very last byte of memory with that integer size, that extra byte |
| 16 | 15 | // will be out of bounds. Depending on the circumstances of the memory, that might |
| 17 | 16 | // mean the OS fatally kills the program. Thus, we use a larger container (MaxIo) |
| 18 | 17 | // most of the time, but a smaller container (MinIo) when touching the last byte |
| 19 | 18 | // of the memory. |
| 20 | | |
| 21 | 19 | const int_bits = comptime std.meta.bitCount(Int); |
| 22 | | |
| 20 | |
| 23 | 21 | //in the best case, this is the number of bytes we need to touch |
| 24 | 22 | // to read or write a value, as bits |
| 25 | 23 | const min_io_bits = ((int_bits + 7) / 8) * 8; |
| 26 | | |
| 24 | |
| 27 | 25 | //in the worst case, this is the number of bytes we need to touch |
| 28 | 26 | // to read or write a value, as bits |
| 29 | | const max_io_bits = switch(int_bits) |
| 30 | | { |
| 27 | const max_io_bits = switch (int_bits) { |
| 31 | 28 | 0 => 0, |
| 32 | 29 | 1 => 8, |
| 33 | 30 | 2...9 => 16, |
| 34 | 31 | 10...65535 => ((int_bits / 8) + 2) * 8, |
| 35 | 32 | else => unreachable, |
| 36 | 33 | }; |
| 37 | | |
| 34 | |
| 38 | 35 | //we bitcast the desired Int type to an unsigned version of itself |
| 39 | 36 | // to avoid issues with shifting signed ints. |
| 40 | 37 | const UnInt = @IntType(false, int_bits); |
| 41 | | |
| 38 | |
| 42 | 39 | //The maximum container int type |
| 43 | 40 | const MinIo = @IntType(false, min_io_bits); |
| 44 | | |
| 41 | |
| 45 | 42 | //The minimum container int type |
| 46 | 43 | const MaxIo = @IntType(false, max_io_bits); |
| 47 | | |
| 48 | | return struct |
| 49 | | { |
| 50 | | pub fn get(bytes: []const u8, index: usize, bit_offset: u7) Int |
| 51 | | { |
| 52 | | if(int_bits == 0) return 0; |
| 53 | | |
| 44 | |
| 45 | return struct { |
| 46 | pub fn get(bytes: []const u8, index: usize, bit_offset: u7) Int { |
| 47 | if (int_bits == 0) return 0; |
| 48 | |
| 54 | 49 | const bit_index = (index * int_bits) + bit_offset; |
| 55 | 50 | const max_end_byte = (bit_index + max_io_bits) / 8; |
| 56 | | |
| 51 | |
| 57 | 52 | //Using the larger container size will potentially read out of bounds |
| 58 | | if(max_end_byte > bytes.len) return getBits(bytes, MinIo, bit_index); |
| 53 | if (max_end_byte > bytes.len) return getBits(bytes, MinIo, bit_index); |
| 59 | 54 | return getBits(bytes, MaxIo, bit_index); |
| 60 | 55 | } |
| 61 | | |
| 62 | | fn getBits(bytes: []const u8, comptime Container: type, bit_index: usize) Int |
| 63 | | { |
| 56 | |
| 57 | fn getBits(bytes: []const u8, comptime Container: type, bit_index: usize) Int { |
| 64 | 58 | const container_bits = comptime std.meta.bitCount(Container); |
| 65 | 59 | const Shift = std.math.Log2Int(Container); |
| 66 | | |
| 60 | |
| 67 | 61 | const start_byte = bit_index / 8; |
| 68 | 62 | const head_keep_bits = bit_index - (start_byte * 8); |
| 69 | 63 | const tail_keep_bits = container_bits - (int_bits + head_keep_bits); |
| 70 | | |
| 64 | |
| 71 | 65 | //read bytes as container |
| 72 | | const value_ptr = @ptrCast(*const align(1) Container, &bytes[start_byte]); |
| 66 | const value_ptr = @ptrCast(*align(1) const Container, &bytes[start_byte]); |
| 73 | 67 | var value = value_ptr.*; |
| 74 | 68 | |
| 75 | | switch(builtin.endian) |
| 76 | | { |
| 77 | | .Big => |
| 78 | | { |
| 69 | switch (builtin.endian) { |
| 70 | .Big => { |
| 79 | 71 | value <<= @intCast(Shift, head_keep_bits); |
| 80 | 72 | value >>= @intCast(Shift, head_keep_bits); |
| 81 | 73 | value >>= @intCast(Shift, tail_keep_bits); |
| 82 | 74 | }, |
| 83 | | .Little => |
| 84 | | { |
| 75 | .Little => { |
| 85 | 76 | value <<= @intCast(Shift, tail_keep_bits); |
| 86 | 77 | value >>= @intCast(Shift, tail_keep_bits); |
| 87 | 78 | value >>= @intCast(Shift, head_keep_bits); |
| 88 | 79 | }, |
| 89 | 80 | } |
| 90 | | |
| 81 | |
| 91 | 82 | return @bitCast(Int, @truncate(UnInt, value)); |
| 92 | 83 | } |
| 93 | | |
| 94 | | pub fn set(bytes: []u8, index: usize, bit_offset: u3, int: Int) void |
| 95 | | { |
| 96 | | if(int_bits == 0) return; |
| 84 | |
| 85 | pub fn set(bytes: []u8, index: usize, bit_offset: u3, int: Int) void { |
| 86 | if (int_bits == 0) return; |
| 97 | 87 | |
| 98 | 88 | const bit_index = (index * int_bits) + bit_offset; |
| 99 | 89 | const max_end_byte = (bit_index + max_io_bits) / 8; |
| 100 | | |
| 90 | |
| 101 | 91 | //Using the larger container size will potentially write out of bounds |
| 102 | | if(max_end_byte > bytes.len) return setBits(bytes, MinIo, bit_index, int); |
| 92 | if (max_end_byte > bytes.len) return setBits(bytes, MinIo, bit_index, int); |
| 103 | 93 | setBits(bytes, MaxIo, bit_index, int); |
| 104 | 94 | } |
| 105 | | |
| 106 | | fn setBits(bytes: []u8, comptime Container: type, bit_index: usize, int: Int) void |
| 107 | | { |
| 95 | |
| 96 | fn setBits(bytes: []u8, comptime Container: type, bit_index: usize, int: Int) void { |
| 108 | 97 | const container_bits = comptime std.meta.bitCount(Container); |
| 109 | 98 | const Shift = std.math.Log2Int(Container); |
| 110 | | |
| 99 | |
| 111 | 100 | const start_byte = bit_index / 8; |
| 112 | 101 | const head_keep_bits = bit_index - (start_byte * 8); |
| 113 | 102 | const tail_keep_bits = container_bits - (int_bits + head_keep_bits); |
| 114 | | const keep_shift = switch(builtin.endian) |
| 115 | | { |
| 103 | const keep_shift = switch (builtin.endian) { |
| 116 | 104 | .Big => @intCast(Shift, tail_keep_bits), |
| 117 | 105 | .Little => @intCast(Shift, head_keep_bits), |
| 118 | 106 | }; |
| 119 | | |
| 107 | |
| 120 | 108 | //position the bits where they need to be in the container |
| 121 | 109 | const value = @intCast(Container, @bitCast(UnInt, int)) << keep_shift; |
| 122 | | |
| 110 | |
| 123 | 111 | //read existing bytes |
| 124 | 112 | const target_ptr = @ptrCast(*align(1) Container, &bytes[start_byte]); |
| 125 | 113 | var target = target_ptr.*; |
| 126 | | |
| 114 | |
| 127 | 115 | //zero the bits we want to replace in the existing bytes |
| 128 | 116 | const inv_mask = @intCast(Container, std.math.maxInt(UnInt)) << keep_shift; |
| 129 | 117 | const mask = ~inv_mask; |
| 130 | 118 | target &= mask; |
| 131 | | |
| 119 | |
| 132 | 120 | //merge the new value |
| 133 | 121 | target |= value; |
| 134 | | |
| 122 | |
| 135 | 123 | //save it back |
| 136 | 124 | target_ptr.* = target; |
| 137 | 125 | } |
| 138 | | |
| 139 | | fn slice(bytes: []u8, bit_offset: u3, start: usize, end: usize) PackedIntSlice(Int) |
| 140 | | { |
| 126 | |
| 127 | fn slice(bytes: []u8, bit_offset: u3, start: usize, end: usize) PackedIntSlice(Int) { |
| 141 | 128 | debug.assert(end >= start); |
| 142 | 129 | |
| 143 | 130 | const length = end - start; |
| ... | ... | @@ -145,25 +132,23 @@ pub fn PackedIntIo(comptime Int: type) type |
| 145 | 132 | const start_byte = bit_index / 8; |
| 146 | 133 | const end_byte = (bit_index + (length * int_bits) + 7) / 8; |
| 147 | 134 | const new_bytes = bytes[start_byte..end_byte]; |
| 148 | | |
| 149 | | if(length == 0) return PackedIntSlice(Int).init(new_bytes[0..0], 0); |
| 150 | | |
| 135 | |
| 136 | if (length == 0) return PackedIntSlice(Int).init(new_bytes[0..0], 0); |
| 137 | |
| 151 | 138 | var new_slice = PackedIntSlice(Int).init(new_bytes, length); |
| 152 | 139 | new_slice.bit_offset = @intCast(u3, (bit_index - (start_byte * 8))); |
| 153 | 140 | return new_slice; |
| 154 | 141 | } |
| 155 | | |
| 156 | | fn sliceCast(bytes: []u8, comptime NewInt: type, bit_offset: u3, old_len: usize) |
| 157 | | PackedIntSlice(NewInt) |
| 158 | | { |
| 142 | |
| 143 | fn sliceCast(bytes: []u8, comptime NewInt: type, bit_offset: u3, old_len: usize) PackedIntSlice(NewInt) { |
| 159 | 144 | const new_int_bits = comptime std.meta.bitCount(NewInt); |
| 160 | 145 | const New = PackedIntSlice(NewInt); |
| 161 | | |
| 146 | |
| 162 | 147 | const total_bits = (old_len * int_bits); |
| 163 | 148 | const new_int_count = total_bits / new_int_bits; |
| 164 | | |
| 149 | |
| 165 | 150 | debug.assert(total_bits == new_int_count * new_int_bits); |
| 166 | | |
| 151 | |
| 167 | 152 | var new = New.init(bytes, new_int_count); |
| 168 | 153 | new.bit_offset = bit_offset; |
| 169 | 154 | return new; |
| ... | ... | @@ -174,152 +159,132 @@ pub fn PackedIntIo(comptime Int: type) type |
| 174 | 159 | ///Creates a bit-packed array of integers of type Int. Bits |
| 175 | 160 | /// are packed using native endianess and without storing any meta |
| 176 | 161 | /// data. PackedIntArray(i3, 8) will occupy exactly 3 bytes of memory. |
| 177 | | pub fn PackedIntArray(comptime Int: type, comptime int_count: usize) type |
| 178 | | { |
| 162 | pub fn PackedIntArray(comptime Int: type, comptime int_count: usize) type { |
| 179 | 163 | const int_bits = comptime std.meta.bitCount(Int); |
| 180 | 164 | const total_bits = int_bits * int_count; |
| 181 | 165 | const total_bytes = (total_bits + 7) / 8; |
| 182 | | |
| 166 | |
| 183 | 167 | const Io = PackedIntIo(Int); |
| 184 | | |
| 185 | | return struct |
| 186 | | { |
| 168 | |
| 169 | return struct { |
| 187 | 170 | const Self = @This(); |
| 188 | | |
| 171 | |
| 189 | 172 | bytes: [total_bytes]u8, |
| 190 | | |
| 173 | |
| 191 | 174 | ///Returns the number of elements in the packed array |
| 192 | | pub fn len(self: Self) usize |
| 193 | | { |
| 175 | pub fn len(self: Self) usize { |
| 194 | 176 | return int_count; |
| 195 | 177 | } |
| 196 | | |
| 178 | |
| 197 | 179 | ///Initialize a packed array using an unpacked array |
| 198 | 180 | /// or, more likely, an array literal. |
| 199 | | pub fn init(ints: [int_count]Int) Self |
| 200 | | { |
| 181 | pub fn init(ints: [int_count]Int) Self { |
| 201 | 182 | var self = Self(undefined); |
| 202 | | for(ints) |int, i| self.set(i, int); |
| 183 | for (ints) |int, i| self.set(i, int); |
| 203 | 184 | return self; |
| 204 | 185 | } |
| 205 | | |
| 186 | |
| 206 | 187 | ///Return the Int stored at index |
| 207 | | pub fn get(self: Self, index: usize) Int |
| 208 | | { |
| 188 | pub fn get(self: Self, index: usize) Int { |
| 209 | 189 | debug.assert(index < int_count); |
| 210 | 190 | return Io.get(self.bytes, index, 0); |
| 211 | 191 | } |
| 212 | | |
| 192 | |
| 213 | 193 | ///Copy int into the array at index |
| 214 | | pub fn set(self: *Self, index: usize, int: Int) void |
| 215 | | { |
| 194 | pub fn set(self: *Self, index: usize, int: Int) void { |
| 216 | 195 | debug.assert(index < int_count); |
| 217 | 196 | return Io.set(&self.bytes, index, 0, int); |
| 218 | 197 | } |
| 219 | | |
| 198 | |
| 220 | 199 | ///Create a PackedIntSlice of the array from given start to given end |
| 221 | | pub fn slice(self: *Self, start: usize, end: usize) PackedIntSlice(Int) |
| 222 | | { |
| 200 | pub fn slice(self: *Self, start: usize, end: usize) PackedIntSlice(Int) { |
| 223 | 201 | debug.assert(start < int_count); |
| 224 | 202 | debug.assert(end <= int_count); |
| 225 | 203 | return Io.slice(&self.bytes, 0, start, end); |
| 226 | 204 | } |
| 227 | | |
| 205 | |
| 228 | 206 | ///Create a PackedIntSlice of the array using NewInt as the bit width integer. |
| 229 | 207 | /// NewInt's bit width must fit evenly within the array's Int's total bits. |
| 230 | | pub fn sliceCast(self: *Self, comptime NewInt: type) PackedIntSlice(NewInt) |
| 231 | | { |
| 208 | pub fn sliceCast(self: *Self, comptime NewInt: type) PackedIntSlice(NewInt) { |
| 232 | 209 | return Io.sliceCast(&self.bytes, NewInt, 0, int_count); |
| 233 | 210 | } |
| 234 | 211 | }; |
| 235 | 212 | } |
| 236 | 213 | |
| 237 | | ///Uses a slice as a bit-packed block of int_count integers of type Int. |
| 214 | ///Uses a slice as a bit-packed block of int_count integers of type Int. |
| 238 | 215 | /// Bits are packed using native endianess and without storing any meta |
| 239 | 216 | /// data. |
| 240 | | pub fn PackedIntSlice(comptime Int: type) type |
| 241 | | { |
| 217 | pub fn PackedIntSlice(comptime Int: type) type { |
| 242 | 218 | const int_bits = comptime std.meta.bitCount(Int); |
| 243 | 219 | const Io = PackedIntIo(Int); |
| 244 | | |
| 245 | | return struct |
| 246 | | { |
| 220 | |
| 221 | return struct { |
| 247 | 222 | const Self = @This(); |
| 248 | | |
| 223 | |
| 249 | 224 | bytes: []u8, |
| 250 | 225 | int_count: usize, |
| 251 | 226 | bit_offset: u3, |
| 252 | | |
| 227 | |
| 253 | 228 | ///Returns the number of elements in the packed slice |
| 254 | | pub fn len(self: Self) usize |
| 255 | | { |
| 229 | pub fn len(self: Self) usize { |
| 256 | 230 | return self.int_count; |
| 257 | 231 | } |
| 258 | | |
| 232 | |
| 259 | 233 | ///Calculates the number of bytes required to store a desired count |
| 260 | 234 | /// of Ints |
| 261 | | pub fn bytesRequired(int_count: usize) usize |
| 262 | | { |
| 235 | pub fn bytesRequired(int_count: usize) usize { |
| 263 | 236 | const total_bits = int_bits * int_count; |
| 264 | 237 | const total_bytes = (total_bits + 7) / 8; |
| 265 | 238 | return total_bytes; |
| 266 | 239 | } |
| 267 | | |
| 240 | |
| 268 | 241 | ///Initialize a packed slice using the memory at bytes, with int_count |
| 269 | 242 | /// elements. bytes must be large enough to accomodate the requested |
| 270 | 243 | /// count. |
| 271 | | pub fn init(bytes: []u8, int_count: usize) Self |
| 272 | | { |
| 244 | pub fn init(bytes: []u8, int_count: usize) Self { |
| 273 | 245 | debug.assert(bytes.len >= bytesRequired(int_count)); |
| 274 | | |
| 275 | | return Self |
| 276 | | { |
| 246 | |
| 247 | return Self{ |
| 277 | 248 | .bytes = bytes, |
| 278 | 249 | .int_count = int_count, |
| 279 | 250 | .bit_offset = 0, |
| 280 | 251 | }; |
| 281 | 252 | } |
| 282 | | |
| 253 | |
| 283 | 254 | ///Return the Int stored at index |
| 284 | | pub fn get(self: Self, index: usize) Int |
| 285 | | { |
| 255 | pub fn get(self: Self, index: usize) Int { |
| 286 | 256 | debug.assert(index < self.int_count); |
| 287 | 257 | return Io.get(self.bytes, index, self.bit_offset); |
| 288 | 258 | } |
| 289 | | |
| 259 | |
| 290 | 260 | ///Copy int into the array at index |
| 291 | | pub fn set(self: *Self, index: usize, int: Int) void |
| 292 | | { |
| 261 | pub fn set(self: *Self, index: usize, int: Int) void { |
| 293 | 262 | debug.assert(index < self.int_count); |
| 294 | 263 | return Io.set(self.bytes, index, self.bit_offset, int); |
| 295 | 264 | } |
| 296 | | |
| 265 | |
| 297 | 266 | ///Create a PackedIntSlice of this slice from given start to given end |
| 298 | | pub fn slice(self: Self, start: usize, end: usize) PackedIntSlice(Int) |
| 299 | | { |
| 267 | pub fn slice(self: Self, start: usize, end: usize) PackedIntSlice(Int) { |
| 300 | 268 | debug.assert(start < self.int_count); |
| 301 | 269 | debug.assert(end <= self.int_count); |
| 302 | 270 | return Io.slice(self.bytes, self.bit_offset, start, end); |
| 303 | 271 | } |
| 304 | | |
| 272 | |
| 305 | 273 | ///Create a PackedIntSlice of this slice using NewInt as the bit width integer. |
| 306 | 274 | /// NewInt's bit width must fit evenly within this slice's Int's total bits. |
| 307 | | pub fn sliceCast(self: Self, comptime NewInt: type) PackedIntSlice(NewInt) |
| 308 | | { |
| 275 | pub fn sliceCast(self: Self, comptime NewInt: type) PackedIntSlice(NewInt) { |
| 309 | 276 | return Io.sliceCast(self.bytes, NewInt, self.bit_offset, self.int_count); |
| 310 | 277 | } |
| 311 | 278 | }; |
| 312 | 279 | } |
| 313 | 280 | |
| 314 | | test "PackedIntArray" |
| 315 | | { |
| 281 | test "PackedIntArray" { |
| 316 | 282 | @setEvalBranchQuota(10000); |
| 317 | 283 | const max_bits = 256; |
| 318 | 284 | const int_count = 19; |
| 319 | | |
| 285 | |
| 320 | 286 | comptime var bits = 0; |
| 321 | | inline while(bits <= 256):(bits += 1) |
| 322 | | { |
| 287 | inline while (bits <= 256) : (bits += 1) { |
| 323 | 288 | //alternate unsigned and signed |
| 324 | 289 | const even = bits % 2 == 0; |
| 325 | 290 | const I = @IntType(even, bits); |
| ... | ... | @@ -327,100 +292,90 @@ test "PackedIntArray" |
| 327 | 292 | const PackedArray = PackedIntArray(I, int_count); |
| 328 | 293 | const expected_bytes = ((bits * int_count) + 7) / 8; |
| 329 | 294 | testing.expect(@sizeOf(PackedArray) == expected_bytes); |
| 330 | | |
| 295 | |
| 331 | 296 | var data = PackedArray(undefined); |
| 332 | | |
| 297 | |
| 333 | 298 | //write values, counting up |
| 334 | 299 | var i = usize(0); |
| 335 | 300 | var count = I(0); |
| 336 | | while(i < data.len()):(i += 1) |
| 337 | | { |
| 301 | while (i < data.len()) : (i += 1) { |
| 338 | 302 | data.set(i, count); |
| 339 | | if(bits > 0) count +%= 1; |
| 303 | if (bits > 0) count +%= 1; |
| 340 | 304 | } |
| 341 | | |
| 305 | |
| 342 | 306 | //read and verify values |
| 343 | 307 | i = 0; |
| 344 | 308 | count = 0; |
| 345 | | while(i < data.len()):(i += 1) |
| 346 | | { |
| 309 | while (i < data.len()) : (i += 1) { |
| 347 | 310 | const val = data.get(i); |
| 348 | 311 | testing.expect(val == count); |
| 349 | | if(bits > 0) count +%= 1; |
| 312 | if (bits > 0) count +%= 1; |
| 350 | 313 | } |
| 351 | 314 | } |
| 352 | 315 | } |
| 353 | 316 | |
| 354 | | test "PackedIntArray init" |
| 355 | | { |
| 317 | test "PackedIntArray init" { |
| 356 | 318 | const PackedArray = PackedIntArray(u3, 8); |
| 357 | | var packed_array = PackedArray.init([]u3{0,1,2,3,4,5,6,7}); |
| 319 | var packed_array = PackedArray.init([]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| 358 | 320 | var i = usize(0); |
| 359 | | while(i < packed_array.len()):(i += 1) testing.expect(packed_array.get(i) == i); |
| 321 | while (i < packed_array.len()) : (i += 1) testing.expect(packed_array.get(i) == i); |
| 360 | 322 | } |
| 361 | 323 | |
| 362 | | test "PackedIntSlice" |
| 363 | | { |
| 324 | test "PackedIntSlice" { |
| 364 | 325 | @setEvalBranchQuota(10000); |
| 365 | 326 | const max_bits = 256; |
| 366 | 327 | const int_count = 19; |
| 367 | 328 | const total_bits = max_bits * int_count; |
| 368 | 329 | const total_bytes = (total_bits + 7) / 8; |
| 369 | | |
| 330 | |
| 370 | 331 | var buffer: [total_bytes]u8 = undefined; |
| 371 | | |
| 332 | |
| 372 | 333 | comptime var bits = 0; |
| 373 | | inline while(bits <= 256):(bits += 1) |
| 374 | | { |
| 334 | inline while (bits <= 256) : (bits += 1) { |
| 375 | 335 | //alternate unsigned and signed |
| 376 | 336 | const even = bits % 2 == 0; |
| 377 | 337 | const I = @IntType(even, bits); |
| 378 | 338 | const P = PackedIntSlice(I); |
| 379 | | |
| 339 | |
| 380 | 340 | var data = P.init(&buffer, int_count); |
| 381 | | |
| 341 | |
| 382 | 342 | //write values, counting up |
| 383 | 343 | var i = usize(0); |
| 384 | 344 | var count = I(0); |
| 385 | | while(i < data.len()):(i += 1) |
| 386 | | { |
| 345 | while (i < data.len()) : (i += 1) { |
| 387 | 346 | data.set(i, count); |
| 388 | | if(bits > 0) count +%= 1; |
| 347 | if (bits > 0) count +%= 1; |
| 389 | 348 | } |
| 390 | | |
| 349 | |
| 391 | 350 | //read and verify values |
| 392 | 351 | i = 0; |
| 393 | 352 | count = 0; |
| 394 | | while(i < data.len()):(i += 1) |
| 395 | | { |
| 353 | while (i < data.len()) : (i += 1) { |
| 396 | 354 | const val = data.get(i); |
| 397 | 355 | testing.expect(val == count); |
| 398 | | if(bits > 0) count +%= 1; |
| 356 | if (bits > 0) count +%= 1; |
| 399 | 357 | } |
| 400 | 358 | } |
| 401 | 359 | } |
| 402 | 360 | |
| 403 | | test "PackedIntSlice of PackedInt(Array/Slice)" |
| 404 | | { |
| 361 | test "PackedIntSlice of PackedInt(Array/Slice)" { |
| 405 | 362 | const max_bits = 16; |
| 406 | 363 | const int_count = 19; |
| 407 | | |
| 364 | |
| 408 | 365 | comptime var bits = 0; |
| 409 | | inline while(bits <= max_bits):(bits += 1) |
| 410 | | { |
| 366 | inline while (bits <= max_bits) : (bits += 1) { |
| 411 | 367 | const Int = @IntType(false, bits); |
| 412 | | |
| 368 | |
| 413 | 369 | const PackedArray = PackedIntArray(Int, int_count); |
| 414 | 370 | var packed_array = PackedArray(undefined); |
| 415 | | |
| 371 | |
| 416 | 372 | const limit = (1 << bits); |
| 417 | | |
| 373 | |
| 418 | 374 | var i = usize(0); |
| 419 | | while(i < packed_array.len()):(i += 1) |
| 420 | | { |
| 375 | while (i < packed_array.len()) : (i += 1) { |
| 421 | 376 | packed_array.set(i, @intCast(Int, i % limit)); |
| 422 | 377 | } |
| 423 | | |
| 378 | |
| 424 | 379 | //slice of array |
| 425 | 380 | var packed_slice = packed_array.slice(2, 5); |
| 426 | 381 | testing.expect(packed_slice.len() == 3); |
| ... | ... | @@ -432,10 +387,10 @@ test "PackedIntSlice of PackedInt(Array/Slice)" |
| 432 | 387 | testing.expect(packed_slice.get(2) == 4 % limit); |
| 433 | 388 | packed_slice.set(1, 7 % limit); |
| 434 | 389 | testing.expect(packed_slice.get(1) == 7 % limit); |
| 435 | | |
| 390 | |
| 436 | 391 | //write through slice |
| 437 | 392 | testing.expect(packed_array.get(3) == 7 % limit); |
| 438 | | |
| 393 | |
| 439 | 394 | //slice of a slice |
| 440 | 395 | const packed_slice_two = packed_slice.slice(0, 3); |
| 441 | 396 | testing.expect(packed_slice_two.len() == 3); |
| ... | ... | @@ -444,7 +399,7 @@ test "PackedIntSlice of PackedInt(Array/Slice)" |
| 444 | 399 | testing.expect(packed_slice_two.bytes.len == ps2_expected_bytes); |
| 445 | 400 | testing.expect(packed_slice_two.get(1) == 7 % limit); |
| 446 | 401 | testing.expect(packed_slice_two.get(2) == 4 % limit); |
| 447 | | |
| 402 | |
| 448 | 403 | //size one case |
| 449 | 404 | const packed_slice_three = packed_slice_two.slice(1, 2); |
| 450 | 405 | testing.expect(packed_slice_three.len() == 1); |
| ... | ... | @@ -452,12 +407,12 @@ test "PackedIntSlice of PackedInt(Array/Slice)" |
| 452 | 407 | const ps3_expected_bytes = (ps3_bit_count + 7) / 8; |
| 453 | 408 | testing.expect(packed_slice_three.bytes.len == ps3_expected_bytes); |
| 454 | 409 | testing.expect(packed_slice_three.get(0) == 7 % limit); |
| 455 | | |
| 410 | |
| 456 | 411 | //empty slice case |
| 457 | 412 | const packed_slice_empty = packed_slice.slice(0, 0); |
| 458 | 413 | testing.expect(packed_slice_empty.len() == 0); |
| 459 | 414 | testing.expect(packed_slice_empty.bytes.len == 0); |
| 460 | | |
| 415 | |
| 461 | 416 | //slicing at byte boundaries |
| 462 | 417 | const packed_slice_edge = packed_array.slice(8, 16); |
| 463 | 418 | testing.expect(packed_slice_edge.len() == 8); |
| ... | ... | @@ -466,84 +421,70 @@ test "PackedIntSlice of PackedInt(Array/Slice)" |
| 466 | 421 | testing.expect(packed_slice_edge.bytes.len == pse_expected_bytes); |
| 467 | 422 | testing.expect(packed_slice_edge.bit_offset == 0); |
| 468 | 423 | } |
| 469 | | |
| 470 | 424 | } |
| 471 | 425 | |
| 472 | | test "PackedIntSlice accumulating bit offsets" |
| 473 | | { |
| 426 | test "PackedIntSlice accumulating bit offsets" { |
| 474 | 427 | //bit_offset is u3, so standard debugging asserts should catch |
| 475 | 428 | // anything |
| 476 | 429 | { |
| 477 | 430 | const PackedArray = PackedIntArray(u3, 16); |
| 478 | 431 | var packed_array = PackedArray(undefined); |
| 479 | | |
| 432 | |
| 480 | 433 | var packed_slice = packed_array.slice(0, packed_array.len()); |
| 481 | 434 | var i = usize(0); |
| 482 | | while(i < packed_array.len() - 1):(i += 1) |
| 483 | | { |
| 484 | | |
| 435 | while (i < packed_array.len() - 1) : (i += 1) { |
| 485 | 436 | packed_slice = packed_slice.slice(1, packed_slice.len()); |
| 486 | 437 | } |
| 487 | 438 | } |
| 488 | 439 | { |
| 489 | 440 | const PackedArray = PackedIntArray(u11, 88); |
| 490 | 441 | var packed_array = PackedArray(undefined); |
| 491 | | |
| 442 | |
| 492 | 443 | var packed_slice = packed_array.slice(0, packed_array.len()); |
| 493 | 444 | var i = usize(0); |
| 494 | | while(i < packed_array.len() - 1):(i += 1) |
| 495 | | { |
| 445 | while (i < packed_array.len() - 1) : (i += 1) { |
| 496 | 446 | packed_slice = packed_slice.slice(1, packed_slice.len()); |
| 497 | 447 | } |
| 498 | 448 | } |
| 499 | | |
| 500 | 449 | } |
| 501 | 450 | |
| 502 | 451 | //@NOTE: As I do not have a big endian system to test this on, |
| 503 | 452 | // big endian values were not tested |
| 504 | | test "PackedInt(Array/Slice) sliceCast" |
| 505 | | { |
| 453 | test "PackedInt(Array/Slice) sliceCast" { |
| 506 | 454 | const PackedArray = PackedIntArray(u1, 16); |
| 507 | | var packed_array = PackedArray.init([]u1{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}); |
| 455 | var packed_array = PackedArray.init([]u1{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }); |
| 508 | 456 | const packed_slice_cast_2 = packed_array.sliceCast(u2); |
| 509 | 457 | const packed_slice_cast_4 = packed_slice_cast_2.sliceCast(u4); |
| 510 | 458 | var packed_slice_cast_9 = packed_array.slice(0, (packed_array.len() / 9) * 9).sliceCast(u9); |
| 511 | 459 | const packed_slice_cast_3 = packed_slice_cast_9.sliceCast(u3); |
| 512 | | |
| 460 | |
| 513 | 461 | var i = usize(0); |
| 514 | | while(i < packed_slice_cast_2.len()):(i += 1) |
| 515 | | { |
| 516 | | const val = switch(builtin.endian) |
| 517 | | { |
| 462 | while (i < packed_slice_cast_2.len()) : (i += 1) { |
| 463 | const val = switch (builtin.endian) { |
| 518 | 464 | .Big => 0b01, |
| 519 | 465 | .Little => 0b10, |
| 520 | 466 | }; |
| 521 | 467 | testing.expect(packed_slice_cast_2.get(i) == val); |
| 522 | 468 | } |
| 523 | 469 | i = 0; |
| 524 | | while(i < packed_slice_cast_4.len()):(i += 1) |
| 525 | | { |
| 526 | | const val = switch(builtin.endian) |
| 527 | | { |
| 470 | while (i < packed_slice_cast_4.len()) : (i += 1) { |
| 471 | const val = switch (builtin.endian) { |
| 528 | 472 | .Big => 0b0101, |
| 529 | 473 | .Little => 0b1010, |
| 530 | 474 | }; |
| 531 | 475 | testing.expect(packed_slice_cast_4.get(i) == val); |
| 532 | 476 | } |
| 533 | 477 | i = 0; |
| 534 | | while(i < packed_slice_cast_9.len()):(i += 1) |
| 535 | | { |
| 478 | while (i < packed_slice_cast_9.len()) : (i += 1) { |
| 536 | 479 | const val = 0b010101010; |
| 537 | 480 | testing.expect(packed_slice_cast_9.get(i) == val); |
| 538 | 481 | packed_slice_cast_9.set(i, 0b111000111); |
| 539 | 482 | } |
| 540 | 483 | i = 0; |
| 541 | | while(i < packed_slice_cast_3.len()):(i += 1) |
| 542 | | { |
| 543 | | const val = switch(builtin.endian) |
| 544 | | { |
| 545 | | .Big => if(i % 2 == 0) u3(0b111) else u3(0b000), |
| 546 | | .Little => if(i % 2 == 0) u3(0b111) else u3(0b000), |
| 484 | while (i < packed_slice_cast_3.len()) : (i += 1) { |
| 485 | const val = switch (builtin.endian) { |
| 486 | .Big => if (i % 2 == 0) u3(0b111) else u3(0b000), |
| 487 | .Little => if (i % 2 == 0) u3(0b111) else u3(0b000), |
| 547 | 488 | }; |
| 548 | 489 | testing.expect(packed_slice_cast_3.get(i) == val); |
| 549 | 490 | } |
| ... | ... | @@ -558,44 +499,39 @@ test "PackedInt(Array/Slice) sliceCast" |
| 558 | 499 | // and reading the last element. The assumption is that the page |
| 559 | 500 | // after this one is not mapped and will cause a segfault if we |
| 560 | 501 | // don't account for the bounds. |
| 561 | | test "PackedIntArray at end of available memory" |
| 562 | | { |
| 563 | | switch(builtin.os) |
| 564 | | { |
| 502 | test "PackedIntArray at end of available memory" { |
| 503 | switch (builtin.os) { |
| 565 | 504 | .linux, .macosx, .ios, .freebsd, .netbsd => {}, |
| 566 | 505 | else => return, |
| 567 | 506 | } |
| 568 | 507 | const PackedArray = PackedIntArray(u3, 8); |
| 569 | | |
| 570 | | const Padded = struct |
| 571 | | { |
| 508 | |
| 509 | const Padded = struct { |
| 572 | 510 | _: [std.os.page_size - @sizeOf(PackedArray)]u8, |
| 573 | 511 | p: PackedArray, |
| 574 | 512 | }; |
| 575 | | |
| 513 | |
| 576 | 514 | var da = std.heap.DirectAllocator.init(); |
| 577 | 515 | const allocator = &da.allocator; |
| 578 | | |
| 516 | |
| 579 | 517 | var pad = try allocator.create(Padded); |
| 580 | 518 | defer allocator.destroy(pad); |
| 581 | 519 | pad.p.set(7, std.math.maxInt(u3)); |
| 582 | 520 | } |
| 583 | 521 | |
| 584 | | test "PackedIntSlice at end of available memory" |
| 585 | | { |
| 586 | | switch(builtin.os) |
| 587 | | { |
| 522 | test "PackedIntSlice at end of available memory" { |
| 523 | switch (builtin.os) { |
| 588 | 524 | .linux, .macosx, .ios, .freebsd, .netbsd => {}, |
| 589 | 525 | else => return, |
| 590 | 526 | } |
| 591 | 527 | const PackedSlice = PackedIntSlice(u11); |
| 592 | | |
| 528 | |
| 593 | 529 | var da = std.heap.DirectAllocator.init(); |
| 594 | 530 | const allocator = &da.allocator; |
| 595 | | |
| 531 | |
| 596 | 532 | var page = try allocator.alloc(u8, std.os.page_size); |
| 597 | 533 | defer allocator.free(page); |
| 598 | | |
| 599 | | var p = PackedSlice.init(page[std.os.page_size - 2..], 1); |
| 534 | |
| 535 | var p = PackedSlice.init(page[std.os.page_size - 2 ..], 1); |
| 600 | 536 | p.set(0, std.math.maxInt(u11)); |
| 601 | 537 | } |