authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-09-27 04:58:27+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-04 11:25:29+02:00
log69be6ba8eea8e0b64153a0d0676f1b8749df8195
treea441df4004b5aa60d4b23ba2b7bc5be299b5fc08
parentfdb37743fa48bc1ca41a11148dd5d801e9c3a34e

Comptime wrapping addition/subtraction


2 files changed, 36 insertions(+), 21 deletions(-)

lib/std/math/big/int.zig+2-2
......@@ -444,7 +444,7 @@ pub const Mutable = struct {
444444 const b_limbs = b.limbs[0..math.min(req_limbs, b.limbs.len)];
445445
446446 if (a.positive != b.positive) {
447 if (a.positive) {
447 if (a.positive) {
448448 // (a) - (-b) => a + b
449449 r.addWrap(a, b.abs(), signedness, bit_count);
450450 } else {
......@@ -1107,7 +1107,7 @@ pub const Mutable = struct {
11071107
11081108 // Zero-extend the result
11091109 if (req_limbs > r.len) {
1110 mem.set(Limb, r.limbs[r.len .. req_limbs], 0);
1110 mem.set(Limb, r.limbs[r.len..req_limbs], 0);
11111111 }
11121112
11131113 // Truncate to required number of limbs.
src/value.zig+34-19
......@@ -1660,19 +1660,26 @@ pub const Value = extern union {
16601660 if (ty.isAnyFloat()) {
16611661 return floatAdd(lhs, rhs, ty, arena);
16621662 }
1663 const result = try intAdd(lhs, rhs, arena);
16641663
1665 const max = try ty.maxInt(arena, target);
1666 if (compare(result, .gt, max, ty)) {
1667 @panic("TODO comptime wrapping integer addition");
1668 }
1664 const info = ty.intInfo(target);
16691665
1670 const min = try ty.minInt(arena, target);
1671 if (compare(result, .lt, min, ty)) {
1672 @panic("TODO comptime wrapping integer addition");
1673 }
1666 var lhs_space: Value.BigIntSpace = undefined;
1667 var rhs_space: Value.BigIntSpace = undefined;
1668 const lhs_bigint = lhs.toBigInt(&lhs_space);
1669 const rhs_bigint = rhs.toBigInt(&rhs_space);
1670 const limbs = try arena.alloc(
1671 std.math.big.Limb,
1672 std.math.max(lhs_bigint.limbs.len, rhs_bigint.limbs.len) + 1,
1673 );
1674 var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined };
1675 result_bigint.addWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits);
1676 const result_limbs = result_bigint.limbs[0..result_bigint.len];
16741677
1675 return result;
1678 if (result_bigint.positive) {
1679 return Value.Tag.int_big_positive.create(arena, result_limbs);
1680 } else {
1681 return Value.Tag.int_big_negative.create(arena, result_limbs);
1682 }
16761683 }
16771684
16781685 /// Supports integers only; asserts neither operand is undefined.
......@@ -1714,19 +1721,27 @@ pub const Value = extern union {
17141721 if (ty.isAnyFloat()) {
17151722 return floatSub(lhs, rhs, ty, arena);
17161723 }
1717 const result = try intSub(lhs, rhs, arena);
17181724
1719 const max = try ty.maxInt(arena, target);
1720 if (compare(result, .gt, max, ty)) {
1721 @panic("TODO comptime wrapping integer subtraction");
1722 }
1725 const info = ty.intInfo(target);
17231726
1724 const min = try ty.minInt(arena, target);
1725 if (compare(result, .lt, min, ty)) {
1726 @panic("TODO comptime wrapping integer subtraction");
1727 var lhs_space: Value.BigIntSpace = undefined;
1728 var rhs_space: Value.BigIntSpace = undefined;
1729 const lhs_bigint = lhs.toBigInt(&lhs_space);
1730 const rhs_bigint = rhs.toBigInt(&rhs_space);
1731 const limbs = try arena.alloc(
1732 std.math.big.Limb,
1733 std.math.max(lhs_bigint.limbs.len, rhs_bigint.limbs.len) + 1,
1734 );
1735 var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined };
1736 result_bigint.subWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits);
1737 const result_limbs = result_bigint.limbs[0..result_bigint.len];
1738
1739 if (result_bigint.positive) {
1740 return Value.Tag.int_big_positive.create(arena, result_limbs);
1741 } else {
1742 return Value.Tag.int_big_negative.create(arena, result_limbs);
17271743 }
17281744
1729 return result;
17301745 }
17311746
17321747 /// Supports integers only; asserts neither operand is undefined.