From f758b97bfd5e28e4ff1c8d9eca34de9b22990f85 Mon Sep 17 00:00:00 2001 From: Fri3dNstuff <102751849+Fri3dNstuff@users.noreply.github.com> Date: Sat, 16 Aug 2025 00:45:33 +0300 Subject: [PATCH] std.math: Add splat for vectors of u0s in rotl/rotr (#24822) --- lib/std/math.zig | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/std/math.zig b/lib/std/math.zig index c36f19ec85..9fd97df855 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -693,7 +693,7 @@ test shr { pub fn rotr(comptime T: type, x: T, r: anytype) T { if (@typeInfo(T) == .vector) { const C = @typeInfo(T).vector.child; - if (C == u0) return 0; + if (C == u0) return @splat(0); if (@typeInfo(C).int.signedness == .signed) { @compileError("cannot rotate signed integers"); @@ -725,8 +725,10 @@ test rotr { try testing.expect(rotr(u8, 0b00000001, @as(usize, 4)) == 0b00010000); try testing.expect(rotr(u8, 0b00000001, @as(isize, -1)) == 0b00000010); try testing.expect(rotr(u12, 0o7777, 1) == 0o7777); - try testing.expect(rotr(@Vector(1, u32), @Vector(1, u32){1}, @as(usize, 1))[0] == @as(u32, 1) << 31); - try testing.expect(rotr(@Vector(1, u32), @Vector(1, u32){1}, @as(isize, -1))[0] == @as(u32, 1) << 1); + try testing.expect(rotr(@Vector(1, u32), .{1}, @as(usize, 1))[0] == @as(u32, 1) << 31); + try testing.expect(rotr(@Vector(1, u32), .{1}, @as(isize, -1))[0] == @as(u32, 1) << 1); + try std.testing.expect(@reduce(.And, rotr(@Vector(2, u0), .{ 0, 0 }, @as(usize, 42)) == + @Vector(2, u0){ 0, 0 })); } /// Rotates left. Only unsigned values can be rotated. Negative shift @@ -734,7 +736,7 @@ test rotr { pub fn rotl(comptime T: type, x: T, r: anytype) T { if (@typeInfo(T) == .vector) { const C = @typeInfo(T).vector.child; - if (C == u0) return 0; + if (C == u0) return @splat(0); if (@typeInfo(C).int.signedness == .signed) { @compileError("cannot rotate signed integers"); @@ -766,8 +768,10 @@ test rotl { try testing.expect(rotl(u8, 0b00000001, @as(usize, 4)) == 0b00010000); try testing.expect(rotl(u8, 0b00000001, @as(isize, -1)) == 0b10000000); try testing.expect(rotl(u12, 0o7777, 1) == 0o7777); - try testing.expect(rotl(@Vector(1, u32), @Vector(1, u32){1 << 31}, @as(usize, 1))[0] == 1); - try testing.expect(rotl(@Vector(1, u32), @Vector(1, u32){1 << 31}, @as(isize, -1))[0] == @as(u32, 1) << 30); + try testing.expect(rotl(@Vector(1, u32), .{1 << 31}, @as(usize, 1))[0] == 1); + try testing.expect(rotl(@Vector(1, u32), .{1 << 31}, @as(isize, -1))[0] == @as(u32, 1) << 30); + try std.testing.expect(@reduce(.And, rotl(@Vector(2, u0), .{ 0, 0 }, @as(usize, 42)) == + @Vector(2, u0){ 0, 0 })); } /// Returns an unsigned int type that can hold the number of bits in T - 1.