authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-09-23 06:22:18+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-09-23 06:28:09+02:00
logcd3dcc225b52be981b52b71dcdb34ba176f4081b
tree84ee71c1b73a8c4374cea816e6abc64ff261e29d
parent351e4f07cebc8299c107bd3de760efe17d184af7

big ints: only write xor overflow if required


1 files changed, 8 insertions(+), 1 deletions(-)

lib/std/math/big/int.zig+8-1
...@@ -2495,6 +2495,8 @@ fn llsignedand(r: []Limb, a: []const Limb, a_positive: bool, b: []const Limb, b_...@@ -2495,6 +2495,8 @@ fn llsignedand(r: []Limb, a: []const Limb, a_positive: bool, b: []const Limb, b_
2495// r may alias.2495// r may alias.
2496// a and b must not be -0.2496// a and b must not be -0.
2497// Returns `true` when the result is positive.2497// Returns `true` when the result is positive.
2498// If the sign of a and b is equal, then r requires at least `max(a.len, b.len)` limbs are required.
2499// Otherwise, r requires at least `max(a.len, b.len) + 1` limbs.
2498fn llsignedxor(r: []Limb, a: []const Limb, a_positive: bool, b: []const Limb, b_positive: bool) bool {2500fn llsignedxor(r: []Limb, a: []const Limb, a_positive: bool, b: []const Limb, b_positive: bool) bool {
2499 @setRuntimeSafety(debug_safety);2501 @setRuntimeSafety(debug_safety);
2500 assert(a.len != 0 and b.len != 0);2502 assert(a.len != 0 and b.len != 0);
...@@ -2538,7 +2540,12 @@ fn llsignedxor(r: []Limb, a: []const Limb, a_positive: bool, b: []const Limb, b_...@@ -2538,7 +2540,12 @@ fn llsignedxor(r: []Limb, a: []const Limb, a_positive: bool, b: []const Limb, b_
2538 r_carry = @boolToInt(@addWithOverflow(Limb, r[i], r_carry, &r[i]));2540 r_carry = @boolToInt(@addWithOverflow(Limb, r[i], r_carry, &r[i]));
2539 }2541 }
25402542
2541 r[i] = r_carry;2543 // If both inputs don't share the same sign, an extra limb is required.
2544 if (a_positive != b_positive) {
2545 r[i] = r_carry;
2546 } else {
2547 assert(r_carry == 0);
2548 }
25422549
2543 assert(a_borrow == 0);2550 assert(a_borrow == 0);
2544 assert(b_borrow == 0);2551 assert(b_borrow == 0);