authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-12 08:56:13-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-12 08:56:13-04:00
loge78d7704a4fcde4e1851582212908b6762720216
tree6a4491014ae0beaa286b9a85290fd726e75a77a6
parent38ee512a25776976caf787dff53126eed33ff835

math.big.int: document the purpose of limb_len in scalar methods

Ideally this duplicated code could be factored out into a function, but there doesn't seem to be any way in the Zig type system to represent an argument to a function called at comptime that is only needed if it is comptime-known. Instead, we document what is going on in an adjacent comment in case it gets copy-pasted into new methods in the future.

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

lib/std/math/big/int.zig+12
......@@ -394,6 +394,12 @@ pub const Mutable = struct {
394394 /// Asserts the result fits in `r`. An upper bound on the number of limbs needed by
395395 /// r is `math.max(a.limbs.len, calcLimbLen(scalar)) + 1`.
396396 pub fn addScalar(r: *Mutable, a: Const, scalar: anytype) void {
397 // Normally we could just determine the number of limbs needed with calcLimbLen,
398 // but that is not comptime-known when scalar is not a comptime_int. Instead, we
399 // use calcTwosCompLimbCount for a non-comptime_int scalar, which can be pessimistic
400 // in the case that scalar happens to be small in magnitude within its type, but it
401 // is well worth being able to use the stack and not needing an allocator passed in.
402 // Note that Mutable.init still sets operand.len to calcLimbLen(scalar) in any case.
397403 const limb_len = comptime switch (@typeInfo(@TypeOf(scalar))) {
398404 .ComptimeInt => calcLimbLen(scalar),
399405 .Int => |info| calcTwosCompLimbCount(info.bits),
......@@ -2311,6 +2317,12 @@ pub const Const = struct {
23112317
23122318 /// Same as `order` but the right-hand operand is a primitive integer.
23132319 pub fn orderAgainstScalar(lhs: Const, scalar: anytype) math.Order {
2320 // Normally we could just determine the number of limbs needed with calcLimbLen,
2321 // but that is not comptime-known when scalar is not a comptime_int. Instead, we
2322 // use calcTwosCompLimbCount for a non-comptime_int scalar, which can be pessimistic
2323 // in the case that scalar happens to be small in magnitude within its type, but it
2324 // is well worth being able to use the stack and not needing an allocator passed in.
2325 // Note that Mutable.init still sets rhs.len to calcLimbLen(scalar) in any case.
23142326 const limb_len = comptime switch (@typeInfo(@TypeOf(scalar))) {
23152327 .ComptimeInt => calcLimbLen(scalar),
23162328 .Int => |info| calcTwosCompLimbCount(info.bits),