commit 83bdbb2abb97d2f28915611636d8bbda463c7bbb (tree)
parent c905ceb23c9bbb2bae3056e1a7651b73d4855dfe
Author: Robin Voetter <robin@voetter.nl>
Date: Sun, 24 Oct 2021 02:44:47 +0200
big ints: Make calcLimbLen always work at comptime, even if parameter is runtime
Diffstat:
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig
@@ -18,11 +18,14 @@ const debug_safety = false;
/// Returns the number of limbs needed to store `scalar`, which must be a
/// primitive integer value.
pub fn calcLimbLen(scalar: anytype) usize {
- if (scalar == 0) {
- return 1;
- }
+ const T = @TypeOf(scalar);
+ 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;
}