| ... | ... | @@ -1,10 +1,12 @@ |
| 1 | 1 | const Configuration = @This(); |
| 2 | 2 | |
| 3 | 3 | const std = @import("../std.zig"); |
| 4 | const builtin = @import("builtin"); |
| 4 | 5 | const Io = std.Io; |
| 5 | 6 | const Allocator = std.mem.Allocator; |
| 6 | 7 | const assert = std.debug.assert; |
| 7 | 8 | const max_u32 = std.math.maxInt(u32); |
| 9 | const native_endian = builtin.target.cpu.arch.endian(); |
| 8 | 10 | |
| 9 | 11 | string_bytes: []u8, |
| 10 | 12 | steps: []Step, |
| ... | ... | @@ -3434,18 +3436,46 @@ pub fn load(arena: Allocator, reader: *Io.Reader) LoadError!Configuration { |
| 3434 | 3436 | return result; |
| 3435 | 3437 | } |
| 3436 | 3438 | |
| 3439 | /// Loads bits using native endianness when `value` spans multiple bytes. |
| 3440 | /// On big endian architectures, `bit_offset` uses MSb 0 bit numbering. |
| 3441 | /// On little endian architectures, `bit_offset` uses LSb 0 bit numbering. |
| 3442 | /// See `storeBits`. |
| 3437 | 3443 | pub fn loadBits(comptime Int: type, buffer: []const Int, bit_offset: usize, comptime Result: type) Result { |
| 3438 | 3444 | const index = bit_offset / @bitSizeOf(Int); |
| 3439 | 3445 | const small_bit_offset = bit_offset % @bitSizeOf(Int); |
| 3440 | 3446 | const ResultInt = @Int(.unsigned, @bitSizeOf(Result)); |
| 3441 | | const result: ResultInt = @truncate(buffer[index] >> @intCast(small_bit_offset)); |
| 3442 | | const available_bits = @bitSizeOf(Int) - small_bit_offset; |
| 3443 | | if (available_bits >= @bitSizeOf(ResultInt)) return @bitCast(result); |
| 3444 | | const missing_bits = @bitSizeOf(ResultInt) - available_bits; |
| 3445 | | const upper: ResultInt = @truncate(buffer[index + 1] & ((@as(usize, 1) << @intCast(missing_bits)) - 1)); |
| 3446 | | return @bitCast(result | (upper << @intCast(available_bits))); |
| 3447 | switch (native_endian) { |
| 3448 | .little => { |
| 3449 | const result: ResultInt = @truncate(buffer[index] >> @intCast(small_bit_offset)); |
| 3450 | const available_bits = @bitSizeOf(Int) - small_bit_offset; |
| 3451 | if (available_bits >= @bitSizeOf(ResultInt)) return @bitCast(result); |
| 3452 | const missing_bits = @bitSizeOf(ResultInt) - available_bits; |
| 3453 | const upper: ResultInt = @truncate(buffer[index + 1] & ((@as(usize, 1) << @intCast(missing_bits)) - 1)); |
| 3454 | return @bitCast(result | (upper << @intCast(available_bits))); |
| 3455 | }, |
| 3456 | .big => { |
| 3457 | const available_bits = @bitSizeOf(Int) - small_bit_offset; |
| 3458 | if (available_bits >= @bitSizeOf(ResultInt)) { |
| 3459 | const shift = available_bits - @bitSizeOf(ResultInt); |
| 3460 | const result: ResultInt = @truncate(buffer[index] >> @intCast(shift)); |
| 3461 | return @bitCast(result); |
| 3462 | } |
| 3463 | const mask = (@as(Int, 1) << @intCast(available_bits)) - 1; |
| 3464 | const result: ResultInt = @intCast(buffer[index] & mask); |
| 3465 | const missing_bits = @bitSizeOf(ResultInt) - available_bits; |
| 3466 | const lower: ResultInt = @truncate(buffer[index + 1] >> @intCast(@bitSizeOf(Int) - missing_bits)); |
| 3467 | return @bitCast((result << @intCast(missing_bits)) | lower); |
| 3468 | }, |
| 3469 | } |
| 3447 | 3470 | } |
| 3448 | 3471 | |
| 3472 | /// Store bits using native endianness when `value` spans multiple bytes. |
| 3473 | /// On big endian architectures: |
| 3474 | /// - For a given value, the bits of an earlier byte are more significant than the bits of subsequent bytes. |
| 3475 | /// - `bit_offset` uses MSb 0 bit numbering. |
| 3476 | /// On little endian architectures: |
| 3477 | /// - For a given value, the bits of an earlier byte are less significant than the bits of subsequent bytes. |
| 3478 | /// - `bit_offset` uses LSb 0 bit numbering. |
| 3449 | 3479 | pub fn storeBits(comptime Int: type, buffer: []Int, bit_offset: usize, value: anytype) void { |
| 3450 | 3480 | const Value = @TypeOf(value); |
| 3451 | 3481 | const ValueInt = @Int(.unsigned, @bitSizeOf(Value)); |
| ... | ... | @@ -3454,27 +3484,70 @@ pub fn storeBits(comptime Int: type, buffer: []Int, bit_offset: usize, value: an |
| 3454 | 3484 | const small_bit_offset = bit_offset % @bitSizeOf(Int); |
| 3455 | 3485 | const available_bits = @bitSizeOf(Int) - small_bit_offset; |
| 3456 | 3486 | if (available_bits >= @bitSizeOf(ValueInt)) { |
| 3457 | | buffer[index] &= ~(((@as(Int, 1) << @intCast(@bitSizeOf(Value))) - 1) << @intCast(small_bit_offset)); |
| 3458 | | buffer[index] |= @as(Int, value_int) << @intCast(small_bit_offset); |
| 3487 | const shift = switch (native_endian) { |
| 3488 | .little => small_bit_offset, |
| 3489 | .big => available_bits - @bitSizeOf(ValueInt), |
| 3490 | }; |
| 3491 | buffer[index] &= ~(((@as(Int, 1) << @intCast(@bitSizeOf(Value))) - 1) << @intCast(shift)); |
| 3492 | buffer[index] |= @as(Int, value_int) << @intCast(shift); |
| 3459 | 3493 | } else { |
| 3460 | 3494 | const DoubleInt = @Int(.unsigned, @bitSizeOf(Int) * 2); |
| 3495 | const shift = switch (native_endian) { |
| 3496 | .little => small_bit_offset, |
| 3497 | .big => @bitSizeOf(DoubleInt) - small_bit_offset - @bitSizeOf(ValueInt), |
| 3498 | }; |
| 3461 | 3499 | const ptr: *align(@alignOf(Int)) DoubleInt = @ptrCast(buffer[index..][0..2]); |
| 3462 | | ptr.* &= ~(((@as(DoubleInt, 1) << @intCast(@bitSizeOf(Value))) - 1) << @intCast(small_bit_offset)); |
| 3463 | | ptr.* |= @as(DoubleInt, value_int) << @intCast(small_bit_offset); |
| 3500 | ptr.* &= ~(((@as(DoubleInt, 1) << @intCast(@bitSizeOf(Value))) - 1) << @intCast(shift)); |
| 3501 | ptr.* |= @as(DoubleInt, value_int) << @intCast(shift); |
| 3464 | 3502 | } |
| 3465 | 3503 | } |
| 3466 | 3504 | |
| 3467 | 3505 | test "loadBits and storeBits" { |
| 3468 | | var buffer: [2]u32 = .{ |
| 3469 | | 0b01111111000000001111111100000000, |
| 3470 | | 0b11111111000000001111111100000100, |
| 3506 | var buffer: [2]u32 = switch (native_endian) { |
| 3507 | .little => .{ |
| 3508 | //──┐ 0b100011 (end) ┌─┐ 0b100 |
| 3509 | 0b01111111000000001111111100000000, |
| 3510 | // n <── bit offset 0 ┘ |
| 3511 | // ┌── 0b100011 (start) |
| 3512 | 0b11111111000000001111111100000100, |
| 3513 | }, |
| 3514 | .big => .{ |
| 3515 | // ┌─┐ 0b100 ┌── 0b100011 (start) |
| 3516 | 0b11111110000000001111111100000100, |
| 3517 | //└ bit offset 0 ──> n |
| 3518 | //──┐ 0b100011 (end) |
| 3519 | 0b01111111000000001111111100000000, |
| 3520 | }, |
| 3471 | 3521 | }; |
| 3522 | |
| 3472 | 3523 | try std.testing.expectEqual(0b100, loadBits(u32, &buffer, 6, u3)); |
| 3473 | 3524 | try std.testing.expectEqual(0b100011, loadBits(u32, &buffer, 29, u6)); |
| 3474 | 3525 | |
| 3526 | storeBits(u32, &buffer, 0, @as(u1, 0b0)); |
| 3475 | 3527 | storeBits(u32, &buffer, 6, @as(u3, 0b010)); |
| 3476 | | storeBits(u32, &buffer, 29, @as(u6, 0b010010)); |
| 3528 | storeBits(u32, &buffer, 29, @as(u6, 0b010110)); |
| 3529 | storeBits(u32, &buffer, 40, @as(u17, 0b01110110011111110)); |
| 3477 | 3530 | |
| 3531 | try std.testing.expectEqual(0b0, loadBits(u32, &buffer, 0, u1)); |
| 3478 | 3532 | try std.testing.expectEqual(0b010, loadBits(u32, &buffer, 6, u3)); |
| 3479 | | try std.testing.expectEqual(0b010010, loadBits(u32, &buffer, 29, u6)); |
| 3533 | try std.testing.expectEqual(0b010110, loadBits(u32, &buffer, 29, u6)); |
| 3534 | try std.testing.expectEqual(0b01110110011111110, loadBits(u32, &buffer, 40, u17)); |
| 3535 | |
| 3536 | // Test roundtripping of size/offset combinations |
| 3537 | inline for (1..32) |value_size| { |
| 3538 | for (0..64) |bit_offset| { |
| 3539 | if (value_size + bit_offset > @bitSizeOf(@TypeOf(buffer))) continue; |
| 3540 | |
| 3541 | buffer = .{ 0, 0 }; |
| 3542 | |
| 3543 | const Value = @Int(.unsigned, value_size); |
| 3544 | const value: Value = @intCast((@as(u32, 1) << @intCast(@bitSizeOf(Value))) - 1); |
| 3545 | storeBits(u32, &buffer, bit_offset, value); |
| 3546 | std.testing.expectEqual(value, loadBits(u32, &buffer, bit_offset, Value)) catch |err| { |
| 3547 | std.debug.print("value size: {} bit offset: {}\n", .{ value_size, bit_offset }); |
| 3548 | std.debug.print("buffer: {b:0>32} {b:0>32}\n", .{ buffer[0], buffer[1] }); |
| 3549 | return err; |
| 3550 | }; |
| 3551 | } |
| 3552 | } |
| 3480 | 3553 | } |