commit eb26aeb1e527b5f43f7f789d3a995dca8c73e2e9 (tree)
parent cd2f65ff6ace9f1e426d5e8a4721666d347b289d
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Sat, 19 Aug 2017 02:29:18 -0400
std: better int log2 implementation for number literals
Diffstat:
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/std/math/log2.zig b/std/math/log2.zig
@@ -26,8 +26,11 @@ fn log2_workaround(x: var) -> @typeOf(x) {
else => @compileError("log2 not implemented for " ++ @typeName(T)),
};
},
- TypeId.IntLiteral => {
- return @typeOf(1)(log2_int(u128, x))
+ TypeId.IntLiteral => comptime {
+ var result = 0;
+ var x_shifted = x;
+ while ({x_shifted >>= 1; x_shifted != 0}) : (result += 1) {}
+ return result;
},
TypeId.Int => {
return log2_int(T, x);