authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-12 08:18:47-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-12 08:18:47-04:00
log38ee512a25776976caf787dff53126eed33ff835
tree60ad869a07d0150008d66e4a76875914768c32c9
parent2fe5bdb9ed4e17c94055a9539092bdf35a55f752

math.big.int: add `calcLimbLen` doc comment note

When trying to allocate memory for functions like `Managed.init` and `Managed.set` on the stack, a comptime-known allocation size is desired. The doc comments for these functions indicate that `calcLimbLen` can be used to determine how many limbs to allocate, but if `value` is not comptime-known, then neither is `calcLimbLen(value)`. However, an upper bound on the allocation size is still computable at comptime in this case, so this note documents an expression that can be used, rather than trying to add it to every doc comment that mentions `calcLimbLen`.

1 files changed, 3 insertions(+), 0 deletions(-)

lib/std/math/big/int.zig+3
...@@ -21,6 +21,9 @@ const debug_safety = false;...@@ -21,6 +21,9 @@ const debug_safety = false;
2121
22/// Returns the number of limbs needed to store `scalar`, which must be a22/// Returns the number of limbs needed to store `scalar`, which must be a
23/// primitive integer value.23/// primitive integer value.
24/// Note: A comptime-known upper bound of this value that may be used
25/// instead if `scalar` is not already comptime-known is
26/// `calcTwosCompLimbCount(@typeInfo(@TypeOf(scalar)).Int.bits)`
24pub fn calcLimbLen(scalar: anytype) usize {27pub fn calcLimbLen(scalar: anytype) usize {
25 if (scalar == 0) {28 if (scalar == 0) {
26 return 1;29 return 1;