From 11a034658a1a837ffcb3648ef96651ae14f71d46 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 8 Apr 2022 02:13:41 -0300 Subject: [PATCH] chore: change implementation of rotr --- src/util.zig | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/util.zig b/src/util.zig index a68e78a..f116c39 100644 --- a/src/util.zig +++ b/src/util.zig @@ -9,11 +9,12 @@ pub inline fn sext(comptime bits: comptime_int, value: u32) u32 { } /// See https://godbolt.org/z/W3en9Eche -pub inline fn rotr(comptime T: type, value: T, r: anytype) T { - comptime std.debug.assert(@typeInfo(T).Int.signedness == .unsigned); - const ar = @truncate(Log2Int(T), r); +pub inline fn rotr(comptime T: type, x: T, r: anytype) T { + if (@typeInfo(T).Int.signedness == .signed) + @compileError("cannot rotate signed integer"); - return value >> ar | value << @truncate(Log2Int(T), @typeInfo(T).Int.bits - @as(T, ar)); + const ar = @intCast(Log2Int(T), @mod(r, @typeInfo(T).Int.bits)); + return x >> ar | x << (1 +% ~ar); } pub const FpsAverage = struct {