Improve f128 standard library support

* Add functions: floor128, ceil128, trunc128 and round128
* Add corresponding tests
master
antlilja 2020-06-17 18:18:45 +02:00
parent 1157ee1307
commit eb7fad28f8
5 changed files with 178 additions and 0 deletions

View File

@ -122,6 +122,11 @@ pub fn forceEval(value: var) void {
const p = @ptrCast(*volatile f64, &x);
p.* = x;
},
f128 => {
var x: f128 = undefined;
const p = @ptrCast(*volatile f128, &x);
p.* = x;
},
else => {
@compileError("forceEval not implemented for " ++ @typeName(T));
},

View File

@ -20,6 +20,7 @@ pub fn ceil(x: var) @TypeOf(x) {
return switch (T) {
f32 => ceil32(x),
f64 => ceil64(x),
f128 => ceil128(x),
else => @compileError("ceil not implemented for " ++ @typeName(T)),
};
}
@ -86,9 +87,37 @@ fn ceil64(x: f64) f64 {
}
}
fn ceil128(x: f128) f128 {
const u = @bitCast(u128, x);
const e = (u >> 112) & 0x7FFF;
var y: f128 = undefined;
if (e >= 0x3FFF + 112 or x == 0) return x;
if (u >> 127 != 0) {
y = x - math.f128_toint + math.f128_toint - x;
} else {
y = x + math.f128_toint - math.f128_toint - x;
}
if (e <= 0x3FFF - 1) {
math.forceEval(y);
if (u >> 127 != 0) {
return -0.0;
} else {
return 1.0;
}
} else if (y < 0) {
return x + y + 1;
} else {
return x + y;
}
}
test "math.ceil" {
expect(ceil(@as(f32, 0.0)) == ceil32(0.0));
expect(ceil(@as(f64, 0.0)) == ceil64(0.0));
expect(ceil(@as(f128, 0.0)) == ceil128(0.0));
}
test "math.ceil32" {
@ -103,6 +132,12 @@ test "math.ceil64" {
expect(ceil64(0.2) == 1.0);
}
test "math.ceil128" {
expect(ceil128(1.3) == 2.0);
expect(ceil128(-1.3) == -1.0);
expect(ceil128(0.2) == 1.0);
}
test "math.ceil32.special" {
expect(ceil32(0.0) == 0.0);
expect(ceil32(-0.0) == -0.0);
@ -118,3 +153,11 @@ test "math.ceil64.special" {
expect(math.isNegativeInf(ceil64(-math.inf(f64))));
expect(math.isNan(ceil64(math.nan(f64))));
}
test "math.ceil128.special" {
expect(ceil128(0.0) == 0.0);
expect(ceil128(-0.0) == -0.0);
expect(math.isPositiveInf(ceil128(math.inf(f128))));
expect(math.isNegativeInf(ceil128(-math.inf(f128))));
expect(math.isNan(ceil128(math.nan(f128))));
}

View File

@ -21,6 +21,7 @@ pub fn floor(x: var) @TypeOf(x) {
f16 => floor16(x),
f32 => floor32(x),
f64 => floor64(x),
f128 => floor128(x),
else => @compileError("floor not implemented for " ++ @typeName(T)),
};
}
@ -122,10 +123,38 @@ fn floor64(x: f64) f64 {
}
}
fn floor128(x: f128) f128 {
const u = @bitCast(u128, x);
const e = (u >> 112) & 0x7FFF;
var y: f128 = undefined;
if (e >= 0x3FFF + 112 or x == 0) return x;
if (u >> 127 != 0) {
y = x - math.f128_toint + math.f128_toint - x;
} else {
y = x + math.f128_toint - math.f128_toint - x;
}
if (e <= 0x3FFF - 1) {
math.forceEval(y);
if (u >> 127 != 0) {
return -1.0;
} else {
return 0.0;
}
} else if (y > 0) {
return x + y - 1;
} else {
return x + y;
}
}
test "math.floor" {
expect(floor(@as(f16, 1.3)) == floor16(1.3));
expect(floor(@as(f32, 1.3)) == floor32(1.3));
expect(floor(@as(f64, 1.3)) == floor64(1.3));
expect(floor(@as(f128, 1.3)) == floor128(1.3));
}
test "math.floor16" {
@ -146,6 +175,12 @@ test "math.floor64" {
expect(floor64(0.2) == 0.0);
}
test "math.floor128" {
expect(floor128(1.3) == 1.0);
expect(floor128(-1.3) == -2.0);
expect(floor128(0.2) == 0.0);
}
test "math.floor16.special" {
expect(floor16(0.0) == 0.0);
expect(floor16(-0.0) == -0.0);
@ -169,3 +204,11 @@ test "math.floor64.special" {
expect(math.isNegativeInf(floor64(-math.inf(f64))));
expect(math.isNan(floor64(math.nan(f64))));
}
test "math.floor128.special" {
expect(floor128(0.0) == 0.0);
expect(floor128(-0.0) == -0.0);
expect(math.isPositiveInf(floor128(math.inf(f128))));
expect(math.isNegativeInf(floor128(-math.inf(f128))));
expect(math.isNan(floor128(math.nan(f128))));
}

View File

@ -20,6 +20,7 @@ pub fn round(x: var) @TypeOf(x) {
return switch (T) {
f32 => round32(x),
f64 => round64(x),
f128 => round128(x),
else => @compileError("round not implemented for " ++ @typeName(T)),
};
}
@ -90,9 +91,43 @@ fn round64(x_: f64) f64 {
}
}
fn round128(x_: f128) f128 {
var x = x_;
const u = @bitCast(u128, x);
const e = (u >> 112) & 0x7FFF;
var y: f128 = undefined;
if (e >= 0x3FFF + 112) {
return x;
}
if (u >> 127 != 0) {
x = -x;
}
if (e < 0x3FFF - 1) {
math.forceEval(x + math.f64_toint);
return 0 * @bitCast(f128, u);
}
y = x + math.f128_toint - math.f128_toint - x;
if (y > 0.5) {
y = y + x - 1;
} else if (y <= -0.5) {
y = y + x + 1;
} else {
y = y + x;
}
if (u >> 127 != 0) {
return -y;
} else {
return y;
}
}
test "math.round" {
expect(round(@as(f32, 1.3)) == round32(1.3));
expect(round(@as(f64, 1.3)) == round64(1.3));
expect(round(@as(f128, 1.3)) == round128(1.3));
}
test "math.round32" {
@ -109,6 +144,13 @@ test "math.round64" {
expect(round64(1.8) == 2.0);
}
test "math.round128" {
expect(round128(1.3) == 1.0);
expect(round128(-1.3) == -1.0);
expect(round128(0.2) == 0.0);
expect(round128(1.8) == 2.0);
}
test "math.round32.special" {
expect(round32(0.0) == 0.0);
expect(round32(-0.0) == -0.0);
@ -124,3 +166,11 @@ test "math.round64.special" {
expect(math.isNegativeInf(round64(-math.inf(f64))));
expect(math.isNan(round64(math.nan(f64))));
}
test "math.round128.special" {
expect(round128(0.0) == 0.0);
expect(round128(-0.0) == -0.0);
expect(math.isPositiveInf(round128(math.inf(f128))));
expect(math.isNegativeInf(round128(-math.inf(f128))));
expect(math.isNan(round128(math.nan(f128))));
}

View File

@ -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))));
}