2019-04-30 23:15:57 -07:00
|
|
|
// Ported from musl, which is licensed under the MIT license:
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
|
2017-06-20 04:01:04 -07:00
|
|
|
//
|
2019-04-30 23:15:57 -07:00
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/truncf.c
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/trunc.c
|
2017-06-20 04:01:04 -07:00
|
|
|
|
2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("../std.zig");
|
2017-12-23 19:08:53 -08:00
|
|
|
const math = std.math;
|
2019-02-08 15:18:47 -08:00
|
|
|
const expect = std.testing.expect;
|
2018-10-26 11:59:58 -07:00
|
|
|
const maxInt = std.math.maxInt;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns the integer value of x.
|
|
|
|
///
|
|
|
|
/// Special Cases:
|
|
|
|
/// - trunc(+-0) = +-0
|
|
|
|
/// - trunc(+-inf) = +-inf
|
|
|
|
/// - trunc(nan) = nan
|
2019-12-09 12:56:19 -08:00
|
|
|
pub fn trunc(x: var) @TypeOf(x) {
|
|
|
|
const T = @TypeOf(x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => trunc32(x),
|
|
|
|
f64 => trunc64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("trunc not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn trunc32(x: f32) f32 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const u = @bitCast(u32, x);
|
2018-06-16 23:57:07 -07:00
|
|
|
var e = @intCast(i32, ((u >> 23) & 0xFF)) - 0x7F + 9;
|
2017-06-16 01:26:10 -07:00
|
|
|
var m: u32 = undefined;
|
|
|
|
|
|
|
|
if (e >= 23 + 9) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
if (e < 9) {
|
|
|
|
e = 1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 20:25:57 -08:00
|
|
|
m = @as(u32, maxInt(u32)) >> @intCast(u5, e);
|
2017-06-16 01:26:10 -07:00
|
|
|
if (u & m == 0) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return x;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
|
|
|
math.forceEval(x + 0x1p120);
|
2017-12-21 21:50:30 -08:00
|
|
|
return @bitCast(f32, u & ~m);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn trunc64(x: f64) f64 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const u = @bitCast(u64, x);
|
2018-06-16 23:57:07 -07:00
|
|
|
var e = @intCast(i32, ((u >> 52) & 0x7FF)) - 0x3FF + 12;
|
2017-06-16 01:26:10 -07:00
|
|
|
var m: u64 = undefined;
|
|
|
|
|
|
|
|
if (e >= 52 + 12) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
if (e < 12) {
|
|
|
|
e = 1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 20:25:57 -08:00
|
|
|
m = @as(u64, maxInt(u64)) >> @intCast(u6, e);
|
2017-06-16 01:26:10 -07:00
|
|
|
if (u & m == 0) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return x;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
|
|
|
math.forceEval(x + 0x1p120);
|
2017-12-21 21:50:30 -08:00
|
|
|
return @bitCast(f64, u & ~m);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.trunc" {
|
2019-11-06 20:25:57 -08:00
|
|
|
expect(trunc(@as(f32, 1.3)) == trunc32(1.3));
|
|
|
|
expect(trunc(@as(f64, 1.3)) == trunc64(1.3));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.trunc32" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(trunc32(1.3) == 1.0);
|
|
|
|
expect(trunc32(-1.3) == -1.0);
|
|
|
|
expect(trunc32(0.2) == 0.0);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.trunc64" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(trunc64(1.3) == 1.0);
|
|
|
|
expect(trunc64(-1.3) == -1.0);
|
|
|
|
expect(trunc64(0.2) == 0.0);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.trunc32.special" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(trunc32(0.0) == 0.0); // 0x3F800000
|
|
|
|
expect(trunc32(-0.0) == -0.0);
|
|
|
|
expect(math.isPositiveInf(trunc32(math.inf(f32))));
|
|
|
|
expect(math.isNegativeInf(trunc32(-math.inf(f32))));
|
|
|
|
expect(math.isNan(trunc32(math.nan(f32))));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "math.trunc64.special" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(trunc64(0.0) == 0.0);
|
|
|
|
expect(trunc64(-0.0) == -0.0);
|
|
|
|
expect(math.isPositiveInf(trunc64(math.inf(f64))));
|
|
|
|
expect(math.isNegativeInf(trunc64(-math.inf(f64))));
|
|
|
|
expect(math.isNan(trunc64(math.nan(f64))));
|
2017-06-20 04:01:04 -07:00
|
|
|
}
|