aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/trunc.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-06-22 23:22:17 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-06-22 23:22:17 -0400
commit6938245fcc1daa6a63bcfcb3ba1092d569efc875 (patch)
tree035b27a399c418cab679043f87282dc3de1ef5b1 /lib/std/math/trunc.zig
parent7b68385d7d4448e81cc882d9a5464bf58d10dc0d (diff)
parent78c6d39cd49225bdfd2de4da7b1730ba26a41ba4 (diff)
downloadzig-6938245fcc1daa6a63bcfcb3ba1092d569efc875.tar.gz
zig-6938245fcc1daa6a63bcfcb3ba1092d569efc875.zip
Merge remote-tracking branch 'origin/master' into zig-ast-to-zir
Diffstat (limited to 'lib/std/math/trunc.zig')
-rw-r--r--lib/std/math/trunc.zig37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/std/math/trunc.zig b/lib/std/math/trunc.zig
index b70f0c6be3..cdd2fa3c6b 100644
--- a/lib/std/math/trunc.zig
+++ b/lib/std/math/trunc.zig
@@ -20,6 +20,7 @@ pub fn trunc(x: var) @TypeOf(x) {
return switch (T) {
f32 => trunc32(x),
f64 => trunc64(x),
+ f128 => trunc128(x),
else => @compileError("trunc not implemented for " ++ @typeName(T)),
};
}
@@ -66,9 +67,31 @@ fn trunc64(x: f64) f64 {
}
}
+fn trunc128(x: f128) f128 {
+ const u = @bitCast(u128, x);
+ var e = @intCast(i32, ((u >> 112) & 0x7FFF)) - 0x3FFF + 16;
+ var m: u128 = undefined;
+
+ if (e >= 112 + 16) {
+ return x;
+ }
+ if (e < 16) {
+ e = 1;
+ }
+
+ m = @as(u128, maxInt(u128)) >> @intCast(u7, e);
+ if (u & m == 0) {
+ return x;
+ } else {
+ math.forceEval(x + 0x1p120);
+ return @bitCast(f128, u & ~m);
+ }
+}
+
test "math.trunc" {
expect(trunc(@as(f32, 1.3)) == trunc32(1.3));
expect(trunc(@as(f64, 1.3)) == trunc64(1.3));
+ expect(trunc(@as(f128, 1.3)) == trunc128(1.3));
}
test "math.trunc32" {
@@ -83,6 +106,12 @@ test "math.trunc64" {
expect(trunc64(0.2) == 0.0);
}
+test "math.trunc128" {
+ expect(trunc128(1.3) == 1.0);
+ expect(trunc128(-1.3) == -1.0);
+ expect(trunc128(0.2) == 0.0);
+}
+
test "math.trunc32.special" {
expect(trunc32(0.0) == 0.0); // 0x3F800000
expect(trunc32(-0.0) == -0.0);
@@ -98,3 +127,11 @@ test "math.trunc64.special" {
expect(math.isNegativeInf(trunc64(-math.inf(f64))));
expect(math.isNan(trunc64(math.nan(f64))));
}
+
+test "math.trunc128.special" {
+ expect(trunc128(0.0) == 0.0);
+ expect(trunc128(-0.0) == -0.0);
+ expect(math.isPositiveInf(trunc128(math.inf(f128))));
+ expect(math.isNegativeInf(trunc128(-math.inf(f128))));
+ expect(math.isNan(trunc128(math.nan(f128))));
+}