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 {...@@ -763,8 +763,8 @@ pub const Mutable = struct {
763 /// q may alias with a or b.763 /// q may alias with a or b.
764 ///764 ///
765 /// Asserts there is enough memory to store q and r.765 /// Asserts there is enough memory to store q and r.
766 /// The upper bound for r limb count is 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.len + b.limbs.len`.767 /// The upper bound for q limb count is given by `a.limbs`.
768 ///768 ///
769 /// If `allocator` is provided, it will be used for temporary storage to improve769 /// If `allocator` is provided, it will be used for temporary storage to improve
770 /// multiplication performance. `error.OutOfMemory` is handled with a fallback algorithm.770 /// multiplication performance. `error.OutOfMemory` is handled with a fallback algorithm.
...@@ -893,9 +893,8 @@ pub const Mutable = struct {...@@ -893,9 +893,8 @@ pub const Mutable = struct {
893 /// q may alias with a or b.893 /// q may alias with a or b.
894 ///894 ///
895 /// Asserts there is enough memory to store q and r.895 /// Asserts there is enough memory to store q and r.
896 /// The upper bound for r limb count is a.limbs.len.896 /// The upper bound for r limb count is `b.limbs.len`.
897 /// The upper bound for q limb count is given by `calcQuotientLimbLen`. This accounts897 /// The upper bound for q limb count is given by `a.limbs.len`.
898 /// for temporary space used by the division algorithm.
899 ///898 ///
900 /// If `allocator` is provided, it will be used for temporary storage to improve899 /// If `allocator` is provided, it will be used for temporary storage to improve
901 /// multiplication performance. `error.OutOfMemory` is handled with a fallback algorithm.900 /// multiplication performance. `error.OutOfMemory` is handled with a fallback algorithm.
...@@ -2569,7 +2568,7 @@ pub const Managed = struct {...@@ -2569,7 +2568,7 @@ pub const Managed = struct {
2569 ///2568 ///
2570 /// Returns an error if memory could not be allocated.2569 /// Returns an error if memory could not be allocated.
2571 pub fn divFloor(q: *Managed, r: *Managed, a: Const, b: Const) !void {2570 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);
2573 try r.ensureCapacity(b.limbs.len);2572 try r.ensureCapacity(b.limbs.len);
2574 var mq = q.toMutable();2573 var mq = q.toMutable();
2575 var mr = r.toMutable();2574 var mr = r.toMutable();
...@@ -2586,7 +2585,7 @@ pub const Managed = struct {...@@ -2586,7 +2585,7 @@ pub const Managed = struct {
2586 ///2585 ///
2587 /// Returns an error if memory could not be allocated.2586 /// Returns an error if memory could not be allocated.
2588 pub fn divTrunc(q: *Managed, r: *Managed, a: Const, b: Const) !void {2587 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);
2590 try r.ensureCapacity(b.limbs.len);2589 try r.ensureCapacity(b.limbs.len);
2591 var mq = q.toMutable();2590 var mq = q.toMutable();
2592 var mr = r.toMutable();2591 var mr = r.toMutable();
src/value.zig+4-4
...@@ -2301,7 +2301,7 @@ pub const Value = extern union {...@@ -2301,7 +2301,7 @@ pub const Value = extern union {
2301 const rhs_bigint = rhs.toBigInt(&rhs_space);2301 const rhs_bigint = rhs.toBigInt(&rhs_space);
2302 const limbs_q = try allocator.alloc(2302 const limbs_q = try allocator.alloc(
2303 std.math.big.Limb,2303 std.math.big.Limb,
2304 lhs_bigint.limbs.len + rhs_bigint.limbs.len,2304 lhs_bigint.limbs.len,
2305 );2305 );
2306 const limbs_r = try allocator.alloc(2306 const limbs_r = try allocator.alloc(
2307 std.math.big.Limb,2307 std.math.big.Limb,
...@@ -2332,7 +2332,7 @@ pub const Value = extern union {...@@ -2332,7 +2332,7 @@ pub const Value = extern union {
2332 const rhs_bigint = rhs.toBigInt(&rhs_space);2332 const rhs_bigint = rhs.toBigInt(&rhs_space);
2333 const limbs_q = try allocator.alloc(2333 const limbs_q = try allocator.alloc(
2334 std.math.big.Limb,2334 std.math.big.Limb,
2335 lhs_bigint.limbs.len + rhs_bigint.limbs.len,2335 lhs_bigint.limbs.len,
2336 );2336 );
2337 const limbs_r = try allocator.alloc(2337 const limbs_r = try allocator.alloc(
2338 std.math.big.Limb,2338 std.math.big.Limb,
...@@ -2363,7 +2363,7 @@ pub const Value = extern union {...@@ -2363,7 +2363,7 @@ pub const Value = extern union {
2363 const rhs_bigint = rhs.toBigInt(&rhs_space);2363 const rhs_bigint = rhs.toBigInt(&rhs_space);
2364 const limbs_q = try allocator.alloc(2364 const limbs_q = try allocator.alloc(
2365 std.math.big.Limb,2365 std.math.big.Limb,
2366 lhs_bigint.limbs.len + rhs_bigint.limbs.len,2366 lhs_bigint.limbs.len,
2367 );2367 );
2368 const limbs_r = try allocator.alloc(2368 const limbs_r = try allocator.alloc(
2369 std.math.big.Limb,2369 std.math.big.Limb,
...@@ -2396,7 +2396,7 @@ pub const Value = extern union {...@@ -2396,7 +2396,7 @@ pub const Value = extern union {
2396 const rhs_bigint = rhs.toBigInt(&rhs_space);2396 const rhs_bigint = rhs.toBigInt(&rhs_space);
2397 const limbs_q = try allocator.alloc(2397 const limbs_q = try allocator.alloc(
2398 std.math.big.Limb,2398 std.math.big.Limb,
2399 lhs_bigint.limbs.len + rhs_bigint.limbs.len,2399 lhs_bigint.limbs.len,
2400 );2400 );
2401 const limbs_r = try allocator.alloc(2401 const limbs_r = try allocator.alloc(
2402 std.math.big.Limb,2402 std.math.big.Limb,