authorgravatar for protoss2017@mail.ruCortex <protoss2017@mail.ru> 2023-05-29 13:01:54+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-05-29 13:01:54+03:00
log6e6a61a3847092be8a754f70f19ad3c779648ba3
tree9cd9c6578d0fd0ea5eb6e37be8538ea7be920e8f
parent235b776d619d64dee62fc88e85bd53421cce37f7
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.io.Writer: add support for non-power-of-two int sizes


2 files changed, 22 insertions(+), 10 deletions(-)

lib/std/io/writer.zig+5-10
......@@ -46,39 +46,34 @@ pub fn Writer(
4646 }
4747
4848 /// Write a native-endian integer.
49 /// TODO audit non-power-of-two int sizes
5049 pub fn writeIntNative(self: Self, comptime T: type, value: T) Error!void {
5150 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
52 mem.writeIntNative(T, &bytes, value);
51 mem.writeIntNative(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
5352 return self.writeAll(&bytes);
5453 }
5554
5655 /// Write a foreign-endian integer.
57 /// TODO audit non-power-of-two int sizes
5856 pub fn writeIntForeign(self: Self, comptime T: type, value: T) Error!void {
5957 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
60 mem.writeIntForeign(T, &bytes, value);
58 mem.writeIntForeign(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
6159 return self.writeAll(&bytes);
6260 }
6361
64 /// TODO audit non-power-of-two int sizes
6562 pub fn writeIntLittle(self: Self, comptime T: type, value: T) Error!void {
6663 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
67 mem.writeIntLittle(T, &bytes, value);
64 mem.writeIntLittle(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
6865 return self.writeAll(&bytes);
6966 }
7067
71 /// TODO audit non-power-of-two int sizes
7268 pub fn writeIntBig(self: Self, comptime T: type, value: T) Error!void {
7369 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
74 mem.writeIntBig(T, &bytes, value);
70 mem.writeIntBig(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
7571 return self.writeAll(&bytes);
7672 }
7773
78 /// TODO audit non-power-of-two int sizes
7974 pub fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) Error!void {
8075 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
81 mem.writeInt(T, &bytes, value, endian);
76 mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian);
8277 return self.writeAll(&bytes);
8378 }
8479
lib/std/math.zig+17
......@@ -1122,6 +1122,23 @@ pub fn isPowerOfTwo(v: anytype) bool {
11221122 return (v & (v - 1)) == 0;
11231123}
11241124
1125/// Aligns the given integer type bit width to a width divisible by 8.
1126pub fn ByteAlignedInt(comptime T: type) type {
1127 const info = @typeInfo(T).Int;
1128 const bits = (info.bits + 7) / 8 * 8;
1129 const extended_type = std.meta.Int(info.signedness, bits);
1130 return extended_type;
1131}
1132
1133test "ByteAlignedInt" {
1134 try testing.expect(ByteAlignedInt(u0) == u0);
1135 try testing.expect(ByteAlignedInt(i0) == i0);
1136 try testing.expect(ByteAlignedInt(u3) == u8);
1137 try testing.expect(ByteAlignedInt(u8) == u8);
1138 try testing.expect(ByteAlignedInt(i111) == i112);
1139 try testing.expect(ByteAlignedInt(u129) == u136);
1140}
1141
11251142/// Rounds the given floating point number to an integer, away from zero.
11261143/// Uses a dedicated hardware instruction when available.
11271144/// This is the same as calling the builtin @round