Add f128 support for fabs, isinf, isnan, inf and nan functions

This commit is contained in:
Marc Tiehuis
2019-02-13 23:27:23 +13:00
parent be861a85c8
commit cf007e37b9
6 changed files with 66 additions and 7 deletions

View File

@@ -14,6 +14,7 @@ pub fn fabs(x: var) @typeOf(x) {
f16 => fabs16(x),
f32 => fabs32(x),
f64 => fabs64(x),
f128 => fabs128(x),
else => @compileError("fabs not implemented for " ++ @typeName(T)),
};
}
@@ -36,10 +37,17 @@ fn fabs64(x: f64) f64 {
return @bitCast(f64, u);
}
fn fabs128(x: f128) f128 {
var u = @bitCast(u128, x);
u &= maxInt(u128) >> 1;
return @bitCast(f128, u);
}
test "math.fabs" {
expect(fabs(f16(1.0)) == fabs16(1.0));
expect(fabs(f32(1.0)) == fabs32(1.0));
expect(fabs(f64(1.0)) == fabs64(1.0));
expect(fabs(f128(1.0)) == fabs128(1.0));
}
test "math.fabs16" {
@@ -57,6 +65,11 @@ test "math.fabs64" {
expect(fabs64(-1.0) == 1.0);
}
test "math.fabs128" {
expect(fabs128(1.0) == 1.0);
expect(fabs128(-1.0) == 1.0);
}
test "math.fabs16.special" {
expect(math.isPositiveInf(fabs(math.inf(f16))));
expect(math.isPositiveInf(fabs(-math.inf(f16))));
@@ -74,3 +87,9 @@ test "math.fabs64.special" {
expect(math.isPositiveInf(fabs(-math.inf(f64))));
expect(math.isNan(fabs(math.nan(f64))));
}
test "math.fabs128.special" {
expect(math.isPositiveInf(fabs(math.inf(f128))));
expect(math.isPositiveInf(fabs(-math.inf(f128))));
expect(math.isNan(fabs(math.nan(f128))));
}