authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-29 15:34:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-29 22:06:27-04:00
log54454fd0102af8b25dbc85751d37fd265380d920
treecdab71c06e0a8269fa3349c5b4b62be91d8cac4e
parent8f2f0d8f0805f10523ced7e6a166ce81a097a54a

std.math.big.int: breaking API changes to prevent UAF

Many of the Managed methods accepted by-val parameters which could reference Limb slices that became invalid memory after any ensureCapacity calls. Now, Managed methods accept `*const Managed` parameters so that if the function allows aliasing and the ensure-capacity call resizes the Limb slice, it also affects the aliased parameters, avoiding use-after-free bugs. This is a breaking change that reduces the requirement for callsites to manually make the ensure-capacity changes prior to calling many of the Managed methods. Closes #11897

5 files changed, 299 insertions(+), 283 deletions(-)

lib/std/math/big/int.zig+93-78
......@@ -1391,28 +1391,28 @@ pub const Mutable = struct {
13911391
13921392 if (B == 0) {
13931393 // t_big = x % y, r is unused
1394 try r.divTrunc(&t_big, x.toConst(), y.toConst());
1394 try r.divTrunc(&t_big, &x, &y);
13951395 assert(t_big.isPositive());
13961396
13971397 x.swap(&y);
13981398 y.swap(&t_big);
13991399 } else {
14001400 var storage: [8]Limb = undefined;
1401 const Ap = fixedIntFromSignedDoubleLimb(A, storage[0..2]).toConst();
1402 const Bp = fixedIntFromSignedDoubleLimb(B, storage[2..4]).toConst();
1403 const Cp = fixedIntFromSignedDoubleLimb(C, storage[4..6]).toConst();
1404 const Dp = fixedIntFromSignedDoubleLimb(D, storage[6..8]).toConst();
1401 const Ap = fixedIntFromSignedDoubleLimb(A, storage[0..2]).toManaged(limbs_buffer.allocator);
1402 const Bp = fixedIntFromSignedDoubleLimb(B, storage[2..4]).toManaged(limbs_buffer.allocator);
1403 const Cp = fixedIntFromSignedDoubleLimb(C, storage[4..6]).toManaged(limbs_buffer.allocator);
1404 const Dp = fixedIntFromSignedDoubleLimb(D, storage[6..8]).toManaged(limbs_buffer.allocator);
14051405
14061406 // t_big = Ax + By
1407 try r.mul(x.toConst(), Ap);
1408 try t_big.mul(y.toConst(), Bp);
1409 try t_big.add(r.toConst(), t_big.toConst());
1407 try r.mul(&x, &Ap);
1408 try t_big.mul(&y, &Bp);
1409 try t_big.add(&r, &t_big);
14101410
14111411 // u = Cx + Dy, r as u
14121412 try tmp_x.copy(x.toConst());
1413 try x.mul(tmp_x.toConst(), Cp);
1414 try r.mul(y.toConst(), Dp);
1415 try r.add(x.toConst(), r.toConst());
1413 try x.mul(&tmp_x, &Cp);
1414 try r.mul(&y, &Dp);
1415 try r.add(&x, &r);
14161416
14171417 x.swap(&t_big);
14181418 y.swap(&r);
......@@ -1423,7 +1423,7 @@ pub const Mutable = struct {
14231423 assert(x.toConst().order(y.toConst()) != .lt);
14241424
14251425 while (!y.toConst().eqZero()) {
1426 try t_big.divTrunc(&r, x.toConst(), y.toConst());
1426 try t_big.divTrunc(&r, &x, &y);
14271427 x.swap(&y);
14281428 y.swap(&r);
14291429 }
......@@ -2660,57 +2660,56 @@ pub const Managed = struct {
26602660
26612661 /// r = a + scalar
26622662 ///
2663 /// r and a may be aliases. If r aliases a, then caller must call
2664 /// `r.ensureAddScalarCapacity` prior to calling `add`.
2665 /// scalar is a primitive integer type.
2663 /// r and a may be aliases.
26662664 ///
26672665 /// Returns an error if memory could not be allocated.
2668 pub fn addScalar(r: *Managed, a: Const, scalar: anytype) Allocator.Error!void {
2669 assert((r.limbs.ptr != a.limbs.ptr) or r.limbs.len >= math.max(a.limbs.len, calcLimbLen(scalar)) + 1);
2670 try r.ensureAddScalarCapacity(a, scalar);
2666 pub fn addScalar(r: *Managed, a: *const Managed, scalar: anytype) Allocator.Error!void {
2667 try r.ensureAddScalarCapacity(a.toConst(), scalar);
26712668 var m = r.toMutable();
2672 m.addScalar(a, scalar);
2669 m.addScalar(a.toConst(), scalar);
26732670 r.setMetadata(m.positive, m.len);
26742671 }
26752672
26762673 /// r = a + b
26772674 ///
2678 /// r, a and b may be aliases. If r aliases a or b, then caller must call
2679 /// `r.ensureAddCapacity` prior to calling `add`.
2675 /// r, a and b may be aliases.
26802676 ///
26812677 /// Returns an error if memory could not be allocated.
2682 pub fn add(r: *Managed, a: Const, b: Const) Allocator.Error!void {
2683 assert((r.limbs.ptr != a.limbs.ptr and r.limbs.ptr != b.limbs.ptr) or r.limbs.len >= math.max(a.limbs.len, b.limbs.len) + 1);
2684 try r.ensureAddCapacity(a, b);
2678 pub fn add(r: *Managed, a: *const Managed, b: *const Managed) Allocator.Error!void {
2679 try r.ensureAddCapacity(a.toConst(), b.toConst());
26852680 var m = r.toMutable();
2686 m.add(a, b);
2681 m.add(a.toConst(), b.toConst());
26872682 r.setMetadata(m.positive, m.len);
26882683 }
26892684
26902685 /// r = a + b with 2s-complement wrapping semantics. Returns whether any overflow occured.
26912686 ///
2692 /// r, a and b may be aliases. If r aliases a or b, then caller must call
2693 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2687 /// r, a and b may be aliases.
26942688 ///
26952689 /// Returns an error if memory could not be allocated.
2696 pub fn addWrap(r: *Managed, a: Const, b: Const, signedness: Signedness, bit_count: usize) Allocator.Error!bool {
2690 pub fn addWrap(
2691 r: *Managed,
2692 a: *const Managed,
2693 b: *const Managed,
2694 signedness: Signedness,
2695 bit_count: usize,
2696 ) Allocator.Error!bool {
26972697 try r.ensureTwosCompCapacity(bit_count);
26982698 var m = r.toMutable();
2699 const wrapped = m.addWrap(a, b, signedness, bit_count);
2699 const wrapped = m.addWrap(a.toConst(), b.toConst(), signedness, bit_count);
27002700 r.setMetadata(m.positive, m.len);
27012701 return wrapped;
27022702 }
27032703
27042704 /// r = a + b with 2s-complement saturating semantics.
27052705 ///
2706 /// r, a and b may be aliases. If r aliases a or b, then caller must call
2707 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2706 /// r, a and b may be aliases.
27082707 ///
27092708 /// Returns an error if memory could not be allocated.
2710 pub fn addSat(r: *Managed, a: Const, b: Const, signedness: Signedness, bit_count: usize) Allocator.Error!void {
2709 pub fn addSat(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!void {
27112710 try r.ensureTwosCompCapacity(bit_count);
27122711 var m = r.toMutable();
2713 m.addSat(a, b, signedness, bit_count);
2712 m.addSat(a.toConst(), b.toConst(), signedness, bit_count);
27142713 r.setMetadata(m.positive, m.len);
27152714 }
27162715
......@@ -2719,64 +2718,72 @@ pub const Managed = struct {
27192718 /// r, a and b may be aliases.
27202719 ///
27212720 /// Returns an error if memory could not be allocated.
2722 pub fn sub(r: *Managed, a: Const, b: Const) !void {
2721 pub fn sub(r: *Managed, a: *const Managed, b: *const Managed) !void {
27232722 try r.ensureCapacity(math.max(a.limbs.len, b.limbs.len) + 1);
27242723 var m = r.toMutable();
2725 m.sub(a, b);
2724 m.sub(a.toConst(), b.toConst());
27262725 r.setMetadata(m.positive, m.len);
27272726 }
27282727
27292728 /// r = a - b with 2s-complement wrapping semantics. Returns whether any overflow occured.
27302729 ///
2731 /// r, a and b may be aliases. If r aliases a or b, then caller must call
2732 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2730 /// r, a and b may be aliases.
27332731 ///
27342732 /// Returns an error if memory could not be allocated.
2735 pub fn subWrap(r: *Managed, a: Const, b: Const, signedness: Signedness, bit_count: usize) Allocator.Error!bool {
2733 pub fn subWrap(
2734 r: *Managed,
2735 a: *const Managed,
2736 b: *const Managed,
2737 signedness: Signedness,
2738 bit_count: usize,
2739 ) Allocator.Error!bool {
27362740 try r.ensureTwosCompCapacity(bit_count);
27372741 var m = r.toMutable();
2738 const wrapped = m.subWrap(a, b, signedness, bit_count);
2742 const wrapped = m.subWrap(a.toConst(), b.toConst(), signedness, bit_count);
27392743 r.setMetadata(m.positive, m.len);
27402744 return wrapped;
27412745 }
27422746
27432747 /// r = a - b with 2s-complement saturating semantics.
27442748 ///
2745 /// r, a and b may be aliases. If r aliases a or b, then caller must call
2746 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2749 /// r, a and b may be aliases.
27472750 ///
27482751 /// Returns an error if memory could not be allocated.
2749 pub fn subSat(r: *Managed, a: Const, b: Const, signedness: Signedness, bit_count: usize) Allocator.Error!void {
2752 pub fn subSat(
2753 r: *Managed,
2754 a: *const Managed,
2755 b: *const Managed,
2756 signedness: Signedness,
2757 bit_count: usize,
2758 ) Allocator.Error!void {
27502759 try r.ensureTwosCompCapacity(bit_count);
27512760 var m = r.toMutable();
2752 m.subSat(a, b, signedness, bit_count);
2761 m.subSat(a.toConst(), b.toConst(), signedness, bit_count);
27532762 r.setMetadata(m.positive, m.len);
27542763 }
27552764
27562765 /// rma = a * b
27572766 ///
27582767 /// rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.
2759 /// If rma aliases a or b, then caller must call `rma.ensureMulCapacity` prior to calling `mul`.
27602768 ///
27612769 /// Returns an error if memory could not be allocated.
27622770 ///
27632771 /// rma's allocator is used for temporary storage to speed up the multiplication.
2764 pub fn mul(rma: *Managed, a: Const, b: Const) !void {
2772 pub fn mul(rma: *Managed, a: *const Managed, b: *const Managed) !void {
27652773 var alias_count: usize = 0;
27662774 if (rma.limbs.ptr == a.limbs.ptr)
27672775 alias_count += 1;
27682776 if (rma.limbs.ptr == b.limbs.ptr)
27692777 alias_count += 1;
2770 assert(alias_count == 0 or rma.limbs.len >= a.limbs.len + b.limbs.len + 1);
2771 try rma.ensureMulCapacity(a, b);
2778 try rma.ensureMulCapacity(a.toConst(), b.toConst());
27722779 var m = rma.toMutable();
27732780 if (alias_count == 0) {
2774 m.mulNoAlias(a, b, rma.allocator);
2781 m.mulNoAlias(a.toConst(), b.toConst(), rma.allocator);
27752782 } else {
27762783 const limb_count = calcMulLimbsBufferLen(a.limbs.len, b.limbs.len, alias_count);
27772784 const limbs_buffer = try rma.allocator.alloc(Limb, limb_count);
27782785 defer rma.allocator.free(limbs_buffer);
2779 m.mul(a, b, limbs_buffer, rma.allocator);
2786 m.mul(a.toConst(), b.toConst(), limbs_buffer, rma.allocator);
27802787 }
27812788 rma.setMetadata(m.positive, m.len);
27822789 }
......@@ -2784,13 +2791,17 @@ pub const Managed = struct {
27842791 /// rma = a * b with 2s-complement wrapping semantics.
27852792 ///
27862793 /// rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.
2787 /// If rma aliases a or b, then caller must call `ensureTwosCompCapacity`
2788 /// prior to calling `mul`.
27892794 ///
27902795 /// Returns an error if memory could not be allocated.
27912796 ///
27922797 /// rma's allocator is used for temporary storage to speed up the multiplication.
2793 pub fn mulWrap(rma: *Managed, a: Const, b: Const, signedness: Signedness, bit_count: usize) !void {
2798 pub fn mulWrap(
2799 rma: *Managed,
2800 a: *const Managed,
2801 b: *const Managed,
2802 signedness: Signedness,
2803 bit_count: usize,
2804 ) !void {
27942805 var alias_count: usize = 0;
27952806 if (rma.limbs.ptr == a.limbs.ptr)
27962807 alias_count += 1;
......@@ -2800,12 +2811,12 @@ pub const Managed = struct {
28002811 try rma.ensureTwosCompCapacity(bit_count);
28012812 var m = rma.toMutable();
28022813 if (alias_count == 0) {
2803 m.mulWrapNoAlias(a, b, signedness, bit_count, rma.allocator);
2814 m.mulWrapNoAlias(a.toConst(), b.toConst(), signedness, bit_count, rma.allocator);
28042815 } else {
28052816 const limb_count = calcMulWrapLimbsBufferLen(bit_count, a.limbs.len, b.limbs.len, alias_count);
28062817 const limbs_buffer = try rma.allocator.alloc(Limb, limb_count);
28072818 defer rma.allocator.free(limbs_buffer);
2808 m.mulWrap(a, b, signedness, bit_count, limbs_buffer, rma.allocator);
2819 m.mulWrap(a.toConst(), b.toConst(), signedness, bit_count, limbs_buffer, rma.allocator);
28092820 }
28102821 rma.setMetadata(m.positive, m.len);
28112822 }
......@@ -2831,14 +2842,14 @@ pub const Managed = struct {
28312842 /// a / b are floored (rounded towards 0).
28322843 ///
28332844 /// Returns an error if memory could not be allocated.
2834 pub fn divFloor(q: *Managed, r: *Managed, a: Const, b: Const) !void {
2845 pub fn divFloor(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !void {
28352846 try q.ensureCapacity(a.limbs.len);
28362847 try r.ensureCapacity(b.limbs.len);
28372848 var mq = q.toMutable();
28382849 var mr = r.toMutable();
28392850 const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.limbs.len, b.limbs.len));
28402851 defer q.allocator.free(limbs_buffer);
2841 mq.divFloor(&mr, a, b, limbs_buffer);
2852 mq.divFloor(&mr, a.toConst(), b.toConst(), limbs_buffer);
28422853 q.setMetadata(mq.positive, mq.len);
28432854 r.setMetadata(mr.positive, mr.len);
28442855 }
......@@ -2848,20 +2859,21 @@ pub const Managed = struct {
28482859 /// a / b are truncated (rounded towards -inf).
28492860 ///
28502861 /// Returns an error if memory could not be allocated.
2851 pub fn divTrunc(q: *Managed, r: *Managed, a: Const, b: Const) !void {
2862 pub fn divTrunc(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !void {
28522863 try q.ensureCapacity(a.limbs.len);
28532864 try r.ensureCapacity(b.limbs.len);
28542865 var mq = q.toMutable();
28552866 var mr = r.toMutable();
28562867 const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.limbs.len, b.limbs.len));
28572868 defer q.allocator.free(limbs_buffer);
2858 mq.divTrunc(&mr, a, b, limbs_buffer);
2869 mq.divTrunc(&mr, a.toConst(), b.toConst(), limbs_buffer);
28592870 q.setMetadata(mq.positive, mq.len);
28602871 r.setMetadata(mr.positive, mr.len);
28612872 }
28622873
28632874 /// r = a << shift, in other words, r = a * 2^shift
2864 pub fn shiftLeft(r: *Managed, a: Managed, shift: usize) !void {
2875 /// r and a may alias.
2876 pub fn shiftLeft(r: *Managed, a: *const Managed, shift: usize) !void {
28652877 try r.ensureCapacity(a.len() + (shift / limb_bits) + 1);
28662878 var m = r.toMutable();
28672879 m.shiftLeft(a.toConst(), shift);
......@@ -2869,7 +2881,8 @@ pub const Managed = struct {
28692881 }
28702882
28712883 /// r = a <<| shift with 2s-complement saturating semantics.
2872 pub fn shiftLeftSat(r: *Managed, a: Managed, shift: usize, signedness: Signedness, bit_count: usize) !void {
2884 /// r and a may alias.
2885 pub fn shiftLeftSat(r: *Managed, a: *const Managed, shift: usize, signedness: Signedness, bit_count: usize) !void {
28732886 try r.ensureTwosCompCapacity(bit_count);
28742887 var m = r.toMutable();
28752888 m.shiftLeftSat(a.toConst(), shift, signedness, bit_count);
......@@ -2877,7 +2890,8 @@ pub const Managed = struct {
28772890 }
28782891
28792892 /// r = a >> shift
2880 pub fn shiftRight(r: *Managed, a: Managed, shift: usize) !void {
2893 /// r and a may alias.
2894 pub fn shiftRight(r: *Managed, a: *const Managed, shift: usize) !void {
28812895 if (a.len() <= shift / limb_bits) {
28822896 r.metadata = 1;
28832897 r.limbs[0] = 0;
......@@ -2891,7 +2905,8 @@ pub const Managed = struct {
28912905 }
28922906
28932907 /// r = ~a under 2s-complement wrapping semantics.
2894 pub fn bitNotWrap(r: *Managed, a: Managed, signedness: Signedness, bit_count: usize) !void {
2908 /// r and a may alias.
2909 pub fn bitNotWrap(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void {
28952910 try r.ensureTwosCompCapacity(bit_count);
28962911 var m = r.toMutable();
28972912 m.bitNotWrap(a.toConst(), signedness, bit_count);
......@@ -2901,7 +2916,7 @@ pub const Managed = struct {
29012916 /// r = a | b
29022917 ///
29032918 /// a and b are zero-extended to the longer of a or b.
2904 pub fn bitOr(r: *Managed, a: Managed, b: Managed) !void {
2919 pub fn bitOr(r: *Managed, a: *const Managed, b: *const Managed) !void {
29052920 try r.ensureCapacity(math.max(a.len(), b.len()));
29062921 var m = r.toMutable();
29072922 m.bitOr(a.toConst(), b.toConst());
......@@ -2909,7 +2924,7 @@ pub const Managed = struct {
29092924 }
29102925
29112926 /// r = a & b
2912 pub fn bitAnd(r: *Managed, a: Managed, b: Managed) !void {
2927 pub fn bitAnd(r: *Managed, a: *const Managed, b: *const Managed) !void {
29132928 const cap = if (a.isPositive() or b.isPositive())
29142929 math.min(a.len(), b.len())
29152930 else
......@@ -2921,7 +2936,7 @@ pub const Managed = struct {
29212936 }
29222937
29232938 /// r = a ^ b
2924 pub fn bitXor(r: *Managed, a: Managed, b: Managed) !void {
2939 pub fn bitXor(r: *Managed, a: *const Managed, b: *const Managed) !void {
29252940 var cap = math.max(a.len(), b.len()) + @boolToInt(a.isPositive() != b.isPositive());
29262941 try r.ensureCapacity(cap);
29272942
......@@ -2934,7 +2949,7 @@ pub const Managed = struct {
29342949 /// x and y may alias each other.
29352950 ///
29362951 /// rma's allocator is used for temporary storage to boost multiplication performance.
2937 pub fn gcd(rma: *Managed, x: Managed, y: Managed) !void {
2952 pub fn gcd(rma: *Managed, x: *const Managed, y: *const Managed) !void {
29382953 try rma.ensureCapacity(math.min(x.len(), y.len()));
29392954 var m = rma.toMutable();
29402955 var limbs_buffer = std.ArrayList(Limb).init(rma.allocator);
......@@ -2944,14 +2959,14 @@ pub const Managed = struct {
29442959 }
29452960
29462961 /// r = a * a
2947 pub fn sqr(rma: *Managed, a: Const) !void {
2962 pub fn sqr(rma: *Managed, a: *const Managed) !void {
29482963 const needed_limbs = 2 * a.limbs.len + 1;
29492964
29502965 if (rma.limbs.ptr == a.limbs.ptr) {
29512966 var m = try Managed.initCapacity(rma.allocator, needed_limbs);
29522967 errdefer m.deinit();
29532968 var m_mut = m.toMutable();
2954 m_mut.sqrNoAlias(a, rma.allocator);
2969 m_mut.sqrNoAlias(a.toConst(), rma.allocator);
29552970 m.setMetadata(m_mut.positive, m_mut.len);
29562971
29572972 rma.deinit();
......@@ -2959,12 +2974,12 @@ pub const Managed = struct {
29592974 } else {
29602975 try rma.ensureCapacity(needed_limbs);
29612976 var rma_mut = rma.toMutable();
2962 rma_mut.sqrNoAlias(a, rma.allocator);
2977 rma_mut.sqrNoAlias(a.toConst(), rma.allocator);
29632978 rma.setMetadata(rma_mut.positive, rma_mut.len);
29642979 }
29652980 }
29662981
2967 pub fn pow(rma: *Managed, a: Const, b: u32) !void {
2982 pub fn pow(rma: *Managed, a: *const Managed, b: u32) !void {
29682983 const needed_limbs = calcPowLimbsBufferLen(a.bitCountAbs(), b);
29692984
29702985 const limbs_buffer = try rma.allocator.alloc(Limb, needed_limbs);
......@@ -2974,7 +2989,7 @@ pub const Managed = struct {
29742989 var m = try Managed.initCapacity(rma.allocator, needed_limbs);
29752990 errdefer m.deinit();
29762991 var m_mut = m.toMutable();
2977 try m_mut.pow(a, b, limbs_buffer);
2992 try m_mut.pow(a.toConst(), b, limbs_buffer);
29782993 m.setMetadata(m_mut.positive, m_mut.len);
29792994
29802995 rma.deinit();
......@@ -2982,33 +2997,33 @@ pub const Managed = struct {
29822997 } else {
29832998 try rma.ensureCapacity(needed_limbs);
29842999 var rma_mut = rma.toMutable();
2985 try rma_mut.pow(a, b, limbs_buffer);
3000 try rma_mut.pow(a.toConst(), b, limbs_buffer);
29863001 rma.setMetadata(rma_mut.positive, rma_mut.len);
29873002 }
29883003 }
29893004
29903005 /// r = truncate(Int(signedness, bit_count), a)
2991 pub fn truncate(r: *Managed, a: Const, signedness: Signedness, bit_count: usize) !void {
3006 pub fn truncate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void {
29923007 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
29933008 var m = r.toMutable();
2994 m.truncate(a, signedness, bit_count);
3009 m.truncate(a.toConst(), signedness, bit_count);
29953010 r.setMetadata(m.positive, m.len);
29963011 }
29973012
29983013 /// r = saturate(Int(signedness, bit_count), a)
2999 pub fn saturate(r: *Managed, a: Const, signedness: Signedness, bit_count: usize) !void {
3014 pub fn saturate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void {
30003015 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
30013016 var m = r.toMutable();
3002 m.saturate(a, signedness, bit_count);
3017 m.saturate(a.toConst(), signedness, bit_count);
30033018 r.setMetadata(m.positive, m.len);
30043019 }
30053020
30063021 /// r = @popCount(a) with 2s-complement semantics.
30073022 /// r and a may be aliases.
3008 pub fn popCount(r: *Managed, a: Const, bit_count: usize) !void {
3023 pub fn popCount(r: *Managed, a: *const Managed, bit_count: usize) !void {
30093024 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
30103025 var m = r.toMutable();
3011 m.popCount(a, bit_count);
3026 m.popCount(a.toConst(), bit_count);
30123027 r.setMetadata(m.positive, m.len);
30133028 }
30143029};
lib/std/math/big/int_test.zig+173-173
......@@ -166,7 +166,7 @@ test "big.int bitcount + sizeInBaseUpperBound" {
166166 try testing.expect(a.sizeInBaseUpperBound(2) >= 32);
167167 try testing.expect(a.sizeInBaseUpperBound(10) >= 10);
168168
169 try a.shiftLeft(a, 5000);
169 try a.shiftLeft(&a, 5000);
170170 try testing.expect(a.bitCountAbs() == 5032);
171171 try testing.expect(a.sizeInBaseUpperBound(2) >= 5032);
172172 a.setSign(false);
......@@ -486,7 +486,7 @@ test "big.int add single-single" {
486486
487487 var c = try Managed.init(testing.allocator);
488488 defer c.deinit();
489 try c.add(a.toConst(), b.toConst());
489 try c.add(&a, &b);
490490
491491 try testing.expect((try c.to(u32)) == 55);
492492}
......@@ -500,10 +500,10 @@ test "big.int add multi-single" {
500500 var c = try Managed.init(testing.allocator);
501501 defer c.deinit();
502502
503 try c.add(a.toConst(), b.toConst());
503 try c.add(&a, &b);
504504 try testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
505505
506 try c.add(b.toConst(), a.toConst());
506 try c.add(&b, &a);
507507 try testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
508508}
509509
......@@ -517,7 +517,7 @@ test "big.int add multi-multi" {
517517
518518 var c = try Managed.init(testing.allocator);
519519 defer c.deinit();
520 try c.add(a.toConst(), b.toConst());
520 try c.add(&a, &b);
521521
522522 try testing.expect((try c.to(u128)) == op1 + op2);
523523}
......@@ -530,7 +530,7 @@ test "big.int add zero-zero" {
530530
531531 var c = try Managed.init(testing.allocator);
532532 defer c.deinit();
533 try c.add(a.toConst(), b.toConst());
533 try c.add(&a, &b);
534534
535535 try testing.expect((try c.to(u32)) == 0);
536536}
......@@ -542,7 +542,7 @@ test "big.int add alias multi-limb nonzero-zero" {
542542 var b = try Managed.initSet(testing.allocator, 0);
543543 defer b.deinit();
544544
545 try a.add(a.toConst(), b.toConst());
545 try a.add(&a, &b);
546546
547547 try testing.expect((try a.to(u128)) == op1);
548548}
......@@ -560,16 +560,16 @@ test "big.int add sign" {
560560 var neg_two = try Managed.initSet(testing.allocator, -2);
561561 defer neg_two.deinit();
562562
563 try a.add(one.toConst(), two.toConst());
563 try a.add(&one, &two);
564564 try testing.expect((try a.to(i32)) == 3);
565565
566 try a.add(neg_one.toConst(), two.toConst());
566 try a.add(&neg_one, &two);
567567 try testing.expect((try a.to(i32)) == 1);
568568
569 try a.add(one.toConst(), neg_two.toConst());
569 try a.add(&one, &neg_two);
570570 try testing.expect((try a.to(i32)) == -1);
571571
572 try a.add(neg_one.toConst(), neg_two.toConst());
572 try a.add(&neg_one, &neg_two);
573573 try testing.expect((try a.to(i32)) == -3);
574574}
575575
......@@ -579,7 +579,7 @@ test "big.int add scalar" {
579579
580580 var b = try Managed.init(testing.allocator);
581581 defer b.deinit();
582 try b.addScalar(a.toConst(), 5);
582 try b.addScalar(&a, 5);
583583
584584 try testing.expect((try b.to(u32)) == 55);
585585}
......@@ -591,7 +591,7 @@ test "big.int addWrap single-single, unsigned" {
591591 var b = try Managed.initSet(testing.allocator, 10);
592592 defer b.deinit();
593593
594 const wrapped = try a.addWrap(a.toConst(), b.toConst(), .unsigned, 17);
594 const wrapped = try a.addWrap(&a, &b, .unsigned, 17);
595595
596596 try testing.expect(wrapped);
597597 try testing.expect((try a.to(u17)) == 9);
......@@ -604,7 +604,7 @@ test "big.int subWrap single-single, unsigned" {
604604 var b = try Managed.initSet(testing.allocator, maxInt(u17));
605605 defer b.deinit();
606606
607 const wrapped = try a.subWrap(a.toConst(), b.toConst(), .unsigned, 17);
607 const wrapped = try a.subWrap(&a, &b, .unsigned, 17);
608608
609609 try testing.expect(wrapped);
610610 try testing.expect((try a.to(u17)) == 1);
......@@ -617,7 +617,7 @@ test "big.int addWrap multi-multi, unsigned, limb aligned" {
617617 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb));
618618 defer b.deinit();
619619
620 const wrapped = try a.addWrap(a.toConst(), b.toConst(), .unsigned, @bitSizeOf(DoubleLimb));
620 const wrapped = try a.addWrap(&a, &b, .unsigned, @bitSizeOf(DoubleLimb));
621621
622622 try testing.expect(wrapped);
623623 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb) - 1);
......@@ -630,7 +630,7 @@ test "big.int subWrap single-multi, unsigned, limb aligned" {
630630 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb) + 100);
631631 defer b.deinit();
632632
633 const wrapped = try a.subWrap(a.toConst(), b.toConst(), .unsigned, @bitSizeOf(DoubleLimb));
633 const wrapped = try a.subWrap(&a, &b, .unsigned, @bitSizeOf(DoubleLimb));
634634
635635 try testing.expect(wrapped);
636636 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb) - 88);
......@@ -643,7 +643,7 @@ test "big.int addWrap single-single, signed" {
643643 var b = try Managed.initSet(testing.allocator, 1 + 1 + maxInt(u21));
644644 defer b.deinit();
645645
646 const wrapped = try a.addWrap(a.toConst(), b.toConst(), .signed, @bitSizeOf(i21));
646 const wrapped = try a.addWrap(&a, &b, .signed, @bitSizeOf(i21));
647647
648648 try testing.expect(wrapped);
649649 try testing.expect((try a.to(i21)) == minInt(i21));
......@@ -656,7 +656,7 @@ test "big.int subWrap single-single, signed" {
656656 var b = try Managed.initSet(testing.allocator, 1);
657657 defer b.deinit();
658658
659 const wrapped = try a.subWrap(a.toConst(), b.toConst(), .signed, @bitSizeOf(i21));
659 const wrapped = try a.subWrap(&a, &b, .signed, @bitSizeOf(i21));
660660
661661 try testing.expect(wrapped);
662662 try testing.expect((try a.to(i21)) == maxInt(i21));
......@@ -669,7 +669,7 @@ test "big.int addWrap multi-multi, signed, limb aligned" {
669669 var b = try Managed.initSet(testing.allocator, maxInt(SignedDoubleLimb));
670670 defer b.deinit();
671671
672 const wrapped = try a.addWrap(a.toConst(), b.toConst(), .signed, @bitSizeOf(SignedDoubleLimb));
672 const wrapped = try a.addWrap(&a, &b, .signed, @bitSizeOf(SignedDoubleLimb));
673673
674674 try testing.expect(wrapped);
675675 try testing.expect((try a.to(SignedDoubleLimb)) == -2);
......@@ -682,7 +682,7 @@ test "big.int subWrap single-multi, signed, limb aligned" {
682682 var b = try Managed.initSet(testing.allocator, 1);
683683 defer b.deinit();
684684
685 const wrapped = try a.subWrap(a.toConst(), b.toConst(), .signed, @bitSizeOf(SignedDoubleLimb));
685 const wrapped = try a.subWrap(&a, &b, .signed, @bitSizeOf(SignedDoubleLimb));
686686
687687 try testing.expect(wrapped);
688688 try testing.expect((try a.to(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));
......@@ -695,7 +695,7 @@ test "big.int addSat single-single, unsigned" {
695695 var b = try Managed.initSet(testing.allocator, 10);
696696 defer b.deinit();
697697
698 try a.addSat(a.toConst(), b.toConst(), .unsigned, 17);
698 try a.addSat(&a, &b, .unsigned, 17);
699699
700700 try testing.expect((try a.to(u17)) == maxInt(u17));
701701}
......@@ -707,7 +707,7 @@ test "big.int subSat single-single, unsigned" {
707707 var b = try Managed.initSet(testing.allocator, 4000);
708708 defer b.deinit();
709709
710 try a.subSat(a.toConst(), b.toConst(), .unsigned, 17);
710 try a.subSat(&a, &b, .unsigned, 17);
711711
712712 try testing.expect((try a.to(u17)) == 0);
713713}
......@@ -719,7 +719,7 @@ test "big.int addSat multi-multi, unsigned, limb aligned" {
719719 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb));
720720 defer b.deinit();
721721
722 try a.addSat(a.toConst(), b.toConst(), .unsigned, @bitSizeOf(DoubleLimb));
722 try a.addSat(&a, &b, .unsigned, @bitSizeOf(DoubleLimb));
723723
724724 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb));
725725}
......@@ -731,7 +731,7 @@ test "big.int subSat single-multi, unsigned, limb aligned" {
731731 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb) + 100);
732732 defer b.deinit();
733733
734 try a.subSat(a.toConst(), b.toConst(), .unsigned, @bitSizeOf(DoubleLimb));
734 try a.subSat(&a, &b, .unsigned, @bitSizeOf(DoubleLimb));
735735
736736 try testing.expect((try a.to(DoubleLimb)) == 0);
737737}
......@@ -743,7 +743,7 @@ test "big.int addSat single-single, signed" {
743743 var b = try Managed.initSet(testing.allocator, 1);
744744 defer b.deinit();
745745
746 try a.addSat(a.toConst(), b.toConst(), .signed, @bitSizeOf(i14));
746 try a.addSat(&a, &b, .signed, @bitSizeOf(i14));
747747
748748 try testing.expect((try a.to(i14)) == maxInt(i14));
749749}
......@@ -755,7 +755,7 @@ test "big.int subSat single-single, signed" {
755755 var b = try Managed.initSet(testing.allocator, 1);
756756 defer b.deinit();
757757
758 try a.subSat(a.toConst(), b.toConst(), .signed, @bitSizeOf(i21));
758 try a.subSat(&a, &b, .signed, @bitSizeOf(i21));
759759
760760 try testing.expect((try a.to(i21)) == minInt(i21));
761761}
......@@ -767,7 +767,7 @@ test "big.int addSat multi-multi, signed, limb aligned" {
767767 var b = try Managed.initSet(testing.allocator, maxInt(SignedDoubleLimb));
768768 defer b.deinit();
769769
770 try a.addSat(a.toConst(), b.toConst(), .signed, @bitSizeOf(SignedDoubleLimb));
770 try a.addSat(&a, &b, .signed, @bitSizeOf(SignedDoubleLimb));
771771
772772 try testing.expect((try a.to(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));
773773}
......@@ -779,7 +779,7 @@ test "big.int subSat single-multi, signed, limb aligned" {
779779 var b = try Managed.initSet(testing.allocator, 1);
780780 defer b.deinit();
781781
782 try a.subSat(a.toConst(), b.toConst(), .signed, @bitSizeOf(SignedDoubleLimb));
782 try a.subSat(&a, &b, .signed, @bitSizeOf(SignedDoubleLimb));
783783
784784 try testing.expect((try a.to(SignedDoubleLimb)) == minInt(SignedDoubleLimb));
785785}
......@@ -792,7 +792,7 @@ test "big.int sub single-single" {
792792
793793 var c = try Managed.init(testing.allocator);
794794 defer c.deinit();
795 try c.sub(a.toConst(), b.toConst());
795 try c.sub(&a, &b);
796796
797797 try testing.expect((try c.to(u32)) == 45);
798798}
......@@ -805,7 +805,7 @@ test "big.int sub multi-single" {
805805
806806 var c = try Managed.init(testing.allocator);
807807 defer c.deinit();
808 try c.sub(a.toConst(), b.toConst());
808 try c.sub(&a, &b);
809809
810810 try testing.expect((try c.to(Limb)) == maxInt(Limb));
811811}
......@@ -821,7 +821,7 @@ test "big.int sub multi-multi" {
821821
822822 var c = try Managed.init(testing.allocator);
823823 defer c.deinit();
824 try c.sub(a.toConst(), b.toConst());
824 try c.sub(&a, &b);
825825
826826 try testing.expect((try c.to(u128)) == op1 - op2);
827827}
......@@ -834,7 +834,7 @@ test "big.int sub equal" {
834834
835835 var c = try Managed.init(testing.allocator);
836836 defer c.deinit();
837 try c.sub(a.toConst(), b.toConst());
837 try c.sub(&a, &b);
838838
839839 try testing.expect((try c.to(u32)) == 0);
840840}
......@@ -852,19 +852,19 @@ test "big.int sub sign" {
852852 var neg_two = try Managed.initSet(testing.allocator, -2);
853853 defer neg_two.deinit();
854854
855 try a.sub(one.toConst(), two.toConst());
855 try a.sub(&one, &two);
856856 try testing.expect((try a.to(i32)) == -1);
857857
858 try a.sub(neg_one.toConst(), two.toConst());
858 try a.sub(&neg_one, &two);
859859 try testing.expect((try a.to(i32)) == -3);
860860
861 try a.sub(one.toConst(), neg_two.toConst());
861 try a.sub(&one, &neg_two);
862862 try testing.expect((try a.to(i32)) == 3);
863863
864 try a.sub(neg_one.toConst(), neg_two.toConst());
864 try a.sub(&neg_one, &neg_two);
865865 try testing.expect((try a.to(i32)) == 1);
866866
867 try a.sub(neg_two.toConst(), neg_one.toConst());
867 try a.sub(&neg_two, &neg_one);
868868 try testing.expect((try a.to(i32)) == -1);
869869}
870870
......@@ -876,7 +876,7 @@ test "big.int mul single-single" {
876876
877877 var c = try Managed.init(testing.allocator);
878878 defer c.deinit();
879 try c.mul(a.toConst(), b.toConst());
879 try c.mul(&a, &b);
880880
881881 try testing.expect((try c.to(u64)) == 250);
882882}
......@@ -889,7 +889,7 @@ test "big.int mul multi-single" {
889889
890890 var c = try Managed.init(testing.allocator);
891891 defer c.deinit();
892 try c.mul(a.toConst(), b.toConst());
892 try c.mul(&a, &b);
893893
894894 try testing.expect((try c.to(DoubleLimb)) == 2 * maxInt(Limb));
895895}
......@@ -904,7 +904,7 @@ test "big.int mul multi-multi" {
904904
905905 var c = try Managed.init(testing.allocator);
906906 defer c.deinit();
907 try c.mul(a.toConst(), b.toConst());
907 try c.mul(&a, &b);
908908
909909 try testing.expect((try c.to(u256)) == op1 * op2);
910910}
......@@ -915,7 +915,7 @@ test "big.int mul alias r with a" {
915915 var b = try Managed.initSet(testing.allocator, 2);
916916 defer b.deinit();
917917
918 try a.mul(a.toConst(), b.toConst());
918 try a.mul(&a, &b);
919919
920920 try testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
921921}
......@@ -926,7 +926,7 @@ test "big.int mul alias r with b" {
926926 var b = try Managed.initSet(testing.allocator, 2);
927927 defer b.deinit();
928928
929 try a.mul(b.toConst(), a.toConst());
929 try a.mul(&b, &a);
930930
931931 try testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
932932}
......@@ -935,7 +935,7 @@ test "big.int mul alias r with a and b" {
935935 var a = try Managed.initSet(testing.allocator, maxInt(Limb));
936936 defer a.deinit();
937937
938 try a.mul(a.toConst(), a.toConst());
938 try a.mul(&a, &a);
939939
940940 try testing.expect((try a.to(DoubleLimb)) == maxInt(Limb) * maxInt(Limb));
941941}
......@@ -948,7 +948,7 @@ test "big.int mul a*0" {
948948
949949 var c = try Managed.init(testing.allocator);
950950 defer c.deinit();
951 try c.mul(a.toConst(), b.toConst());
951 try c.mul(&a, &b);
952952
953953 try testing.expect((try c.to(u32)) == 0);
954954}
......@@ -961,7 +961,7 @@ test "big.int mul 0*0" {
961961
962962 var c = try Managed.init(testing.allocator);
963963 defer c.deinit();
964 try c.mul(a.toConst(), b.toConst());
964 try c.mul(&a, &b);
965965
966966 try testing.expect((try c.to(u32)) == 0);
967967}
......@@ -981,8 +981,8 @@ test "big.int mul large" {
981981 }
982982 a.setMetadata(true, 50);
983983
984 try b.mul(a.toConst(), a.toConst());
985 try c.sqr(a.toConst());
984 try b.mul(&a, &a);
985 try c.sqr(&a);
986986
987987 try testing.expect(b.eq(c));
988988}
......@@ -995,7 +995,7 @@ test "big.int mulWrap single-single unsigned" {
995995
996996 var c = try Managed.init(testing.allocator);
997997 defer c.deinit();
998 try c.mulWrap(a.toConst(), b.toConst(), .unsigned, 17);
998 try c.mulWrap(&a, &b, .unsigned, 17);
999999
10001000 try testing.expect((try c.to(u17)) == 59836);
10011001}
......@@ -1008,7 +1008,7 @@ test "big.int mulWrap single-single signed" {
10081008
10091009 var c = try Managed.init(testing.allocator);
10101010 defer c.deinit();
1011 try c.mulWrap(a.toConst(), b.toConst(), .signed, 17);
1011 try c.mulWrap(&a, &b, .signed, 17);
10121012
10131013 try testing.expect((try c.to(i17)) == -59836);
10141014}
......@@ -1023,7 +1023,7 @@ test "big.int mulWrap multi-multi unsigned" {
10231023
10241024 var c = try Managed.init(testing.allocator);
10251025 defer c.deinit();
1026 try c.mulWrap(a.toConst(), b.toConst(), .unsigned, 65);
1026 try c.mulWrap(&a, &b, .unsigned, 65);
10271027
10281028 try testing.expect((try c.to(u128)) == (op1 * op2) & ((1 << 65) - 1));
10291029}
......@@ -1036,7 +1036,7 @@ test "big.int mulWrap multi-multi signed" {
10361036
10371037 var c = try Managed.init(testing.allocator);
10381038 defer c.deinit();
1039 try c.mulWrap(a.toConst(), b.toConst(), .signed, @bitSizeOf(SignedDoubleLimb));
1039 try c.mulWrap(&a, &b, .signed, @bitSizeOf(SignedDoubleLimb));
10401040
10411041 try testing.expect((try c.to(SignedDoubleLimb)) == minInt(SignedDoubleLimb) + 2);
10421042}
......@@ -1058,9 +1058,9 @@ test "big.int mulWrap large" {
10581058
10591059 const testbits = @bitSizeOf(Limb) * 64 + 45;
10601060
1061 try b.mulWrap(a.toConst(), a.toConst(), .signed, testbits);
1062 try c.sqr(a.toConst());
1063 try c.truncate(c.toConst(), .signed, testbits);
1061 try b.mulWrap(&a, &a, .signed, testbits);
1062 try c.sqr(&a);
1063 try c.truncate(&c, .signed, testbits);
10641064
10651065 try testing.expect(b.eq(c));
10661066}
......@@ -1075,7 +1075,7 @@ test "big.int div single-half no rem" {
10751075 defer q.deinit();
10761076 var r = try Managed.init(testing.allocator);
10771077 defer r.deinit();
1078 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1078 try Managed.divTrunc(&q, &r, &a, &b);
10791079
10801080 try testing.expect((try q.to(u32)) == 10);
10811081 try testing.expect((try r.to(u32)) == 0);
......@@ -1091,7 +1091,7 @@ test "big.int div single-half with rem" {
10911091 defer q.deinit();
10921092 var r = try Managed.init(testing.allocator);
10931093 defer r.deinit();
1094 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1094 try Managed.divTrunc(&q, &r, &a, &b);
10951095
10961096 try testing.expect((try q.to(u32)) == 9);
10971097 try testing.expect((try r.to(u32)) == 4);
......@@ -1108,7 +1108,7 @@ test "big.int div single-single no rem" {
11081108 defer q.deinit();
11091109 var r = try Managed.init(testing.allocator);
11101110 defer r.deinit();
1111 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1111 try Managed.divTrunc(&q, &r, &a, &b);
11121112
11131113 try testing.expect((try q.to(u32)) == 131072);
11141114 try testing.expect((try r.to(u32)) == 0);
......@@ -1124,7 +1124,7 @@ test "big.int div single-single with rem" {
11241124 defer q.deinit();
11251125 var r = try Managed.init(testing.allocator);
11261126 defer r.deinit();
1127 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1127 try Managed.divTrunc(&q, &r, &a, &b);
11281128
11291129 try testing.expect((try q.to(u64)) == 131072);
11301130 try testing.expect((try r.to(u64)) == 8589934592);
......@@ -1143,7 +1143,7 @@ test "big.int div multi-single no rem" {
11431143 defer q.deinit();
11441144 var r = try Managed.init(testing.allocator);
11451145 defer r.deinit();
1146 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1146 try Managed.divTrunc(&q, &r, &a, &b);
11471147
11481148 try testing.expect((try q.to(u64)) == op1 / op2);
11491149 try testing.expect((try r.to(u64)) == 0);
......@@ -1162,7 +1162,7 @@ test "big.int div multi-single with rem" {
11621162 defer q.deinit();
11631163 var r = try Managed.init(testing.allocator);
11641164 defer r.deinit();
1165 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1165 try Managed.divTrunc(&q, &r, &a, &b);
11661166
11671167 try testing.expect((try q.to(u64)) == op1 / op2);
11681168 try testing.expect((try r.to(u64)) == 3);
......@@ -1181,7 +1181,7 @@ test "big.int div multi>2-single" {
11811181 defer q.deinit();
11821182 var r = try Managed.init(testing.allocator);
11831183 defer r.deinit();
1184 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1184 try Managed.divTrunc(&q, &r, &a, &b);
11851185
11861186 try testing.expect((try q.to(u128)) == op1 / op2);
11871187 try testing.expect((try r.to(u32)) == 0x3e4e);
......@@ -1197,7 +1197,7 @@ test "big.int div single-single q < r" {
11971197 defer q.deinit();
11981198 var r = try Managed.init(testing.allocator);
11991199 defer r.deinit();
1200 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1200 try Managed.divTrunc(&q, &r, &a, &b);
12011201
12021202 try testing.expect((try q.to(u64)) == 0);
12031203 try testing.expect((try r.to(u64)) == 0x0078f432);
......@@ -1213,7 +1213,7 @@ test "big.int div single-single q == r" {
12131213 defer q.deinit();
12141214 var r = try Managed.init(testing.allocator);
12151215 defer r.deinit();
1216 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1216 try Managed.divTrunc(&q, &r, &a, &b);
12171217
12181218 try testing.expect((try q.to(u64)) == 1);
12191219 try testing.expect((try r.to(u64)) == 0);
......@@ -1225,7 +1225,7 @@ test "big.int div q=0 alias" {
12251225 var b = try Managed.initSet(testing.allocator, 10);
12261226 defer b.deinit();
12271227
1228 try Managed.divTrunc(&a, &b, a.toConst(), b.toConst());
1228 try Managed.divTrunc(&a, &b, &a, &b);
12291229
12301230 try testing.expect((try a.to(u64)) == 0);
12311231 try testing.expect((try b.to(u64)) == 3);
......@@ -1243,7 +1243,7 @@ test "big.int div multi-multi q < r" {
12431243 defer q.deinit();
12441244 var r = try Managed.init(testing.allocator);
12451245 defer r.deinit();
1246 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1246 try Managed.divTrunc(&q, &r, &a, &b);
12471247
12481248 try testing.expect((try q.to(u128)) == 0);
12491249 try testing.expect((try r.to(u128)) == op1);
......@@ -1262,7 +1262,7 @@ test "big.int div trunc single-single +/+" {
12621262 defer q.deinit();
12631263 var r = try Managed.init(testing.allocator);
12641264 defer r.deinit();
1265 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1265 try Managed.divTrunc(&q, &r, &a, &b);
12661266
12671267 // n = q * d + r
12681268 // 5 = 1 * 3 + 2
......@@ -1286,7 +1286,7 @@ test "big.int div trunc single-single -/+" {
12861286 defer q.deinit();
12871287 var r = try Managed.init(testing.allocator);
12881288 defer r.deinit();
1289 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1289 try Managed.divTrunc(&q, &r, &a, &b);
12901290
12911291 // n = q * d + r
12921292 // -5 = 1 * -3 - 2
......@@ -1310,7 +1310,7 @@ test "big.int div trunc single-single +/-" {
13101310 defer q.deinit();
13111311 var r = try Managed.init(testing.allocator);
13121312 defer r.deinit();
1313 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1313 try Managed.divTrunc(&q, &r, &a, &b);
13141314
13151315 // n = q * d + r
13161316 // 5 = -1 * -3 + 2
......@@ -1334,7 +1334,7 @@ test "big.int div trunc single-single -/-" {
13341334 defer q.deinit();
13351335 var r = try Managed.init(testing.allocator);
13361336 defer r.deinit();
1337 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1337 try Managed.divTrunc(&q, &r, &a, &b);
13381338
13391339 // n = q * d + r
13401340 // -5 = 1 * -3 - 2
......@@ -1361,7 +1361,7 @@ test "big.int divFloor #10932" {
13611361 var mod = try Managed.init(testing.allocator);
13621362 defer mod.deinit();
13631363
1364 try res.divFloor(&mod, a.toConst(), b.toConst());
1364 try res.divFloor(&mod, &a, &b);
13651365
13661366 const ress = try res.toString(testing.allocator, 16, .lower);
13671367 defer testing.allocator.free(ress);
......@@ -1385,7 +1385,7 @@ test "big.int divFloor #11166" {
13851385 var mod = try Managed.init(testing.allocator);
13861386 defer mod.deinit();
13871387
1388 try res.divFloor(&mod, a.toConst(), b.toConst());
1388 try res.divFloor(&mod, &a, &b);
13891389
13901390 const ress = try res.toString(testing.allocator, 10, .lower);
13911391 defer testing.allocator.free(ress);
......@@ -1409,7 +1409,7 @@ test "big.int gcd #10932" {
14091409 try a.setString(10, "3000000000000000000000000000000000000000000000000000000000000000000000001461501637330902918203684832716283019655932542975000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
14101410 try b.setString(10, "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200001001500000000000000000100000000040000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000003000000000000000000000000000000000000000000000000000058715661000000000000000000000000000000000000023553252000000000180000000000000000000000000000000000000000000000000250000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001005000002000000000000000000000000000000000000000021000000001000000000000000000000000100000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000200000000000000000000004000000000000000000000000000000000000000000000301000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
14111411
1412 try res.gcd(a, b);
1412 try res.gcd(&a, &b);
14131413
14141414 const ress = try res.toString(testing.allocator, 16, .lower);
14151415 defer testing.allocator.free(ress);
......@@ -1429,7 +1429,7 @@ test "big.int bitAnd #10932" {
14291429 try a.setString(10, "154954885951624787839743960731760616696");
14301430 try b.setString(10, "55000000000915215865915724129619485917228346934191537590366734850266784978214506142389798064826139649163838075568111457203909393174933092857416500785632012953993352521899237655507306575657169267399324107627651067352600878339870446048204062696260567762088867991835386857942106708741836433444432529637331429212430394179472179237695833247299409249810963487516399177133175950185719220422442438098353430605822151595560743492661038899294517012784306863064670126197566982968906306814338148792888550378533207318063660581924736840687332023636827401670268933229183389040490792300121030647791095178823932734160000000000000000000000000000000000000555555550000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
14311431
1432 try res.bitAnd(a, b);
1432 try res.bitAnd(&a, &b);
14331433
14341434 try testing.expect((try res.to(i32)) == 0);
14351435}
......@@ -1447,7 +1447,7 @@ test "big.int div floor single-single +/+" {
14471447 defer q.deinit();
14481448 var r = try Managed.init(testing.allocator);
14491449 defer r.deinit();
1450 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());
1450 try Managed.divFloor(&q, &r, &a, &b);
14511451
14521452 // n = q * d + r
14531453 // 5 = 1 * 3 + 2
......@@ -1471,7 +1471,7 @@ test "big.int div floor single-single -/+" {
14711471 defer q.deinit();
14721472 var r = try Managed.init(testing.allocator);
14731473 defer r.deinit();
1474 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());
1474 try Managed.divFloor(&q, &r, &a, &b);
14751475
14761476 // n = q * d + r
14771477 // -5 = -2 * 3 + 1
......@@ -1495,7 +1495,7 @@ test "big.int div floor single-single +/-" {
14951495 defer q.deinit();
14961496 var r = try Managed.init(testing.allocator);
14971497 defer r.deinit();
1498 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());
1498 try Managed.divFloor(&q, &r, &a, &b);
14991499
15001500 // n = q * d + r
15011501 // 5 = -2 * -3 - 1
......@@ -1519,7 +1519,7 @@ test "big.int div floor single-single -/-" {
15191519 defer q.deinit();
15201520 var r = try Managed.init(testing.allocator);
15211521 defer r.deinit();
1522 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());
1522 try Managed.divFloor(&q, &r, &a, &b);
15231523
15241524 // n = q * d + r
15251525 // -5 = 2 * -3 + 1
......@@ -1543,7 +1543,7 @@ test "big.int div floor no remainder negative quotient" {
15431543 defer q.deinit();
15441544 var r = try Managed.init(testing.allocator);
15451545 defer r.deinit();
1546 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());
1546 try Managed.divFloor(&q, &r, &a, &b);
15471547
15481548 try testing.expect((try q.to(i32)) == -0x80000000);
15491549 try testing.expect((try r.to(i32)) == 0);
......@@ -1562,7 +1562,7 @@ test "big.int div floor negative close to zero" {
15621562 defer q.deinit();
15631563 var r = try Managed.init(testing.allocator);
15641564 defer r.deinit();
1565 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());
1565 try Managed.divFloor(&q, &r, &a, &b);
15661566
15671567 try testing.expect((try q.to(i32)) == -1);
15681568 try testing.expect((try r.to(i32)) == 10);
......@@ -1581,7 +1581,7 @@ test "big.int div floor positive close to zero" {
15811581 defer q.deinit();
15821582 var r = try Managed.init(testing.allocator);
15831583 defer r.deinit();
1584 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());
1584 try Managed.divFloor(&q, &r, &a, &b);
15851585
15861586 try testing.expect((try q.to(i32)) == 0);
15871587 try testing.expect((try r.to(i32)) == 10);
......@@ -1597,7 +1597,7 @@ test "big.int div multi-multi with rem" {
15971597 defer q.deinit();
15981598 var r = try Managed.init(testing.allocator);
15991599 defer r.deinit();
1600 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1600 try Managed.divTrunc(&q, &r, &a, &b);
16011601
16021602 try testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
16031603 try testing.expect((try r.to(u128)) == 0x28de0acacd806823638);
......@@ -1613,7 +1613,7 @@ test "big.int div multi-multi no rem" {
16131613 defer q.deinit();
16141614 var r = try Managed.init(testing.allocator);
16151615 defer r.deinit();
1616 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1616 try Managed.divTrunc(&q, &r, &a, &b);
16171617
16181618 try testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
16191619 try testing.expect((try r.to(u128)) == 0);
......@@ -1629,7 +1629,7 @@ test "big.int div multi-multi (2 branch)" {
16291629 defer q.deinit();
16301630 var r = try Managed.init(testing.allocator);
16311631 defer r.deinit();
1632 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1632 try Managed.divTrunc(&q, &r, &a, &b);
16331633
16341634 try testing.expect((try q.to(u128)) == 0x10000000000000000);
16351635 try testing.expect((try r.to(u128)) == 0x44444443444444431111111111111111);
......@@ -1645,7 +1645,7 @@ test "big.int div multi-multi (3.1/3.3 branch)" {
16451645 defer q.deinit();
16461646 var r = try Managed.init(testing.allocator);
16471647 defer r.deinit();
1648 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1648 try Managed.divTrunc(&q, &r, &a, &b);
16491649
16501650 try testing.expect((try q.to(u128)) == 0xfffffffffffffffffff);
16511651 try testing.expect((try r.to(u256)) == 0x1111111111111111111110b12222222222222222282);
......@@ -1661,7 +1661,7 @@ test "big.int div multi-single zero-limb trailing" {
16611661 defer q.deinit();
16621662 var r = try Managed.init(testing.allocator);
16631663 defer r.deinit();
1664 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1664 try Managed.divTrunc(&q, &r, &a, &b);
16651665
16661666 var expected = try Managed.initSet(testing.allocator, 0x6000000000000000000000000000000000000000000000000);
16671667 defer expected.deinit();
......@@ -1679,7 +1679,7 @@ test "big.int div multi-multi zero-limb trailing (with rem)" {
16791679 defer q.deinit();
16801680 var r = try Managed.init(testing.allocator);
16811681 defer r.deinit();
1682 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1682 try Managed.divTrunc(&q, &r, &a, &b);
16831683
16841684 try testing.expect((try q.to(u128)) == 0x10000000000000000);
16851685
......@@ -1698,7 +1698,7 @@ test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-li
16981698 defer q.deinit();
16991699 var r = try Managed.init(testing.allocator);
17001700 defer r.deinit();
1701 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1701 try Managed.divTrunc(&q, &r, &a, &b);
17021702
17031703 try testing.expect((try q.to(u128)) == 0x1);
17041704
......@@ -1717,7 +1717,7 @@ test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-li
17171717 defer q.deinit();
17181718 var r = try Managed.init(testing.allocator);
17191719 defer r.deinit();
1720 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1720 try Managed.divTrunc(&q, &r, &a, &b);
17211721
17221722 const qs = try q.toString(testing.allocator, 16, .lower);
17231723 defer testing.allocator.free(qs);
......@@ -1741,7 +1741,7 @@ test "big.int div multi-multi fuzz case #1" {
17411741 defer q.deinit();
17421742 var r = try Managed.init(testing.allocator);
17431743 defer r.deinit();
1744 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1744 try Managed.divTrunc(&q, &r, &a, &b);
17451745
17461746 const qs = try q.toString(testing.allocator, 16, .lower);
17471747 defer testing.allocator.free(qs);
......@@ -1765,7 +1765,7 @@ test "big.int div multi-multi fuzz case #2" {
17651765 defer q.deinit();
17661766 var r = try Managed.init(testing.allocator);
17671767 defer r.deinit();
1768 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());
1768 try Managed.divTrunc(&q, &r, &a, &b);
17691769
17701770 const qs = try q.toString(testing.allocator, 16, .lower);
17711771 defer testing.allocator.free(qs);
......@@ -1780,7 +1780,7 @@ test "big.int truncate single unsigned" {
17801780 var a = try Managed.initSet(testing.allocator, maxInt(u47));
17811781 defer a.deinit();
17821782
1783 try a.truncate(a.toConst(), .unsigned, 17);
1783 try a.truncate(&a, .unsigned, 17);
17841784
17851785 try testing.expect((try a.to(u17)) == maxInt(u17));
17861786}
......@@ -1789,7 +1789,7 @@ test "big.int truncate single signed" {
17891789 var a = try Managed.initSet(testing.allocator, 0x1_0000);
17901790 defer a.deinit();
17911791
1792 try a.truncate(a.toConst(), .signed, 17);
1792 try a.truncate(&a, .signed, 17);
17931793
17941794 try testing.expect((try a.to(i17)) == minInt(i17));
17951795}
......@@ -1798,7 +1798,7 @@ test "big.int truncate multi to single unsigned" {
17981798 var a = try Managed.initSet(testing.allocator, (maxInt(Limb) + 1) | 0x1234_5678_9ABC_DEF0);
17991799 defer a.deinit();
18001800
1801 try a.truncate(a.toConst(), .unsigned, 27);
1801 try a.truncate(&a, .unsigned, 27);
18021802
18031803 try testing.expect((try a.to(u27)) == 0x2BC_DEF0);
18041804}
......@@ -1807,7 +1807,7 @@ test "big.int truncate multi to single signed" {
18071807 var a = try Managed.initSet(testing.allocator, maxInt(Limb) << 10);
18081808 defer a.deinit();
18091809
1810 try a.truncate(a.toConst(), .signed, @bitSizeOf(i11));
1810 try a.truncate(&a, .signed, @bitSizeOf(i11));
18111811
18121812 try testing.expect((try a.to(i11)) == minInt(i11));
18131813}
......@@ -1819,7 +1819,7 @@ test "big.int truncate multi to multi unsigned" {
18191819 var a = try Managed.initSet(testing.allocator, maxInt(SignedDoubleLimb));
18201820 defer a.deinit();
18211821
1822 try a.truncate(a.toConst(), .unsigned, bits - 1);
1822 try a.truncate(&a, .unsigned, bits - 1);
18231823
18241824 try testing.expect((try a.to(Int)) == maxInt(Int));
18251825}
......@@ -1828,7 +1828,7 @@ test "big.int truncate multi to multi signed" {
18281828 var a = try Managed.initSet(testing.allocator, 3 << @bitSizeOf(Limb));
18291829 defer a.deinit();
18301830
1831 try a.truncate(a.toConst(), .signed, @bitSizeOf(Limb) + 1);
1831 try a.truncate(&a, .signed, @bitSizeOf(Limb) + 1);
18321832
18331833 try testing.expect((try a.to(std.meta.Int(.signed, @bitSizeOf(Limb) + 1))) == -1 << @bitSizeOf(Limb));
18341834}
......@@ -1837,7 +1837,7 @@ test "big.int truncate negative multi to single" {
18371837 var a = try Managed.initSet(testing.allocator, -@as(SignedDoubleLimb, maxInt(Limb) + 1));
18381838 defer a.deinit();
18391839
1840 try a.truncate(a.toConst(), .signed, @bitSizeOf(i17));
1840 try a.truncate(&a, .signed, @bitSizeOf(i17));
18411841
18421842 try testing.expect((try a.to(i17)) == 0);
18431843}
......@@ -1845,11 +1845,11 @@ test "big.int truncate negative multi to single" {
18451845test "big.int truncate multi unsigned many" {
18461846 var a = try Managed.initSet(testing.allocator, 1);
18471847 defer a.deinit();
1848 try a.shiftLeft(a, 1023);
1848 try a.shiftLeft(&a, 1023);
18491849
18501850 var b = try Managed.init(testing.allocator);
18511851 defer b.deinit();
1852 try b.truncate(a.toConst(), .signed, @bitSizeOf(i1));
1852 try b.truncate(&a, .signed, @bitSizeOf(i1));
18531853
18541854 try testing.expect((try b.to(i1)) == 0);
18551855}
......@@ -1858,7 +1858,7 @@ test "big.int saturate single signed positive" {
18581858 var a = try Managed.initSet(testing.allocator, 0xBBBB_BBBB);
18591859 defer a.deinit();
18601860
1861 try a.saturate(a.toConst(), .signed, 17);
1861 try a.saturate(&a, .signed, 17);
18621862
18631863 try testing.expect((try a.to(i17)) == maxInt(i17));
18641864}
......@@ -1867,7 +1867,7 @@ test "big.int saturate single signed negative" {
18671867 var a = try Managed.initSet(testing.allocator, -1_234_567);
18681868 defer a.deinit();
18691869
1870 try a.saturate(a.toConst(), .signed, 17);
1870 try a.saturate(&a, .signed, 17);
18711871
18721872 try testing.expect((try a.to(i17)) == minInt(i17));
18731873}
......@@ -1876,7 +1876,7 @@ test "big.int saturate single signed" {
18761876 var a = try Managed.initSet(testing.allocator, maxInt(i17) - 1);
18771877 defer a.deinit();
18781878
1879 try a.saturate(a.toConst(), .signed, 17);
1879 try a.saturate(&a, .signed, 17);
18801880
18811881 try testing.expect((try a.to(i17)) == maxInt(i17) - 1);
18821882}
......@@ -1885,7 +1885,7 @@ test "big.int saturate multi signed" {
18851885 var a = try Managed.initSet(testing.allocator, maxInt(Limb) << @bitSizeOf(SignedDoubleLimb));
18861886 defer a.deinit();
18871887
1888 try a.saturate(a.toConst(), .signed, @bitSizeOf(SignedDoubleLimb));
1888 try a.saturate(&a, .signed, @bitSizeOf(SignedDoubleLimb));
18891889
18901890 try testing.expect((try a.to(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));
18911891}
......@@ -1894,7 +1894,7 @@ test "big.int saturate single unsigned" {
18941894 var a = try Managed.initSet(testing.allocator, 0xFEFE_FEFE);
18951895 defer a.deinit();
18961896
1897 try a.saturate(a.toConst(), .unsigned, 23);
1897 try a.saturate(&a, .unsigned, 23);
18981898
18991899 try testing.expect((try a.to(u23)) == maxInt(u23));
19001900}
......@@ -1903,7 +1903,7 @@ test "big.int saturate multi unsigned zero" {
19031903 var a = try Managed.initSet(testing.allocator, -1);
19041904 defer a.deinit();
19051905
1906 try a.saturate(a.toConst(), .unsigned, @bitSizeOf(DoubleLimb));
1906 try a.saturate(&a, .unsigned, @bitSizeOf(DoubleLimb));
19071907
19081908 try testing.expect(a.eqZero());
19091909}
......@@ -1912,7 +1912,7 @@ test "big.int saturate multi unsigned" {
19121912 var a = try Managed.initSet(testing.allocator, maxInt(Limb) << @bitSizeOf(DoubleLimb));
19131913 defer a.deinit();
19141914
1915 try a.saturate(a.toConst(), .unsigned, @bitSizeOf(DoubleLimb));
1915 try a.saturate(&a, .unsigned, @bitSizeOf(DoubleLimb));
19161916
19171917 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb));
19181918}
......@@ -1920,7 +1920,7 @@ test "big.int saturate multi unsigned" {
19201920test "big.int shift-right single" {
19211921 var a = try Managed.initSet(testing.allocator, 0xffff0000);
19221922 defer a.deinit();
1923 try a.shiftRight(a, 16);
1923 try a.shiftRight(&a, 16);
19241924
19251925 try testing.expect((try a.to(u32)) == 0xffff);
19261926}
......@@ -1928,21 +1928,21 @@ test "big.int shift-right single" {
19281928test "big.int shift-right multi" {
19291929 var a = try Managed.initSet(testing.allocator, 0xffff0000eeee1111dddd2222cccc3333);
19301930 defer a.deinit();
1931 try a.shiftRight(a, 67);
1931 try a.shiftRight(&a, 67);
19321932
19331933 try testing.expect((try a.to(u64)) == 0x1fffe0001dddc222);
19341934
19351935 try a.set(0xffff0000eeee1111dddd2222cccc3333);
1936 try a.shiftRight(a, 63);
1937 try a.shiftRight(a, 63);
1938 try a.shiftRight(a, 2);
1936 try a.shiftRight(&a, 63);
1937 try a.shiftRight(&a, 63);
1938 try a.shiftRight(&a, 2);
19391939 try testing.expect(a.eqZero());
19401940}
19411941
19421942test "big.int shift-left single" {
19431943 var a = try Managed.initSet(testing.allocator, 0xffff);
19441944 defer a.deinit();
1945 try a.shiftLeft(a, 16);
1945 try a.shiftLeft(&a, 16);
19461946
19471947 try testing.expect((try a.to(u64)) == 0xffff0000);
19481948}
......@@ -1950,7 +1950,7 @@ test "big.int shift-left single" {
19501950test "big.int shift-left multi" {
19511951 var a = try Managed.initSet(testing.allocator, 0x1fffe0001dddc222);
19521952 defer a.deinit();
1953 try a.shiftLeft(a, 67);
1953 try a.shiftLeft(&a, 67);
19541954
19551955 try testing.expect((try a.to(u128)) == 0xffff0000eeee11100000000000000000);
19561956}
......@@ -1961,12 +1961,12 @@ test "big.int shift-right negative" {
19611961
19621962 var arg = try Managed.initSet(testing.allocator, -20);
19631963 defer arg.deinit();
1964 try a.shiftRight(arg, 2);
1964 try a.shiftRight(&arg, 2);
19651965 try testing.expect((try a.to(i32)) == -20 >> 2);
19661966
19671967 var arg2 = try Managed.initSet(testing.allocator, -5);
19681968 defer arg2.deinit();
1969 try a.shiftRight(arg2, 10);
1969 try a.shiftRight(&arg2, 10);
19701970 try testing.expect((try a.to(i32)) == -5 >> 10);
19711971}
19721972
......@@ -1976,14 +1976,14 @@ test "big.int shift-left negative" {
19761976
19771977 var arg = try Managed.initSet(testing.allocator, -10);
19781978 defer arg.deinit();
1979 try a.shiftRight(arg, 1232);
1979 try a.shiftRight(&arg, 1232);
19801980 try testing.expect((try a.to(i32)) == -10 >> 1232);
19811981}
19821982
19831983test "big.int sat shift-left simple unsigned" {
19841984 var a = try Managed.initSet(testing.allocator, 0xffff);
19851985 defer a.deinit();
1986 try a.shiftLeftSat(a, 16, .unsigned, 21);
1986 try a.shiftLeftSat(&a, 16, .unsigned, 21);
19871987
19881988 try testing.expect((try a.to(u64)) == 0x1fffff);
19891989}
......@@ -1991,7 +1991,7 @@ test "big.int sat shift-left simple unsigned" {
19911991test "big.int sat shift-left simple unsigned no sat" {
19921992 var a = try Managed.initSet(testing.allocator, 1);
19931993 defer a.deinit();
1994 try a.shiftLeftSat(a, 16, .unsigned, 21);
1994 try a.shiftLeftSat(&a, 16, .unsigned, 21);
19951995
19961996 try testing.expect((try a.to(u64)) == 0x10000);
19971997}
......@@ -1999,7 +1999,7 @@ test "big.int sat shift-left simple unsigned no sat" {
19991999test "big.int sat shift-left multi unsigned" {
20002000 var a = try Managed.initSet(testing.allocator, 16);
20012001 defer a.deinit();
2002 try a.shiftLeftSat(a, @bitSizeOf(DoubleLimb) - 3, .unsigned, @bitSizeOf(DoubleLimb) - 1);
2002 try a.shiftLeftSat(&a, @bitSizeOf(DoubleLimb) - 3, .unsigned, @bitSizeOf(DoubleLimb) - 1);
20032003
20042004 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb) >> 1);
20052005}
......@@ -2007,7 +2007,7 @@ test "big.int sat shift-left multi unsigned" {
20072007test "big.int sat shift-left unsigned shift > bitcount" {
20082008 var a = try Managed.initSet(testing.allocator, 1);
20092009 defer a.deinit();
2010 try a.shiftLeftSat(a, 10, .unsigned, 10);
2010 try a.shiftLeftSat(&a, 10, .unsigned, 10);
20112011
20122012 try testing.expect((try a.to(u10)) == maxInt(u10));
20132013}
......@@ -2015,7 +2015,7 @@ test "big.int sat shift-left unsigned shift > bitcount" {
20152015test "big.int sat shift-left unsigned zero" {
20162016 var a = try Managed.initSet(testing.allocator, 0);
20172017 defer a.deinit();
2018 try a.shiftLeftSat(a, 1, .unsigned, 0);
2018 try a.shiftLeftSat(&a, 1, .unsigned, 0);
20192019
20202020 try testing.expect((try a.to(u64)) == 0);
20212021}
......@@ -2023,7 +2023,7 @@ test "big.int sat shift-left unsigned zero" {
20232023test "big.int sat shift-left unsigned negative" {
20242024 var a = try Managed.initSet(testing.allocator, -100);
20252025 defer a.deinit();
2026 try a.shiftLeftSat(a, 0, .unsigned, 0);
2026 try a.shiftLeftSat(&a, 0, .unsigned, 0);
20272027
20282028 try testing.expect((try a.to(u64)) == 0);
20292029}
......@@ -2031,7 +2031,7 @@ test "big.int sat shift-left unsigned negative" {
20312031test "big.int sat shift-left signed simple negative" {
20322032 var a = try Managed.initSet(testing.allocator, -100);
20332033 defer a.deinit();
2034 try a.shiftLeftSat(a, 3, .signed, 10);
2034 try a.shiftLeftSat(&a, 3, .signed, 10);
20352035
20362036 try testing.expect((try a.to(i10)) == minInt(i10));
20372037}
......@@ -2039,7 +2039,7 @@ test "big.int sat shift-left signed simple negative" {
20392039test "big.int sat shift-left signed simple positive" {
20402040 var a = try Managed.initSet(testing.allocator, 100);
20412041 defer a.deinit();
2042 try a.shiftLeftSat(a, 3, .signed, 10);
2042 try a.shiftLeftSat(&a, 3, .signed, 10);
20432043
20442044 try testing.expect((try a.to(i10)) == maxInt(i10));
20452045}
......@@ -2050,7 +2050,7 @@ test "big.int sat shift-left signed multi positive" {
20502050
20512051 var a = try Managed.initSet(testing.allocator, x);
20522052 defer a.deinit();
2053 try a.shiftLeftSat(a, shift, .signed, @bitSizeOf(SignedDoubleLimb));
2053 try a.shiftLeftSat(&a, shift, .signed, @bitSizeOf(SignedDoubleLimb));
20542054
20552055 try testing.expect((try a.to(SignedDoubleLimb)) == @as(SignedDoubleLimb, x) <<| shift);
20562056}
......@@ -2061,7 +2061,7 @@ test "big.int sat shift-left signed multi negative" {
20612061
20622062 var a = try Managed.initSet(testing.allocator, x);
20632063 defer a.deinit();
2064 try a.shiftLeftSat(a, shift, .signed, @bitSizeOf(SignedDoubleLimb));
2064 try a.shiftLeftSat(&a, shift, .signed, @bitSizeOf(SignedDoubleLimb));
20652065
20662066 try testing.expect((try a.to(SignedDoubleLimb)) == @as(SignedDoubleLimb, x) <<| shift);
20672067}
......@@ -2070,7 +2070,7 @@ test "big.int bitNotWrap unsigned simple" {
20702070 var a = try Managed.initSet(testing.allocator, 123);
20712071 defer a.deinit();
20722072
2073 try a.bitNotWrap(a, .unsigned, 10);
2073 try a.bitNotWrap(&a, .unsigned, 10);
20742074
20752075 try testing.expect((try a.to(u10)) == ~@as(u10, 123));
20762076}
......@@ -2079,7 +2079,7 @@ test "big.int bitNotWrap unsigned multi" {
20792079 var a = try Managed.initSet(testing.allocator, 0);
20802080 defer a.deinit();
20812081
2082 try a.bitNotWrap(a, .unsigned, @bitSizeOf(DoubleLimb));
2082 try a.bitNotWrap(&a, .unsigned, @bitSizeOf(DoubleLimb));
20832083
20842084 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb));
20852085}
......@@ -2088,7 +2088,7 @@ test "big.int bitNotWrap signed simple" {
20882088 var a = try Managed.initSet(testing.allocator, -456);
20892089 defer a.deinit();
20902090
2091 try a.bitNotWrap(a, .signed, 11);
2091 try a.bitNotWrap(&a, .signed, 11);
20922092
20932093 try testing.expect((try a.to(i11)) == ~@as(i11, -456));
20942094}
......@@ -2097,7 +2097,7 @@ test "big.int bitNotWrap signed multi" {
20972097 var a = try Managed.initSet(testing.allocator, 0);
20982098 defer a.deinit();
20992099
2100 try a.bitNotWrap(a, .signed, @bitSizeOf(SignedDoubleLimb));
2100 try a.bitNotWrap(&a, .signed, @bitSizeOf(SignedDoubleLimb));
21012101
21022102 try testing.expect((try a.to(SignedDoubleLimb)) == -1);
21032103}
......@@ -2108,7 +2108,7 @@ test "big.int bitwise and simple" {
21082108 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
21092109 defer b.deinit();
21102110
2111 try a.bitAnd(a, b);
2111 try a.bitAnd(&a, &b);
21122112
21132113 try testing.expect((try a.to(u64)) == 0xeeeeeeee00000000);
21142114}
......@@ -2119,7 +2119,7 @@ test "big.int bitwise and multi-limb" {
21192119 var b = try Managed.initSet(testing.allocator, maxInt(Limb));
21202120 defer b.deinit();
21212121
2122 try a.bitAnd(a, b);
2122 try a.bitAnd(&a, &b);
21232123
21242124 try testing.expect((try a.to(u128)) == 0);
21252125}
......@@ -2130,7 +2130,7 @@ test "big.int bitwise and negative-positive simple" {
21302130 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
21312131 defer b.deinit();
21322132
2133 try a.bitAnd(a, b);
2133 try a.bitAnd(&a, &b);
21342134
21352135 try testing.expect((try a.to(u64)) == 0x22222222);
21362136}
......@@ -2141,7 +2141,7 @@ test "big.int bitwise and negative-positive multi-limb" {
21412141 var b = try Managed.initSet(testing.allocator, maxInt(Limb));
21422142 defer b.deinit();
21432143
2144 try a.bitAnd(a, b);
2144 try a.bitAnd(&a, &b);
21452145
21462146 try testing.expect(a.eqZero());
21472147}
......@@ -2152,7 +2152,7 @@ test "big.int bitwise and positive-negative simple" {
21522152 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);
21532153 defer b.deinit();
21542154
2155 try a.bitAnd(a, b);
2155 try a.bitAnd(&a, &b);
21562156
21572157 try testing.expect((try a.to(u64)) == 0x1111111111111110);
21582158}
......@@ -2163,7 +2163,7 @@ test "big.int bitwise and positive-negative multi-limb" {
21632163 var b = try Managed.initSet(testing.allocator, -maxInt(Limb) - 1);
21642164 defer b.deinit();
21652165
2166 try a.bitAnd(a, b);
2166 try a.bitAnd(&a, &b);
21672167
21682168 try testing.expect(a.eqZero());
21692169}
......@@ -2174,7 +2174,7 @@ test "big.int bitwise and negative-negative simple" {
21742174 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);
21752175 defer b.deinit();
21762176
2177 try a.bitAnd(a, b);
2177 try a.bitAnd(&a, &b);
21782178
21792179 try testing.expect((try a.to(i128)) == -0xffffffff33333332);
21802180}
......@@ -2185,7 +2185,7 @@ test "big.int bitwise and negative-negative multi-limb" {
21852185 var b = try Managed.initSet(testing.allocator, -maxInt(Limb) - 2);
21862186 defer b.deinit();
21872187
2188 try a.bitAnd(a, b);
2188 try a.bitAnd(&a, &b);
21892189
21902190 try testing.expect((try a.to(i128)) == -maxInt(Limb) * 2 - 2);
21912191}
......@@ -2196,7 +2196,7 @@ test "big.int bitwise and negative overflow" {
21962196 var b = try Managed.initSet(testing.allocator, -2);
21972197 defer b.deinit();
21982198
2199 try a.bitAnd(a, b);
2199 try a.bitAnd(&a, &b);
22002200
22012201 try testing.expect((try a.to(SignedDoubleLimb)) == -maxInt(Limb) - 1);
22022202}
......@@ -2207,7 +2207,7 @@ test "big.int bitwise xor simple" {
22072207 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
22082208 defer b.deinit();
22092209
2210 try a.bitXor(a, b);
2210 try a.bitXor(&a, &b);
22112211
22122212 try testing.expect((try a.to(u64)) == 0x1111111133333333);
22132213}
......@@ -2218,7 +2218,7 @@ test "big.int bitwise xor multi-limb" {
22182218 var b = try Managed.initSet(testing.allocator, maxInt(Limb));
22192219 defer b.deinit();
22202220
2221 try a.bitXor(a, b);
2221 try a.bitXor(&a, &b);
22222222
22232223 try testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) ^ maxInt(Limb));
22242224}
......@@ -2229,7 +2229,7 @@ test "big.int bitwise xor single negative simple" {
22292229 var b = try Managed.initSet(testing.allocator, -0x45fd3acef9191fad);
22302230 defer b.deinit();
22312231
2232 try a.bitXor(a, b);
2232 try a.bitXor(&a, &b);
22332233
22342234 try testing.expect((try a.to(i64)) == -0x2efed94fcb932ef9);
22352235}
......@@ -2240,7 +2240,7 @@ test "big.int bitwise xor single negative zero" {
22402240 var b = try Managed.initSet(testing.allocator, -0);
22412241 defer b.deinit();
22422242
2243 try a.bitXor(a, b);
2243 try a.bitXor(&a, &b);
22442244
22452245 try testing.expect(a.eqZero());
22462246}
......@@ -2251,7 +2251,7 @@ test "big.int bitwise xor single negative multi-limb" {
22512251 var b = try Managed.initSet(testing.allocator, 0xf2194e7d1c855272a997fcde16f6d5a8);
22522252 defer b.deinit();
22532253
2254 try a.bitXor(a, b);
2254 try a.bitXor(&a, &b);
22552255
22562256 try testing.expect((try a.to(i128)) == -0x6a50889abd8834a24db1f19650d3999a);
22572257}
......@@ -2262,7 +2262,7 @@ test "big.int bitwise xor single negative overflow" {
22622262 var b = try Managed.initSet(testing.allocator, -1);
22632263 defer b.deinit();
22642264
2265 try a.bitXor(a, b);
2265 try a.bitXor(&a, &b);
22662266
22672267 try testing.expect((try a.to(SignedDoubleLimb)) == -(maxInt(Limb) + 1));
22682268}
......@@ -2273,7 +2273,7 @@ test "big.int bitwise xor double negative simple" {
22732273 var b = try Managed.initSet(testing.allocator, -0x4dd4fa576f3046ac);
22742274 defer b.deinit();
22752275
2276 try a.bitXor(a, b);
2276 try a.bitXor(&a, &b);
22772277
22782278 try testing.expect((try a.to(u64)) == 0xc39c47081a6eb759);
22792279}
......@@ -2284,7 +2284,7 @@ test "big.int bitwise xor double negative multi-limb" {
22842284 var b = try Managed.initSet(testing.allocator, -0xcb07736a7b62289c78d967c3985eebeb);
22852285 defer b.deinit();
22862286
2287 try a.bitXor(a, b);
2287 try a.bitXor(&a, &b);
22882288
22892289 try testing.expect((try a.to(u128)) == 0xa3492ec28e62c410dff92bf0549bf771);
22902290}
......@@ -2295,7 +2295,7 @@ test "big.int bitwise or simple" {
22952295 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
22962296 defer b.deinit();
22972297
2298 try a.bitOr(a, b);
2298 try a.bitOr(&a, &b);
22992299
23002300 try testing.expect((try a.to(u64)) == 0xffffffff33333333);
23012301}
......@@ -2306,7 +2306,7 @@ test "big.int bitwise or multi-limb" {
23062306 var b = try Managed.initSet(testing.allocator, maxInt(Limb));
23072307 defer b.deinit();
23082308
2309 try a.bitOr(a, b);
2309 try a.bitOr(&a, &b);
23102310
23112311 // TODO: big.int.cpp or is wrong on multi-limb.
23122312 try testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) + maxInt(Limb));
......@@ -2318,7 +2318,7 @@ test "big.int bitwise or negative-positive simple" {
23182318 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
23192319 defer b.deinit();
23202320
2321 try a.bitOr(a, b);
2321 try a.bitOr(&a, &b);
23222322
23232323 try testing.expect((try a.to(i64)) == -0x1111111111111111);
23242324}
......@@ -2329,7 +2329,7 @@ test "big.int bitwise or negative-positive multi-limb" {
23292329 var b = try Managed.initSet(testing.allocator, 1);
23302330 defer b.deinit();
23312331
2332 try a.bitOr(a, b);
2332 try a.bitOr(&a, &b);
23332333
23342334 try testing.expect((try a.to(SignedDoubleLimb)) == -maxInt(Limb));
23352335}
......@@ -2340,7 +2340,7 @@ test "big.int bitwise or positive-negative simple" {
23402340 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);
23412341 defer b.deinit();
23422342
2343 try a.bitOr(a, b);
2343 try a.bitOr(&a, &b);
23442344
23452345 try testing.expect((try a.to(i64)) == -0x22222221);
23462346}
......@@ -2351,7 +2351,7 @@ test "big.int bitwise or positive-negative multi-limb" {
23512351 var b = try Managed.initSet(testing.allocator, -1);
23522352 defer b.deinit();
23532353
2354 try a.bitOr(a, b);
2354 try a.bitOr(&a, &b);
23552355
23562356 try testing.expect((try a.to(SignedDoubleLimb)) == -1);
23572357}
......@@ -2362,7 +2362,7 @@ test "big.int bitwise or negative-negative simple" {
23622362 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);
23632363 defer b.deinit();
23642364
2365 try a.bitOr(a, b);
2365 try a.bitOr(&a, &b);
23662366
23672367 try testing.expect((try a.to(i128)) == -0xeeeeeeee00000001);
23682368}
......@@ -2373,7 +2373,7 @@ test "big.int bitwise or negative-negative multi-limb" {
23732373 var b = try Managed.initSet(testing.allocator, -maxInt(Limb));
23742374 defer b.deinit();
23752375
2376 try a.bitOr(a, b);
2376 try a.bitOr(&a, &b);
23772377
23782378 try testing.expect((try a.to(SignedDoubleLimb)) == -maxInt(Limb));
23792379}
......@@ -2384,7 +2384,7 @@ test "big.int var args" {
23842384
23852385 var b = try Managed.initSet(testing.allocator, 6);
23862386 defer b.deinit();
2387 try a.add(a.toConst(), b.toConst());
2387 try a.add(&a, &b);
23882388 try testing.expect((try a.to(u64)) == 11);
23892389
23902390 var c = try Managed.initSet(testing.allocator, 11);
......@@ -2404,7 +2404,7 @@ test "big.int gcd non-one small" {
24042404 var r = try Managed.init(testing.allocator);
24052405 defer r.deinit();
24062406
2407 try r.gcd(a, b);
2407 try r.gcd(&a, &b);
24082408
24092409 try testing.expect((try r.to(u32)) == 1);
24102410}
......@@ -2417,7 +2417,7 @@ test "big.int gcd non-one small" {
24172417 var r = try Managed.init(testing.allocator);
24182418 defer r.deinit();
24192419
2420 try r.gcd(a, b);
2420 try r.gcd(&a, &b);
24212421
24222422 try testing.expect((try r.to(u32)) == 38);
24232423}
......@@ -2430,7 +2430,7 @@ test "big.int gcd non-one large" {
24302430 var r = try Managed.init(testing.allocator);
24312431 defer r.deinit();
24322432
2433 try r.gcd(a, b);
2433 try r.gcd(&a, &b);
24342434
24352435 try testing.expect((try r.to(u32)) == 4369);
24362436}
......@@ -2443,7 +2443,7 @@ test "big.int gcd large multi-limb result" {
24432443 var r = try Managed.init(testing.allocator);
24442444 defer r.deinit();
24452445
2446 try r.gcd(a, b);
2446 try r.gcd(&a, &b);
24472447
24482448 const answer = (try r.to(u256));
24492449 try testing.expect(answer == 0xf000000ff00000fff0000ffff000fffff00ffffff1);
......@@ -2457,7 +2457,7 @@ test "big.int gcd one large" {
24572457 var r = try Managed.init(testing.allocator);
24582458 defer r.deinit();
24592459
2460 try r.gcd(a, b);
2460 try r.gcd(&a, &b);
24612461
24622462 try testing.expect((try r.to(u64)) == 1);
24632463}
......@@ -2488,10 +2488,10 @@ test "big.int pow" {
24882488 var a = try Managed.initSet(testing.allocator, -3);
24892489 defer a.deinit();
24902490
2491 try a.pow(a.toConst(), 3);
2491 try a.pow(&a, 3);
24922492 try testing.expectEqual(@as(i32, -27), try a.to(i32));
24932493
2494 try a.pow(a.toConst(), 4);
2494 try a.pow(&a, 4);
24952495 try testing.expectEqual(@as(i32, 531441), try a.to(i32));
24962496 }
24972497 {
......@@ -2502,9 +2502,9 @@ test "big.int pow" {
25022502 defer y.deinit();
25032503
25042504 // y and a are not aliased
2505 try y.pow(a.toConst(), 123);
2505 try y.pow(&a, 123);
25062506 // y and a are aliased
2507 try a.pow(a.toConst(), 123);
2507 try a.pow(&a, 123);
25082508
25092509 try testing.expect(a.eq(y));
25102510
......@@ -2522,18 +2522,18 @@ test "big.int pow" {
25222522 var a = try Managed.initSet(testing.allocator, 0);
25232523 defer a.deinit();
25242524
2525 try a.pow(a.toConst(), 100);
2525 try a.pow(&a, 100);
25262526 try testing.expectEqual(@as(i32, 0), try a.to(i32));
25272527
25282528 try a.set(1);
2529 try a.pow(a.toConst(), 0);
2529 try a.pow(&a, 0);
25302530 try testing.expectEqual(@as(i32, 1), try a.to(i32));
2531 try a.pow(a.toConst(), 100);
2531 try a.pow(&a, 100);
25322532 try testing.expectEqual(@as(i32, 1), try a.to(i32));
25332533 try a.set(-1);
2534 try a.pow(a.toConst(), 15);
2534 try a.pow(&a, 15);
25352535 try testing.expectEqual(@as(i32, -1), try a.to(i32));
2536 try a.pow(a.toConst(), 16);
2536 try a.pow(&a, 16);
25372537 try testing.expectEqual(@as(i32, 1), try a.to(i32));
25382538 }
25392539}
......@@ -2547,7 +2547,7 @@ test "big.int regression test for 1 limb overflow with alias" {
25472547 defer b.deinit();
25482548
25492549 try a.ensureAddCapacity(a.toConst(), b.toConst());
2550 try a.add(a.toConst(), b.toConst());
2550 try a.add(&a, &b);
25512551
25522552 try testing.expect(a.toConst().orderAgainstScalar(19740274219868223167) == .eq);
25532553}
......@@ -2561,7 +2561,7 @@ test "big.int regression test for realloc with alias" {
25612561 defer b.deinit();
25622562
25632563 try a.ensureAddCapacity(a.toConst(), b.toConst());
2564 try a.add(a.toConst(), b.toConst());
2564 try a.add(&a, &b);
25652565
25662566 try testing.expect(a.toConst().orderAgainstScalar(14691098406862188148944207245954912110548093601382197697835) == .eq);
25672567}
......@@ -2572,7 +2572,7 @@ test "big int popcount" {
25722572 var b = try Managed.initSet(testing.allocator, -1);
25732573 defer b.deinit();
25742574
2575 try a.popCount(b.toConst(), 16);
2575 try a.popCount(&b, 16);
25762576
25772577 try testing.expect(a.toConst().orderAgainstScalar(16) == .eq);
25782578}
lib/std/math/big/rational.zig+28-27
......@@ -102,20 +102,23 @@ pub const Rational = struct {
102102 try self.p.setString(10, str[0..i]);
103103
104104 const base = IntConst{ .limbs = &[_]Limb{10}, .positive = true };
105 var local_buf: [@sizeOf(Limb) * Int.default_capacity]u8 align(@alignOf(Limb)) = undefined;
106 var fba = std.heap.FixedBufferAllocator.init(&local_buf);
107 const base_managed = try base.toManaged(fba.allocator());
105108
106109 var j: usize = start;
107110 while (j < str.len - i - 1) : (j += 1) {
108111 try self.p.ensureMulCapacity(self.p.toConst(), base);
109 try self.p.mul(self.p.toConst(), base);
112 try self.p.mul(&self.p, &base_managed);
110113 }
111114
112115 try self.q.setString(10, str[i + 1 ..]);
113 try self.p.add(self.p.toConst(), self.q.toConst());
116 try self.p.add(&self.p, &self.q);
114117
115118 try self.q.set(1);
116119 var k: usize = i + 1;
117120 while (k < str.len) : (k += 1) {
118 try self.q.mul(self.q.toConst(), base);
121 try self.q.mul(&self.q, &base_managed);
119122 }
120123
121124 try self.reduce();
......@@ -172,9 +175,9 @@ pub const Rational = struct {
172175
173176 try self.q.set(1);
174177 if (shift >= 0) {
175 try self.q.shiftLeft(self.q, @intCast(usize, shift));
178 try self.q.shiftLeft(&self.q, @intCast(usize, shift));
176179 } else {
177 try self.p.shiftLeft(self.p, @intCast(usize, -shift));
180 try self.p.shiftLeft(&self.p, @intCast(usize, -shift));
178181 }
179182
180183 try self.reduce();
......@@ -215,9 +218,9 @@ pub const Rational = struct {
215218
216219 const shift = msize2 - exp;
217220 if (shift >= 0) {
218 try a2.shiftLeft(a2, @intCast(usize, shift));
221 try a2.shiftLeft(&a2, @intCast(usize, shift));
219222 } else {
220 try b2.shiftLeft(b2, @intCast(usize, -shift));
223 try b2.shiftLeft(&b2, @intCast(usize, -shift));
221224 }
222225
223226 // 2. compute quotient and remainder
......@@ -228,7 +231,7 @@ pub const Rational = struct {
228231 var r = try Int.init(self.p.allocator);
229232 defer r.deinit();
230233
231 try Int.divTrunc(&q, &r, a2.toConst(), b2.toConst());
234 try Int.divTrunc(&q, &r, &a2, &b2);
232235
233236 var mantissa = extractLowBits(q, BitReprType);
234237 var have_rem = r.len() > 0;
......@@ -350,8 +353,8 @@ pub const Rational = struct {
350353 var p = try Int.init(b.p.allocator);
351354 defer p.deinit();
352355
353 try q.mul(a.p.toConst(), b.q.toConst());
354 try p.mul(b.p.toConst(), a.q.toConst());
356 try q.mul(&a.p, &b.q);
357 try p.mul(&b.p, &a.q);
355358
356359 return if (is_abs) q.orderAbs(p) else q.order(p);
357360 }
......@@ -376,11 +379,11 @@ pub const Rational = struct {
376379 r.deinit();
377380 };
378381
379 try r.p.mul(a.p.toConst(), b.q.toConst());
380 try r.q.mul(b.p.toConst(), a.q.toConst());
381 try r.p.add(r.p.toConst(), r.q.toConst());
382 try r.p.mul(&a.p, &b.q);
383 try r.q.mul(&b.p, &a.q);
384 try r.p.add(&r.p, &r.q);
382385
383 try r.q.mul(a.q.toConst(), b.q.toConst());
386 try r.q.mul(&a.q, &b.q);
384387 try r.reduce();
385388 }
386389
......@@ -404,11 +407,11 @@ pub const Rational = struct {
404407 r.deinit();
405408 };
406409
407 try r.p.mul(a.p.toConst(), b.q.toConst());
408 try r.q.mul(b.p.toConst(), a.q.toConst());
409 try r.p.sub(r.p.toConst(), r.q.toConst());
410 try r.p.mul(&a.p, &b.q);
411 try r.q.mul(&b.p, &a.q);
412 try r.p.sub(&r.p, &r.q);
410413
411 try r.q.mul(a.q.toConst(), b.q.toConst());
414 try r.q.mul(&a.q, &b.q);
412415 try r.reduce();
413416 }
414417
......@@ -418,8 +421,8 @@ pub const Rational = struct {
418421 ///
419422 /// Returns an error if memory could not be allocated.
420423 pub fn mul(r: *Rational, a: Rational, b: Rational) !void {
421 try r.p.mul(a.p.toConst(), b.p.toConst());
422 try r.q.mul(a.q.toConst(), b.q.toConst());
424 try r.p.mul(&a.p, &b.p);
425 try r.q.mul(&a.q, &b.q);
423426 try r.reduce();
424427 }
425428
......@@ -433,8 +436,8 @@ pub const Rational = struct {
433436 @panic("division by zero");
434437 }
435438
436 try r.p.mul(a.p.toConst(), b.q.toConst());
437 try r.q.mul(b.p.toConst(), a.q.toConst());
439 try r.p.mul(&a.p, &b.q);
440 try r.q.mul(&b.p, &a.q);
438441 try r.reduce();
439442 }
440443
......@@ -450,7 +453,7 @@ pub const Rational = struct {
450453
451454 const sign = r.p.isPositive();
452455 r.p.abs();
453 try a.gcd(r.p, r.q);
456 try a.gcd(&r.p, &r.q);
454457 r.p.setSign(sign);
455458
456459 const one = IntConst{ .limbs = &[_]Limb{1}, .positive = true };
......@@ -460,8 +463,8 @@ pub const Rational = struct {
460463
461464 // TODO: divexact would be useful here
462465 // TODO: don't copy r.q for div
463 try Int.divTrunc(&r.p, &unused, r.p.toConst(), a.toConst());
464 try Int.divTrunc(&r.q, &unused, r.q.toConst(), a.toConst());
466 try Int.divTrunc(&r.p, &unused, &r.p, &a);
467 try Int.divTrunc(&r.q, &unused, &r.q, &a);
465468 }
466469 }
467470};
......@@ -573,7 +576,6 @@ test "big.rational setFloatString" {
573576}
574577
575578test "big.rational toFloat" {
576 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
577579 var a = try Rational.init(testing.allocator);
578580 defer a.deinit();
579581
......@@ -587,7 +589,6 @@ test "big.rational toFloat" {
587589}
588590
589591test "big.rational set/to Float round-trip" {
590 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
591592 var a = try Rational.init(testing.allocator);
592593 defer a.deinit();
593594 var prng = std.rand.DefaultPrng.init(0x5EED);
src/RangeSet.zig+1-1
......@@ -85,7 +85,7 @@ pub fn spans(self: *RangeSet, first: Value, last: Value, ty: Type) !bool {
8585
8686 // prev.last + 1 == cur.first
8787 try counter.copy(prev.last.toBigInt(&space, target));
88 try counter.addScalar(counter.toConst(), 1);
88 try counter.addScalar(&counter, 1);
8989
9090 const cur_start_int = cur.first.toBigInt(&space, target);
9191 if (!cur_start_int.eq(counter.toConst())) {
src/Sema.zig+4-4
......@@ -22706,9 +22706,9 @@ fn cmpNumeric(
2270622706 else => {},
2270722707 }
2270822708 if (lhs_is_signed) {
22709 try bigint.addScalar(bigint.toConst(), -1);
22709 try bigint.addScalar(&bigint, -1);
2271022710 } else {
22711 try bigint.addScalar(bigint.toConst(), 1);
22711 try bigint.addScalar(&bigint, 1);
2271222712 }
2271322713 }
2271422714 lhs_bits = bigint.toConst().bitCountTwosComp();
......@@ -22752,9 +22752,9 @@ fn cmpNumeric(
2275222752 else => {},
2275322753 }
2275422754 if (rhs_is_signed) {
22755 try bigint.addScalar(bigint.toConst(), -1);
22755 try bigint.addScalar(&bigint, -1);
2275622756 } else {
22757 try bigint.addScalar(bigint.toConst(), 1);
22757 try bigint.addScalar(&bigint, 1);
2275822758 }
2275922759 }
2276022760 rhs_bits = bigint.toConst().bitCountTwosComp();