aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math.zig
diff options
context:
space:
mode:
authorFri3dNstuff <102751849+Fri3dNstuff@users.noreply.github.com>2025-08-16 00:45:33 +0300
committerGitHub <noreply@github.com>2025-08-15 23:45:33 +0200
commitf758b97bfd5e28e4ff1c8d9eca34de9b22990f85 (patch)
tree08a72700ec77de33452b1c8e10be7b2297e7d458 /lib/std/math.zig
parent39aca6f37e83e263236339f9e67bace2a098cd16 (diff)
downloadzig-f758b97bfd5e28e4ff1c8d9eca34de9b22990f85.tar.gz
zig-f758b97bfd5e28e4ff1c8d9eca34de9b22990f85.zip
std.math: Add splat for vectors of u0s in rotl/rotr (#24822)
Diffstat (limited to 'lib/std/math.zig')
-rw-r--r--lib/std/math.zig16
1 files 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.