mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 22:04:21 +00:00
big ints: Make calcLimbLen always work at comptime, even if parameter is runtime
This commit is contained in:
parent
c905ceb23c
commit
83bdbb2abb
1 changed files with 7 additions and 4 deletions
|
|
@ -18,11 +18,14 @@ const debug_safety = false;
|
||||||
/// Returns the number of limbs needed to store `scalar`, which must be a
|
/// Returns the number of limbs needed to store `scalar`, which must be a
|
||||||
/// primitive integer value.
|
/// primitive integer value.
|
||||||
pub fn calcLimbLen(scalar: anytype) usize {
|
pub fn calcLimbLen(scalar: anytype) usize {
|
||||||
if (scalar == 0) {
|
const T = @TypeOf(scalar);
|
||||||
return 1;
|
const max_scalar = switch (@typeInfo(T)) {
|
||||||
}
|
.Int => maxInt(T),
|
||||||
|
.ComptimeInt => scalar,
|
||||||
|
else => @compileError("parameter must be a primitive integer type"),
|
||||||
|
};
|
||||||
|
|
||||||
const w_value = std.math.absCast(scalar);
|
const w_value = std.math.absCast(max_scalar);
|
||||||
return @divFloor(@intCast(Limb, math.log2(w_value)), limb_bits) + 1;
|
return @divFloor(@intCast(Limb, math.log2(w_value)), limb_bits) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue