authorgravatar for rekai@musuka.devpaoda <rekai@musuka.dev> 2024-03-09 04:46:43-06:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-03-15 16:19:48+02:00
loga4508ad7167513d178fd237e4cd361185800d05d
treef12a04a17516bf6812cfd6afed82ae7a53dd603b
parentd565c5dfcdcd6753ed0d00a7f6cccdc87d2b99a2

fix(math): eval isPowerOfTwo at comptime in rotl/rotr


1 files changed, 8 insertions(+), 6 deletions(-)

lib/std/math.zig+8-6
...@@ -682,15 +682,15 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {...@@ -682,15 +682,15 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {
682 if (@typeInfo(C).Int.signedness == .signed) {682 if (@typeInfo(C).Int.signedness == .signed) {
683 @compileError("cannot rotate signed integers");683 @compileError("cannot rotate signed integers");
684 }684 }
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));
686 return (x >> @splat(ar)) | (x << @splat(1 + ~ar));686 return (x >> @splat(ar)) | (x << @splat(1 + ~ar));
687 } else if (@typeInfo(T).Int.signedness == .signed) {687 } else if (@typeInfo(T).Int.signedness == .signed) {
688 @compileError("cannot rotate signed integer");688 @compileError("cannot rotate signed integer");
689 } else {689 } else {
690 if (T == u0) return 0;690 if (T == u0) return 0;
691691
692 if (isPowerOfTwo(@typeInfo(T).Int.bits)) {692 if (comptime isPowerOfTwo(@typeInfo(T).Int.bits)) {
693 const ar = @as(Log2Int(T), @intCast(@mod(r, @typeInfo(T).Int.bits)));693 const ar: Log2Int(T) = @intCast(@mod(r, @typeInfo(T).Int.bits));
694 return x >> ar | x << (1 +% ~ar);694 return x >> ar | x << (1 +% ~ar);
695 } else {695 } else {
696 const ar = @mod(r, @typeInfo(T).Int.bits);696 const ar = @mod(r, @typeInfo(T).Int.bits);
...@@ -713,6 +713,7 @@ test "rotr" {...@@ -713,6 +713,7 @@ test "rotr" {
713 try testing.expect(rotr(u8, 0b00000001, @as(usize, 8)) == 0b00000001);713 try testing.expect(rotr(u8, 0b00000001, @as(usize, 8)) == 0b00000001);
714 try testing.expect(rotr(u8, 0b00000001, @as(usize, 4)) == 0b00010000);714 try testing.expect(rotr(u8, 0b00000001, @as(usize, 4)) == 0b00010000);
715 try testing.expect(rotr(u8, 0b00000001, @as(isize, -1)) == 0b00000010);715 try testing.expect(rotr(u8, 0b00000001, @as(isize, -1)) == 0b00000010);
716 try testing.expect(rotr(u12, 0o7777, 1) == 0o7777);
716 try testing.expect(rotr(@Vector(1, u32), @Vector(1, u32){1}, @as(usize, 1))[0] == @as(u32, 1) << 31);717 try testing.expect(rotr(@Vector(1, u32), @Vector(1, u32){1}, @as(usize, 1))[0] == @as(u32, 1) << 31);
717 try testing.expect(rotr(@Vector(1, u32), @Vector(1, u32){1}, @as(isize, -1))[0] == @as(u32, 1) << 1);718 try testing.expect(rotr(@Vector(1, u32), @Vector(1, u32){1}, @as(isize, -1))[0] == @as(u32, 1) << 1);
718}719}
...@@ -727,15 +728,15 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {...@@ -727,15 +728,15 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {
727 if (@typeInfo(C).Int.signedness == .signed) {728 if (@typeInfo(C).Int.signedness == .signed) {
728 @compileError("cannot rotate signed integers");729 @compileError("cannot rotate signed integers");
729 }730 }
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));
731 return (x << @splat(ar)) | (x >> @splat(1 +% ~ar));732 return (x << @splat(ar)) | (x >> @splat(1 +% ~ar));
732 } else if (@typeInfo(T).Int.signedness == .signed) {733 } else if (@typeInfo(T).Int.signedness == .signed) {
733 @compileError("cannot rotate signed integer");734 @compileError("cannot rotate signed integer");
734 } else {735 } else {
735 if (T == u0) return 0;736 if (T == u0) return 0;
736737
737 if (isPowerOfTwo(@typeInfo(T).Int.bits)) {738 if (comptime isPowerOfTwo(@typeInfo(T).Int.bits)) {
738 const ar = @as(Log2Int(T), @intCast(@mod(r, @typeInfo(T).Int.bits)));739 const ar: Log2Int(T) = @intCast(@mod(r, @typeInfo(T).Int.bits));
739 return x << ar | x >> 1 +% ~ar;740 return x << ar | x >> 1 +% ~ar;
740 } else {741 } else {
741 const ar = @mod(r, @typeInfo(T).Int.bits);742 const ar = @mod(r, @typeInfo(T).Int.bits);
...@@ -758,6 +759,7 @@ test "rotl" {...@@ -758,6 +759,7 @@ test "rotl" {
758 try testing.expect(rotl(u8, 0b00000001, @as(usize, 8)) == 0b00000001);759 try testing.expect(rotl(u8, 0b00000001, @as(usize, 8)) == 0b00000001);
759 try testing.expect(rotl(u8, 0b00000001, @as(usize, 4)) == 0b00010000);760 try testing.expect(rotl(u8, 0b00000001, @as(usize, 4)) == 0b00010000);
760 try testing.expect(rotl(u8, 0b00000001, @as(isize, -1)) == 0b10000000);761 try testing.expect(rotl(u8, 0b00000001, @as(isize, -1)) == 0b10000000);
762 try testing.expect(rotl(u12, 0o7777, 1) == 0o7777);
761 try testing.expect(rotl(@Vector(1, u32), @Vector(1, u32){1 << 31}, @as(usize, 1))[0] == 1);763 try testing.expect(rotl(@Vector(1, u32), @Vector(1, u32){1 << 31}, @as(usize, 1))[0] == 1);
762 try testing.expect(rotl(@Vector(1, u32), @Vector(1, u32){1 << 31}, @as(isize, -1))[0] == @as(u32, 1) << 30);764 try testing.expect(rotl(@Vector(1, u32), @Vector(1, u32){1 << 31}, @as(isize, -1))[0] == @as(u32, 1) << 30);
763}765}