From c563521d44e857e2ef80885a43304f1e6c64713b Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sun, 24 Oct 2021 02:55:23 +0200 Subject: [PATCH] big ints: tighten some more division memory requirements --- lib/std/math/big/int.zig | 13 ++++++------- src/value.zig | 8 ++++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index 91a8d37813..3b344f89db 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -763,8 +763,8 @@ pub const Mutable = struct { /// q may alias with a or b. /// /// Asserts there is enough memory to store q and r. - /// The upper bound for r limb count is b.limbs.len. - /// The upper bound for q limb count is given by `a.limbs.len + b.limbs.len`. + /// The upper bound for r limb count is `b.limbs.len`. + /// The upper bound for q limb count is given by `a.limbs`. /// /// If `allocator` is provided, it will be used for temporary storage to improve /// multiplication performance. `error.OutOfMemory` is handled with a fallback algorithm. @@ -893,9 +893,8 @@ pub const Mutable = struct { /// q may alias with a or b. /// /// Asserts there is enough memory to store q and r. - /// The upper bound for r limb count is a.limbs.len. - /// The upper bound for q limb count is given by `calcQuotientLimbLen`. This accounts - /// for temporary space used by the division algorithm. + /// The upper bound for r limb count is `b.limbs.len`. + /// The upper bound for q limb count is given by `a.limbs.len`. /// /// If `allocator` is provided, it will be used for temporary storage to improve /// multiplication performance. `error.OutOfMemory` is handled with a fallback algorithm. @@ -2569,7 +2568,7 @@ pub const Managed = struct { /// /// Returns an error if memory could not be allocated. pub fn divFloor(q: *Managed, r: *Managed, a: Const, b: Const) !void { - try q.ensureCapacity(a.limbs.len + b.limbs.len); + try q.ensureCapacity(a.limbs.len); try r.ensureCapacity(b.limbs.len); var mq = q.toMutable(); var mr = r.toMutable(); @@ -2586,7 +2585,7 @@ pub const Managed = struct { /// /// Returns an error if memory could not be allocated. pub fn divTrunc(q: *Managed, r: *Managed, a: Const, b: Const) !void { - try q.ensureCapacity(a.limbs.len + b.limbs.len); + try q.ensureCapacity(a.limbs.len); try r.ensureCapacity(b.limbs.len); var mq = q.toMutable(); var mr = r.toMutable(); diff --git a/src/value.zig b/src/value.zig index 15970390ba..8fe62b82b9 100644 --- a/src/value.zig +++ b/src/value.zig @@ -2301,7 +2301,7 @@ pub const Value = extern union { const rhs_bigint = rhs.toBigInt(&rhs_space); const limbs_q = try allocator.alloc( std.math.big.Limb, - lhs_bigint.limbs.len + rhs_bigint.limbs.len, + lhs_bigint.limbs.len, ); const limbs_r = try allocator.alloc( std.math.big.Limb, @@ -2332,7 +2332,7 @@ pub const Value = extern union { const rhs_bigint = rhs.toBigInt(&rhs_space); const limbs_q = try allocator.alloc( std.math.big.Limb, - lhs_bigint.limbs.len + rhs_bigint.limbs.len, + lhs_bigint.limbs.len, ); const limbs_r = try allocator.alloc( std.math.big.Limb, @@ -2363,7 +2363,7 @@ pub const Value = extern union { const rhs_bigint = rhs.toBigInt(&rhs_space); const limbs_q = try allocator.alloc( std.math.big.Limb, - lhs_bigint.limbs.len + rhs_bigint.limbs.len, + lhs_bigint.limbs.len, ); const limbs_r = try allocator.alloc( std.math.big.Limb, @@ -2396,7 +2396,7 @@ pub const Value = extern union { const rhs_bigint = rhs.toBigInt(&rhs_space); const limbs_q = try allocator.alloc( std.math.big.Limb, - lhs_bigint.limbs.len + rhs_bigint.limbs.len, + lhs_bigint.limbs.len, ); const limbs_r = try allocator.alloc( std.math.big.Limb,