From 02a43f325bfbccb73fb773f15b162d531c3c3ada Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 14 Apr 2022 07:42:49 -0300 Subject: [PATCH] std/math.zig: resolve missed optimization in rotates --- lib/std/math.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/math.zig b/lib/std/math.zig index be49ba8030..387faf4c97 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -548,8 +548,8 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T { } else if (@typeInfo(T).Int.signedness == .signed) { @compileError("cannot rotate signed integer"); } else { - const ar = @mod(r, @typeInfo(T).Int.bits); - return shr(T, x, ar) | shl(T, x, @typeInfo(T).Int.bits - ar); + const ar = @intCast(Log2Int(T), @mod(r, @typeInfo(T).Int.bits)); + return x >> ar | x << (1 +% ~ar); } } @@ -576,8 +576,8 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T { } else if (@typeInfo(T).Int.signedness == .signed) { @compileError("cannot rotate signed integer"); } else { - const ar = @mod(r, @typeInfo(T).Int.bits); - return shl(T, x, ar) | shr(T, x, @typeInfo(T).Int.bits - ar); + const ar = @intCast(Log2Int(T), @mod(r, @typeInfo(T).Int.bits)); + return x << ar | x >> 1 +% ~ar; } }