authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-24 02:55:23+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-24 02:56:48+02:00
logc563521d44e857e2ef80885a43304f1e6c64713b
tree60c7a087b016f06d2da4988f1f5d313ec3a735ac
parent83bdbb2abb97d2f28915611636d8bbda463c7bbb

big ints: tighten some more division memory requirements


2 files changed, 10 insertions(+), 11 deletions(-)

lib/std/math/big/int.zig+6-7
......@@ -763,8 +763,8 @@ pub const Mutable = struct {
763763 /// q may alias with a or b.
764764 ///
765765 /// Asserts there is enough memory to store q and r.
766 /// The upper bound for r limb count is b.limbs.len.
767 /// The upper bound for q limb count is given by `a.limbs.len + b.limbs.len`.
766 /// The upper bound for r limb count is `b.limbs.len`.
767 /// The upper bound for q limb count is given by `a.limbs`.
768768 ///
769769 /// If `allocator` is provided, it will be used for temporary storage to improve
770770 /// multiplication performance. `error.OutOfMemory` is handled with a fallback algorithm.
......@@ -893,9 +893,8 @@ pub const Mutable = struct {
893893 /// q may alias with a or b.
894894 ///
895895 /// Asserts there is enough memory to store q and r.
896 /// The upper bound for r limb count is a.limbs.len.
897 /// The upper bound for q limb count is given by `calcQuotientLimbLen`. This accounts
898 /// for temporary space used by the division algorithm.
896 /// The upper bound for r limb count is `b.limbs.len`.
897 /// The upper bound for q limb count is given by `a.limbs.len`.
899898 ///
900899 /// If `allocator` is provided, it will be used for temporary storage to improve
901900 /// multiplication performance. `error.OutOfMemory` is handled with a fallback algorithm.
......@@ -2569,7 +2568,7 @@ pub const Managed = struct {
25692568 ///
25702569 /// Returns an error if memory could not be allocated.
25712570 pub fn divFloor(q: *Managed, r: *Managed, a: Const, b: Const) !void {
2572 try q.ensureCapacity(a.limbs.len + b.limbs.len);
2571 try q.ensureCapacity(a.limbs.len);
25732572 try r.ensureCapacity(b.limbs.len);
25742573 var mq = q.toMutable();
25752574 var mr = r.toMutable();
......@@ -2586,7 +2585,7 @@ pub const Managed = struct {
25862585 ///
25872586 /// Returns an error if memory could not be allocated.
25882587 pub fn divTrunc(q: *Managed, r: *Managed, a: Const, b: Const) !void {
2589 try q.ensureCapacity(a.limbs.len + b.limbs.len);
2588 try q.ensureCapacity(a.limbs.len);
25902589 try r.ensureCapacity(b.limbs.len);
25912590 var mq = q.toMutable();
25922591 var mr = r.toMutable();
src/value.zig+4-4
......@@ -2301,7 +2301,7 @@ pub const Value = extern union {
23012301 const rhs_bigint = rhs.toBigInt(&rhs_space);
23022302 const limbs_q = try allocator.alloc(
23032303 std.math.big.Limb,
2304 lhs_bigint.limbs.len + rhs_bigint.limbs.len,
2304 lhs_bigint.limbs.len,
23052305 );
23062306 const limbs_r = try allocator.alloc(
23072307 std.math.big.Limb,
......@@ -2332,7 +2332,7 @@ pub const Value = extern union {
23322332 const rhs_bigint = rhs.toBigInt(&rhs_space);
23332333 const limbs_q = try allocator.alloc(
23342334 std.math.big.Limb,
2335 lhs_bigint.limbs.len + rhs_bigint.limbs.len,
2335 lhs_bigint.limbs.len,
23362336 );
23372337 const limbs_r = try allocator.alloc(
23382338 std.math.big.Limb,
......@@ -2363,7 +2363,7 @@ pub const Value = extern union {
23632363 const rhs_bigint = rhs.toBigInt(&rhs_space);
23642364 const limbs_q = try allocator.alloc(
23652365 std.math.big.Limb,
2366 lhs_bigint.limbs.len + rhs_bigint.limbs.len,
2366 lhs_bigint.limbs.len,
23672367 );
23682368 const limbs_r = try allocator.alloc(
23692369 std.math.big.Limb,
......@@ -2396,7 +2396,7 @@ pub const Value = extern union {
23962396 const rhs_bigint = rhs.toBigInt(&rhs_space);
23972397 const limbs_q = try allocator.alloc(
23982398 std.math.big.Limb,
2399 lhs_bigint.limbs.len + rhs_bigint.limbs.len,
2399 lhs_bigint.limbs.len,
24002400 );
24012401 const limbs_r = try allocator.alloc(
24022402 std.math.big.Limb,