Skip to content

Commit a4508ad

Browse files
paodaVexu
authored andcommitted
fix(math): eval isPowerOfTwo at comptime in rotl/rotr
1 parent d565c5d commit a4508ad

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/std/math.zig

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -682,15 +682,15 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {
682682
if (@typeInfo(C).Int.signedness == .signed) {
683683
@compileError("cannot rotate signed integers");
684684
}
685-
const ar = @as(Log2Int(C), @intCast(@mod(r, @typeInfo(C).Int.bits)));
685+
const ar: Log2Int(C) = @intCast(@mod(r, @typeInfo(C).Int.bits));
686686
return (x >> @splat(ar)) | (x << @splat(1 + ~ar));
687687
} else if (@typeInfo(T).Int.signedness == .signed) {
688688
@compileError("cannot rotate signed integer");
689689
} else {
690690
if (T == u0) return 0;
691691

692-
if (isPowerOfTwo(@typeInfo(T).Int.bits)) {
693-
const ar = @as(Log2Int(T), @intCast(@mod(r, @typeInfo(T).Int.bits)));
692+
if (comptime isPowerOfTwo(@typeInfo(T).Int.bits)) {
693+
const ar: Log2Int(T) = @intCast(@mod(r, @typeInfo(T).Int.bits));
694694
return x >> ar | x << (1 +% ~ar);
695695
} else {
696696
const ar = @mod(r, @typeInfo(T).Int.bits);
@@ -713,6 +713,7 @@ test "rotr" {
713713
try testing.expect(rotr(u8, 0b00000001, @as(usize, 8)) == 0b00000001);
714714
try testing.expect(rotr(u8, 0b00000001, @as(usize, 4)) == 0b00010000);
715715
try testing.expect(rotr(u8, 0b00000001, @as(isize, -1)) == 0b00000010);
716+
try testing.expect(rotr(u12, 0o7777, 1) == 0o7777);
716717
try testing.expect(rotr(@Vector(1, u32), @Vector(1, u32){1}, @as(usize, 1))[0] == @as(u32, 1) << 31);
717718
try testing.expect(rotr(@Vector(1, u32), @Vector(1, u32){1}, @as(isize, -1))[0] == @as(u32, 1) << 1);
718719
}
@@ -727,15 +728,15 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {
727728
if (@typeInfo(C).Int.signedness == .signed) {
728729
@compileError("cannot rotate signed integers");
729730
}
730-
const ar = @as(Log2Int(C), @intCast(@mod(r, @typeInfo(C).Int.bits)));
731+
const ar: Log2Int(C) = @intCast(@mod(r, @typeInfo(C).Int.bits));
731732
return (x << @splat(ar)) | (x >> @splat(1 +% ~ar));
732733
} else if (@typeInfo(T).Int.signedness == .signed) {
733734
@compileError("cannot rotate signed integer");
734735
} else {
735736
if (T == u0) return 0;
736737

737-
if (isPowerOfTwo(@typeInfo(T).Int.bits)) {
738-
const ar = @as(Log2Int(T), @intCast(@mod(r, @typeInfo(T).Int.bits)));
738+
if (comptime isPowerOfTwo(@typeInfo(T).Int.bits)) {
739+
const ar: Log2Int(T) = @intCast(@mod(r, @typeInfo(T).Int.bits));
739740
return x << ar | x >> 1 +% ~ar;
740741
} else {
741742
const ar = @mod(r, @typeInfo(T).Int.bits);
@@ -758,6 +759,7 @@ test "rotl" {
758759
try testing.expect(rotl(u8, 0b00000001, @as(usize, 8)) == 0b00000001);
759760
try testing.expect(rotl(u8, 0b00000001, @as(usize, 4)) == 0b00010000);
760761
try testing.expect(rotl(u8, 0b00000001, @as(isize, -1)) == 0b10000000);
762+
try testing.expect(rotl(u12, 0o7777, 1) == 0o7777);
761763
try testing.expect(rotl(@Vector(1, u32), @Vector(1, u32){1 << 31}, @as(usize, 1))[0] == 1);
762764
try testing.expect(rotl(@Vector(1, u32), @Vector(1, u32){1 << 31}, @as(isize, -1))[0] == @as(u32, 1) << 30);
763765
}

0 commit comments

Comments
 (0)