aboutsummaryrefslogtreecommitdiff
path: root/src/value.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-09-28 20:27:28 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-09-28 20:27:28 -0700
commit51a40f9a666085b53353f65f017bcf9bf18daaa3 (patch)
tree64b9886e75ebaf65e59d66b218c411b15c677561 /src/value.zig
parentcf90cb7218d1baa56586477bab50e88fdb6be0bb (diff)
downloadzig-51a40f9a666085b53353f65f017bcf9bf18daaa3.tar.gz
zig-51a40f9a666085b53353f65f017bcf9bf18daaa3.zip
saturating arithmetic supports integers only
Diffstat (limited to 'src/value.zig')
-rw-r--r--src/value.zig33
1 files changed, 12 insertions, 21 deletions
diff --git a/src/value.zig b/src/value.zig
index 73a2b3a49f..0ead2ff1d9 100644
--- a/src/value.zig
+++ b/src/value.zig
@@ -1588,20 +1588,17 @@ pub const Value = extern union {
return result;
}
- /// Supports both floats and ints; handles undefined.
- pub fn numberAddSat(
+ /// Supports integers only; asserts neither operand is undefined.
+ pub fn intAddSat(
lhs: Value,
rhs: Value,
ty: Type,
arena: *Allocator,
target: Target,
) !Value {
- if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef);
+ assert(!lhs.isUndef());
+ assert(!rhs.isUndef());
- if (ty.isAnyFloat()) {
- // TODO: handle outside float range
- return floatAdd(lhs, rhs, ty, arena);
- }
const result = try intAdd(lhs, rhs, arena);
const max = try ty.maxInt(arena, target);
@@ -1645,20 +1642,17 @@ pub const Value = extern union {
return result;
}
- /// Supports both floats and ints; handles undefined.
- pub fn numberSubSat(
+ /// Supports integers only; asserts neither operand is undefined.
+ pub fn intSubSat(
lhs: Value,
rhs: Value,
ty: Type,
arena: *Allocator,
target: Target,
) !Value {
- if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef);
+ assert(!lhs.isUndef());
+ assert(!rhs.isUndef());
- if (ty.isAnyFloat()) {
- // TODO: handle outside float range
- return floatSub(lhs, rhs, ty, arena);
- }
const result = try intSub(lhs, rhs, arena);
const max = try ty.maxInt(arena, target);
@@ -1702,20 +1696,17 @@ pub const Value = extern union {
return result;
}
- /// Supports both floats and ints; handles undefined.
- pub fn numberMulSat(
+ /// Supports integers only; asserts neither operand is undefined.
+ pub fn intMulSat(
lhs: Value,
rhs: Value,
ty: Type,
arena: *Allocator,
target: Target,
) !Value {
- if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef);
+ assert(!lhs.isUndef());
+ assert(!rhs.isUndef());
- if (ty.isAnyFloat()) {
- // TODO: handle outside float range
- return floatMul(lhs, rhs, ty, arena);
- }
const result = try intMul(lhs, rhs, arena);
const max = try ty.maxInt(arena, target);