authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-24 02:44:47+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-24 02:56:48+02:00
log83bdbb2abb97d2f28915611636d8bbda463c7bbb
tree470fba0a763f2b05094f10bf2c59dd6a691e5d21
parentc905ceb23c9bbb2bae3056e1a7651b73d4855dfe

big ints: Make calcLimbLen always work at comptime, even if parameter is runtime


1 files changed, 7 insertions(+), 4 deletions(-)

lib/std/math/big/int.zig+7-4
...@@ -18,11 +18,14 @@ const debug_safety = false;...@@ -18,11 +18,14 @@ const debug_safety = false;
18/// Returns the number of limbs needed to store `scalar`, which must be a18/// Returns the number of limbs needed to store `scalar`, which must be a
19/// primitive integer value.19/// primitive integer value.
20pub fn calcLimbLen(scalar: anytype) usize {20pub fn calcLimbLen(scalar: anytype) usize {
21 if (scalar == 0) {21 const T = @TypeOf(scalar);
22 return 1;22 const max_scalar = switch (@typeInfo(T)) {
23 }23 .Int => maxInt(T),
24 .ComptimeInt => scalar,
25 else => @compileError("parameter must be a primitive integer type"),
26 };
2427
25 const w_value = std.math.absCast(scalar);28 const w_value = std.math.absCast(max_scalar);
26 return @divFloor(@intCast(Limb, math.log2(w_value)), limb_bits) + 1;29 return @divFloor(@intCast(Limb, math.log2(w_value)), limb_bits) + 1;
27}30}
2831