diff options
| author | Cortex <protoss2017@mail.ru> | 2023-05-29 13:01:54 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-29 13:01:54 +0300 |
| commit | 6e6a61a3847092be8a754f70f19ad3c779648ba3 (patch) | |
| tree | 9cd9c6578d0fd0ea5eb6e37be8538ea7be920e8f /lib/std/math.zig | |
| parent | 235b776d619d64dee62fc88e85bd53421cce37f7 (diff) | |
| download | zig-6e6a61a3847092be8a754f70f19ad3c779648ba3.tar.gz zig-6e6a61a3847092be8a754f70f19ad3c779648ba3.zip | |
std.io.Writer: add support for non-power-of-two int sizes
Diffstat (limited to 'lib/std/math.zig')
| -rw-r--r-- | lib/std/math.zig | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/std/math.zig b/lib/std/math.zig index 4221118ba5..8bd7c364f9 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -1122,6 +1122,23 @@ pub fn isPowerOfTwo(v: anytype) bool { return (v & (v - 1)) == 0; } +/// Aligns the given integer type bit width to a width divisible by 8. +pub fn ByteAlignedInt(comptime T: type) type { + const info = @typeInfo(T).Int; + const bits = (info.bits + 7) / 8 * 8; + const extended_type = std.meta.Int(info.signedness, bits); + return extended_type; +} + +test "ByteAlignedInt" { + try testing.expect(ByteAlignedInt(u0) == u0); + try testing.expect(ByteAlignedInt(i0) == i0); + try testing.expect(ByteAlignedInt(u3) == u8); + try testing.expect(ByteAlignedInt(u8) == u8); + try testing.expect(ByteAlignedInt(i111) == i112); + try testing.expect(ByteAlignedInt(u129) == u136); +} + /// Rounds the given floating point number to an integer, away from zero. /// Uses a dedicated hardware instruction when available. /// This is the same as calling the builtin @round |
