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 {...@@ -1391,28 +1391,28 @@ pub const Mutable = struct {
13911391
1392 if (B == 0) {1392 if (B == 0) {
1393 // t_big = x % y, r is unused1393 // 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);
1395 assert(t_big.isPositive());1395 assert(t_big.isPositive());
13961396
1397 x.swap(&y);1397 x.swap(&y);
1398 y.swap(&t_big);1398 y.swap(&t_big);
1399 } else {1399 } else {
1400 var storage: [8]Limb = undefined;1400 var storage: [8]Limb = undefined;
1401 const Ap = fixedIntFromSignedDoubleLimb(A, storage[0..2]).toConst();1401 const Ap = fixedIntFromSignedDoubleLimb(A, storage[0..2]).toManaged(limbs_buffer.allocator);
1402 const Bp = fixedIntFromSignedDoubleLimb(B, storage[2..4]).toConst();1402 const Bp = fixedIntFromSignedDoubleLimb(B, storage[2..4]).toManaged(limbs_buffer.allocator);
1403 const Cp = fixedIntFromSignedDoubleLimb(C, storage[4..6]).toConst();1403 const Cp = fixedIntFromSignedDoubleLimb(C, storage[4..6]).toManaged(limbs_buffer.allocator);
1404 const Dp = fixedIntFromSignedDoubleLimb(D, storage[6..8]).toConst();1404 const Dp = fixedIntFromSignedDoubleLimb(D, storage[6..8]).toManaged(limbs_buffer.allocator);
14051405
1406 // t_big = Ax + By1406 // t_big = Ax + By
1407 try r.mul(x.toConst(), Ap);1407 try r.mul(&x, &Ap);
1408 try t_big.mul(y.toConst(), Bp);1408 try t_big.mul(&y, &Bp);
1409 try t_big.add(r.toConst(), t_big.toConst());1409 try t_big.add(&r, &t_big);
14101410
1411 // u = Cx + Dy, r as u1411 // u = Cx + Dy, r as u
1412 try tmp_x.copy(x.toConst());1412 try tmp_x.copy(x.toConst());
1413 try x.mul(tmp_x.toConst(), Cp);1413 try x.mul(&tmp_x, &Cp);
1414 try r.mul(y.toConst(), Dp);1414 try r.mul(&y, &Dp);
1415 try r.add(x.toConst(), r.toConst());1415 try r.add(&x, &r);
14161416
1417 x.swap(&t_big);1417 x.swap(&t_big);
1418 y.swap(&r);1418 y.swap(&r);
...@@ -1423,7 +1423,7 @@ pub const Mutable = struct {...@@ -1423,7 +1423,7 @@ pub const Mutable = struct {
1423 assert(x.toConst().order(y.toConst()) != .lt);1423 assert(x.toConst().order(y.toConst()) != .lt);
14241424
1425 while (!y.toConst().eqZero()) {1425 while (!y.toConst().eqZero()) {
1426 try t_big.divTrunc(&r, x.toConst(), y.toConst());1426 try t_big.divTrunc(&r, &x, &y);
1427 x.swap(&y);1427 x.swap(&y);
1428 y.swap(&r);1428 y.swap(&r);
1429 }1429 }
...@@ -2660,57 +2660,56 @@ pub const Managed = struct {...@@ -2660,57 +2660,56 @@ pub const Managed = struct {
26602660
2661 /// r = a + scalar2661 /// r = a + scalar
2662 ///2662 ///
2663 /// r and a may be aliases. If r aliases a, then caller must call2663 /// r and a may be aliases.
2664 /// `r.ensureAddScalarCapacity` prior to calling `add`.
2665 /// scalar is a primitive integer type.
2666 ///2664 ///
2667 /// Returns an error if memory could not be allocated.2665 /// Returns an error if memory could not be allocated.
2668 pub fn addScalar(r: *Managed, a: Const, scalar: anytype) Allocator.Error!void {2666 pub fn addScalar(r: *Managed, a: *const Managed, 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);2667 try r.ensureAddScalarCapacity(a.toConst(), scalar);
2670 try r.ensureAddScalarCapacity(a, scalar);
2671 var m = r.toMutable();2668 var m = r.toMutable();
2672 m.addScalar(a, scalar);2669 m.addScalar(a.toConst(), scalar);
2673 r.setMetadata(m.positive, m.len);2670 r.setMetadata(m.positive, m.len);
2674 }2671 }
26752672
2676 /// r = a + b2673 /// r = a + b
2677 ///2674 ///
2678 /// r, a and b may be aliases. If r aliases a or b, then caller must call2675 /// r, a and b may be aliases.
2679 /// `r.ensureAddCapacity` prior to calling `add`.
2680 ///2676 ///
2681 /// Returns an error if memory could not be allocated.2677 /// Returns an error if memory could not be allocated.
2682 pub fn add(r: *Managed, a: Const, b: Const) Allocator.Error!void {2678 pub fn add(r: *Managed, a: *const Managed, b: *const Managed) 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);2679 try r.ensureAddCapacity(a.toConst(), b.toConst());
2684 try r.ensureAddCapacity(a, b);
2685 var m = r.toMutable();2680 var m = r.toMutable();
2686 m.add(a, b);2681 m.add(a.toConst(), b.toConst());
2687 r.setMetadata(m.positive, m.len);2682 r.setMetadata(m.positive, m.len);
2688 }2683 }
26892684
2690 /// r = a + b with 2s-complement wrapping semantics. Returns whether any overflow occured.2685 /// r = a + b with 2s-complement wrapping semantics. Returns whether any overflow occured.
2691 ///2686 ///
2692 /// r, a and b may be aliases. If r aliases a or b, then caller must call2687 /// r, a and b may be aliases.
2693 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2694 ///2688 ///
2695 /// Returns an error if memory could not be allocated.2689 /// 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 {
2697 try r.ensureTwosCompCapacity(bit_count);2697 try r.ensureTwosCompCapacity(bit_count);
2698 var m = r.toMutable();2698 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);
2700 r.setMetadata(m.positive, m.len);2700 r.setMetadata(m.positive, m.len);
2701 return wrapped;2701 return wrapped;
2702 }2702 }
27032703
2704 /// r = a + b with 2s-complement saturating semantics.2704 /// r = a + b with 2s-complement saturating semantics.
2705 ///2705 ///
2706 /// r, a and b may be aliases. If r aliases a or b, then caller must call2706 /// r, a and b may be aliases.
2707 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2708 ///2707 ///
2709 /// Returns an error if memory could not be allocated.2708 /// 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 {
2711 try r.ensureTwosCompCapacity(bit_count);2710 try r.ensureTwosCompCapacity(bit_count);
2712 var m = r.toMutable();2711 var m = r.toMutable();
2713 m.addSat(a, b, signedness, bit_count);2712 m.addSat(a.toConst(), b.toConst(), signedness, bit_count);
2714 r.setMetadata(m.positive, m.len);2713 r.setMetadata(m.positive, m.len);
2715 }2714 }
27162715
...@@ -2719,64 +2718,72 @@ pub const Managed = struct {...@@ -2719,64 +2718,72 @@ pub const Managed = struct {
2719 /// r, a and b may be aliases.2718 /// r, a and b may be aliases.
2720 ///2719 ///
2721 /// Returns an error if memory could not be allocated.2720 /// 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 {
2723 try r.ensureCapacity(math.max(a.limbs.len, b.limbs.len) + 1);2722 try r.ensureCapacity(math.max(a.limbs.len, b.limbs.len) + 1);
2724 var m = r.toMutable();2723 var m = r.toMutable();
2725 m.sub(a, b);2724 m.sub(a.toConst(), b.toConst());
2726 r.setMetadata(m.positive, m.len);2725 r.setMetadata(m.positive, m.len);
2727 }2726 }
27282727
2729 /// r = a - b with 2s-complement wrapping semantics. Returns whether any overflow occured.2728 /// r = a - b with 2s-complement wrapping semantics. Returns whether any overflow occured.
2730 ///2729 ///
2731 /// r, a and b may be aliases. If r aliases a or b, then caller must call2730 /// r, a and b may be aliases.
2732 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2733 ///2731 ///
2734 /// Returns an error if memory could not be allocated.2732 /// 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 {
2736 try r.ensureTwosCompCapacity(bit_count);2740 try r.ensureTwosCompCapacity(bit_count);
2737 var m = r.toMutable();2741 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);
2739 r.setMetadata(m.positive, m.len);2743 r.setMetadata(m.positive, m.len);
2740 return wrapped;2744 return wrapped;
2741 }2745 }
27422746
2743 /// r = a - b with 2s-complement saturating semantics.2747 /// r = a - b with 2s-complement saturating semantics.
2744 ///2748 ///
2745 /// r, a and b may be aliases. If r aliases a or b, then caller must call2749 /// r, a and b may be aliases.
2746 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2747 ///2750 ///
2748 /// Returns an error if memory could not be allocated.2751 /// 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 {
2750 try r.ensureTwosCompCapacity(bit_count);2759 try r.ensureTwosCompCapacity(bit_count);
2751 var m = r.toMutable();2760 var m = r.toMutable();
2752 m.subSat(a, b, signedness, bit_count);2761 m.subSat(a.toConst(), b.toConst(), signedness, bit_count);
2753 r.setMetadata(m.positive, m.len);2762 r.setMetadata(m.positive, m.len);
2754 }2763 }
27552764
2756 /// rma = a * b2765 /// rma = a * b
2757 ///2766 ///
2758 /// rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.2767 /// 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`.
2760 ///2768 ///
2761 /// Returns an error if memory could not be allocated.2769 /// Returns an error if memory could not be allocated.
2762 ///2770 ///
2763 /// rma's allocator is used for temporary storage to speed up the multiplication.2771 /// 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 {
2765 var alias_count: usize = 0;2773 var alias_count: usize = 0;
2766 if (rma.limbs.ptr == a.limbs.ptr)2774 if (rma.limbs.ptr == a.limbs.ptr)
2767 alias_count += 1;2775 alias_count += 1;
2768 if (rma.limbs.ptr == b.limbs.ptr)2776 if (rma.limbs.ptr == b.limbs.ptr)
2769 alias_count += 1;2777 alias_count += 1;
2770 assert(alias_count == 0 or rma.limbs.len >= a.limbs.len + b.limbs.len + 1);2778 try rma.ensureMulCapacity(a.toConst(), b.toConst());
2771 try rma.ensureMulCapacity(a, b);
2772 var m = rma.toMutable();2779 var m = rma.toMutable();
2773 if (alias_count == 0) {2780 if (alias_count == 0) {
2774 m.mulNoAlias(a, b, rma.allocator);2781 m.mulNoAlias(a.toConst(), b.toConst(), rma.allocator);
2775 } else {2782 } else {
2776 const limb_count = calcMulLimbsBufferLen(a.limbs.len, b.limbs.len, alias_count);2783 const limb_count = calcMulLimbsBufferLen(a.limbs.len, b.limbs.len, alias_count);
2777 const limbs_buffer = try rma.allocator.alloc(Limb, limb_count);2784 const limbs_buffer = try rma.allocator.alloc(Limb, limb_count);
2778 defer rma.allocator.free(limbs_buffer);2785 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);
2780 }2787 }
2781 rma.setMetadata(m.positive, m.len);2788 rma.setMetadata(m.positive, m.len);
2782 }2789 }
...@@ -2784,13 +2791,17 @@ pub const Managed = struct {...@@ -2784,13 +2791,17 @@ pub const Managed = struct {
2784 /// rma = a * b with 2s-complement wrapping semantics.2791 /// rma = a * b with 2s-complement wrapping semantics.
2785 ///2792 ///
2786 /// rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.2793 /// 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`.
2789 ///2794 ///
2790 /// Returns an error if memory could not be allocated.2795 /// Returns an error if memory could not be allocated.
2791 ///2796 ///
2792 /// rma's allocator is used for temporary storage to speed up the multiplication.2797 /// 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 {
2794 var alias_count: usize = 0;2805 var alias_count: usize = 0;
2795 if (rma.limbs.ptr == a.limbs.ptr)2806 if (rma.limbs.ptr == a.limbs.ptr)
2796 alias_count += 1;2807 alias_count += 1;
...@@ -2800,12 +2811,12 @@ pub const Managed = struct {...@@ -2800,12 +2811,12 @@ pub const Managed = struct {
2800 try rma.ensureTwosCompCapacity(bit_count);2811 try rma.ensureTwosCompCapacity(bit_count);
2801 var m = rma.toMutable();2812 var m = rma.toMutable();
2802 if (alias_count == 0) {2813 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);
2804 } else {2815 } else {
2805 const limb_count = calcMulWrapLimbsBufferLen(bit_count, a.limbs.len, b.limbs.len, alias_count);2816 const limb_count = calcMulWrapLimbsBufferLen(bit_count, a.limbs.len, b.limbs.len, alias_count);
2806 const limbs_buffer = try rma.allocator.alloc(Limb, limb_count);2817 const limbs_buffer = try rma.allocator.alloc(Limb, limb_count);
2807 defer rma.allocator.free(limbs_buffer);2818 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);
2809 }2820 }
2810 rma.setMetadata(m.positive, m.len);2821 rma.setMetadata(m.positive, m.len);
2811 }2822 }
...@@ -2831,14 +2842,14 @@ pub const Managed = struct {...@@ -2831,14 +2842,14 @@ pub const Managed = struct {
2831 /// a / b are floored (rounded towards 0).2842 /// a / b are floored (rounded towards 0).
2832 ///2843 ///
2833 /// Returns an error if memory could not be allocated.2844 /// 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 {
2835 try q.ensureCapacity(a.limbs.len);2846 try q.ensureCapacity(a.limbs.len);
2836 try r.ensureCapacity(b.limbs.len);2847 try r.ensureCapacity(b.limbs.len);
2837 var mq = q.toMutable();2848 var mq = q.toMutable();
2838 var mr = r.toMutable();2849 var mr = r.toMutable();
2839 const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.limbs.len, b.limbs.len));2850 const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.limbs.len, b.limbs.len));
2840 defer q.allocator.free(limbs_buffer);2851 defer q.allocator.free(limbs_buffer);
2841 mq.divFloor(&mr, a, b, limbs_buffer);2852 mq.divFloor(&mr, a.toConst(), b.toConst(), limbs_buffer);
2842 q.setMetadata(mq.positive, mq.len);2853 q.setMetadata(mq.positive, mq.len);
2843 r.setMetadata(mr.positive, mr.len);2854 r.setMetadata(mr.positive, mr.len);
2844 }2855 }
...@@ -2848,20 +2859,21 @@ pub const Managed = struct {...@@ -2848,20 +2859,21 @@ pub const Managed = struct {
2848 /// a / b are truncated (rounded towards -inf).2859 /// a / b are truncated (rounded towards -inf).
2849 ///2860 ///
2850 /// Returns an error if memory could not be allocated.2861 /// 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 {
2852 try q.ensureCapacity(a.limbs.len);2863 try q.ensureCapacity(a.limbs.len);
2853 try r.ensureCapacity(b.limbs.len);2864 try r.ensureCapacity(b.limbs.len);
2854 var mq = q.toMutable();2865 var mq = q.toMutable();
2855 var mr = r.toMutable();2866 var mr = r.toMutable();
2856 const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.limbs.len, b.limbs.len));2867 const limbs_buffer = try q.allocator.alloc(Limb, calcDivLimbsBufferLen(a.limbs.len, b.limbs.len));
2857 defer q.allocator.free(limbs_buffer);2868 defer q.allocator.free(limbs_buffer);
2858 mq.divTrunc(&mr, a, b, limbs_buffer);2869 mq.divTrunc(&mr, a.toConst(), b.toConst(), limbs_buffer);
2859 q.setMetadata(mq.positive, mq.len);2870 q.setMetadata(mq.positive, mq.len);
2860 r.setMetadata(mr.positive, mr.len);2871 r.setMetadata(mr.positive, mr.len);
2861 }2872 }
28622873
2863 /// r = a << shift, in other words, r = a * 2^shift2874 /// 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 {
2865 try r.ensureCapacity(a.len() + (shift / limb_bits) + 1);2877 try r.ensureCapacity(a.len() + (shift / limb_bits) + 1);
2866 var m = r.toMutable();2878 var m = r.toMutable();
2867 m.shiftLeft(a.toConst(), shift);2879 m.shiftLeft(a.toConst(), shift);
...@@ -2869,7 +2881,8 @@ pub const Managed = struct {...@@ -2869,7 +2881,8 @@ pub const Managed = struct {
2869 }2881 }
28702882
2871 /// r = a <<| shift with 2s-complement saturating semantics.2883 /// 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 {
2873 try r.ensureTwosCompCapacity(bit_count);2886 try r.ensureTwosCompCapacity(bit_count);
2874 var m = r.toMutable();2887 var m = r.toMutable();
2875 m.shiftLeftSat(a.toConst(), shift, signedness, bit_count);2888 m.shiftLeftSat(a.toConst(), shift, signedness, bit_count);
...@@ -2877,7 +2890,8 @@ pub const Managed = struct {...@@ -2877,7 +2890,8 @@ pub const Managed = struct {
2877 }2890 }
28782891
2879 /// r = a >> shift2892 /// 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 {
2881 if (a.len() <= shift / limb_bits) {2895 if (a.len() <= shift / limb_bits) {
2882 r.metadata = 1;2896 r.metadata = 1;
2883 r.limbs[0] = 0;2897 r.limbs[0] = 0;
...@@ -2891,7 +2905,8 @@ pub const Managed = struct {...@@ -2891,7 +2905,8 @@ pub const Managed = struct {
2891 }2905 }
28922906
2893 /// r = ~a under 2s-complement wrapping semantics.2907 /// 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 {
2895 try r.ensureTwosCompCapacity(bit_count);2910 try r.ensureTwosCompCapacity(bit_count);
2896 var m = r.toMutable();2911 var m = r.toMutable();
2897 m.bitNotWrap(a.toConst(), signedness, bit_count);2912 m.bitNotWrap(a.toConst(), signedness, bit_count);
...@@ -2901,7 +2916,7 @@ pub const Managed = struct {...@@ -2901,7 +2916,7 @@ pub const Managed = struct {
2901 /// r = a | b2916 /// r = a | b
2902 ///2917 ///
2903 /// a and b are zero-extended to the longer of a or b.2918 /// 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 {
2905 try r.ensureCapacity(math.max(a.len(), b.len()));2920 try r.ensureCapacity(math.max(a.len(), b.len()));
2906 var m = r.toMutable();2921 var m = r.toMutable();
2907 m.bitOr(a.toConst(), b.toConst());2922 m.bitOr(a.toConst(), b.toConst());
...@@ -2909,7 +2924,7 @@ pub const Managed = struct {...@@ -2909,7 +2924,7 @@ pub const Managed = struct {
2909 }2924 }
29102925
2911 /// r = a & b2926 /// 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 {
2913 const cap = if (a.isPositive() or b.isPositive())2928 const cap = if (a.isPositive() or b.isPositive())
2914 math.min(a.len(), b.len())2929 math.min(a.len(), b.len())
2915 else2930 else
...@@ -2921,7 +2936,7 @@ pub const Managed = struct {...@@ -2921,7 +2936,7 @@ pub const Managed = struct {
2921 }2936 }
29222937
2923 /// r = a ^ b2938 /// 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 {
2925 var cap = math.max(a.len(), b.len()) + @boolToInt(a.isPositive() != b.isPositive());2940 var cap = math.max(a.len(), b.len()) + @boolToInt(a.isPositive() != b.isPositive());
2926 try r.ensureCapacity(cap);2941 try r.ensureCapacity(cap);
29272942
...@@ -2934,7 +2949,7 @@ pub const Managed = struct {...@@ -2934,7 +2949,7 @@ pub const Managed = struct {
2934 /// x and y may alias each other.2949 /// x and y may alias each other.
2935 ///2950 ///
2936 /// rma's allocator is used for temporary storage to boost multiplication performance.2951 /// 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 {
2938 try rma.ensureCapacity(math.min(x.len(), y.len()));2953 try rma.ensureCapacity(math.min(x.len(), y.len()));
2939 var m = rma.toMutable();2954 var m = rma.toMutable();
2940 var limbs_buffer = std.ArrayList(Limb).init(rma.allocator);2955 var limbs_buffer = std.ArrayList(Limb).init(rma.allocator);
...@@ -2944,14 +2959,14 @@ pub const Managed = struct {...@@ -2944,14 +2959,14 @@ pub const Managed = struct {
2944 }2959 }
29452960
2946 /// r = a * a2961 /// r = a * a
2947 pub fn sqr(rma: *Managed, a: Const) !void {2962 pub fn sqr(rma: *Managed, a: *const Managed) !void {
2948 const needed_limbs = 2 * a.limbs.len + 1;2963 const needed_limbs = 2 * a.limbs.len + 1;
29492964
2950 if (rma.limbs.ptr == a.limbs.ptr) {2965 if (rma.limbs.ptr == a.limbs.ptr) {
2951 var m = try Managed.initCapacity(rma.allocator, needed_limbs);2966 var m = try Managed.initCapacity(rma.allocator, needed_limbs);
2952 errdefer m.deinit();2967 errdefer m.deinit();
2953 var m_mut = m.toMutable();2968 var m_mut = m.toMutable();
2954 m_mut.sqrNoAlias(a, rma.allocator);2969 m_mut.sqrNoAlias(a.toConst(), rma.allocator);
2955 m.setMetadata(m_mut.positive, m_mut.len);2970 m.setMetadata(m_mut.positive, m_mut.len);
29562971
2957 rma.deinit();2972 rma.deinit();
...@@ -2959,12 +2974,12 @@ pub const Managed = struct {...@@ -2959,12 +2974,12 @@ pub const Managed = struct {
2959 } else {2974 } else {
2960 try rma.ensureCapacity(needed_limbs);2975 try rma.ensureCapacity(needed_limbs);
2961 var rma_mut = rma.toMutable();2976 var rma_mut = rma.toMutable();
2962 rma_mut.sqrNoAlias(a, rma.allocator);2977 rma_mut.sqrNoAlias(a.toConst(), rma.allocator);
2963 rma.setMetadata(rma_mut.positive, rma_mut.len);2978 rma.setMetadata(rma_mut.positive, rma_mut.len);
2964 }2979 }
2965 }2980 }
29662981
2967 pub fn pow(rma: *Managed, a: Const, b: u32) !void {2982 pub fn pow(rma: *Managed, a: *const Managed, b: u32) !void {
2968 const needed_limbs = calcPowLimbsBufferLen(a.bitCountAbs(), b);2983 const needed_limbs = calcPowLimbsBufferLen(a.bitCountAbs(), b);
29692984
2970 const limbs_buffer = try rma.allocator.alloc(Limb, needed_limbs);2985 const limbs_buffer = try rma.allocator.alloc(Limb, needed_limbs);
...@@ -2974,7 +2989,7 @@ pub const Managed = struct {...@@ -2974,7 +2989,7 @@ pub const Managed = struct {
2974 var m = try Managed.initCapacity(rma.allocator, needed_limbs);2989 var m = try Managed.initCapacity(rma.allocator, needed_limbs);
2975 errdefer m.deinit();2990 errdefer m.deinit();
2976 var m_mut = m.toMutable();2991 var m_mut = m.toMutable();
2977 try m_mut.pow(a, b, limbs_buffer);2992 try m_mut.pow(a.toConst(), b, limbs_buffer);
2978 m.setMetadata(m_mut.positive, m_mut.len);2993 m.setMetadata(m_mut.positive, m_mut.len);
29792994
2980 rma.deinit();2995 rma.deinit();
...@@ -2982,33 +2997,33 @@ pub const Managed = struct {...@@ -2982,33 +2997,33 @@ pub const Managed = struct {
2982 } else {2997 } else {
2983 try rma.ensureCapacity(needed_limbs);2998 try rma.ensureCapacity(needed_limbs);
2984 var rma_mut = rma.toMutable();2999 var rma_mut = rma.toMutable();
2985 try rma_mut.pow(a, b, limbs_buffer);3000 try rma_mut.pow(a.toConst(), b, limbs_buffer);
2986 rma.setMetadata(rma_mut.positive, rma_mut.len);3001 rma.setMetadata(rma_mut.positive, rma_mut.len);
2987 }3002 }
2988 }3003 }
29893004
2990 /// r = truncate(Int(signedness, bit_count), a)3005 /// 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 {
2992 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));3007 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
2993 var m = r.toMutable();3008 var m = r.toMutable();
2994 m.truncate(a, signedness, bit_count);3009 m.truncate(a.toConst(), signedness, bit_count);
2995 r.setMetadata(m.positive, m.len);3010 r.setMetadata(m.positive, m.len);
2996 }3011 }
29973012
2998 /// r = saturate(Int(signedness, bit_count), a)3013 /// 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 {
3000 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));3015 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
3001 var m = r.toMutable();3016 var m = r.toMutable();
3002 m.saturate(a, signedness, bit_count);3017 m.saturate(a.toConst(), signedness, bit_count);
3003 r.setMetadata(m.positive, m.len);3018 r.setMetadata(m.positive, m.len);
3004 }3019 }
30053020
3006 /// r = @popCount(a) with 2s-complement semantics.3021 /// r = @popCount(a) with 2s-complement semantics.
3007 /// r and a may be aliases.3022 /// 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 {
3009 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));3024 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
3010 var m = r.toMutable();3025 var m = r.toMutable();
3011 m.popCount(a, bit_count);3026 m.popCount(a.toConst(), bit_count);
3012 r.setMetadata(m.positive, m.len);3027 r.setMetadata(m.positive, m.len);
3013 }3028 }
3014};3029};
lib/std/math/big/int_test.zig+173-173
...@@ -166,7 +166,7 @@ test "big.int bitcount + sizeInBaseUpperBound" {...@@ -166,7 +166,7 @@ test "big.int bitcount + sizeInBaseUpperBound" {
166 try testing.expect(a.sizeInBaseUpperBound(2) >= 32);166 try testing.expect(a.sizeInBaseUpperBound(2) >= 32);
167 try testing.expect(a.sizeInBaseUpperBound(10) >= 10);167 try testing.expect(a.sizeInBaseUpperBound(10) >= 10);
168168
169 try a.shiftLeft(a, 5000);169 try a.shiftLeft(&a, 5000);
170 try testing.expect(a.bitCountAbs() == 5032);170 try testing.expect(a.bitCountAbs() == 5032);
171 try testing.expect(a.sizeInBaseUpperBound(2) >= 5032);171 try testing.expect(a.sizeInBaseUpperBound(2) >= 5032);
172 a.setSign(false);172 a.setSign(false);
...@@ -486,7 +486,7 @@ test "big.int add single-single" {...@@ -486,7 +486,7 @@ test "big.int add single-single" {
486486
487 var c = try Managed.init(testing.allocator);487 var c = try Managed.init(testing.allocator);
488 defer c.deinit();488 defer c.deinit();
489 try c.add(a.toConst(), b.toConst());489 try c.add(&a, &b);
490490
491 try testing.expect((try c.to(u32)) == 55);491 try testing.expect((try c.to(u32)) == 55);
492}492}
...@@ -500,10 +500,10 @@ test "big.int add multi-single" {...@@ -500,10 +500,10 @@ test "big.int add multi-single" {
500 var c = try Managed.init(testing.allocator);500 var c = try Managed.init(testing.allocator);
501 defer c.deinit();501 defer c.deinit();
502502
503 try c.add(a.toConst(), b.toConst());503 try c.add(&a, &b);
504 try testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2);504 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);
507 try testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2);507 try testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
508}508}
509509
...@@ -517,7 +517,7 @@ test "big.int add multi-multi" {...@@ -517,7 +517,7 @@ test "big.int add multi-multi" {
517517
518 var c = try Managed.init(testing.allocator);518 var c = try Managed.init(testing.allocator);
519 defer c.deinit();519 defer c.deinit();
520 try c.add(a.toConst(), b.toConst());520 try c.add(&a, &b);
521521
522 try testing.expect((try c.to(u128)) == op1 + op2);522 try testing.expect((try c.to(u128)) == op1 + op2);
523}523}
...@@ -530,7 +530,7 @@ test "big.int add zero-zero" {...@@ -530,7 +530,7 @@ test "big.int add zero-zero" {
530530
531 var c = try Managed.init(testing.allocator);531 var c = try Managed.init(testing.allocator);
532 defer c.deinit();532 defer c.deinit();
533 try c.add(a.toConst(), b.toConst());533 try c.add(&a, &b);
534534
535 try testing.expect((try c.to(u32)) == 0);535 try testing.expect((try c.to(u32)) == 0);
536}536}
...@@ -542,7 +542,7 @@ test "big.int add alias multi-limb nonzero-zero" {...@@ -542,7 +542,7 @@ test "big.int add alias multi-limb nonzero-zero" {
542 var b = try Managed.initSet(testing.allocator, 0);542 var b = try Managed.initSet(testing.allocator, 0);
543 defer b.deinit();543 defer b.deinit();
544544
545 try a.add(a.toConst(), b.toConst());545 try a.add(&a, &b);
546546
547 try testing.expect((try a.to(u128)) == op1);547 try testing.expect((try a.to(u128)) == op1);
548}548}
...@@ -560,16 +560,16 @@ test "big.int add sign" {...@@ -560,16 +560,16 @@ test "big.int add sign" {
560 var neg_two = try Managed.initSet(testing.allocator, -2);560 var neg_two = try Managed.initSet(testing.allocator, -2);
561 defer neg_two.deinit();561 defer neg_two.deinit();
562562
563 try a.add(one.toConst(), two.toConst());563 try a.add(&one, &two);
564 try testing.expect((try a.to(i32)) == 3);564 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);
567 try testing.expect((try a.to(i32)) == 1);567 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);
570 try testing.expect((try a.to(i32)) == -1);570 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);
573 try testing.expect((try a.to(i32)) == -3);573 try testing.expect((try a.to(i32)) == -3);
574}574}
575575
...@@ -579,7 +579,7 @@ test "big.int add scalar" {...@@ -579,7 +579,7 @@ test "big.int add scalar" {
579579
580 var b = try Managed.init(testing.allocator);580 var b = try Managed.init(testing.allocator);
581 defer b.deinit();581 defer b.deinit();
582 try b.addScalar(a.toConst(), 5);582 try b.addScalar(&a, 5);
583583
584 try testing.expect((try b.to(u32)) == 55);584 try testing.expect((try b.to(u32)) == 55);
585}585}
...@@ -591,7 +591,7 @@ test "big.int addWrap single-single, unsigned" {...@@ -591,7 +591,7 @@ test "big.int addWrap single-single, unsigned" {
591 var b = try Managed.initSet(testing.allocator, 10);591 var b = try Managed.initSet(testing.allocator, 10);
592 defer b.deinit();592 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
596 try testing.expect(wrapped);596 try testing.expect(wrapped);
597 try testing.expect((try a.to(u17)) == 9);597 try testing.expect((try a.to(u17)) == 9);
...@@ -604,7 +604,7 @@ test "big.int subWrap single-single, unsigned" {...@@ -604,7 +604,7 @@ test "big.int subWrap single-single, unsigned" {
604 var b = try Managed.initSet(testing.allocator, maxInt(u17));604 var b = try Managed.initSet(testing.allocator, maxInt(u17));
605 defer b.deinit();605 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
609 try testing.expect(wrapped);609 try testing.expect(wrapped);
610 try testing.expect((try a.to(u17)) == 1);610 try testing.expect((try a.to(u17)) == 1);
...@@ -617,7 +617,7 @@ test "big.int addWrap multi-multi, unsigned, limb aligned" {...@@ -617,7 +617,7 @@ test "big.int addWrap multi-multi, unsigned, limb aligned" {
617 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb));617 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb));
618 defer b.deinit();618 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
622 try testing.expect(wrapped);622 try testing.expect(wrapped);
623 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb) - 1);623 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb) - 1);
...@@ -630,7 +630,7 @@ test "big.int subWrap single-multi, unsigned, limb aligned" {...@@ -630,7 +630,7 @@ test "big.int subWrap single-multi, unsigned, limb aligned" {
630 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb) + 100);630 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb) + 100);
631 defer b.deinit();631 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
635 try testing.expect(wrapped);635 try testing.expect(wrapped);
636 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb) - 88);636 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb) - 88);
...@@ -643,7 +643,7 @@ test "big.int addWrap single-single, signed" {...@@ -643,7 +643,7 @@ test "big.int addWrap single-single, signed" {
643 var b = try Managed.initSet(testing.allocator, 1 + 1 + maxInt(u21));643 var b = try Managed.initSet(testing.allocator, 1 + 1 + maxInt(u21));
644 defer b.deinit();644 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
648 try testing.expect(wrapped);648 try testing.expect(wrapped);
649 try testing.expect((try a.to(i21)) == minInt(i21));649 try testing.expect((try a.to(i21)) == minInt(i21));
...@@ -656,7 +656,7 @@ test "big.int subWrap single-single, signed" {...@@ -656,7 +656,7 @@ test "big.int subWrap single-single, signed" {
656 var b = try Managed.initSet(testing.allocator, 1);656 var b = try Managed.initSet(testing.allocator, 1);
657 defer b.deinit();657 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
661 try testing.expect(wrapped);661 try testing.expect(wrapped);
662 try testing.expect((try a.to(i21)) == maxInt(i21));662 try testing.expect((try a.to(i21)) == maxInt(i21));
...@@ -669,7 +669,7 @@ test "big.int addWrap multi-multi, signed, limb aligned" {...@@ -669,7 +669,7 @@ test "big.int addWrap multi-multi, signed, limb aligned" {
669 var b = try Managed.initSet(testing.allocator, maxInt(SignedDoubleLimb));669 var b = try Managed.initSet(testing.allocator, maxInt(SignedDoubleLimb));
670 defer b.deinit();670 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
674 try testing.expect(wrapped);674 try testing.expect(wrapped);
675 try testing.expect((try a.to(SignedDoubleLimb)) == -2);675 try testing.expect((try a.to(SignedDoubleLimb)) == -2);
...@@ -682,7 +682,7 @@ test "big.int subWrap single-multi, signed, limb aligned" {...@@ -682,7 +682,7 @@ test "big.int subWrap single-multi, signed, limb aligned" {
682 var b = try Managed.initSet(testing.allocator, 1);682 var b = try Managed.initSet(testing.allocator, 1);
683 defer b.deinit();683 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
687 try testing.expect(wrapped);687 try testing.expect(wrapped);
688 try testing.expect((try a.to(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));688 try testing.expect((try a.to(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));
...@@ -695,7 +695,7 @@ test "big.int addSat single-single, unsigned" {...@@ -695,7 +695,7 @@ test "big.int addSat single-single, unsigned" {
695 var b = try Managed.initSet(testing.allocator, 10);695 var b = try Managed.initSet(testing.allocator, 10);
696 defer b.deinit();696 defer b.deinit();
697697
698 try a.addSat(a.toConst(), b.toConst(), .unsigned, 17);698 try a.addSat(&a, &b, .unsigned, 17);
699699
700 try testing.expect((try a.to(u17)) == maxInt(u17));700 try testing.expect((try a.to(u17)) == maxInt(u17));
701}701}
...@@ -707,7 +707,7 @@ test "big.int subSat single-single, unsigned" {...@@ -707,7 +707,7 @@ test "big.int subSat single-single, unsigned" {
707 var b = try Managed.initSet(testing.allocator, 4000);707 var b = try Managed.initSet(testing.allocator, 4000);
708 defer b.deinit();708 defer b.deinit();
709709
710 try a.subSat(a.toConst(), b.toConst(), .unsigned, 17);710 try a.subSat(&a, &b, .unsigned, 17);
711711
712 try testing.expect((try a.to(u17)) == 0);712 try testing.expect((try a.to(u17)) == 0);
713}713}
...@@ -719,7 +719,7 @@ test "big.int addSat multi-multi, unsigned, limb aligned" {...@@ -719,7 +719,7 @@ test "big.int addSat multi-multi, unsigned, limb aligned" {
719 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb));719 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb));
720 defer b.deinit();720 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
724 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb));724 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb));
725}725}
...@@ -731,7 +731,7 @@ test "big.int subSat single-multi, unsigned, limb aligned" {...@@ -731,7 +731,7 @@ test "big.int subSat single-multi, unsigned, limb aligned" {
731 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb) + 100);731 var b = try Managed.initSet(testing.allocator, maxInt(DoubleLimb) + 100);
732 defer b.deinit();732 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
736 try testing.expect((try a.to(DoubleLimb)) == 0);736 try testing.expect((try a.to(DoubleLimb)) == 0);
737}737}
...@@ -743,7 +743,7 @@ test "big.int addSat single-single, signed" {...@@ -743,7 +743,7 @@ test "big.int addSat single-single, signed" {
743 var b = try Managed.initSet(testing.allocator, 1);743 var b = try Managed.initSet(testing.allocator, 1);
744 defer b.deinit();744 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
748 try testing.expect((try a.to(i14)) == maxInt(i14));748 try testing.expect((try a.to(i14)) == maxInt(i14));
749}749}
...@@ -755,7 +755,7 @@ test "big.int subSat single-single, signed" {...@@ -755,7 +755,7 @@ test "big.int subSat single-single, signed" {
755 var b = try Managed.initSet(testing.allocator, 1);755 var b = try Managed.initSet(testing.allocator, 1);
756 defer b.deinit();756 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
760 try testing.expect((try a.to(i21)) == minInt(i21));760 try testing.expect((try a.to(i21)) == minInt(i21));
761}761}
...@@ -767,7 +767,7 @@ test "big.int addSat multi-multi, signed, limb aligned" {...@@ -767,7 +767,7 @@ test "big.int addSat multi-multi, signed, limb aligned" {
767 var b = try Managed.initSet(testing.allocator, maxInt(SignedDoubleLimb));767 var b = try Managed.initSet(testing.allocator, maxInt(SignedDoubleLimb));
768 defer b.deinit();768 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
772 try testing.expect((try a.to(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));772 try testing.expect((try a.to(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));
773}773}
...@@ -779,7 +779,7 @@ test "big.int subSat single-multi, signed, limb aligned" {...@@ -779,7 +779,7 @@ test "big.int subSat single-multi, signed, limb aligned" {
779 var b = try Managed.initSet(testing.allocator, 1);779 var b = try Managed.initSet(testing.allocator, 1);
780 defer b.deinit();780 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
784 try testing.expect((try a.to(SignedDoubleLimb)) == minInt(SignedDoubleLimb));784 try testing.expect((try a.to(SignedDoubleLimb)) == minInt(SignedDoubleLimb));
785}785}
...@@ -792,7 +792,7 @@ test "big.int sub single-single" {...@@ -792,7 +792,7 @@ test "big.int sub single-single" {
792792
793 var c = try Managed.init(testing.allocator);793 var c = try Managed.init(testing.allocator);
794 defer c.deinit();794 defer c.deinit();
795 try c.sub(a.toConst(), b.toConst());795 try c.sub(&a, &b);
796796
797 try testing.expect((try c.to(u32)) == 45);797 try testing.expect((try c.to(u32)) == 45);
798}798}
...@@ -805,7 +805,7 @@ test "big.int sub multi-single" {...@@ -805,7 +805,7 @@ test "big.int sub multi-single" {
805805
806 var c = try Managed.init(testing.allocator);806 var c = try Managed.init(testing.allocator);
807 defer c.deinit();807 defer c.deinit();
808 try c.sub(a.toConst(), b.toConst());808 try c.sub(&a, &b);
809809
810 try testing.expect((try c.to(Limb)) == maxInt(Limb));810 try testing.expect((try c.to(Limb)) == maxInt(Limb));
811}811}
...@@ -821,7 +821,7 @@ test "big.int sub multi-multi" {...@@ -821,7 +821,7 @@ test "big.int sub multi-multi" {
821821
822 var c = try Managed.init(testing.allocator);822 var c = try Managed.init(testing.allocator);
823 defer c.deinit();823 defer c.deinit();
824 try c.sub(a.toConst(), b.toConst());824 try c.sub(&a, &b);
825825
826 try testing.expect((try c.to(u128)) == op1 - op2);826 try testing.expect((try c.to(u128)) == op1 - op2);
827}827}
...@@ -834,7 +834,7 @@ test "big.int sub equal" {...@@ -834,7 +834,7 @@ test "big.int sub equal" {
834834
835 var c = try Managed.init(testing.allocator);835 var c = try Managed.init(testing.allocator);
836 defer c.deinit();836 defer c.deinit();
837 try c.sub(a.toConst(), b.toConst());837 try c.sub(&a, &b);
838838
839 try testing.expect((try c.to(u32)) == 0);839 try testing.expect((try c.to(u32)) == 0);
840}840}
...@@ -852,19 +852,19 @@ test "big.int sub sign" {...@@ -852,19 +852,19 @@ test "big.int sub sign" {
852 var neg_two = try Managed.initSet(testing.allocator, -2);852 var neg_two = try Managed.initSet(testing.allocator, -2);
853 defer neg_two.deinit();853 defer neg_two.deinit();
854854
855 try a.sub(one.toConst(), two.toConst());855 try a.sub(&one, &two);
856 try testing.expect((try a.to(i32)) == -1);856 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);
859 try testing.expect((try a.to(i32)) == -3);859 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);
862 try testing.expect((try a.to(i32)) == 3);862 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);
865 try testing.expect((try a.to(i32)) == 1);865 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);
868 try testing.expect((try a.to(i32)) == -1);868 try testing.expect((try a.to(i32)) == -1);
869}869}
870870
...@@ -876,7 +876,7 @@ test "big.int mul single-single" {...@@ -876,7 +876,7 @@ test "big.int mul single-single" {
876876
877 var c = try Managed.init(testing.allocator);877 var c = try Managed.init(testing.allocator);
878 defer c.deinit();878 defer c.deinit();
879 try c.mul(a.toConst(), b.toConst());879 try c.mul(&a, &b);
880880
881 try testing.expect((try c.to(u64)) == 250);881 try testing.expect((try c.to(u64)) == 250);
882}882}
...@@ -889,7 +889,7 @@ test "big.int mul multi-single" {...@@ -889,7 +889,7 @@ test "big.int mul multi-single" {
889889
890 var c = try Managed.init(testing.allocator);890 var c = try Managed.init(testing.allocator);
891 defer c.deinit();891 defer c.deinit();
892 try c.mul(a.toConst(), b.toConst());892 try c.mul(&a, &b);
893893
894 try testing.expect((try c.to(DoubleLimb)) == 2 * maxInt(Limb));894 try testing.expect((try c.to(DoubleLimb)) == 2 * maxInt(Limb));
895}895}
...@@ -904,7 +904,7 @@ test "big.int mul multi-multi" {...@@ -904,7 +904,7 @@ test "big.int mul multi-multi" {
904904
905 var c = try Managed.init(testing.allocator);905 var c = try Managed.init(testing.allocator);
906 defer c.deinit();906 defer c.deinit();
907 try c.mul(a.toConst(), b.toConst());907 try c.mul(&a, &b);
908908
909 try testing.expect((try c.to(u256)) == op1 * op2);909 try testing.expect((try c.to(u256)) == op1 * op2);
910}910}
...@@ -915,7 +915,7 @@ test "big.int mul alias r with a" {...@@ -915,7 +915,7 @@ test "big.int mul alias r with a" {
915 var b = try Managed.initSet(testing.allocator, 2);915 var b = try Managed.initSet(testing.allocator, 2);
916 defer b.deinit();916 defer b.deinit();
917917
918 try a.mul(a.toConst(), b.toConst());918 try a.mul(&a, &b);
919919
920 try testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb));920 try testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
921}921}
...@@ -926,7 +926,7 @@ test "big.int mul alias r with b" {...@@ -926,7 +926,7 @@ test "big.int mul alias r with b" {
926 var b = try Managed.initSet(testing.allocator, 2);926 var b = try Managed.initSet(testing.allocator, 2);
927 defer b.deinit();927 defer b.deinit();
928928
929 try a.mul(b.toConst(), a.toConst());929 try a.mul(&b, &a);
930930
931 try testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb));931 try testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
932}932}
...@@ -935,7 +935,7 @@ test "big.int mul alias r with a and b" {...@@ -935,7 +935,7 @@ test "big.int mul alias r with a and b" {
935 var a = try Managed.initSet(testing.allocator, maxInt(Limb));935 var a = try Managed.initSet(testing.allocator, maxInt(Limb));
936 defer a.deinit();936 defer a.deinit();
937937
938 try a.mul(a.toConst(), a.toConst());938 try a.mul(&a, &a);
939939
940 try testing.expect((try a.to(DoubleLimb)) == maxInt(Limb) * maxInt(Limb));940 try testing.expect((try a.to(DoubleLimb)) == maxInt(Limb) * maxInt(Limb));
941}941}
...@@ -948,7 +948,7 @@ test "big.int mul a*0" {...@@ -948,7 +948,7 @@ test "big.int mul a*0" {
948948
949 var c = try Managed.init(testing.allocator);949 var c = try Managed.init(testing.allocator);
950 defer c.deinit();950 defer c.deinit();
951 try c.mul(a.toConst(), b.toConst());951 try c.mul(&a, &b);
952952
953 try testing.expect((try c.to(u32)) == 0);953 try testing.expect((try c.to(u32)) == 0);
954}954}
...@@ -961,7 +961,7 @@ test "big.int mul 0*0" {...@@ -961,7 +961,7 @@ test "big.int mul 0*0" {
961961
962 var c = try Managed.init(testing.allocator);962 var c = try Managed.init(testing.allocator);
963 defer c.deinit();963 defer c.deinit();
964 try c.mul(a.toConst(), b.toConst());964 try c.mul(&a, &b);
965965
966 try testing.expect((try c.to(u32)) == 0);966 try testing.expect((try c.to(u32)) == 0);
967}967}
...@@ -981,8 +981,8 @@ test "big.int mul large" {...@@ -981,8 +981,8 @@ test "big.int mul large" {
981 }981 }
982 a.setMetadata(true, 50);982 a.setMetadata(true, 50);
983983
984 try b.mul(a.toConst(), a.toConst());984 try b.mul(&a, &a);
985 try c.sqr(a.toConst());985 try c.sqr(&a);
986986
987 try testing.expect(b.eq(c));987 try testing.expect(b.eq(c));
988}988}
...@@ -995,7 +995,7 @@ test "big.int mulWrap single-single unsigned" {...@@ -995,7 +995,7 @@ test "big.int mulWrap single-single unsigned" {
995995
996 var c = try Managed.init(testing.allocator);996 var c = try Managed.init(testing.allocator);
997 defer c.deinit();997 defer c.deinit();
998 try c.mulWrap(a.toConst(), b.toConst(), .unsigned, 17);998 try c.mulWrap(&a, &b, .unsigned, 17);
999999
1000 try testing.expect((try c.to(u17)) == 59836);1000 try testing.expect((try c.to(u17)) == 59836);
1001}1001}
...@@ -1008,7 +1008,7 @@ test "big.int mulWrap single-single signed" {...@@ -1008,7 +1008,7 @@ test "big.int mulWrap single-single signed" {
10081008
1009 var c = try Managed.init(testing.allocator);1009 var c = try Managed.init(testing.allocator);
1010 defer c.deinit();1010 defer c.deinit();
1011 try c.mulWrap(a.toConst(), b.toConst(), .signed, 17);1011 try c.mulWrap(&a, &b, .signed, 17);
10121012
1013 try testing.expect((try c.to(i17)) == -59836);1013 try testing.expect((try c.to(i17)) == -59836);
1014}1014}
...@@ -1023,7 +1023,7 @@ test "big.int mulWrap multi-multi unsigned" {...@@ -1023,7 +1023,7 @@ test "big.int mulWrap multi-multi unsigned" {
10231023
1024 var c = try Managed.init(testing.allocator);1024 var c = try Managed.init(testing.allocator);
1025 defer c.deinit();1025 defer c.deinit();
1026 try c.mulWrap(a.toConst(), b.toConst(), .unsigned, 65);1026 try c.mulWrap(&a, &b, .unsigned, 65);
10271027
1028 try testing.expect((try c.to(u128)) == (op1 * op2) & ((1 << 65) - 1));1028 try testing.expect((try c.to(u128)) == (op1 * op2) & ((1 << 65) - 1));
1029}1029}
...@@ -1036,7 +1036,7 @@ test "big.int mulWrap multi-multi signed" {...@@ -1036,7 +1036,7 @@ test "big.int mulWrap multi-multi signed" {
10361036
1037 var c = try Managed.init(testing.allocator);1037 var c = try Managed.init(testing.allocator);
1038 defer c.deinit();1038 defer c.deinit();
1039 try c.mulWrap(a.toConst(), b.toConst(), .signed, @bitSizeOf(SignedDoubleLimb));1039 try c.mulWrap(&a, &b, .signed, @bitSizeOf(SignedDoubleLimb));
10401040
1041 try testing.expect((try c.to(SignedDoubleLimb)) == minInt(SignedDoubleLimb) + 2);1041 try testing.expect((try c.to(SignedDoubleLimb)) == minInt(SignedDoubleLimb) + 2);
1042}1042}
...@@ -1058,9 +1058,9 @@ test "big.int mulWrap large" {...@@ -1058,9 +1058,9 @@ test "big.int mulWrap large" {
10581058
1059 const testbits = @bitSizeOf(Limb) * 64 + 45;1059 const testbits = @bitSizeOf(Limb) * 64 + 45;
10601060
1061 try b.mulWrap(a.toConst(), a.toConst(), .signed, testbits);1061 try b.mulWrap(&a, &a, .signed, testbits);
1062 try c.sqr(a.toConst());1062 try c.sqr(&a);
1063 try c.truncate(c.toConst(), .signed, testbits);1063 try c.truncate(&c, .signed, testbits);
10641064
1065 try testing.expect(b.eq(c));1065 try testing.expect(b.eq(c));
1066}1066}
...@@ -1075,7 +1075,7 @@ test "big.int div single-half no rem" {...@@ -1075,7 +1075,7 @@ test "big.int div single-half no rem" {
1075 defer q.deinit();1075 defer q.deinit();
1076 var r = try Managed.init(testing.allocator);1076 var r = try Managed.init(testing.allocator);
1077 defer r.deinit();1077 defer r.deinit();
1078 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1078 try Managed.divTrunc(&q, &r, &a, &b);
10791079
1080 try testing.expect((try q.to(u32)) == 10);1080 try testing.expect((try q.to(u32)) == 10);
1081 try testing.expect((try r.to(u32)) == 0);1081 try testing.expect((try r.to(u32)) == 0);
...@@ -1091,7 +1091,7 @@ test "big.int div single-half with rem" {...@@ -1091,7 +1091,7 @@ test "big.int div single-half with rem" {
1091 defer q.deinit();1091 defer q.deinit();
1092 var r = try Managed.init(testing.allocator);1092 var r = try Managed.init(testing.allocator);
1093 defer r.deinit();1093 defer r.deinit();
1094 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1094 try Managed.divTrunc(&q, &r, &a, &b);
10951095
1096 try testing.expect((try q.to(u32)) == 9);1096 try testing.expect((try q.to(u32)) == 9);
1097 try testing.expect((try r.to(u32)) == 4);1097 try testing.expect((try r.to(u32)) == 4);
...@@ -1108,7 +1108,7 @@ test "big.int div single-single no rem" {...@@ -1108,7 +1108,7 @@ test "big.int div single-single no rem" {
1108 defer q.deinit();1108 defer q.deinit();
1109 var r = try Managed.init(testing.allocator);1109 var r = try Managed.init(testing.allocator);
1110 defer r.deinit();1110 defer r.deinit();
1111 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1111 try Managed.divTrunc(&q, &r, &a, &b);
11121112
1113 try testing.expect((try q.to(u32)) == 131072);1113 try testing.expect((try q.to(u32)) == 131072);
1114 try testing.expect((try r.to(u32)) == 0);1114 try testing.expect((try r.to(u32)) == 0);
...@@ -1124,7 +1124,7 @@ test "big.int div single-single with rem" {...@@ -1124,7 +1124,7 @@ test "big.int div single-single with rem" {
1124 defer q.deinit();1124 defer q.deinit();
1125 var r = try Managed.init(testing.allocator);1125 var r = try Managed.init(testing.allocator);
1126 defer r.deinit();1126 defer r.deinit();
1127 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1127 try Managed.divTrunc(&q, &r, &a, &b);
11281128
1129 try testing.expect((try q.to(u64)) == 131072);1129 try testing.expect((try q.to(u64)) == 131072);
1130 try testing.expect((try r.to(u64)) == 8589934592);1130 try testing.expect((try r.to(u64)) == 8589934592);
...@@ -1143,7 +1143,7 @@ test "big.int div multi-single no rem" {...@@ -1143,7 +1143,7 @@ test "big.int div multi-single no rem" {
1143 defer q.deinit();1143 defer q.deinit();
1144 var r = try Managed.init(testing.allocator);1144 var r = try Managed.init(testing.allocator);
1145 defer r.deinit();1145 defer r.deinit();
1146 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1146 try Managed.divTrunc(&q, &r, &a, &b);
11471147
1148 try testing.expect((try q.to(u64)) == op1 / op2);1148 try testing.expect((try q.to(u64)) == op1 / op2);
1149 try testing.expect((try r.to(u64)) == 0);1149 try testing.expect((try r.to(u64)) == 0);
...@@ -1162,7 +1162,7 @@ test "big.int div multi-single with rem" {...@@ -1162,7 +1162,7 @@ test "big.int div multi-single with rem" {
1162 defer q.deinit();1162 defer q.deinit();
1163 var r = try Managed.init(testing.allocator);1163 var r = try Managed.init(testing.allocator);
1164 defer r.deinit();1164 defer r.deinit();
1165 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1165 try Managed.divTrunc(&q, &r, &a, &b);
11661166
1167 try testing.expect((try q.to(u64)) == op1 / op2);1167 try testing.expect((try q.to(u64)) == op1 / op2);
1168 try testing.expect((try r.to(u64)) == 3);1168 try testing.expect((try r.to(u64)) == 3);
...@@ -1181,7 +1181,7 @@ test "big.int div multi>2-single" {...@@ -1181,7 +1181,7 @@ test "big.int div multi>2-single" {
1181 defer q.deinit();1181 defer q.deinit();
1182 var r = try Managed.init(testing.allocator);1182 var r = try Managed.init(testing.allocator);
1183 defer r.deinit();1183 defer r.deinit();
1184 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1184 try Managed.divTrunc(&q, &r, &a, &b);
11851185
1186 try testing.expect((try q.to(u128)) == op1 / op2);1186 try testing.expect((try q.to(u128)) == op1 / op2);
1187 try testing.expect((try r.to(u32)) == 0x3e4e);1187 try testing.expect((try r.to(u32)) == 0x3e4e);
...@@ -1197,7 +1197,7 @@ test "big.int div single-single q < r" {...@@ -1197,7 +1197,7 @@ test "big.int div single-single q < r" {
1197 defer q.deinit();1197 defer q.deinit();
1198 var r = try Managed.init(testing.allocator);1198 var r = try Managed.init(testing.allocator);
1199 defer r.deinit();1199 defer r.deinit();
1200 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1200 try Managed.divTrunc(&q, &r, &a, &b);
12011201
1202 try testing.expect((try q.to(u64)) == 0);1202 try testing.expect((try q.to(u64)) == 0);
1203 try testing.expect((try r.to(u64)) == 0x0078f432);1203 try testing.expect((try r.to(u64)) == 0x0078f432);
...@@ -1213,7 +1213,7 @@ test "big.int div single-single q == r" {...@@ -1213,7 +1213,7 @@ test "big.int div single-single q == r" {
1213 defer q.deinit();1213 defer q.deinit();
1214 var r = try Managed.init(testing.allocator);1214 var r = try Managed.init(testing.allocator);
1215 defer r.deinit();1215 defer r.deinit();
1216 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1216 try Managed.divTrunc(&q, &r, &a, &b);
12171217
1218 try testing.expect((try q.to(u64)) == 1);1218 try testing.expect((try q.to(u64)) == 1);
1219 try testing.expect((try r.to(u64)) == 0);1219 try testing.expect((try r.to(u64)) == 0);
...@@ -1225,7 +1225,7 @@ test "big.int div q=0 alias" {...@@ -1225,7 +1225,7 @@ test "big.int div q=0 alias" {
1225 var b = try Managed.initSet(testing.allocator, 10);1225 var b = try Managed.initSet(testing.allocator, 10);
1226 defer b.deinit();1226 defer b.deinit();
12271227
1228 try Managed.divTrunc(&a, &b, a.toConst(), b.toConst());1228 try Managed.divTrunc(&a, &b, &a, &b);
12291229
1230 try testing.expect((try a.to(u64)) == 0);1230 try testing.expect((try a.to(u64)) == 0);
1231 try testing.expect((try b.to(u64)) == 3);1231 try testing.expect((try b.to(u64)) == 3);
...@@ -1243,7 +1243,7 @@ test "big.int div multi-multi q < r" {...@@ -1243,7 +1243,7 @@ test "big.int div multi-multi q < r" {
1243 defer q.deinit();1243 defer q.deinit();
1244 var r = try Managed.init(testing.allocator);1244 var r = try Managed.init(testing.allocator);
1245 defer r.deinit();1245 defer r.deinit();
1246 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1246 try Managed.divTrunc(&q, &r, &a, &b);
12471247
1248 try testing.expect((try q.to(u128)) == 0);1248 try testing.expect((try q.to(u128)) == 0);
1249 try testing.expect((try r.to(u128)) == op1);1249 try testing.expect((try r.to(u128)) == op1);
...@@ -1262,7 +1262,7 @@ test "big.int div trunc single-single +/+" {...@@ -1262,7 +1262,7 @@ test "big.int div trunc single-single +/+" {
1262 defer q.deinit();1262 defer q.deinit();
1263 var r = try Managed.init(testing.allocator);1263 var r = try Managed.init(testing.allocator);
1264 defer r.deinit();1264 defer r.deinit();
1265 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1265 try Managed.divTrunc(&q, &r, &a, &b);
12661266
1267 // n = q * d + r1267 // n = q * d + r
1268 // 5 = 1 * 3 + 21268 // 5 = 1 * 3 + 2
...@@ -1286,7 +1286,7 @@ test "big.int div trunc single-single -/+" {...@@ -1286,7 +1286,7 @@ test "big.int div trunc single-single -/+" {
1286 defer q.deinit();1286 defer q.deinit();
1287 var r = try Managed.init(testing.allocator);1287 var r = try Managed.init(testing.allocator);
1288 defer r.deinit();1288 defer r.deinit();
1289 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1289 try Managed.divTrunc(&q, &r, &a, &b);
12901290
1291 // n = q * d + r1291 // n = q * d + r
1292 // -5 = 1 * -3 - 21292 // -5 = 1 * -3 - 2
...@@ -1310,7 +1310,7 @@ test "big.int div trunc single-single +/-" {...@@ -1310,7 +1310,7 @@ test "big.int div trunc single-single +/-" {
1310 defer q.deinit();1310 defer q.deinit();
1311 var r = try Managed.init(testing.allocator);1311 var r = try Managed.init(testing.allocator);
1312 defer r.deinit();1312 defer r.deinit();
1313 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1313 try Managed.divTrunc(&q, &r, &a, &b);
13141314
1315 // n = q * d + r1315 // n = q * d + r
1316 // 5 = -1 * -3 + 21316 // 5 = -1 * -3 + 2
...@@ -1334,7 +1334,7 @@ test "big.int div trunc single-single -/-" {...@@ -1334,7 +1334,7 @@ test "big.int div trunc single-single -/-" {
1334 defer q.deinit();1334 defer q.deinit();
1335 var r = try Managed.init(testing.allocator);1335 var r = try Managed.init(testing.allocator);
1336 defer r.deinit();1336 defer r.deinit();
1337 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1337 try Managed.divTrunc(&q, &r, &a, &b);
13381338
1339 // n = q * d + r1339 // n = q * d + r
1340 // -5 = 1 * -3 - 21340 // -5 = 1 * -3 - 2
...@@ -1361,7 +1361,7 @@ test "big.int divFloor #10932" {...@@ -1361,7 +1361,7 @@ test "big.int divFloor #10932" {
1361 var mod = try Managed.init(testing.allocator);1361 var mod = try Managed.init(testing.allocator);
1362 defer mod.deinit();1362 defer mod.deinit();
13631363
1364 try res.divFloor(&mod, a.toConst(), b.toConst());1364 try res.divFloor(&mod, &a, &b);
13651365
1366 const ress = try res.toString(testing.allocator, 16, .lower);1366 const ress = try res.toString(testing.allocator, 16, .lower);
1367 defer testing.allocator.free(ress);1367 defer testing.allocator.free(ress);
...@@ -1385,7 +1385,7 @@ test "big.int divFloor #11166" {...@@ -1385,7 +1385,7 @@ test "big.int divFloor #11166" {
1385 var mod = try Managed.init(testing.allocator);1385 var mod = try Managed.init(testing.allocator);
1386 defer mod.deinit();1386 defer mod.deinit();
13871387
1388 try res.divFloor(&mod, a.toConst(), b.toConst());1388 try res.divFloor(&mod, &a, &b);
13891389
1390 const ress = try res.toString(testing.allocator, 10, .lower);1390 const ress = try res.toString(testing.allocator, 10, .lower);
1391 defer testing.allocator.free(ress);1391 defer testing.allocator.free(ress);
...@@ -1409,7 +1409,7 @@ test "big.int gcd #10932" {...@@ -1409,7 +1409,7 @@ test "big.int gcd #10932" {
1409 try a.setString(10, "3000000000000000000000000000000000000000000000000000000000000000000000001461501637330902918203684832716283019655932542975000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");1409 try a.setString(10, "3000000000000000000000000000000000000000000000000000000000000000000000001461501637330902918203684832716283019655932542975000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
1410 try b.setString(10, "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200001001500000000000000000100000000040000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000003000000000000000000000000000000000000000000000000000058715661000000000000000000000000000000000000023553252000000000180000000000000000000000000000000000000000000000000250000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001005000002000000000000000000000000000000000000000021000000001000000000000000000000000100000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000200000000000000000000004000000000000000000000000000000000000000000000301000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");1410 try b.setString(10, "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200001001500000000000000000100000000040000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000003000000000000000000000000000000000000000000000000000058715661000000000000000000000000000000000000023553252000000000180000000000000000000000000000000000000000000000000250000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001005000002000000000000000000000000000000000000000021000000001000000000000000000000000100000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000200000000000000000000004000000000000000000000000000000000000000000000301000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
14111411
1412 try res.gcd(a, b);1412 try res.gcd(&a, &b);
14131413
1414 const ress = try res.toString(testing.allocator, 16, .lower);1414 const ress = try res.toString(testing.allocator, 16, .lower);
1415 defer testing.allocator.free(ress);1415 defer testing.allocator.free(ress);
...@@ -1429,7 +1429,7 @@ test "big.int bitAnd #10932" {...@@ -1429,7 +1429,7 @@ test "big.int bitAnd #10932" {
1429 try a.setString(10, "154954885951624787839743960731760616696");1429 try a.setString(10, "154954885951624787839743960731760616696");
1430 try b.setString(10, "55000000000915215865915724129619485917228346934191537590366734850266784978214506142389798064826139649163838075568111457203909393174933092857416500785632012953993352521899237655507306575657169267399324107627651067352600878339870446048204062696260567762088867991835386857942106708741836433444432529637331429212430394179472179237695833247299409249810963487516399177133175950185719220422442438098353430605822151595560743492661038899294517012784306863064670126197566982968906306814338148792888550378533207318063660581924736840687332023636827401670268933229183389040490792300121030647791095178823932734160000000000000000000000000000000000000555555550000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");1430 try b.setString(10, "55000000000915215865915724129619485917228346934191537590366734850266784978214506142389798064826139649163838075568111457203909393174933092857416500785632012953993352521899237655507306575657169267399324107627651067352600878339870446048204062696260567762088867991835386857942106708741836433444432529637331429212430394179472179237695833247299409249810963487516399177133175950185719220422442438098353430605822151595560743492661038899294517012784306863064670126197566982968906306814338148792888550378533207318063660581924736840687332023636827401670268933229183389040490792300121030647791095178823932734160000000000000000000000000000000000000555555550000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
14311431
1432 try res.bitAnd(a, b);1432 try res.bitAnd(&a, &b);
14331433
1434 try testing.expect((try res.to(i32)) == 0);1434 try testing.expect((try res.to(i32)) == 0);
1435}1435}
...@@ -1447,7 +1447,7 @@ test "big.int div floor single-single +/+" {...@@ -1447,7 +1447,7 @@ test "big.int div floor single-single +/+" {
1447 defer q.deinit();1447 defer q.deinit();
1448 var r = try Managed.init(testing.allocator);1448 var r = try Managed.init(testing.allocator);
1449 defer r.deinit();1449 defer r.deinit();
1450 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());1450 try Managed.divFloor(&q, &r, &a, &b);
14511451
1452 // n = q * d + r1452 // n = q * d + r
1453 // 5 = 1 * 3 + 21453 // 5 = 1 * 3 + 2
...@@ -1471,7 +1471,7 @@ test "big.int div floor single-single -/+" {...@@ -1471,7 +1471,7 @@ test "big.int div floor single-single -/+" {
1471 defer q.deinit();1471 defer q.deinit();
1472 var r = try Managed.init(testing.allocator);1472 var r = try Managed.init(testing.allocator);
1473 defer r.deinit();1473 defer r.deinit();
1474 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());1474 try Managed.divFloor(&q, &r, &a, &b);
14751475
1476 // n = q * d + r1476 // n = q * d + r
1477 // -5 = -2 * 3 + 11477 // -5 = -2 * 3 + 1
...@@ -1495,7 +1495,7 @@ test "big.int div floor single-single +/-" {...@@ -1495,7 +1495,7 @@ test "big.int div floor single-single +/-" {
1495 defer q.deinit();1495 defer q.deinit();
1496 var r = try Managed.init(testing.allocator);1496 var r = try Managed.init(testing.allocator);
1497 defer r.deinit();1497 defer r.deinit();
1498 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());1498 try Managed.divFloor(&q, &r, &a, &b);
14991499
1500 // n = q * d + r1500 // n = q * d + r
1501 // 5 = -2 * -3 - 11501 // 5 = -2 * -3 - 1
...@@ -1519,7 +1519,7 @@ test "big.int div floor single-single -/-" {...@@ -1519,7 +1519,7 @@ test "big.int div floor single-single -/-" {
1519 defer q.deinit();1519 defer q.deinit();
1520 var r = try Managed.init(testing.allocator);1520 var r = try Managed.init(testing.allocator);
1521 defer r.deinit();1521 defer r.deinit();
1522 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());1522 try Managed.divFloor(&q, &r, &a, &b);
15231523
1524 // n = q * d + r1524 // n = q * d + r
1525 // -5 = 2 * -3 + 11525 // -5 = 2 * -3 + 1
...@@ -1543,7 +1543,7 @@ test "big.int div floor no remainder negative quotient" {...@@ -1543,7 +1543,7 @@ test "big.int div floor no remainder negative quotient" {
1543 defer q.deinit();1543 defer q.deinit();
1544 var r = try Managed.init(testing.allocator);1544 var r = try Managed.init(testing.allocator);
1545 defer r.deinit();1545 defer r.deinit();
1546 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());1546 try Managed.divFloor(&q, &r, &a, &b);
15471547
1548 try testing.expect((try q.to(i32)) == -0x80000000);1548 try testing.expect((try q.to(i32)) == -0x80000000);
1549 try testing.expect((try r.to(i32)) == 0);1549 try testing.expect((try r.to(i32)) == 0);
...@@ -1562,7 +1562,7 @@ test "big.int div floor negative close to zero" {...@@ -1562,7 +1562,7 @@ test "big.int div floor negative close to zero" {
1562 defer q.deinit();1562 defer q.deinit();
1563 var r = try Managed.init(testing.allocator);1563 var r = try Managed.init(testing.allocator);
1564 defer r.deinit();1564 defer r.deinit();
1565 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());1565 try Managed.divFloor(&q, &r, &a, &b);
15661566
1567 try testing.expect((try q.to(i32)) == -1);1567 try testing.expect((try q.to(i32)) == -1);
1568 try testing.expect((try r.to(i32)) == 10);1568 try testing.expect((try r.to(i32)) == 10);
...@@ -1581,7 +1581,7 @@ test "big.int div floor positive close to zero" {...@@ -1581,7 +1581,7 @@ test "big.int div floor positive close to zero" {
1581 defer q.deinit();1581 defer q.deinit();
1582 var r = try Managed.init(testing.allocator);1582 var r = try Managed.init(testing.allocator);
1583 defer r.deinit();1583 defer r.deinit();
1584 try Managed.divFloor(&q, &r, a.toConst(), b.toConst());1584 try Managed.divFloor(&q, &r, &a, &b);
15851585
1586 try testing.expect((try q.to(i32)) == 0);1586 try testing.expect((try q.to(i32)) == 0);
1587 try testing.expect((try r.to(i32)) == 10);1587 try testing.expect((try r.to(i32)) == 10);
...@@ -1597,7 +1597,7 @@ test "big.int div multi-multi with rem" {...@@ -1597,7 +1597,7 @@ test "big.int div multi-multi with rem" {
1597 defer q.deinit();1597 defer q.deinit();
1598 var r = try Managed.init(testing.allocator);1598 var r = try Managed.init(testing.allocator);
1599 defer r.deinit();1599 defer r.deinit();
1600 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1600 try Managed.divTrunc(&q, &r, &a, &b);
16011601
1602 try testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);1602 try testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
1603 try testing.expect((try r.to(u128)) == 0x28de0acacd806823638);1603 try testing.expect((try r.to(u128)) == 0x28de0acacd806823638);
...@@ -1613,7 +1613,7 @@ test "big.int div multi-multi no rem" {...@@ -1613,7 +1613,7 @@ test "big.int div multi-multi no rem" {
1613 defer q.deinit();1613 defer q.deinit();
1614 var r = try Managed.init(testing.allocator);1614 var r = try Managed.init(testing.allocator);
1615 defer r.deinit();1615 defer r.deinit();
1616 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1616 try Managed.divTrunc(&q, &r, &a, &b);
16171617
1618 try testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);1618 try testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
1619 try testing.expect((try r.to(u128)) == 0);1619 try testing.expect((try r.to(u128)) == 0);
...@@ -1629,7 +1629,7 @@ test "big.int div multi-multi (2 branch)" {...@@ -1629,7 +1629,7 @@ test "big.int div multi-multi (2 branch)" {
1629 defer q.deinit();1629 defer q.deinit();
1630 var r = try Managed.init(testing.allocator);1630 var r = try Managed.init(testing.allocator);
1631 defer r.deinit();1631 defer r.deinit();
1632 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1632 try Managed.divTrunc(&q, &r, &a, &b);
16331633
1634 try testing.expect((try q.to(u128)) == 0x10000000000000000);1634 try testing.expect((try q.to(u128)) == 0x10000000000000000);
1635 try testing.expect((try r.to(u128)) == 0x44444443444444431111111111111111);1635 try testing.expect((try r.to(u128)) == 0x44444443444444431111111111111111);
...@@ -1645,7 +1645,7 @@ test "big.int div multi-multi (3.1/3.3 branch)" {...@@ -1645,7 +1645,7 @@ test "big.int div multi-multi (3.1/3.3 branch)" {
1645 defer q.deinit();1645 defer q.deinit();
1646 var r = try Managed.init(testing.allocator);1646 var r = try Managed.init(testing.allocator);
1647 defer r.deinit();1647 defer r.deinit();
1648 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1648 try Managed.divTrunc(&q, &r, &a, &b);
16491649
1650 try testing.expect((try q.to(u128)) == 0xfffffffffffffffffff);1650 try testing.expect((try q.to(u128)) == 0xfffffffffffffffffff);
1651 try testing.expect((try r.to(u256)) == 0x1111111111111111111110b12222222222222222282);1651 try testing.expect((try r.to(u256)) == 0x1111111111111111111110b12222222222222222282);
...@@ -1661,7 +1661,7 @@ test "big.int div multi-single zero-limb trailing" {...@@ -1661,7 +1661,7 @@ test "big.int div multi-single zero-limb trailing" {
1661 defer q.deinit();1661 defer q.deinit();
1662 var r = try Managed.init(testing.allocator);1662 var r = try Managed.init(testing.allocator);
1663 defer r.deinit();1663 defer r.deinit();
1664 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1664 try Managed.divTrunc(&q, &r, &a, &b);
16651665
1666 var expected = try Managed.initSet(testing.allocator, 0x6000000000000000000000000000000000000000000000000);1666 var expected = try Managed.initSet(testing.allocator, 0x6000000000000000000000000000000000000000000000000);
1667 defer expected.deinit();1667 defer expected.deinit();
...@@ -1679,7 +1679,7 @@ test "big.int div multi-multi zero-limb trailing (with rem)" {...@@ -1679,7 +1679,7 @@ test "big.int div multi-multi zero-limb trailing (with rem)" {
1679 defer q.deinit();1679 defer q.deinit();
1680 var r = try Managed.init(testing.allocator);1680 var r = try Managed.init(testing.allocator);
1681 defer r.deinit();1681 defer r.deinit();
1682 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1682 try Managed.divTrunc(&q, &r, &a, &b);
16831683
1684 try testing.expect((try q.to(u128)) == 0x10000000000000000);1684 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...@@ -1698,7 +1698,7 @@ test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-li
1698 defer q.deinit();1698 defer q.deinit();
1699 var r = try Managed.init(testing.allocator);1699 var r = try Managed.init(testing.allocator);
1700 defer r.deinit();1700 defer r.deinit();
1701 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1701 try Managed.divTrunc(&q, &r, &a, &b);
17021702
1703 try testing.expect((try q.to(u128)) == 0x1);1703 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...@@ -1717,7 +1717,7 @@ test "big.int div multi-multi zero-limb trailing (with rem) and dividend zero-li
1717 defer q.deinit();1717 defer q.deinit();
1718 var r = try Managed.init(testing.allocator);1718 var r = try Managed.init(testing.allocator);
1719 defer r.deinit();1719 defer r.deinit();
1720 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1720 try Managed.divTrunc(&q, &r, &a, &b);
17211721
1722 const qs = try q.toString(testing.allocator, 16, .lower);1722 const qs = try q.toString(testing.allocator, 16, .lower);
1723 defer testing.allocator.free(qs);1723 defer testing.allocator.free(qs);
...@@ -1741,7 +1741,7 @@ test "big.int div multi-multi fuzz case #1" {...@@ -1741,7 +1741,7 @@ test "big.int div multi-multi fuzz case #1" {
1741 defer q.deinit();1741 defer q.deinit();
1742 var r = try Managed.init(testing.allocator);1742 var r = try Managed.init(testing.allocator);
1743 defer r.deinit();1743 defer r.deinit();
1744 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1744 try Managed.divTrunc(&q, &r, &a, &b);
17451745
1746 const qs = try q.toString(testing.allocator, 16, .lower);1746 const qs = try q.toString(testing.allocator, 16, .lower);
1747 defer testing.allocator.free(qs);1747 defer testing.allocator.free(qs);
...@@ -1765,7 +1765,7 @@ test "big.int div multi-multi fuzz case #2" {...@@ -1765,7 +1765,7 @@ test "big.int div multi-multi fuzz case #2" {
1765 defer q.deinit();1765 defer q.deinit();
1766 var r = try Managed.init(testing.allocator);1766 var r = try Managed.init(testing.allocator);
1767 defer r.deinit();1767 defer r.deinit();
1768 try Managed.divTrunc(&q, &r, a.toConst(), b.toConst());1768 try Managed.divTrunc(&q, &r, &a, &b);
17691769
1770 const qs = try q.toString(testing.allocator, 16, .lower);1770 const qs = try q.toString(testing.allocator, 16, .lower);
1771 defer testing.allocator.free(qs);1771 defer testing.allocator.free(qs);
...@@ -1780,7 +1780,7 @@ test "big.int truncate single unsigned" {...@@ -1780,7 +1780,7 @@ test "big.int truncate single unsigned" {
1780 var a = try Managed.initSet(testing.allocator, maxInt(u47));1780 var a = try Managed.initSet(testing.allocator, maxInt(u47));
1781 defer a.deinit();1781 defer a.deinit();
17821782
1783 try a.truncate(a.toConst(), .unsigned, 17);1783 try a.truncate(&a, .unsigned, 17);
17841784
1785 try testing.expect((try a.to(u17)) == maxInt(u17));1785 try testing.expect((try a.to(u17)) == maxInt(u17));
1786}1786}
...@@ -1789,7 +1789,7 @@ test "big.int truncate single signed" {...@@ -1789,7 +1789,7 @@ test "big.int truncate single signed" {
1789 var a = try Managed.initSet(testing.allocator, 0x1_0000);1789 var a = try Managed.initSet(testing.allocator, 0x1_0000);
1790 defer a.deinit();1790 defer a.deinit();
17911791
1792 try a.truncate(a.toConst(), .signed, 17);1792 try a.truncate(&a, .signed, 17);
17931793
1794 try testing.expect((try a.to(i17)) == minInt(i17));1794 try testing.expect((try a.to(i17)) == minInt(i17));
1795}1795}
...@@ -1798,7 +1798,7 @@ test "big.int truncate multi to single unsigned" {...@@ -1798,7 +1798,7 @@ test "big.int truncate multi to single unsigned" {
1798 var a = try Managed.initSet(testing.allocator, (maxInt(Limb) + 1) | 0x1234_5678_9ABC_DEF0);1798 var a = try Managed.initSet(testing.allocator, (maxInt(Limb) + 1) | 0x1234_5678_9ABC_DEF0);
1799 defer a.deinit();1799 defer a.deinit();
18001800
1801 try a.truncate(a.toConst(), .unsigned, 27);1801 try a.truncate(&a, .unsigned, 27);
18021802
1803 try testing.expect((try a.to(u27)) == 0x2BC_DEF0);1803 try testing.expect((try a.to(u27)) == 0x2BC_DEF0);
1804}1804}
...@@ -1807,7 +1807,7 @@ test "big.int truncate multi to single signed" {...@@ -1807,7 +1807,7 @@ test "big.int truncate multi to single signed" {
1807 var a = try Managed.initSet(testing.allocator, maxInt(Limb) << 10);1807 var a = try Managed.initSet(testing.allocator, maxInt(Limb) << 10);
1808 defer a.deinit();1808 defer a.deinit();
18091809
1810 try a.truncate(a.toConst(), .signed, @bitSizeOf(i11));1810 try a.truncate(&a, .signed, @bitSizeOf(i11));
18111811
1812 try testing.expect((try a.to(i11)) == minInt(i11));1812 try testing.expect((try a.to(i11)) == minInt(i11));
1813}1813}
...@@ -1819,7 +1819,7 @@ test "big.int truncate multi to multi unsigned" {...@@ -1819,7 +1819,7 @@ test "big.int truncate multi to multi unsigned" {
1819 var a = try Managed.initSet(testing.allocator, maxInt(SignedDoubleLimb));1819 var a = try Managed.initSet(testing.allocator, maxInt(SignedDoubleLimb));
1820 defer a.deinit();1820 defer a.deinit();
18211821
1822 try a.truncate(a.toConst(), .unsigned, bits - 1);1822 try a.truncate(&a, .unsigned, bits - 1);
18231823
1824 try testing.expect((try a.to(Int)) == maxInt(Int));1824 try testing.expect((try a.to(Int)) == maxInt(Int));
1825}1825}
...@@ -1828,7 +1828,7 @@ test "big.int truncate multi to multi signed" {...@@ -1828,7 +1828,7 @@ test "big.int truncate multi to multi signed" {
1828 var a = try Managed.initSet(testing.allocator, 3 << @bitSizeOf(Limb));1828 var a = try Managed.initSet(testing.allocator, 3 << @bitSizeOf(Limb));
1829 defer a.deinit();1829 defer a.deinit();
18301830
1831 try a.truncate(a.toConst(), .signed, @bitSizeOf(Limb) + 1);1831 try a.truncate(&a, .signed, @bitSizeOf(Limb) + 1);
18321832
1833 try testing.expect((try a.to(std.meta.Int(.signed, @bitSizeOf(Limb) + 1))) == -1 << @bitSizeOf(Limb));1833 try testing.expect((try a.to(std.meta.Int(.signed, @bitSizeOf(Limb) + 1))) == -1 << @bitSizeOf(Limb));
1834}1834}
...@@ -1837,7 +1837,7 @@ test "big.int truncate negative multi to single" {...@@ -1837,7 +1837,7 @@ test "big.int truncate negative multi to single" {
1837 var a = try Managed.initSet(testing.allocator, -@as(SignedDoubleLimb, maxInt(Limb) + 1));1837 var a = try Managed.initSet(testing.allocator, -@as(SignedDoubleLimb, maxInt(Limb) + 1));
1838 defer a.deinit();1838 defer a.deinit();
18391839
1840 try a.truncate(a.toConst(), .signed, @bitSizeOf(i17));1840 try a.truncate(&a, .signed, @bitSizeOf(i17));
18411841
1842 try testing.expect((try a.to(i17)) == 0);1842 try testing.expect((try a.to(i17)) == 0);
1843}1843}
...@@ -1845,11 +1845,11 @@ test "big.int truncate negative multi to single" {...@@ -1845,11 +1845,11 @@ test "big.int truncate negative multi to single" {
1845test "big.int truncate multi unsigned many" {1845test "big.int truncate multi unsigned many" {
1846 var a = try Managed.initSet(testing.allocator, 1);1846 var a = try Managed.initSet(testing.allocator, 1);
1847 defer a.deinit();1847 defer a.deinit();
1848 try a.shiftLeft(a, 1023);1848 try a.shiftLeft(&a, 1023);
18491849
1850 var b = try Managed.init(testing.allocator);1850 var b = try Managed.init(testing.allocator);
1851 defer b.deinit();1851 defer b.deinit();
1852 try b.truncate(a.toConst(), .signed, @bitSizeOf(i1));1852 try b.truncate(&a, .signed, @bitSizeOf(i1));
18531853
1854 try testing.expect((try b.to(i1)) == 0);1854 try testing.expect((try b.to(i1)) == 0);
1855}1855}
...@@ -1858,7 +1858,7 @@ test "big.int saturate single signed positive" {...@@ -1858,7 +1858,7 @@ test "big.int saturate single signed positive" {
1858 var a = try Managed.initSet(testing.allocator, 0xBBBB_BBBB);1858 var a = try Managed.initSet(testing.allocator, 0xBBBB_BBBB);
1859 defer a.deinit();1859 defer a.deinit();
18601860
1861 try a.saturate(a.toConst(), .signed, 17);1861 try a.saturate(&a, .signed, 17);
18621862
1863 try testing.expect((try a.to(i17)) == maxInt(i17));1863 try testing.expect((try a.to(i17)) == maxInt(i17));
1864}1864}
...@@ -1867,7 +1867,7 @@ test "big.int saturate single signed negative" {...@@ -1867,7 +1867,7 @@ test "big.int saturate single signed negative" {
1867 var a = try Managed.initSet(testing.allocator, -1_234_567);1867 var a = try Managed.initSet(testing.allocator, -1_234_567);
1868 defer a.deinit();1868 defer a.deinit();
18691869
1870 try a.saturate(a.toConst(), .signed, 17);1870 try a.saturate(&a, .signed, 17);
18711871
1872 try testing.expect((try a.to(i17)) == minInt(i17));1872 try testing.expect((try a.to(i17)) == minInt(i17));
1873}1873}
...@@ -1876,7 +1876,7 @@ test "big.int saturate single signed" {...@@ -1876,7 +1876,7 @@ test "big.int saturate single signed" {
1876 var a = try Managed.initSet(testing.allocator, maxInt(i17) - 1);1876 var a = try Managed.initSet(testing.allocator, maxInt(i17) - 1);
1877 defer a.deinit();1877 defer a.deinit();
18781878
1879 try a.saturate(a.toConst(), .signed, 17);1879 try a.saturate(&a, .signed, 17);
18801880
1881 try testing.expect((try a.to(i17)) == maxInt(i17) - 1);1881 try testing.expect((try a.to(i17)) == maxInt(i17) - 1);
1882}1882}
...@@ -1885,7 +1885,7 @@ test "big.int saturate multi signed" {...@@ -1885,7 +1885,7 @@ test "big.int saturate multi signed" {
1885 var a = try Managed.initSet(testing.allocator, maxInt(Limb) << @bitSizeOf(SignedDoubleLimb));1885 var a = try Managed.initSet(testing.allocator, maxInt(Limb) << @bitSizeOf(SignedDoubleLimb));
1886 defer a.deinit();1886 defer a.deinit();
18871887
1888 try a.saturate(a.toConst(), .signed, @bitSizeOf(SignedDoubleLimb));1888 try a.saturate(&a, .signed, @bitSizeOf(SignedDoubleLimb));
18891889
1890 try testing.expect((try a.to(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));1890 try testing.expect((try a.to(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));
1891}1891}
...@@ -1894,7 +1894,7 @@ test "big.int saturate single unsigned" {...@@ -1894,7 +1894,7 @@ test "big.int saturate single unsigned" {
1894 var a = try Managed.initSet(testing.allocator, 0xFEFE_FEFE);1894 var a = try Managed.initSet(testing.allocator, 0xFEFE_FEFE);
1895 defer a.deinit();1895 defer a.deinit();
18961896
1897 try a.saturate(a.toConst(), .unsigned, 23);1897 try a.saturate(&a, .unsigned, 23);
18981898
1899 try testing.expect((try a.to(u23)) == maxInt(u23));1899 try testing.expect((try a.to(u23)) == maxInt(u23));
1900}1900}
...@@ -1903,7 +1903,7 @@ test "big.int saturate multi unsigned zero" {...@@ -1903,7 +1903,7 @@ test "big.int saturate multi unsigned zero" {
1903 var a = try Managed.initSet(testing.allocator, -1);1903 var a = try Managed.initSet(testing.allocator, -1);
1904 defer a.deinit();1904 defer a.deinit();
19051905
1906 try a.saturate(a.toConst(), .unsigned, @bitSizeOf(DoubleLimb));1906 try a.saturate(&a, .unsigned, @bitSizeOf(DoubleLimb));
19071907
1908 try testing.expect(a.eqZero());1908 try testing.expect(a.eqZero());
1909}1909}
...@@ -1912,7 +1912,7 @@ test "big.int saturate multi unsigned" {...@@ -1912,7 +1912,7 @@ test "big.int saturate multi unsigned" {
1912 var a = try Managed.initSet(testing.allocator, maxInt(Limb) << @bitSizeOf(DoubleLimb));1912 var a = try Managed.initSet(testing.allocator, maxInt(Limb) << @bitSizeOf(DoubleLimb));
1913 defer a.deinit();1913 defer a.deinit();
19141914
1915 try a.saturate(a.toConst(), .unsigned, @bitSizeOf(DoubleLimb));1915 try a.saturate(&a, .unsigned, @bitSizeOf(DoubleLimb));
19161916
1917 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb));1917 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb));
1918}1918}
...@@ -1920,7 +1920,7 @@ test "big.int saturate multi unsigned" {...@@ -1920,7 +1920,7 @@ test "big.int saturate multi unsigned" {
1920test "big.int shift-right single" {1920test "big.int shift-right single" {
1921 var a = try Managed.initSet(testing.allocator, 0xffff0000);1921 var a = try Managed.initSet(testing.allocator, 0xffff0000);
1922 defer a.deinit();1922 defer a.deinit();
1923 try a.shiftRight(a, 16);1923 try a.shiftRight(&a, 16);
19241924
1925 try testing.expect((try a.to(u32)) == 0xffff);1925 try testing.expect((try a.to(u32)) == 0xffff);
1926}1926}
...@@ -1928,21 +1928,21 @@ test "big.int shift-right single" {...@@ -1928,21 +1928,21 @@ test "big.int shift-right single" {
1928test "big.int shift-right multi" {1928test "big.int shift-right multi" {
1929 var a = try Managed.initSet(testing.allocator, 0xffff0000eeee1111dddd2222cccc3333);1929 var a = try Managed.initSet(testing.allocator, 0xffff0000eeee1111dddd2222cccc3333);
1930 defer a.deinit();1930 defer a.deinit();
1931 try a.shiftRight(a, 67);1931 try a.shiftRight(&a, 67);
19321932
1933 try testing.expect((try a.to(u64)) == 0x1fffe0001dddc222);1933 try testing.expect((try a.to(u64)) == 0x1fffe0001dddc222);
19341934
1935 try a.set(0xffff0000eeee1111dddd2222cccc3333);1935 try a.set(0xffff0000eeee1111dddd2222cccc3333);
1936 try a.shiftRight(a, 63);1936 try a.shiftRight(&a, 63);
1937 try a.shiftRight(a, 63);1937 try a.shiftRight(&a, 63);
1938 try a.shiftRight(a, 2);1938 try a.shiftRight(&a, 2);
1939 try testing.expect(a.eqZero());1939 try testing.expect(a.eqZero());
1940}1940}
19411941
1942test "big.int shift-left single" {1942test "big.int shift-left single" {
1943 var a = try Managed.initSet(testing.allocator, 0xffff);1943 var a = try Managed.initSet(testing.allocator, 0xffff);
1944 defer a.deinit();1944 defer a.deinit();
1945 try a.shiftLeft(a, 16);1945 try a.shiftLeft(&a, 16);
19461946
1947 try testing.expect((try a.to(u64)) == 0xffff0000);1947 try testing.expect((try a.to(u64)) == 0xffff0000);
1948}1948}
...@@ -1950,7 +1950,7 @@ test "big.int shift-left single" {...@@ -1950,7 +1950,7 @@ test "big.int shift-left single" {
1950test "big.int shift-left multi" {1950test "big.int shift-left multi" {
1951 var a = try Managed.initSet(testing.allocator, 0x1fffe0001dddc222);1951 var a = try Managed.initSet(testing.allocator, 0x1fffe0001dddc222);
1952 defer a.deinit();1952 defer a.deinit();
1953 try a.shiftLeft(a, 67);1953 try a.shiftLeft(&a, 67);
19541954
1955 try testing.expect((try a.to(u128)) == 0xffff0000eeee11100000000000000000);1955 try testing.expect((try a.to(u128)) == 0xffff0000eeee11100000000000000000);
1956}1956}
...@@ -1961,12 +1961,12 @@ test "big.int shift-right negative" {...@@ -1961,12 +1961,12 @@ test "big.int shift-right negative" {
19611961
1962 var arg = try Managed.initSet(testing.allocator, -20);1962 var arg = try Managed.initSet(testing.allocator, -20);
1963 defer arg.deinit();1963 defer arg.deinit();
1964 try a.shiftRight(arg, 2);1964 try a.shiftRight(&arg, 2);
1965 try testing.expect((try a.to(i32)) == -20 >> 2);1965 try testing.expect((try a.to(i32)) == -20 >> 2);
19661966
1967 var arg2 = try Managed.initSet(testing.allocator, -5);1967 var arg2 = try Managed.initSet(testing.allocator, -5);
1968 defer arg2.deinit();1968 defer arg2.deinit();
1969 try a.shiftRight(arg2, 10);1969 try a.shiftRight(&arg2, 10);
1970 try testing.expect((try a.to(i32)) == -5 >> 10);1970 try testing.expect((try a.to(i32)) == -5 >> 10);
1971}1971}
19721972
...@@ -1976,14 +1976,14 @@ test "big.int shift-left negative" {...@@ -1976,14 +1976,14 @@ test "big.int shift-left negative" {
19761976
1977 var arg = try Managed.initSet(testing.allocator, -10);1977 var arg = try Managed.initSet(testing.allocator, -10);
1978 defer arg.deinit();1978 defer arg.deinit();
1979 try a.shiftRight(arg, 1232);1979 try a.shiftRight(&arg, 1232);
1980 try testing.expect((try a.to(i32)) == -10 >> 1232);1980 try testing.expect((try a.to(i32)) == -10 >> 1232);
1981}1981}
19821982
1983test "big.int sat shift-left simple unsigned" {1983test "big.int sat shift-left simple unsigned" {
1984 var a = try Managed.initSet(testing.allocator, 0xffff);1984 var a = try Managed.initSet(testing.allocator, 0xffff);
1985 defer a.deinit();1985 defer a.deinit();
1986 try a.shiftLeftSat(a, 16, .unsigned, 21);1986 try a.shiftLeftSat(&a, 16, .unsigned, 21);
19871987
1988 try testing.expect((try a.to(u64)) == 0x1fffff);1988 try testing.expect((try a.to(u64)) == 0x1fffff);
1989}1989}
...@@ -1991,7 +1991,7 @@ test "big.int sat shift-left simple unsigned" {...@@ -1991,7 +1991,7 @@ test "big.int sat shift-left simple unsigned" {
1991test "big.int sat shift-left simple unsigned no sat" {1991test "big.int sat shift-left simple unsigned no sat" {
1992 var a = try Managed.initSet(testing.allocator, 1);1992 var a = try Managed.initSet(testing.allocator, 1);
1993 defer a.deinit();1993 defer a.deinit();
1994 try a.shiftLeftSat(a, 16, .unsigned, 21);1994 try a.shiftLeftSat(&a, 16, .unsigned, 21);
19951995
1996 try testing.expect((try a.to(u64)) == 0x10000);1996 try testing.expect((try a.to(u64)) == 0x10000);
1997}1997}
...@@ -1999,7 +1999,7 @@ test "big.int sat shift-left simple unsigned no sat" {...@@ -1999,7 +1999,7 @@ test "big.int sat shift-left simple unsigned no sat" {
1999test "big.int sat shift-left multi unsigned" {1999test "big.int sat shift-left multi unsigned" {
2000 var a = try Managed.initSet(testing.allocator, 16);2000 var a = try Managed.initSet(testing.allocator, 16);
2001 defer a.deinit();2001 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
2004 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb) >> 1);2004 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb) >> 1);
2005}2005}
...@@ -2007,7 +2007,7 @@ test "big.int sat shift-left multi unsigned" {...@@ -2007,7 +2007,7 @@ test "big.int sat shift-left multi unsigned" {
2007test "big.int sat shift-left unsigned shift > bitcount" {2007test "big.int sat shift-left unsigned shift > bitcount" {
2008 var a = try Managed.initSet(testing.allocator, 1);2008 var a = try Managed.initSet(testing.allocator, 1);
2009 defer a.deinit();2009 defer a.deinit();
2010 try a.shiftLeftSat(a, 10, .unsigned, 10);2010 try a.shiftLeftSat(&a, 10, .unsigned, 10);
20112011
2012 try testing.expect((try a.to(u10)) == maxInt(u10));2012 try testing.expect((try a.to(u10)) == maxInt(u10));
2013}2013}
...@@ -2015,7 +2015,7 @@ test "big.int sat shift-left unsigned shift > bitcount" {...@@ -2015,7 +2015,7 @@ test "big.int sat shift-left unsigned shift > bitcount" {
2015test "big.int sat shift-left unsigned zero" {2015test "big.int sat shift-left unsigned zero" {
2016 var a = try Managed.initSet(testing.allocator, 0);2016 var a = try Managed.initSet(testing.allocator, 0);
2017 defer a.deinit();2017 defer a.deinit();
2018 try a.shiftLeftSat(a, 1, .unsigned, 0);2018 try a.shiftLeftSat(&a, 1, .unsigned, 0);
20192019
2020 try testing.expect((try a.to(u64)) == 0);2020 try testing.expect((try a.to(u64)) == 0);
2021}2021}
...@@ -2023,7 +2023,7 @@ test "big.int sat shift-left unsigned zero" {...@@ -2023,7 +2023,7 @@ test "big.int sat shift-left unsigned zero" {
2023test "big.int sat shift-left unsigned negative" {2023test "big.int sat shift-left unsigned negative" {
2024 var a = try Managed.initSet(testing.allocator, -100);2024 var a = try Managed.initSet(testing.allocator, -100);
2025 defer a.deinit();2025 defer a.deinit();
2026 try a.shiftLeftSat(a, 0, .unsigned, 0);2026 try a.shiftLeftSat(&a, 0, .unsigned, 0);
20272027
2028 try testing.expect((try a.to(u64)) == 0);2028 try testing.expect((try a.to(u64)) == 0);
2029}2029}
...@@ -2031,7 +2031,7 @@ test "big.int sat shift-left unsigned negative" {...@@ -2031,7 +2031,7 @@ test "big.int sat shift-left unsigned negative" {
2031test "big.int sat shift-left signed simple negative" {2031test "big.int sat shift-left signed simple negative" {
2032 var a = try Managed.initSet(testing.allocator, -100);2032 var a = try Managed.initSet(testing.allocator, -100);
2033 defer a.deinit();2033 defer a.deinit();
2034 try a.shiftLeftSat(a, 3, .signed, 10);2034 try a.shiftLeftSat(&a, 3, .signed, 10);
20352035
2036 try testing.expect((try a.to(i10)) == minInt(i10));2036 try testing.expect((try a.to(i10)) == minInt(i10));
2037}2037}
...@@ -2039,7 +2039,7 @@ test "big.int sat shift-left signed simple negative" {...@@ -2039,7 +2039,7 @@ test "big.int sat shift-left signed simple negative" {
2039test "big.int sat shift-left signed simple positive" {2039test "big.int sat shift-left signed simple positive" {
2040 var a = try Managed.initSet(testing.allocator, 100);2040 var a = try Managed.initSet(testing.allocator, 100);
2041 defer a.deinit();2041 defer a.deinit();
2042 try a.shiftLeftSat(a, 3, .signed, 10);2042 try a.shiftLeftSat(&a, 3, .signed, 10);
20432043
2044 try testing.expect((try a.to(i10)) == maxInt(i10));2044 try testing.expect((try a.to(i10)) == maxInt(i10));
2045}2045}
...@@ -2050,7 +2050,7 @@ test "big.int sat shift-left signed multi positive" {...@@ -2050,7 +2050,7 @@ test "big.int sat shift-left signed multi positive" {
20502050
2051 var a = try Managed.initSet(testing.allocator, x);2051 var a = try Managed.initSet(testing.allocator, x);
2052 defer a.deinit();2052 defer a.deinit();
2053 try a.shiftLeftSat(a, shift, .signed, @bitSizeOf(SignedDoubleLimb));2053 try a.shiftLeftSat(&a, shift, .signed, @bitSizeOf(SignedDoubleLimb));
20542054
2055 try testing.expect((try a.to(SignedDoubleLimb)) == @as(SignedDoubleLimb, x) <<| shift);2055 try testing.expect((try a.to(SignedDoubleLimb)) == @as(SignedDoubleLimb, x) <<| shift);
2056}2056}
...@@ -2061,7 +2061,7 @@ test "big.int sat shift-left signed multi negative" {...@@ -2061,7 +2061,7 @@ test "big.int sat shift-left signed multi negative" {
20612061
2062 var a = try Managed.initSet(testing.allocator, x);2062 var a = try Managed.initSet(testing.allocator, x);
2063 defer a.deinit();2063 defer a.deinit();
2064 try a.shiftLeftSat(a, shift, .signed, @bitSizeOf(SignedDoubleLimb));2064 try a.shiftLeftSat(&a, shift, .signed, @bitSizeOf(SignedDoubleLimb));
20652065
2066 try testing.expect((try a.to(SignedDoubleLimb)) == @as(SignedDoubleLimb, x) <<| shift);2066 try testing.expect((try a.to(SignedDoubleLimb)) == @as(SignedDoubleLimb, x) <<| shift);
2067}2067}
...@@ -2070,7 +2070,7 @@ test "big.int bitNotWrap unsigned simple" {...@@ -2070,7 +2070,7 @@ test "big.int bitNotWrap unsigned simple" {
2070 var a = try Managed.initSet(testing.allocator, 123);2070 var a = try Managed.initSet(testing.allocator, 123);
2071 defer a.deinit();2071 defer a.deinit();
20722072
2073 try a.bitNotWrap(a, .unsigned, 10);2073 try a.bitNotWrap(&a, .unsigned, 10);
20742074
2075 try testing.expect((try a.to(u10)) == ~@as(u10, 123));2075 try testing.expect((try a.to(u10)) == ~@as(u10, 123));
2076}2076}
...@@ -2079,7 +2079,7 @@ test "big.int bitNotWrap unsigned multi" {...@@ -2079,7 +2079,7 @@ test "big.int bitNotWrap unsigned multi" {
2079 var a = try Managed.initSet(testing.allocator, 0);2079 var a = try Managed.initSet(testing.allocator, 0);
2080 defer a.deinit();2080 defer a.deinit();
20812081
2082 try a.bitNotWrap(a, .unsigned, @bitSizeOf(DoubleLimb));2082 try a.bitNotWrap(&a, .unsigned, @bitSizeOf(DoubleLimb));
20832083
2084 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb));2084 try testing.expect((try a.to(DoubleLimb)) == maxInt(DoubleLimb));
2085}2085}
...@@ -2088,7 +2088,7 @@ test "big.int bitNotWrap signed simple" {...@@ -2088,7 +2088,7 @@ test "big.int bitNotWrap signed simple" {
2088 var a = try Managed.initSet(testing.allocator, -456);2088 var a = try Managed.initSet(testing.allocator, -456);
2089 defer a.deinit();2089 defer a.deinit();
20902090
2091 try a.bitNotWrap(a, .signed, 11);2091 try a.bitNotWrap(&a, .signed, 11);
20922092
2093 try testing.expect((try a.to(i11)) == ~@as(i11, -456));2093 try testing.expect((try a.to(i11)) == ~@as(i11, -456));
2094}2094}
...@@ -2097,7 +2097,7 @@ test "big.int bitNotWrap signed multi" {...@@ -2097,7 +2097,7 @@ test "big.int bitNotWrap signed multi" {
2097 var a = try Managed.initSet(testing.allocator, 0);2097 var a = try Managed.initSet(testing.allocator, 0);
2098 defer a.deinit();2098 defer a.deinit();
20992099
2100 try a.bitNotWrap(a, .signed, @bitSizeOf(SignedDoubleLimb));2100 try a.bitNotWrap(&a, .signed, @bitSizeOf(SignedDoubleLimb));
21012101
2102 try testing.expect((try a.to(SignedDoubleLimb)) == -1);2102 try testing.expect((try a.to(SignedDoubleLimb)) == -1);
2103}2103}
...@@ -2108,7 +2108,7 @@ test "big.int bitwise and simple" {...@@ -2108,7 +2108,7 @@ test "big.int bitwise and simple" {
2108 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);2108 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
2109 defer b.deinit();2109 defer b.deinit();
21102110
2111 try a.bitAnd(a, b);2111 try a.bitAnd(&a, &b);
21122112
2113 try testing.expect((try a.to(u64)) == 0xeeeeeeee00000000);2113 try testing.expect((try a.to(u64)) == 0xeeeeeeee00000000);
2114}2114}
...@@ -2119,7 +2119,7 @@ test "big.int bitwise and multi-limb" {...@@ -2119,7 +2119,7 @@ test "big.int bitwise and multi-limb" {
2119 var b = try Managed.initSet(testing.allocator, maxInt(Limb));2119 var b = try Managed.initSet(testing.allocator, maxInt(Limb));
2120 defer b.deinit();2120 defer b.deinit();
21212121
2122 try a.bitAnd(a, b);2122 try a.bitAnd(&a, &b);
21232123
2124 try testing.expect((try a.to(u128)) == 0);2124 try testing.expect((try a.to(u128)) == 0);
2125}2125}
...@@ -2130,7 +2130,7 @@ test "big.int bitwise and negative-positive simple" {...@@ -2130,7 +2130,7 @@ test "big.int bitwise and negative-positive simple" {
2130 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);2130 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
2131 defer b.deinit();2131 defer b.deinit();
21322132
2133 try a.bitAnd(a, b);2133 try a.bitAnd(&a, &b);
21342134
2135 try testing.expect((try a.to(u64)) == 0x22222222);2135 try testing.expect((try a.to(u64)) == 0x22222222);
2136}2136}
...@@ -2141,7 +2141,7 @@ test "big.int bitwise and negative-positive multi-limb" {...@@ -2141,7 +2141,7 @@ test "big.int bitwise and negative-positive multi-limb" {
2141 var b = try Managed.initSet(testing.allocator, maxInt(Limb));2141 var b = try Managed.initSet(testing.allocator, maxInt(Limb));
2142 defer b.deinit();2142 defer b.deinit();
21432143
2144 try a.bitAnd(a, b);2144 try a.bitAnd(&a, &b);
21452145
2146 try testing.expect(a.eqZero());2146 try testing.expect(a.eqZero());
2147}2147}
...@@ -2152,7 +2152,7 @@ test "big.int bitwise and positive-negative simple" {...@@ -2152,7 +2152,7 @@ test "big.int bitwise and positive-negative simple" {
2152 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);2152 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);
2153 defer b.deinit();2153 defer b.deinit();
21542154
2155 try a.bitAnd(a, b);2155 try a.bitAnd(&a, &b);
21562156
2157 try testing.expect((try a.to(u64)) == 0x1111111111111110);2157 try testing.expect((try a.to(u64)) == 0x1111111111111110);
2158}2158}
...@@ -2163,7 +2163,7 @@ test "big.int bitwise and positive-negative multi-limb" {...@@ -2163,7 +2163,7 @@ test "big.int bitwise and positive-negative multi-limb" {
2163 var b = try Managed.initSet(testing.allocator, -maxInt(Limb) - 1);2163 var b = try Managed.initSet(testing.allocator, -maxInt(Limb) - 1);
2164 defer b.deinit();2164 defer b.deinit();
21652165
2166 try a.bitAnd(a, b);2166 try a.bitAnd(&a, &b);
21672167
2168 try testing.expect(a.eqZero());2168 try testing.expect(a.eqZero());
2169}2169}
...@@ -2174,7 +2174,7 @@ test "big.int bitwise and negative-negative simple" {...@@ -2174,7 +2174,7 @@ test "big.int bitwise and negative-negative simple" {
2174 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);2174 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);
2175 defer b.deinit();2175 defer b.deinit();
21762176
2177 try a.bitAnd(a, b);2177 try a.bitAnd(&a, &b);
21782178
2179 try testing.expect((try a.to(i128)) == -0xffffffff33333332);2179 try testing.expect((try a.to(i128)) == -0xffffffff33333332);
2180}2180}
...@@ -2185,7 +2185,7 @@ test "big.int bitwise and negative-negative multi-limb" {...@@ -2185,7 +2185,7 @@ test "big.int bitwise and negative-negative multi-limb" {
2185 var b = try Managed.initSet(testing.allocator, -maxInt(Limb) - 2);2185 var b = try Managed.initSet(testing.allocator, -maxInt(Limb) - 2);
2186 defer b.deinit();2186 defer b.deinit();
21872187
2188 try a.bitAnd(a, b);2188 try a.bitAnd(&a, &b);
21892189
2190 try testing.expect((try a.to(i128)) == -maxInt(Limb) * 2 - 2);2190 try testing.expect((try a.to(i128)) == -maxInt(Limb) * 2 - 2);
2191}2191}
...@@ -2196,7 +2196,7 @@ test "big.int bitwise and negative overflow" {...@@ -2196,7 +2196,7 @@ test "big.int bitwise and negative overflow" {
2196 var b = try Managed.initSet(testing.allocator, -2);2196 var b = try Managed.initSet(testing.allocator, -2);
2197 defer b.deinit();2197 defer b.deinit();
21982198
2199 try a.bitAnd(a, b);2199 try a.bitAnd(&a, &b);
22002200
2201 try testing.expect((try a.to(SignedDoubleLimb)) == -maxInt(Limb) - 1);2201 try testing.expect((try a.to(SignedDoubleLimb)) == -maxInt(Limb) - 1);
2202}2202}
...@@ -2207,7 +2207,7 @@ test "big.int bitwise xor simple" {...@@ -2207,7 +2207,7 @@ test "big.int bitwise xor simple" {
2207 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);2207 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
2208 defer b.deinit();2208 defer b.deinit();
22092209
2210 try a.bitXor(a, b);2210 try a.bitXor(&a, &b);
22112211
2212 try testing.expect((try a.to(u64)) == 0x1111111133333333);2212 try testing.expect((try a.to(u64)) == 0x1111111133333333);
2213}2213}
...@@ -2218,7 +2218,7 @@ test "big.int bitwise xor multi-limb" {...@@ -2218,7 +2218,7 @@ test "big.int bitwise xor multi-limb" {
2218 var b = try Managed.initSet(testing.allocator, maxInt(Limb));2218 var b = try Managed.initSet(testing.allocator, maxInt(Limb));
2219 defer b.deinit();2219 defer b.deinit();
22202220
2221 try a.bitXor(a, b);2221 try a.bitXor(&a, &b);
22222222
2223 try testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) ^ maxInt(Limb));2223 try testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) ^ maxInt(Limb));
2224}2224}
...@@ -2229,7 +2229,7 @@ test "big.int bitwise xor single negative simple" {...@@ -2229,7 +2229,7 @@ test "big.int bitwise xor single negative simple" {
2229 var b = try Managed.initSet(testing.allocator, -0x45fd3acef9191fad);2229 var b = try Managed.initSet(testing.allocator, -0x45fd3acef9191fad);
2230 defer b.deinit();2230 defer b.deinit();
22312231
2232 try a.bitXor(a, b);2232 try a.bitXor(&a, &b);
22332233
2234 try testing.expect((try a.to(i64)) == -0x2efed94fcb932ef9);2234 try testing.expect((try a.to(i64)) == -0x2efed94fcb932ef9);
2235}2235}
...@@ -2240,7 +2240,7 @@ test "big.int bitwise xor single negative zero" {...@@ -2240,7 +2240,7 @@ test "big.int bitwise xor single negative zero" {
2240 var b = try Managed.initSet(testing.allocator, -0);2240 var b = try Managed.initSet(testing.allocator, -0);
2241 defer b.deinit();2241 defer b.deinit();
22422242
2243 try a.bitXor(a, b);2243 try a.bitXor(&a, &b);
22442244
2245 try testing.expect(a.eqZero());2245 try testing.expect(a.eqZero());
2246}2246}
...@@ -2251,7 +2251,7 @@ test "big.int bitwise xor single negative multi-limb" {...@@ -2251,7 +2251,7 @@ test "big.int bitwise xor single negative multi-limb" {
2251 var b = try Managed.initSet(testing.allocator, 0xf2194e7d1c855272a997fcde16f6d5a8);2251 var b = try Managed.initSet(testing.allocator, 0xf2194e7d1c855272a997fcde16f6d5a8);
2252 defer b.deinit();2252 defer b.deinit();
22532253
2254 try a.bitXor(a, b);2254 try a.bitXor(&a, &b);
22552255
2256 try testing.expect((try a.to(i128)) == -0x6a50889abd8834a24db1f19650d3999a);2256 try testing.expect((try a.to(i128)) == -0x6a50889abd8834a24db1f19650d3999a);
2257}2257}
...@@ -2262,7 +2262,7 @@ test "big.int bitwise xor single negative overflow" {...@@ -2262,7 +2262,7 @@ test "big.int bitwise xor single negative overflow" {
2262 var b = try Managed.initSet(testing.allocator, -1);2262 var b = try Managed.initSet(testing.allocator, -1);
2263 defer b.deinit();2263 defer b.deinit();
22642264
2265 try a.bitXor(a, b);2265 try a.bitXor(&a, &b);
22662266
2267 try testing.expect((try a.to(SignedDoubleLimb)) == -(maxInt(Limb) + 1));2267 try testing.expect((try a.to(SignedDoubleLimb)) == -(maxInt(Limb) + 1));
2268}2268}
...@@ -2273,7 +2273,7 @@ test "big.int bitwise xor double negative simple" {...@@ -2273,7 +2273,7 @@ test "big.int bitwise xor double negative simple" {
2273 var b = try Managed.initSet(testing.allocator, -0x4dd4fa576f3046ac);2273 var b = try Managed.initSet(testing.allocator, -0x4dd4fa576f3046ac);
2274 defer b.deinit();2274 defer b.deinit();
22752275
2276 try a.bitXor(a, b);2276 try a.bitXor(&a, &b);
22772277
2278 try testing.expect((try a.to(u64)) == 0xc39c47081a6eb759);2278 try testing.expect((try a.to(u64)) == 0xc39c47081a6eb759);
2279}2279}
...@@ -2284,7 +2284,7 @@ test "big.int bitwise xor double negative multi-limb" {...@@ -2284,7 +2284,7 @@ test "big.int bitwise xor double negative multi-limb" {
2284 var b = try Managed.initSet(testing.allocator, -0xcb07736a7b62289c78d967c3985eebeb);2284 var b = try Managed.initSet(testing.allocator, -0xcb07736a7b62289c78d967c3985eebeb);
2285 defer b.deinit();2285 defer b.deinit();
22862286
2287 try a.bitXor(a, b);2287 try a.bitXor(&a, &b);
22882288
2289 try testing.expect((try a.to(u128)) == 0xa3492ec28e62c410dff92bf0549bf771);2289 try testing.expect((try a.to(u128)) == 0xa3492ec28e62c410dff92bf0549bf771);
2290}2290}
...@@ -2295,7 +2295,7 @@ test "big.int bitwise or simple" {...@@ -2295,7 +2295,7 @@ test "big.int bitwise or simple" {
2295 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);2295 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
2296 defer b.deinit();2296 defer b.deinit();
22972297
2298 try a.bitOr(a, b);2298 try a.bitOr(&a, &b);
22992299
2300 try testing.expect((try a.to(u64)) == 0xffffffff33333333);2300 try testing.expect((try a.to(u64)) == 0xffffffff33333333);
2301}2301}
...@@ -2306,7 +2306,7 @@ test "big.int bitwise or multi-limb" {...@@ -2306,7 +2306,7 @@ test "big.int bitwise or multi-limb" {
2306 var b = try Managed.initSet(testing.allocator, maxInt(Limb));2306 var b = try Managed.initSet(testing.allocator, maxInt(Limb));
2307 defer b.deinit();2307 defer b.deinit();
23082308
2309 try a.bitOr(a, b);2309 try a.bitOr(&a, &b);
23102310
2311 // TODO: big.int.cpp or is wrong on multi-limb.2311 // TODO: big.int.cpp or is wrong on multi-limb.
2312 try testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) + maxInt(Limb));2312 try testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) + maxInt(Limb));
...@@ -2318,7 +2318,7 @@ test "big.int bitwise or negative-positive simple" {...@@ -2318,7 +2318,7 @@ test "big.int bitwise or negative-positive simple" {
2318 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);2318 var b = try Managed.initSet(testing.allocator, 0xeeeeeeee22222222);
2319 defer b.deinit();2319 defer b.deinit();
23202320
2321 try a.bitOr(a, b);2321 try a.bitOr(&a, &b);
23222322
2323 try testing.expect((try a.to(i64)) == -0x1111111111111111);2323 try testing.expect((try a.to(i64)) == -0x1111111111111111);
2324}2324}
...@@ -2329,7 +2329,7 @@ test "big.int bitwise or negative-positive multi-limb" {...@@ -2329,7 +2329,7 @@ test "big.int bitwise or negative-positive multi-limb" {
2329 var b = try Managed.initSet(testing.allocator, 1);2329 var b = try Managed.initSet(testing.allocator, 1);
2330 defer b.deinit();2330 defer b.deinit();
23312331
2332 try a.bitOr(a, b);2332 try a.bitOr(&a, &b);
23332333
2334 try testing.expect((try a.to(SignedDoubleLimb)) == -maxInt(Limb));2334 try testing.expect((try a.to(SignedDoubleLimb)) == -maxInt(Limb));
2335}2335}
...@@ -2340,7 +2340,7 @@ test "big.int bitwise or positive-negative simple" {...@@ -2340,7 +2340,7 @@ test "big.int bitwise or positive-negative simple" {
2340 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);2340 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);
2341 defer b.deinit();2341 defer b.deinit();
23422342
2343 try a.bitOr(a, b);2343 try a.bitOr(&a, &b);
23442344
2345 try testing.expect((try a.to(i64)) == -0x22222221);2345 try testing.expect((try a.to(i64)) == -0x22222221);
2346}2346}
...@@ -2351,7 +2351,7 @@ test "big.int bitwise or positive-negative multi-limb" {...@@ -2351,7 +2351,7 @@ test "big.int bitwise or positive-negative multi-limb" {
2351 var b = try Managed.initSet(testing.allocator, -1);2351 var b = try Managed.initSet(testing.allocator, -1);
2352 defer b.deinit();2352 defer b.deinit();
23532353
2354 try a.bitOr(a, b);2354 try a.bitOr(&a, &b);
23552355
2356 try testing.expect((try a.to(SignedDoubleLimb)) == -1);2356 try testing.expect((try a.to(SignedDoubleLimb)) == -1);
2357}2357}
...@@ -2362,7 +2362,7 @@ test "big.int bitwise or negative-negative simple" {...@@ -2362,7 +2362,7 @@ test "big.int bitwise or negative-negative simple" {
2362 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);2362 var b = try Managed.initSet(testing.allocator, -0xeeeeeeee22222222);
2363 defer b.deinit();2363 defer b.deinit();
23642364
2365 try a.bitOr(a, b);2365 try a.bitOr(&a, &b);
23662366
2367 try testing.expect((try a.to(i128)) == -0xeeeeeeee00000001);2367 try testing.expect((try a.to(i128)) == -0xeeeeeeee00000001);
2368}2368}
...@@ -2373,7 +2373,7 @@ test "big.int bitwise or negative-negative multi-limb" {...@@ -2373,7 +2373,7 @@ test "big.int bitwise or negative-negative multi-limb" {
2373 var b = try Managed.initSet(testing.allocator, -maxInt(Limb));2373 var b = try Managed.initSet(testing.allocator, -maxInt(Limb));
2374 defer b.deinit();2374 defer b.deinit();
23752375
2376 try a.bitOr(a, b);2376 try a.bitOr(&a, &b);
23772377
2378 try testing.expect((try a.to(SignedDoubleLimb)) == -maxInt(Limb));2378 try testing.expect((try a.to(SignedDoubleLimb)) == -maxInt(Limb));
2379}2379}
...@@ -2384,7 +2384,7 @@ test "big.int var args" {...@@ -2384,7 +2384,7 @@ test "big.int var args" {
23842384
2385 var b = try Managed.initSet(testing.allocator, 6);2385 var b = try Managed.initSet(testing.allocator, 6);
2386 defer b.deinit();2386 defer b.deinit();
2387 try a.add(a.toConst(), b.toConst());2387 try a.add(&a, &b);
2388 try testing.expect((try a.to(u64)) == 11);2388 try testing.expect((try a.to(u64)) == 11);
23892389
2390 var c = try Managed.initSet(testing.allocator, 11);2390 var c = try Managed.initSet(testing.allocator, 11);
...@@ -2404,7 +2404,7 @@ test "big.int gcd non-one small" {...@@ -2404,7 +2404,7 @@ test "big.int gcd non-one small" {
2404 var r = try Managed.init(testing.allocator);2404 var r = try Managed.init(testing.allocator);
2405 defer r.deinit();2405 defer r.deinit();
24062406
2407 try r.gcd(a, b);2407 try r.gcd(&a, &b);
24082408
2409 try testing.expect((try r.to(u32)) == 1);2409 try testing.expect((try r.to(u32)) == 1);
2410}2410}
...@@ -2417,7 +2417,7 @@ test "big.int gcd non-one small" {...@@ -2417,7 +2417,7 @@ test "big.int gcd non-one small" {
2417 var r = try Managed.init(testing.allocator);2417 var r = try Managed.init(testing.allocator);
2418 defer r.deinit();2418 defer r.deinit();
24192419
2420 try r.gcd(a, b);2420 try r.gcd(&a, &b);
24212421
2422 try testing.expect((try r.to(u32)) == 38);2422 try testing.expect((try r.to(u32)) == 38);
2423}2423}
...@@ -2430,7 +2430,7 @@ test "big.int gcd non-one large" {...@@ -2430,7 +2430,7 @@ test "big.int gcd non-one large" {
2430 var r = try Managed.init(testing.allocator);2430 var r = try Managed.init(testing.allocator);
2431 defer r.deinit();2431 defer r.deinit();
24322432
2433 try r.gcd(a, b);2433 try r.gcd(&a, &b);
24342434
2435 try testing.expect((try r.to(u32)) == 4369);2435 try testing.expect((try r.to(u32)) == 4369);
2436}2436}
...@@ -2443,7 +2443,7 @@ test "big.int gcd large multi-limb result" {...@@ -2443,7 +2443,7 @@ test "big.int gcd large multi-limb result" {
2443 var r = try Managed.init(testing.allocator);2443 var r = try Managed.init(testing.allocator);
2444 defer r.deinit();2444 defer r.deinit();
24452445
2446 try r.gcd(a, b);2446 try r.gcd(&a, &b);
24472447
2448 const answer = (try r.to(u256));2448 const answer = (try r.to(u256));
2449 try testing.expect(answer == 0xf000000ff00000fff0000ffff000fffff00ffffff1);2449 try testing.expect(answer == 0xf000000ff00000fff0000ffff000fffff00ffffff1);
...@@ -2457,7 +2457,7 @@ test "big.int gcd one large" {...@@ -2457,7 +2457,7 @@ test "big.int gcd one large" {
2457 var r = try Managed.init(testing.allocator);2457 var r = try Managed.init(testing.allocator);
2458 defer r.deinit();2458 defer r.deinit();
24592459
2460 try r.gcd(a, b);2460 try r.gcd(&a, &b);
24612461
2462 try testing.expect((try r.to(u64)) == 1);2462 try testing.expect((try r.to(u64)) == 1);
2463}2463}
...@@ -2488,10 +2488,10 @@ test "big.int pow" {...@@ -2488,10 +2488,10 @@ test "big.int pow" {
2488 var a = try Managed.initSet(testing.allocator, -3);2488 var a = try Managed.initSet(testing.allocator, -3);
2489 defer a.deinit();2489 defer a.deinit();
24902490
2491 try a.pow(a.toConst(), 3);2491 try a.pow(&a, 3);
2492 try testing.expectEqual(@as(i32, -27), try a.to(i32));2492 try testing.expectEqual(@as(i32, -27), try a.to(i32));
24932493
2494 try a.pow(a.toConst(), 4);2494 try a.pow(&a, 4);
2495 try testing.expectEqual(@as(i32, 531441), try a.to(i32));2495 try testing.expectEqual(@as(i32, 531441), try a.to(i32));
2496 }2496 }
2497 {2497 {
...@@ -2502,9 +2502,9 @@ test "big.int pow" {...@@ -2502,9 +2502,9 @@ test "big.int pow" {
2502 defer y.deinit();2502 defer y.deinit();
25032503
2504 // y and a are not aliased2504 // y and a are not aliased
2505 try y.pow(a.toConst(), 123);2505 try y.pow(&a, 123);
2506 // y and a are aliased2506 // y and a are aliased
2507 try a.pow(a.toConst(), 123);2507 try a.pow(&a, 123);
25082508
2509 try testing.expect(a.eq(y));2509 try testing.expect(a.eq(y));
25102510
...@@ -2522,18 +2522,18 @@ test "big.int pow" {...@@ -2522,18 +2522,18 @@ test "big.int pow" {
2522 var a = try Managed.initSet(testing.allocator, 0);2522 var a = try Managed.initSet(testing.allocator, 0);
2523 defer a.deinit();2523 defer a.deinit();
25242524
2525 try a.pow(a.toConst(), 100);2525 try a.pow(&a, 100);
2526 try testing.expectEqual(@as(i32, 0), try a.to(i32));2526 try testing.expectEqual(@as(i32, 0), try a.to(i32));
25272527
2528 try a.set(1);2528 try a.set(1);
2529 try a.pow(a.toConst(), 0);2529 try a.pow(&a, 0);
2530 try testing.expectEqual(@as(i32, 1), try a.to(i32));2530 try testing.expectEqual(@as(i32, 1), try a.to(i32));
2531 try a.pow(a.toConst(), 100);2531 try a.pow(&a, 100);
2532 try testing.expectEqual(@as(i32, 1), try a.to(i32));2532 try testing.expectEqual(@as(i32, 1), try a.to(i32));
2533 try a.set(-1);2533 try a.set(-1);
2534 try a.pow(a.toConst(), 15);2534 try a.pow(&a, 15);
2535 try testing.expectEqual(@as(i32, -1), try a.to(i32));2535 try testing.expectEqual(@as(i32, -1), try a.to(i32));
2536 try a.pow(a.toConst(), 16);2536 try a.pow(&a, 16);
2537 try testing.expectEqual(@as(i32, 1), try a.to(i32));2537 try testing.expectEqual(@as(i32, 1), try a.to(i32));
2538 }2538 }
2539}2539}
...@@ -2547,7 +2547,7 @@ test "big.int regression test for 1 limb overflow with alias" {...@@ -2547,7 +2547,7 @@ test "big.int regression test for 1 limb overflow with alias" {
2547 defer b.deinit();2547 defer b.deinit();
25482548
2549 try a.ensureAddCapacity(a.toConst(), b.toConst());2549 try a.ensureAddCapacity(a.toConst(), b.toConst());
2550 try a.add(a.toConst(), b.toConst());2550 try a.add(&a, &b);
25512551
2552 try testing.expect(a.toConst().orderAgainstScalar(19740274219868223167) == .eq);2552 try testing.expect(a.toConst().orderAgainstScalar(19740274219868223167) == .eq);
2553}2553}
...@@ -2561,7 +2561,7 @@ test "big.int regression test for realloc with alias" {...@@ -2561,7 +2561,7 @@ test "big.int regression test for realloc with alias" {
2561 defer b.deinit();2561 defer b.deinit();
25622562
2563 try a.ensureAddCapacity(a.toConst(), b.toConst());2563 try a.ensureAddCapacity(a.toConst(), b.toConst());
2564 try a.add(a.toConst(), b.toConst());2564 try a.add(&a, &b);
25652565
2566 try testing.expect(a.toConst().orderAgainstScalar(14691098406862188148944207245954912110548093601382197697835) == .eq);2566 try testing.expect(a.toConst().orderAgainstScalar(14691098406862188148944207245954912110548093601382197697835) == .eq);
2567}2567}
...@@ -2572,7 +2572,7 @@ test "big int popcount" {...@@ -2572,7 +2572,7 @@ test "big int popcount" {
2572 var b = try Managed.initSet(testing.allocator, -1);2572 var b = try Managed.initSet(testing.allocator, -1);
2573 defer b.deinit();2573 defer b.deinit();
25742574
2575 try a.popCount(b.toConst(), 16);2575 try a.popCount(&b, 16);
25762576
2577 try testing.expect(a.toConst().orderAgainstScalar(16) == .eq);2577 try testing.expect(a.toConst().orderAgainstScalar(16) == .eq);
2578}2578}
lib/std/math/big/rational.zig+28-27
...@@ -102,20 +102,23 @@ pub const Rational = struct {...@@ -102,20 +102,23 @@ pub const Rational = struct {
102 try self.p.setString(10, str[0..i]);102 try self.p.setString(10, str[0..i]);
103103
104 const base = IntConst{ .limbs = &[_]Limb{10}, .positive = true };104 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
106 var j: usize = start;109 var j: usize = start;
107 while (j < str.len - i - 1) : (j += 1) {110 while (j < str.len - i - 1) : (j += 1) {
108 try self.p.ensureMulCapacity(self.p.toConst(), base);111 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);
110 }113 }
111114
112 try self.q.setString(10, str[i + 1 ..]);115 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
115 try self.q.set(1);118 try self.q.set(1);
116 var k: usize = i + 1;119 var k: usize = i + 1;
117 while (k < str.len) : (k += 1) {120 while (k < str.len) : (k += 1) {
118 try self.q.mul(self.q.toConst(), base);121 try self.q.mul(&self.q, &base_managed);
119 }122 }
120123
121 try self.reduce();124 try self.reduce();
...@@ -172,9 +175,9 @@ pub const Rational = struct {...@@ -172,9 +175,9 @@ pub const Rational = struct {
172175
173 try self.q.set(1);176 try self.q.set(1);
174 if (shift >= 0) {177 if (shift >= 0) {
175 try self.q.shiftLeft(self.q, @intCast(usize, shift));178 try self.q.shiftLeft(&self.q, @intCast(usize, shift));
176 } else {179 } else {
177 try self.p.shiftLeft(self.p, @intCast(usize, -shift));180 try self.p.shiftLeft(&self.p, @intCast(usize, -shift));
178 }181 }
179182
180 try self.reduce();183 try self.reduce();
...@@ -215,9 +218,9 @@ pub const Rational = struct {...@@ -215,9 +218,9 @@ pub const Rational = struct {
215218
216 const shift = msize2 - exp;219 const shift = msize2 - exp;
217 if (shift >= 0) {220 if (shift >= 0) {
218 try a2.shiftLeft(a2, @intCast(usize, shift));221 try a2.shiftLeft(&a2, @intCast(usize, shift));
219 } else {222 } else {
220 try b2.shiftLeft(b2, @intCast(usize, -shift));223 try b2.shiftLeft(&b2, @intCast(usize, -shift));
221 }224 }
222225
223 // 2. compute quotient and remainder226 // 2. compute quotient and remainder
...@@ -228,7 +231,7 @@ pub const Rational = struct {...@@ -228,7 +231,7 @@ pub const Rational = struct {
228 var r = try Int.init(self.p.allocator);231 var r = try Int.init(self.p.allocator);
229 defer r.deinit();232 defer r.deinit();
230233
231 try Int.divTrunc(&q, &r, a2.toConst(), b2.toConst());234 try Int.divTrunc(&q, &r, &a2, &b2);
232235
233 var mantissa = extractLowBits(q, BitReprType);236 var mantissa = extractLowBits(q, BitReprType);
234 var have_rem = r.len() > 0;237 var have_rem = r.len() > 0;
...@@ -350,8 +353,8 @@ pub const Rational = struct {...@@ -350,8 +353,8 @@ pub const Rational = struct {
350 var p = try Int.init(b.p.allocator);353 var p = try Int.init(b.p.allocator);
351 defer p.deinit();354 defer p.deinit();
352355
353 try q.mul(a.p.toConst(), b.q.toConst());356 try q.mul(&a.p, &b.q);
354 try p.mul(b.p.toConst(), a.q.toConst());357 try p.mul(&b.p, &a.q);
355358
356 return if (is_abs) q.orderAbs(p) else q.order(p);359 return if (is_abs) q.orderAbs(p) else q.order(p);
357 }360 }
...@@ -376,11 +379,11 @@ pub const Rational = struct {...@@ -376,11 +379,11 @@ pub const Rational = struct {
376 r.deinit();379 r.deinit();
377 };380 };
378381
379 try r.p.mul(a.p.toConst(), b.q.toConst());382 try r.p.mul(&a.p, &b.q);
380 try r.q.mul(b.p.toConst(), a.q.toConst());383 try r.q.mul(&b.p, &a.q);
381 try r.p.add(r.p.toConst(), r.q.toConst());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);
384 try r.reduce();387 try r.reduce();
385 }388 }
386389
...@@ -404,11 +407,11 @@ pub const Rational = struct {...@@ -404,11 +407,11 @@ pub const Rational = struct {
404 r.deinit();407 r.deinit();
405 };408 };
406409
407 try r.p.mul(a.p.toConst(), b.q.toConst());410 try r.p.mul(&a.p, &b.q);
408 try r.q.mul(b.p.toConst(), a.q.toConst());411 try r.q.mul(&b.p, &a.q);
409 try r.p.sub(r.p.toConst(), r.q.toConst());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);
412 try r.reduce();415 try r.reduce();
413 }416 }
414417
...@@ -418,8 +421,8 @@ pub const Rational = struct {...@@ -418,8 +421,8 @@ pub const Rational = struct {
418 ///421 ///
419 /// Returns an error if memory could not be allocated.422 /// Returns an error if memory could not be allocated.
420 pub fn mul(r: *Rational, a: Rational, b: Rational) !void {423 pub fn mul(r: *Rational, a: Rational, b: Rational) !void {
421 try r.p.mul(a.p.toConst(), b.p.toConst());424 try r.p.mul(&a.p, &b.p);
422 try r.q.mul(a.q.toConst(), b.q.toConst());425 try r.q.mul(&a.q, &b.q);
423 try r.reduce();426 try r.reduce();
424 }427 }
425428
...@@ -433,8 +436,8 @@ pub const Rational = struct {...@@ -433,8 +436,8 @@ pub const Rational = struct {
433 @panic("division by zero");436 @panic("division by zero");
434 }437 }
435438
436 try r.p.mul(a.p.toConst(), b.q.toConst());439 try r.p.mul(&a.p, &b.q);
437 try r.q.mul(b.p.toConst(), a.q.toConst());440 try r.q.mul(&b.p, &a.q);
438 try r.reduce();441 try r.reduce();
439 }442 }
440443
...@@ -450,7 +453,7 @@ pub const Rational = struct {...@@ -450,7 +453,7 @@ pub const Rational = struct {
450453
451 const sign = r.p.isPositive();454 const sign = r.p.isPositive();
452 r.p.abs();455 r.p.abs();
453 try a.gcd(r.p, r.q);456 try a.gcd(&r.p, &r.q);
454 r.p.setSign(sign);457 r.p.setSign(sign);
455458
456 const one = IntConst{ .limbs = &[_]Limb{1}, .positive = true };459 const one = IntConst{ .limbs = &[_]Limb{1}, .positive = true };
...@@ -460,8 +463,8 @@ pub const Rational = struct {...@@ -460,8 +463,8 @@ pub const Rational = struct {
460463
461 // TODO: divexact would be useful here464 // TODO: divexact would be useful here
462 // TODO: don't copy r.q for div465 // TODO: don't copy r.q for div
463 try Int.divTrunc(&r.p, &unused, r.p.toConst(), a.toConst());466 try Int.divTrunc(&r.p, &unused, &r.p, &a);
464 try Int.divTrunc(&r.q, &unused, r.q.toConst(), a.toConst());467 try Int.divTrunc(&r.q, &unused, &r.q, &a);
465 }468 }
466 }469 }
467};470};
...@@ -573,7 +576,6 @@ test "big.rational setFloatString" {...@@ -573,7 +576,6 @@ test "big.rational setFloatString" {
573}576}
574577
575test "big.rational toFloat" {578test "big.rational toFloat" {
576 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
577 var a = try Rational.init(testing.allocator);579 var a = try Rational.init(testing.allocator);
578 defer a.deinit();580 defer a.deinit();
579581
...@@ -587,7 +589,6 @@ test "big.rational toFloat" {...@@ -587,7 +589,6 @@ test "big.rational toFloat" {
587}589}
588590
589test "big.rational set/to Float round-trip" {591test "big.rational set/to Float round-trip" {
590 if (@import("builtin").zig_backend != .stage1) return error.SkipZigTest;
591 var a = try Rational.init(testing.allocator);592 var a = try Rational.init(testing.allocator);
592 defer a.deinit();593 defer a.deinit();
593 var prng = std.rand.DefaultPrng.init(0x5EED);594 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 {...@@ -85,7 +85,7 @@ pub fn spans(self: *RangeSet, first: Value, last: Value, ty: Type) !bool {
8585
86 // prev.last + 1 == cur.first86 // prev.last + 1 == cur.first
87 try counter.copy(prev.last.toBigInt(&space, target));87 try counter.copy(prev.last.toBigInt(&space, target));
88 try counter.addScalar(counter.toConst(), 1);88 try counter.addScalar(&counter, 1);
8989
90 const cur_start_int = cur.first.toBigInt(&space, target);90 const cur_start_int = cur.first.toBigInt(&space, target);
91 if (!cur_start_int.eq(counter.toConst())) {91 if (!cur_start_int.eq(counter.toConst())) {
src/Sema.zig+4-4
...@@ -22706,9 +22706,9 @@ fn cmpNumeric(...@@ -22706,9 +22706,9 @@ fn cmpNumeric(
22706 else => {},22706 else => {},
22707 }22707 }
22708 if (lhs_is_signed) {22708 if (lhs_is_signed) {
22709 try bigint.addScalar(bigint.toConst(), -1);22709 try bigint.addScalar(&bigint, -1);
22710 } else {22710 } else {
22711 try bigint.addScalar(bigint.toConst(), 1);22711 try bigint.addScalar(&bigint, 1);
22712 }22712 }
22713 }22713 }
22714 lhs_bits = bigint.toConst().bitCountTwosComp();22714 lhs_bits = bigint.toConst().bitCountTwosComp();
...@@ -22752,9 +22752,9 @@ fn cmpNumeric(...@@ -22752,9 +22752,9 @@ fn cmpNumeric(
22752 else => {},22752 else => {},
22753 }22753 }
22754 if (rhs_is_signed) {22754 if (rhs_is_signed) {
22755 try bigint.addScalar(bigint.toConst(), -1);22755 try bigint.addScalar(&bigint, -1);
22756 } else {22756 } else {
22757 try bigint.addScalar(bigint.toConst(), 1);22757 try bigint.addScalar(&bigint, 1);
22758 }22758 }
22759 }22759 }
22760 rhs_bits = bigint.toConst().bitCountTwosComp();22760 rhs_bits = bigint.toConst().bitCountTwosComp();