| ... | ... | @@ -3,148 +3,134 @@ 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, comptime endian: builtin.Endian) type |
| 7 | | { |
| 6 | pub fn PackedIntIo(comptime Int: type, comptime endian: builtin.Endian) 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 | | |
| 75 | | if(endian != builtin.endian) value = @bswap(Container, value); |
| 76 | | |
| 77 | | switch(endian) |
| 78 | | { |
| 79 | | .Big => |
| 80 | | { |
| 68 | |
| 69 | if (endian != builtin.endian) value = @bswap(Container, value); |
| 70 | |
| 71 | switch (endian) { |
| 72 | .Big => { |
| 81 | 73 | value <<= @intCast(Shift, head_keep_bits); |
| 82 | 74 | value >>= @intCast(Shift, head_keep_bits); |
| 83 | 75 | value >>= @intCast(Shift, tail_keep_bits); |
| 84 | 76 | }, |
| 85 | | .Little => |
| 86 | | { |
| 77 | .Little => { |
| 87 | 78 | value <<= @intCast(Shift, tail_keep_bits); |
| 88 | 79 | value >>= @intCast(Shift, tail_keep_bits); |
| 89 | 80 | value >>= @intCast(Shift, head_keep_bits); |
| 90 | 81 | }, |
| 91 | 82 | } |
| 92 | | |
| 83 | |
| 93 | 84 | return @bitCast(Int, @truncate(UnInt, value)); |
| 94 | 85 | } |
| 95 | | |
| 96 | | pub fn set(bytes: []u8, index: usize, bit_offset: u3, int: Int) void |
| 97 | | { |
| 98 | | if(int_bits == 0) return; |
| 86 | |
| 87 | pub fn set(bytes: []u8, index: usize, bit_offset: u3, int: Int) void { |
| 88 | if (int_bits == 0) return; |
| 99 | 89 | |
| 100 | 90 | const bit_index = (index * int_bits) + bit_offset; |
| 101 | 91 | const max_end_byte = (bit_index + max_io_bits) / 8; |
| 102 | | |
| 92 | |
| 103 | 93 | //Using the larger container size will potentially write out of bounds |
| 104 | | if(max_end_byte > bytes.len) return setBits(bytes, MinIo, bit_index, int); |
| 94 | if (max_end_byte > bytes.len) return setBits(bytes, MinIo, bit_index, int); |
| 105 | 95 | setBits(bytes, MaxIo, bit_index, int); |
| 106 | 96 | } |
| 107 | | |
| 108 | | fn setBits(bytes: []u8, comptime Container: type, bit_index: usize, int: Int) void |
| 109 | | { |
| 97 | |
| 98 | fn setBits(bytes: []u8, comptime Container: type, bit_index: usize, int: Int) void { |
| 110 | 99 | const container_bits = comptime std.meta.bitCount(Container); |
| 111 | 100 | const Shift = std.math.Log2Int(Container); |
| 112 | | |
| 101 | |
| 113 | 102 | const start_byte = bit_index / 8; |
| 114 | 103 | const head_keep_bits = bit_index - (start_byte * 8); |
| 115 | 104 | const tail_keep_bits = container_bits - (int_bits + head_keep_bits); |
| 116 | | const keep_shift = switch(endian) |
| 117 | | { |
| 105 | const keep_shift = switch (endian) { |
| 118 | 106 | .Big => @intCast(Shift, tail_keep_bits), |
| 119 | 107 | .Little => @intCast(Shift, head_keep_bits), |
| 120 | 108 | }; |
| 121 | | |
| 109 | |
| 122 | 110 | //position the bits where they need to be in the container |
| 123 | 111 | const value = @intCast(Container, @bitCast(UnInt, int)) << keep_shift; |
| 124 | | |
| 112 | |
| 125 | 113 | //read existing bytes |
| 126 | 114 | const target_ptr = @ptrCast(*align(1) Container, &bytes[start_byte]); |
| 127 | 115 | var target = target_ptr.*; |
| 128 | | |
| 129 | | if(endian != builtin.endian) target = @bswap(Container, target); |
| 130 | | |
| 116 | |
| 117 | if (endian != builtin.endian) target = @bswap(Container, target); |
| 118 | |
| 131 | 119 | //zero the bits we want to replace in the existing bytes |
| 132 | 120 | const inv_mask = @intCast(Container, std.math.maxInt(UnInt)) << keep_shift; |
| 133 | 121 | const mask = ~inv_mask; |
| 134 | 122 | target &= mask; |
| 135 | | |
| 123 | |
| 136 | 124 | //merge the new value |
| 137 | 125 | target |= value; |
| 138 | | |
| 139 | | if(endian != builtin.endian) target = @bswap(Container, target); |
| 140 | | |
| 126 | |
| 127 | if (endian != builtin.endian) target = @bswap(Container, target); |
| 128 | |
| 141 | 129 | //save it back |
| 142 | 130 | target_ptr.* = target; |
| 143 | 131 | } |
| 144 | | |
| 145 | | fn slice(bytes: []u8, bit_offset: u3, start: usize, end: usize) |
| 146 | | PackedIntSliceEndian(Int, endian) |
| 147 | | { |
| 132 | |
| 133 | fn slice(bytes: []u8, bit_offset: u3, start: usize, end: usize) PackedIntSliceEndian(Int, endian) { |
| 148 | 134 | debug.assert(end >= start); |
| 149 | 135 | |
| 150 | 136 | const length = end - start; |
| ... | ... | @@ -152,28 +138,26 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: builtin.Endian) type |
| 152 | 138 | const start_byte = bit_index / 8; |
| 153 | 139 | const end_byte = (bit_index + (length * int_bits) + 7) / 8; |
| 154 | 140 | const new_bytes = bytes[start_byte..end_byte]; |
| 155 | | |
| 156 | | if(length == 0) return PackedIntSliceEndian(Int, endian).init(new_bytes[0..0], 0); |
| 157 | | |
| 141 | |
| 142 | if (length == 0) return PackedIntSliceEndian(Int, endian).init(new_bytes[0..0], 0); |
| 143 | |
| 158 | 144 | var new_slice = PackedIntSliceEndian(Int, endian).init(new_bytes, length); |
| 159 | 145 | new_slice.bit_offset = @intCast(u3, (bit_index - (start_byte * 8))); |
| 160 | 146 | return new_slice; |
| 161 | 147 | } |
| 162 | | |
| 163 | | fn sliceCast(bytes: []u8, comptime NewInt: type, comptime new_endian: builtin.Endian, |
| 164 | | bit_offset: u3, old_len: usize) PackedIntSliceEndian(NewInt, new_endian) |
| 165 | | { |
| 148 | |
| 149 | fn sliceCast(bytes: []u8, comptime NewInt: type, comptime new_endian: builtin.Endian, bit_offset: u3, old_len: usize) PackedIntSliceEndian(NewInt, new_endian) { |
| 166 | 150 | const new_int_bits = comptime std.meta.bitCount(NewInt); |
| 167 | 151 | const New = PackedIntSliceEndian(NewInt, new_endian); |
| 168 | | |
| 152 | |
| 169 | 153 | const total_bits = (old_len * int_bits); |
| 170 | 154 | const new_int_count = total_bits / new_int_bits; |
| 171 | | |
| 155 | |
| 172 | 156 | debug.assert(total_bits == new_int_count * new_int_bits); |
| 173 | | |
| 157 | |
| 174 | 158 | var new = New.init(bytes, new_int_count); |
| 175 | 159 | new.bit_offset = bit_offset; |
| 176 | | |
| 160 | |
| 177 | 161 | return new; |
| 178 | 162 | } |
| 179 | 163 | }; |
| ... | ... | @@ -182,187 +166,160 @@ pub fn PackedIntIo(comptime Int: type, comptime endian: builtin.Endian) type |
| 182 | 166 | ///Creates a bit-packed array of integers of type Int. Bits |
| 183 | 167 | /// are packed using native endianess and without storing any meta |
| 184 | 168 | /// data. PackedIntArray(i3, 8) will occupy exactly 3 bytes of memory. |
| 185 | | pub fn PackedIntArray(comptime Int: type, comptime int_count: usize) type |
| 186 | | { |
| 169 | pub fn PackedIntArray(comptime Int: type, comptime int_count: usize) type { |
| 187 | 170 | return PackedIntArrayEndian(Int, builtin.endian, int_count); |
| 188 | 171 | } |
| 189 | 172 | |
| 190 | 173 | ///Creates a bit-packed array of integers of type Int. Bits |
| 191 | 174 | /// are packed using specified endianess and without storing any meta |
| 192 | 175 | /// data. |
| 193 | | pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: builtin.Endian, |
| 194 | | comptime int_count: usize) type |
| 195 | | { |
| 176 | pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: builtin.Endian, comptime int_count: usize) type { |
| 196 | 177 | const int_bits = comptime std.meta.bitCount(Int); |
| 197 | 178 | const total_bits = int_bits * int_count; |
| 198 | 179 | const total_bytes = (total_bits + 7) / 8; |
| 199 | | |
| 180 | |
| 200 | 181 | const Io = PackedIntIo(Int, endian); |
| 201 | | |
| 202 | | return struct |
| 203 | | { |
| 182 | |
| 183 | return struct { |
| 204 | 184 | const Self = @This(); |
| 205 | | |
| 185 | |
| 206 | 186 | bytes: [total_bytes]u8, |
| 207 | | |
| 187 | |
| 208 | 188 | ///Returns the number of elements in the packed array |
| 209 | | pub fn len(self: Self) usize |
| 210 | | { |
| 189 | pub fn len(self: Self) usize { |
| 211 | 190 | return int_count; |
| 212 | 191 | } |
| 213 | | |
| 192 | |
| 214 | 193 | ///Initialize a packed array using an unpacked array |
| 215 | 194 | /// or, more likely, an array literal. |
| 216 | | pub fn init(ints: [int_count]Int) Self |
| 217 | | { |
| 195 | pub fn init(ints: [int_count]Int) Self { |
| 218 | 196 | var self = Self(undefined); |
| 219 | | for(ints) |int, i| self.set(i, int); |
| 197 | for (ints) |int, i| self.set(i, int); |
| 220 | 198 | return self; |
| 221 | 199 | } |
| 222 | | |
| 200 | |
| 223 | 201 | ///Return the Int stored at index |
| 224 | | pub fn get(self: Self, index: usize) Int |
| 225 | | { |
| 202 | pub fn get(self: Self, index: usize) Int { |
| 226 | 203 | debug.assert(index < int_count); |
| 227 | 204 | return Io.get(self.bytes, index, 0); |
| 228 | 205 | } |
| 229 | | |
| 206 | |
| 230 | 207 | ///Copy int into the array at index |
| 231 | | pub fn set(self: *Self, index: usize, int: Int) void |
| 232 | | { |
| 208 | pub fn set(self: *Self, index: usize, int: Int) void { |
| 233 | 209 | debug.assert(index < int_count); |
| 234 | 210 | return Io.set(&self.bytes, index, 0, int); |
| 235 | 211 | } |
| 236 | | |
| 212 | |
| 237 | 213 | ///Create a PackedIntSlice of the array from given start to given end |
| 238 | | pub fn slice(self: *Self, start: usize, end: usize) PackedIntSliceEndian(Int, endian) |
| 239 | | { |
| 214 | pub fn slice(self: *Self, start: usize, end: usize) PackedIntSliceEndian(Int, endian) { |
| 240 | 215 | debug.assert(start < int_count); |
| 241 | 216 | debug.assert(end <= int_count); |
| 242 | 217 | return Io.slice(&self.bytes, 0, start, end); |
| 243 | 218 | } |
| 244 | | |
| 219 | |
| 245 | 220 | ///Create a PackedIntSlice of the array using NewInt as the bit width integer. |
| 246 | 221 | /// NewInt's bit width must fit evenly within the array's Int's total bits. |
| 247 | | pub fn sliceCast(self: *Self, comptime NewInt: type) PackedIntSlice(NewInt) |
| 248 | | { |
| 222 | pub fn sliceCast(self: *Self, comptime NewInt: type) PackedIntSlice(NewInt) { |
| 249 | 223 | return self.sliceCastEndian(NewInt, endian); |
| 250 | 224 | } |
| 251 | | |
| 225 | |
| 252 | 226 | ///Create a PackedIntSlice of the array using NewInt as the bit width integer |
| 253 | 227 | /// and new_endian as the new endianess. NewInt's bit width must fit evenly within |
| 254 | 228 | /// the array's Int's total bits. |
| 255 | | pub fn sliceCastEndian(self: *Self, comptime NewInt: type, |
| 256 | | comptime new_endian: builtin.Endian) PackedIntSliceEndian(NewInt, new_endian) |
| 257 | | { |
| 229 | pub fn sliceCastEndian(self: *Self, comptime NewInt: type, comptime new_endian: builtin.Endian) PackedIntSliceEndian(NewInt, new_endian) { |
| 258 | 230 | return Io.sliceCast(&self.bytes, NewInt, new_endian, 0, int_count); |
| 259 | 231 | } |
| 260 | 232 | }; |
| 261 | 233 | } |
| 262 | 234 | |
| 263 | | ///Uses a slice as a bit-packed block of int_count integers of type Int. |
| 235 | ///Uses a slice as a bit-packed block of int_count integers of type Int. |
| 264 | 236 | /// Bits are packed using native endianess and without storing any meta |
| 265 | 237 | /// data. |
| 266 | | pub fn PackedIntSlice(comptime Int: type) type |
| 267 | | { |
| 238 | pub fn PackedIntSlice(comptime Int: type) type { |
| 268 | 239 | return PackedIntSliceEndian(Int, builtin.endian); |
| 269 | 240 | } |
| 270 | 241 | |
| 271 | | ///Uses a slice as a bit-packed block of int_count integers of type Int. |
| 242 | ///Uses a slice as a bit-packed block of int_count integers of type Int. |
| 272 | 243 | /// Bits are packed using specified endianess and without storing any meta |
| 273 | 244 | /// data. |
| 274 | | pub fn PackedIntSliceEndian(comptime Int: type, comptime endian: builtin.Endian) type |
| 275 | | { |
| 245 | pub fn PackedIntSliceEndian(comptime Int: type, comptime endian: builtin.Endian) type { |
| 276 | 246 | const int_bits = comptime std.meta.bitCount(Int); |
| 277 | 247 | const Io = PackedIntIo(Int, endian); |
| 278 | | |
| 279 | | return struct |
| 280 | | { |
| 248 | |
| 249 | return struct { |
| 281 | 250 | const Self = @This(); |
| 282 | | |
| 251 | |
| 283 | 252 | bytes: []u8, |
| 284 | 253 | int_count: usize, |
| 285 | 254 | bit_offset: u3, |
| 286 | | |
| 255 | |
| 287 | 256 | ///Returns the number of elements in the packed slice |
| 288 | | pub fn len(self: Self) usize |
| 289 | | { |
| 257 | pub fn len(self: Self) usize { |
| 290 | 258 | return self.int_count; |
| 291 | 259 | } |
| 292 | | |
| 260 | |
| 293 | 261 | ///Calculates the number of bytes required to store a desired count |
| 294 | 262 | /// of Ints |
| 295 | | pub fn bytesRequired(int_count: usize) usize |
| 296 | | { |
| 263 | pub fn bytesRequired(int_count: usize) usize { |
| 297 | 264 | const total_bits = int_bits * int_count; |
| 298 | 265 | const total_bytes = (total_bits + 7) / 8; |
| 299 | 266 | return total_bytes; |
| 300 | 267 | } |
| 301 | | |
| 268 | |
| 302 | 269 | ///Initialize a packed slice using the memory at bytes, with int_count |
| 303 | 270 | /// elements. bytes must be large enough to accomodate the requested |
| 304 | 271 | /// count. |
| 305 | | pub fn init(bytes: []u8, int_count: usize) Self |
| 306 | | { |
| 272 | pub fn init(bytes: []u8, int_count: usize) Self { |
| 307 | 273 | debug.assert(bytes.len >= bytesRequired(int_count)); |
| 308 | | |
| 309 | | return Self |
| 310 | | { |
| 274 | |
| 275 | return Self{ |
| 311 | 276 | .bytes = bytes, |
| 312 | 277 | .int_count = int_count, |
| 313 | 278 | .bit_offset = 0, |
| 314 | 279 | }; |
| 315 | 280 | } |
| 316 | | |
| 281 | |
| 317 | 282 | ///Return the Int stored at index |
| 318 | | pub fn get(self: Self, index: usize) Int |
| 319 | | { |
| 283 | pub fn get(self: Self, index: usize) Int { |
| 320 | 284 | debug.assert(index < self.int_count); |
| 321 | 285 | return Io.get(self.bytes, index, self.bit_offset); |
| 322 | 286 | } |
| 323 | | |
| 287 | |
| 324 | 288 | ///Copy int into the array at index |
| 325 | | pub fn set(self: *Self, index: usize, int: Int) void |
| 326 | | { |
| 289 | pub fn set(self: *Self, index: usize, int: Int) void { |
| 327 | 290 | debug.assert(index < self.int_count); |
| 328 | 291 | return Io.set(self.bytes, index, self.bit_offset, int); |
| 329 | 292 | } |
| 330 | | |
| 293 | |
| 331 | 294 | ///Create a PackedIntSlice of this slice from given start to given end |
| 332 | | pub fn slice(self: Self, start: usize, end: usize) PackedIntSliceEndian(Int, endian) |
| 333 | | { |
| 295 | pub fn slice(self: Self, start: usize, end: usize) PackedIntSliceEndian(Int, endian) { |
| 334 | 296 | debug.assert(start < self.int_count); |
| 335 | 297 | debug.assert(end <= self.int_count); |
| 336 | 298 | return Io.slice(self.bytes, self.bit_offset, start, end); |
| 337 | 299 | } |
| 338 | | |
| 300 | |
| 339 | 301 | ///Create a PackedIntSlice of this slice using NewInt as the bit width integer. |
| 340 | 302 | /// NewInt's bit width must fit evenly within this slice's Int's total bits. |
| 341 | | pub fn sliceCast(self: Self, comptime NewInt: type) PackedIntSliceEndian(NewInt, endian) |
| 342 | | { |
| 303 | pub fn sliceCast(self: Self, comptime NewInt: type) PackedIntSliceEndian(NewInt, endian) { |
| 343 | 304 | return self.sliceCastEndian(NewInt, endian); |
| 344 | 305 | } |
| 345 | | |
| 306 | |
| 346 | 307 | ///Create a PackedIntSlice of this slice using NewInt as the bit width integer |
| 347 | 308 | /// and new_endian as the new endianess. NewInt's bit width must fit evenly within |
| 348 | 309 | /// this slice's Int's total bits. |
| 349 | | pub fn sliceCastEndian(self: Self, comptime NewInt: type, |
| 350 | | comptime new_endian: builtin.Endian) PackedIntSliceEndian(NewInt, new_endian) |
| 351 | | { |
| 310 | pub fn sliceCastEndian(self: Self, comptime NewInt: type, comptime new_endian: builtin.Endian) PackedIntSliceEndian(NewInt, new_endian) { |
| 352 | 311 | return Io.sliceCast(self.bytes, NewInt, new_endian, self.bit_offset, self.int_count); |
| 353 | 312 | } |
| 354 | 313 | }; |
| 355 | 314 | } |
| 356 | 315 | |
| 357 | | test "PackedIntArray" |
| 358 | | { |
| 316 | test "PackedIntArray" { |
| 359 | 317 | @setEvalBranchQuota(10000); |
| 360 | 318 | const max_bits = 256; |
| 361 | 319 | const int_count = 19; |
| 362 | | |
| 320 | |
| 363 | 321 | comptime var bits = 0; |
| 364 | | inline while(bits <= 256):(bits += 1) |
| 365 | | { |
| 322 | inline while (bits <= 256) : (bits += 1) { |
| 366 | 323 | //alternate unsigned and signed |
| 367 | 324 | const even = bits % 2 == 0; |
| 368 | 325 | const I = @IntType(even, bits); |
| ... | ... | @@ -370,100 +327,90 @@ test "PackedIntArray" |
| 370 | 327 | const PackedArray = PackedIntArray(I, int_count); |
| 371 | 328 | const expected_bytes = ((bits * int_count) + 7) / 8; |
| 372 | 329 | testing.expect(@sizeOf(PackedArray) == expected_bytes); |
| 373 | | |
| 330 | |
| 374 | 331 | var data = PackedArray(undefined); |
| 375 | | |
| 332 | |
| 376 | 333 | //write values, counting up |
| 377 | 334 | var i = usize(0); |
| 378 | 335 | var count = I(0); |
| 379 | | while(i < data.len()):(i += 1) |
| 380 | | { |
| 336 | while (i < data.len()) : (i += 1) { |
| 381 | 337 | data.set(i, count); |
| 382 | | if(bits > 0) count +%= 1; |
| 338 | if (bits > 0) count +%= 1; |
| 383 | 339 | } |
| 384 | | |
| 340 | |
| 385 | 341 | //read and verify values |
| 386 | 342 | i = 0; |
| 387 | 343 | count = 0; |
| 388 | | while(i < data.len()):(i += 1) |
| 389 | | { |
| 344 | while (i < data.len()) : (i += 1) { |
| 390 | 345 | const val = data.get(i); |
| 391 | 346 | testing.expect(val == count); |
| 392 | | if(bits > 0) count +%= 1; |
| 347 | if (bits > 0) count +%= 1; |
| 393 | 348 | } |
| 394 | 349 | } |
| 395 | 350 | } |
| 396 | 351 | |
| 397 | | test "PackedIntArray init" |
| 398 | | { |
| 352 | test "PackedIntArray init" { |
| 399 | 353 | const PackedArray = PackedIntArray(u3, 8); |
| 400 | | var packed_array = PackedArray.init([]u3{0,1,2,3,4,5,6,7}); |
| 354 | var packed_array = PackedArray.init([]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| 401 | 355 | var i = usize(0); |
| 402 | | while(i < packed_array.len()):(i += 1) testing.expect(packed_array.get(i) == i); |
| 356 | while (i < packed_array.len()) : (i += 1) testing.expect(packed_array.get(i) == i); |
| 403 | 357 | } |
| 404 | 358 | |
| 405 | | test "PackedIntSlice" |
| 406 | | { |
| 359 | test "PackedIntSlice" { |
| 407 | 360 | @setEvalBranchQuota(10000); |
| 408 | 361 | const max_bits = 256; |
| 409 | 362 | const int_count = 19; |
| 410 | 363 | const total_bits = max_bits * int_count; |
| 411 | 364 | const total_bytes = (total_bits + 7) / 8; |
| 412 | | |
| 365 | |
| 413 | 366 | var buffer: [total_bytes]u8 = undefined; |
| 414 | | |
| 367 | |
| 415 | 368 | comptime var bits = 0; |
| 416 | | inline while(bits <= 256):(bits += 1) |
| 417 | | { |
| 369 | inline while (bits <= 256) : (bits += 1) { |
| 418 | 370 | //alternate unsigned and signed |
| 419 | 371 | const even = bits % 2 == 0; |
| 420 | 372 | const I = @IntType(even, bits); |
| 421 | 373 | const P = PackedIntSlice(I); |
| 422 | | |
| 374 | |
| 423 | 375 | var data = P.init(&buffer, int_count); |
| 424 | | |
| 376 | |
| 425 | 377 | //write values, counting up |
| 426 | 378 | var i = usize(0); |
| 427 | 379 | var count = I(0); |
| 428 | | while(i < data.len()):(i += 1) |
| 429 | | { |
| 380 | while (i < data.len()) : (i += 1) { |
| 430 | 381 | data.set(i, count); |
| 431 | | if(bits > 0) count +%= 1; |
| 382 | if (bits > 0) count +%= 1; |
| 432 | 383 | } |
| 433 | | |
| 384 | |
| 434 | 385 | //read and verify values |
| 435 | 386 | i = 0; |
| 436 | 387 | count = 0; |
| 437 | | while(i < data.len()):(i += 1) |
| 438 | | { |
| 388 | while (i < data.len()) : (i += 1) { |
| 439 | 389 | const val = data.get(i); |
| 440 | 390 | testing.expect(val == count); |
| 441 | | if(bits > 0) count +%= 1; |
| 391 | if (bits > 0) count +%= 1; |
| 442 | 392 | } |
| 443 | 393 | } |
| 444 | 394 | } |
| 445 | 395 | |
| 446 | | test "PackedIntSlice of PackedInt(Array/Slice)" |
| 447 | | { |
| 396 | test "PackedIntSlice of PackedInt(Array/Slice)" { |
| 448 | 397 | const max_bits = 16; |
| 449 | 398 | const int_count = 19; |
| 450 | | |
| 399 | |
| 451 | 400 | comptime var bits = 0; |
| 452 | | inline while(bits <= max_bits):(bits += 1) |
| 453 | | { |
| 401 | inline while (bits <= max_bits) : (bits += 1) { |
| 454 | 402 | const Int = @IntType(false, bits); |
| 455 | | |
| 403 | |
| 456 | 404 | const PackedArray = PackedIntArray(Int, int_count); |
| 457 | 405 | var packed_array = PackedArray(undefined); |
| 458 | | |
| 406 | |
| 459 | 407 | const limit = (1 << bits); |
| 460 | | |
| 408 | |
| 461 | 409 | var i = usize(0); |
| 462 | | while(i < packed_array.len()):(i += 1) |
| 463 | | { |
| 410 | while (i < packed_array.len()) : (i += 1) { |
| 464 | 411 | packed_array.set(i, @intCast(Int, i % limit)); |
| 465 | 412 | } |
| 466 | | |
| 413 | |
| 467 | 414 | //slice of array |
| 468 | 415 | var packed_slice = packed_array.slice(2, 5); |
| 469 | 416 | testing.expect(packed_slice.len() == 3); |
| ... | ... | @@ -475,10 +422,10 @@ test "PackedIntSlice of PackedInt(Array/Slice)" |
| 475 | 422 | testing.expect(packed_slice.get(2) == 4 % limit); |
| 476 | 423 | packed_slice.set(1, 7 % limit); |
| 477 | 424 | testing.expect(packed_slice.get(1) == 7 % limit); |
| 478 | | |
| 425 | |
| 479 | 426 | //write through slice |
| 480 | 427 | testing.expect(packed_array.get(3) == 7 % limit); |
| 481 | | |
| 428 | |
| 482 | 429 | //slice of a slice |
| 483 | 430 | const packed_slice_two = packed_slice.slice(0, 3); |
| 484 | 431 | testing.expect(packed_slice_two.len() == 3); |
| ... | ... | @@ -487,7 +434,7 @@ test "PackedIntSlice of PackedInt(Array/Slice)" |
| 487 | 434 | testing.expect(packed_slice_two.bytes.len == ps2_expected_bytes); |
| 488 | 435 | testing.expect(packed_slice_two.get(1) == 7 % limit); |
| 489 | 436 | testing.expect(packed_slice_two.get(2) == 4 % limit); |
| 490 | | |
| 437 | |
| 491 | 438 | //size one case |
| 492 | 439 | const packed_slice_three = packed_slice_two.slice(1, 2); |
| 493 | 440 | testing.expect(packed_slice_three.len() == 1); |
| ... | ... | @@ -495,12 +442,12 @@ test "PackedIntSlice of PackedInt(Array/Slice)" |
| 495 | 442 | const ps3_expected_bytes = (ps3_bit_count + 7) / 8; |
| 496 | 443 | testing.expect(packed_slice_three.bytes.len == ps3_expected_bytes); |
| 497 | 444 | testing.expect(packed_slice_three.get(0) == 7 % limit); |
| 498 | | |
| 445 | |
| 499 | 446 | //empty slice case |
| 500 | 447 | const packed_slice_empty = packed_slice.slice(0, 0); |
| 501 | 448 | testing.expect(packed_slice_empty.len() == 0); |
| 502 | 449 | testing.expect(packed_slice_empty.bytes.len == 0); |
| 503 | | |
| 450 | |
| 504 | 451 | //slicing at byte boundaries |
| 505 | 452 | const packed_slice_edge = packed_array.slice(8, 16); |
| 506 | 453 | testing.expect(packed_slice_edge.len() == 8); |
| ... | ... | @@ -509,135 +456,134 @@ test "PackedIntSlice of PackedInt(Array/Slice)" |
| 509 | 456 | testing.expect(packed_slice_edge.bytes.len == pse_expected_bytes); |
| 510 | 457 | testing.expect(packed_slice_edge.bit_offset == 0); |
| 511 | 458 | } |
| 512 | | |
| 513 | 459 | } |
| 514 | 460 | |
| 515 | | test "PackedIntSlice accumulating bit offsets" |
| 516 | | { |
| 461 | test "PackedIntSlice accumulating bit offsets" { |
| 517 | 462 | //bit_offset is u3, so standard debugging asserts should catch |
| 518 | 463 | // anything |
| 519 | 464 | { |
| 520 | 465 | const PackedArray = PackedIntArray(u3, 16); |
| 521 | 466 | var packed_array = PackedArray(undefined); |
| 522 | | |
| 467 | |
| 523 | 468 | var packed_slice = packed_array.slice(0, packed_array.len()); |
| 524 | 469 | var i = usize(0); |
| 525 | | while(i < packed_array.len() - 1):(i += 1) |
| 526 | | { |
| 527 | | |
| 470 | while (i < packed_array.len() - 1) : (i += 1) { |
| 528 | 471 | packed_slice = packed_slice.slice(1, packed_slice.len()); |
| 529 | 472 | } |
| 530 | 473 | } |
| 531 | 474 | { |
| 532 | 475 | const PackedArray = PackedIntArray(u11, 88); |
| 533 | 476 | var packed_array = PackedArray(undefined); |
| 534 | | |
| 477 | |
| 535 | 478 | var packed_slice = packed_array.slice(0, packed_array.len()); |
| 536 | 479 | var i = usize(0); |
| 537 | | while(i < packed_array.len() - 1):(i += 1) |
| 538 | | { |
| 480 | while (i < packed_array.len() - 1) : (i += 1) { |
| 539 | 481 | packed_slice = packed_slice.slice(1, packed_slice.len()); |
| 540 | 482 | } |
| 541 | 483 | } |
| 542 | | |
| 543 | 484 | } |
| 544 | 485 | |
| 545 | 486 | //@NOTE: As I do not have a big endian system to test this on, |
| 546 | 487 | // big endian values were not tested |
| 547 | | test "PackedInt(Array/Slice) sliceCast" |
| 548 | | { |
| 488 | test "PackedInt(Array/Slice) sliceCast" { |
| 549 | 489 | const PackedArray = PackedIntArray(u1, 16); |
| 550 | | var packed_array = PackedArray.init([]u1{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}); |
| 490 | var packed_array = PackedArray.init([]u1{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }); |
| 551 | 491 | const packed_slice_cast_2 = packed_array.sliceCast(u2); |
| 552 | 492 | const packed_slice_cast_4 = packed_slice_cast_2.sliceCast(u4); |
| 553 | 493 | var packed_slice_cast_9 = packed_array.slice(0, (packed_array.len() / 9) * 9).sliceCast(u9); |
| 554 | 494 | const packed_slice_cast_3 = packed_slice_cast_9.sliceCast(u3); |
| 555 | | |
| 495 | |
| 556 | 496 | var i = usize(0); |
| 557 | | while(i < packed_slice_cast_2.len()):(i += 1) |
| 558 | | { |
| 559 | | const val = switch(builtin.endian) |
| 560 | | { |
| 497 | while (i < packed_slice_cast_2.len()) : (i += 1) { |
| 498 | const val = switch (builtin.endian) { |
| 561 | 499 | .Big => 0b01, |
| 562 | 500 | .Little => 0b10, |
| 563 | 501 | }; |
| 564 | 502 | testing.expect(packed_slice_cast_2.get(i) == val); |
| 565 | 503 | } |
| 566 | 504 | i = 0; |
| 567 | | while(i < packed_slice_cast_4.len()):(i += 1) |
| 568 | | { |
| 569 | | const val = switch(builtin.endian) |
| 570 | | { |
| 505 | while (i < packed_slice_cast_4.len()) : (i += 1) { |
| 506 | const val = switch (builtin.endian) { |
| 571 | 507 | .Big => 0b0101, |
| 572 | 508 | .Little => 0b1010, |
| 573 | 509 | }; |
| 574 | 510 | testing.expect(packed_slice_cast_4.get(i) == val); |
| 575 | 511 | } |
| 576 | 512 | i = 0; |
| 577 | | while(i < packed_slice_cast_9.len()):(i += 1) |
| 578 | | { |
| 513 | while (i < packed_slice_cast_9.len()) : (i += 1) { |
| 579 | 514 | const val = 0b010101010; |
| 580 | 515 | testing.expect(packed_slice_cast_9.get(i) == val); |
| 581 | 516 | packed_slice_cast_9.set(i, 0b111000111); |
| 582 | 517 | } |
| 583 | 518 | i = 0; |
| 584 | | while(i < packed_slice_cast_3.len()):(i += 1) |
| 585 | | { |
| 586 | | const val = switch(builtin.endian) |
| 587 | | { |
| 588 | | .Big => if(i % 2 == 0) u3(0b111) else u3(0b000), |
| 589 | | .Little => if(i % 2 == 0) u3(0b111) else u3(0b000), |
| 519 | while (i < packed_slice_cast_3.len()) : (i += 1) { |
| 520 | const val = switch (builtin.endian) { |
| 521 | .Big => if (i % 2 == 0) u3(0b111) else u3(0b000), |
| 522 | .Little => if (i % 2 == 0) u3(0b111) else u3(0b000), |
| 590 | 523 | }; |
| 591 | 524 | testing.expect(packed_slice_cast_3.get(i) == val); |
| 592 | 525 | } |
| 593 | 526 | } |
| 594 | 527 | |
| 595 | | test "PackedInt(Array/Slice)Endian" |
| 596 | | { |
| 528 | test "PackedInt(Array/Slice)Endian" { |
| 597 | 529 | { |
| 598 | 530 | const PackedArrayBe = PackedIntArrayEndian(u4, .Big, 8); |
| 599 | | var packed_array_be = PackedArrayBe.init([]u4{0,1,2,3,4,5,6,7,}); |
| 531 | var packed_array_be = PackedArrayBe.init([]u4{ |
| 532 | 0, |
| 533 | 1, |
| 534 | 2, |
| 535 | 3, |
| 536 | 4, |
| 537 | 5, |
| 538 | 6, |
| 539 | 7, |
| 540 | }); |
| 600 | 541 | testing.expect(packed_array_be.bytes[0] == 0b00000001); |
| 601 | 542 | testing.expect(packed_array_be.bytes[1] == 0b00100011); |
| 602 | | |
| 543 | |
| 603 | 544 | var i = usize(0); |
| 604 | | while(i < packed_array_be.len()):(i += 1) |
| 605 | | { |
| 545 | while (i < packed_array_be.len()) : (i += 1) { |
| 606 | 546 | testing.expect(packed_array_be.get(i) == i); |
| 607 | 547 | } |
| 608 | | |
| 548 | |
| 609 | 549 | var packed_slice_le = packed_array_be.sliceCastEndian(u4, .Little); |
| 610 | 550 | i = 0; |
| 611 | | while(i < packed_slice_le.len()):(i += 1) |
| 612 | | { |
| 613 | | const val = if(i % 2 == 0) i + 1 else i - 1; |
| 551 | while (i < packed_slice_le.len()) : (i += 1) { |
| 552 | const val = if (i % 2 == 0) i + 1 else i - 1; |
| 614 | 553 | testing.expect(packed_slice_le.get(i) == val); |
| 615 | 554 | } |
| 616 | | |
| 555 | |
| 617 | 556 | var packed_slice_le_shift = packed_array_be.slice(1, 5).sliceCastEndian(u4, .Little); |
| 618 | 557 | i = 0; |
| 619 | | while(i < packed_slice_le_shift.len()):(i += 1) |
| 620 | | { |
| 621 | | const val = if(i % 2 == 0) i else i + 2; |
| 558 | while (i < packed_slice_le_shift.len()) : (i += 1) { |
| 559 | const val = if (i % 2 == 0) i else i + 2; |
| 622 | 560 | testing.expect(packed_slice_le_shift.get(i) == val); |
| 623 | 561 | } |
| 624 | 562 | } |
| 625 | | |
| 563 | |
| 626 | 564 | { |
| 627 | 565 | const PackedArrayBe = PackedIntArrayEndian(u11, .Big, 8); |
| 628 | | var packed_array_be = PackedArrayBe.init([]u11{0,1,2,3,4,5,6,7,}); |
| 566 | var packed_array_be = PackedArrayBe.init([]u11{ |
| 567 | 0, |
| 568 | 1, |
| 569 | 2, |
| 570 | 3, |
| 571 | 4, |
| 572 | 5, |
| 573 | 6, |
| 574 | 7, |
| 575 | }); |
| 629 | 576 | testing.expect(packed_array_be.bytes[0] == 0b00000000); |
| 630 | 577 | testing.expect(packed_array_be.bytes[1] == 0b00000000); |
| 631 | 578 | testing.expect(packed_array_be.bytes[2] == 0b00000100); |
| 632 | 579 | testing.expect(packed_array_be.bytes[3] == 0b00000001); |
| 633 | 580 | testing.expect(packed_array_be.bytes[4] == 0b00000000); |
| 634 | | |
| 581 | |
| 635 | 582 | var i = usize(0); |
| 636 | | while(i < packed_array_be.len()):(i += 1) |
| 637 | | { |
| 583 | while (i < packed_array_be.len()) : (i += 1) { |
| 638 | 584 | testing.expect(packed_array_be.get(i) == i); |
| 639 | 585 | } |
| 640 | | |
| 586 | |
| 641 | 587 | var packed_slice_le = packed_array_be.sliceCastEndian(u11, .Little); |
| 642 | 588 | testing.expect(packed_slice_le.get(0) == 0b00000000000); |
| 643 | 589 | testing.expect(packed_slice_le.get(1) == 0b00010000000); |
| ... | ... | @@ -647,8 +593,7 @@ test "PackedInt(Array/Slice)Endian" |
| 647 | 593 | testing.expect(packed_slice_le.get(5) == 0b00000000010); |
| 648 | 594 | testing.expect(packed_slice_le.get(6) == 0b10000010000); |
| 649 | 595 | testing.expect(packed_slice_le.get(7) == 0b00000111001); |
| 650 | | |
| 651 | | |
| 596 | |
| 652 | 597 | var packed_slice_le_shift = packed_array_be.slice(1, 5).sliceCastEndian(u11, .Little); |
| 653 | 598 | testing.expect(packed_slice_le_shift.get(0) == 0b00010000000); |
| 654 | 599 | testing.expect(packed_slice_le_shift.get(1) == 0b00000000100); |
| ... | ... | @@ -666,44 +611,39 @@ test "PackedInt(Array/Slice)Endian" |
| 666 | 611 | // and reading the last element. The assumption is that the page |
| 667 | 612 | // after this one is not mapped and will cause a segfault if we |
| 668 | 613 | // don't account for the bounds. |
| 669 | | test "PackedIntArray at end of available memory" |
| 670 | | { |
| 671 | | switch(builtin.os) |
| 672 | | { |
| 614 | test "PackedIntArray at end of available memory" { |
| 615 | switch (builtin.os) { |
| 673 | 616 | .linux, .macosx, .ios, .freebsd, .netbsd => {}, |
| 674 | 617 | else => return, |
| 675 | 618 | } |
| 676 | 619 | const PackedArray = PackedIntArray(u3, 8); |
| 677 | | |
| 678 | | const Padded = struct |
| 679 | | { |
| 620 | |
| 621 | const Padded = struct { |
| 680 | 622 | _: [std.os.page_size - @sizeOf(PackedArray)]u8, |
| 681 | 623 | p: PackedArray, |
| 682 | 624 | }; |
| 683 | | |
| 625 | |
| 684 | 626 | var da = std.heap.DirectAllocator.init(); |
| 685 | 627 | const allocator = &da.allocator; |
| 686 | | |
| 628 | |
| 687 | 629 | var pad = try allocator.create(Padded); |
| 688 | 630 | defer allocator.destroy(pad); |
| 689 | 631 | pad.p.set(7, std.math.maxInt(u3)); |
| 690 | 632 | } |
| 691 | 633 | |
| 692 | | test "PackedIntSlice at end of available memory" |
| 693 | | { |
| 694 | | switch(builtin.os) |
| 695 | | { |
| 634 | test "PackedIntSlice at end of available memory" { |
| 635 | switch (builtin.os) { |
| 696 | 636 | .linux, .macosx, .ios, .freebsd, .netbsd => {}, |
| 697 | 637 | else => return, |
| 698 | 638 | } |
| 699 | 639 | const PackedSlice = PackedIntSlice(u11); |
| 700 | | |
| 640 | |
| 701 | 641 | var da = std.heap.DirectAllocator.init(); |
| 702 | 642 | const allocator = &da.allocator; |
| 703 | | |
| 643 | |
| 704 | 644 | var page = try allocator.alloc(u8, std.os.page_size); |
| 705 | 645 | defer allocator.free(page); |
| 706 | | |
| 707 | | var p = PackedSlice.init(page[std.os.page_size - 2..], 1); |
| 646 | |
| 647 | var p = PackedSlice.init(page[std.os.page_size - 2 ..], 1); |
| 708 | 648 | p.set(0, std.math.maxInt(u11)); |
| 709 | | } |
| | \ No newline at end of file |
| 649 | } |