mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
std: better int log2 implementation for number literals
This commit is contained in:
parent
cd2f65ff6a
commit
eb26aeb1e5
@ -26,8 +26,11 @@ fn log2_workaround(x: var) -> @typeOf(x) {
|
|||||||
else => @compileError("log2 not implemented for " ++ @typeName(T)),
|
else => @compileError("log2 not implemented for " ++ @typeName(T)),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
TypeId.IntLiteral => {
|
TypeId.IntLiteral => comptime {
|
||||||
return @typeOf(1)(log2_int(u128, x))
|
var result = 0;
|
||||||
|
var x_shifted = x;
|
||||||
|
while ({x_shifted >>= 1; x_shifted != 0}) : (result += 1) {}
|
||||||
|
return result;
|
||||||
},
|
},
|
||||||
TypeId.Int => {
|
TypeId.Int => {
|
||||||
return log2_int(T, x);
|
return log2_int(T, x);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user