Comptime wrapping addition/subtraction

This commit is contained in:
Robin Voetter 2021-09-27 04:58:27 +02:00
parent fdb37743fa
commit 69be6ba8ee
2 changed files with 38 additions and 23 deletions

View File

@ -444,7 +444,7 @@ pub const Mutable = struct {
const b_limbs = b.limbs[0..math.min(req_limbs, b.limbs.len)];
if (a.positive != b.positive) {
if (a.positive) {
if (a.positive) {
// (a) - (-b) => a + b
r.addWrap(a, b.abs(), signedness, bit_count);
} else {
@ -1107,7 +1107,7 @@ pub const Mutable = struct {
// Zero-extend the result
if (req_limbs > r.len) {
mem.set(Limb, r.limbs[r.len .. req_limbs], 0);
mem.set(Limb, r.limbs[r.len..req_limbs], 0);
}
// Truncate to required number of limbs.

View File

@ -1660,19 +1660,26 @@ pub const Value = extern union {
if (ty.isAnyFloat()) {
return floatAdd(lhs, rhs, ty, arena);
}
const result = try intAdd(lhs, rhs, arena);
const max = try ty.maxInt(arena, target);
if (compare(result, .gt, max, ty)) {
@panic("TODO comptime wrapping integer addition");
const info = ty.intInfo(target);
var lhs_space: Value.BigIntSpace = undefined;
var rhs_space: Value.BigIntSpace = undefined;
const lhs_bigint = lhs.toBigInt(&lhs_space);
const rhs_bigint = rhs.toBigInt(&rhs_space);
const limbs = try arena.alloc(
std.math.big.Limb,
std.math.max(lhs_bigint.limbs.len, rhs_bigint.limbs.len) + 1,
);
var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined };
result_bigint.addWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits);
const result_limbs = result_bigint.limbs[0..result_bigint.len];
if (result_bigint.positive) {
return Value.Tag.int_big_positive.create(arena, result_limbs);
} else {
return Value.Tag.int_big_negative.create(arena, result_limbs);
}
const min = try ty.minInt(arena, target);
if (compare(result, .lt, min, ty)) {
@panic("TODO comptime wrapping integer addition");
}
return result;
}
/// Supports integers only; asserts neither operand is undefined.
@ -1714,19 +1721,27 @@ pub const Value = extern union {
if (ty.isAnyFloat()) {
return floatSub(lhs, rhs, ty, arena);
}
const result = try intSub(lhs, rhs, arena);
const max = try ty.maxInt(arena, target);
if (compare(result, .gt, max, ty)) {
@panic("TODO comptime wrapping integer subtraction");
const info = ty.intInfo(target);
var lhs_space: Value.BigIntSpace = undefined;
var rhs_space: Value.BigIntSpace = undefined;
const lhs_bigint = lhs.toBigInt(&lhs_space);
const rhs_bigint = rhs.toBigInt(&rhs_space);
const limbs = try arena.alloc(
std.math.big.Limb,
std.math.max(lhs_bigint.limbs.len, rhs_bigint.limbs.len) + 1,
);
var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined };
result_bigint.subWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits);
const result_limbs = result_bigint.limbs[0..result_bigint.len];
if (result_bigint.positive) {
return Value.Tag.int_big_positive.create(arena, result_limbs);
} else {
return Value.Tag.int_big_negative.create(arena, result_limbs);
}
const min = try ty.minInt(arena, target);
if (compare(result, .lt, min, ty)) {
@panic("TODO comptime wrapping integer subtraction");
}
return result;
}
/// Supports integers only; asserts neither operand is undefined.