2017-06-20 04:01:04 -07:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// - round(+-0) = +-0
|
|
|
|
// - round(+-inf) = +-inf
|
|
|
|
// - round(nan) = nan
|
|
|
|
|
2017-06-16 01:26:10 -07:00
|
|
|
const builtin = @import("builtin");
|
2017-12-23 19:08:53 -08:00
|
|
|
const assert = std.debug.assert;
|
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn round(x: var) @typeOf(x) {
|
2017-06-16 01:26:10 -07:00
|
|
|
const T = @typeOf(x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => round32(x),
|
|
|
|
f64 => round64(x),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("round 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 round32(x_: f32) f32 {
|
2017-06-16 01:26:10 -07:00
|
|
|
var x = x_;
|
|
|
|
const u = @bitCast(u32, x);
|
|
|
|
const e = (u >> 23) & 0xFF;
|
|
|
|
var y: f32 = undefined;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
if (e >= 0x7F + 23) {
|
2017-06-16 01:26:10 -07:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
if (u >> 31 != 0) {
|
|
|
|
x = -x;
|
|
|
|
}
|
2018-05-09 21:29:49 -07:00
|
|
|
if (e < 0x7F - 1) {
|
2017-06-16 01:26:10 -07:00
|
|
|
math.forceEval(x + math.f32_toint);
|
|
|
|
return 0 * @bitCast(f32, u);
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:42:09 -07:00
|
|
|
y = x + math.f32_toint - math.f32_toint - x;
|
2017-06-16 01:26:10 -07:00
|
|
|
if (y > 0.5) {
|
|
|
|
y = y + x - 1;
|
|
|
|
} else if (y <= -0.5) {
|
|
|
|
y = y + x + 1;
|
|
|
|
} else {
|
|
|
|
y = y + x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u >> 31 != 0) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return -y;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
2017-12-21 21:50:30 -08:00
|
|
|
return y;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn round64(x_: f64) f64 {
|
2017-06-16 01:26:10 -07:00
|
|
|
var x = x_;
|
|
|
|
const u = @bitCast(u64, x);
|
|
|
|
const e = (u >> 52) & 0x7FF;
|
|
|
|
var y: f64 = undefined;
|
|
|
|
|
2018-05-09 21:29:49 -07:00
|
|
|
if (e >= 0x3FF + 52) {
|
2017-06-16 01:26:10 -07:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
if (u >> 63 != 0) {
|
|
|
|
x = -x;
|
|
|
|
}
|
2018-05-09 21:29:49 -07:00
|
|
|
if (e < 0x3ff - 1) {
|
2017-06-16 01:26:10 -07:00
|
|
|
math.forceEval(x + math.f64_toint);
|
|
|
|
return 0 * @bitCast(f64, u);
|
|
|
|
}
|
|
|
|
|
2018-08-23 02:42:09 -07:00
|
|
|
y = x + math.f64_toint - math.f64_toint - x;
|
2017-06-16 01:26:10 -07:00
|
|
|
if (y > 0.5) {
|
|
|
|
y = y + x - 1;
|
|
|
|
} else if (y <= -0.5) {
|
|
|
|
y = y + x + 1;
|
|
|
|
} else {
|
|
|
|
y = y + x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u >> 63 != 0) {
|
2017-12-21 21:50:30 -08:00
|
|
|
return -y;
|
2017-06-16 01:26:10 -07:00
|
|
|
} else {
|
2017-12-21 21:50:30 -08:00
|
|
|
return y;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.round" {
|
2017-06-16 01:26:10 -07:00
|
|
|
assert(round(f32(1.3)) == round32(1.3));
|
|
|
|
assert(round(f64(1.3)) == round64(1.3));
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.round32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
assert(round32(1.3) == 1.0);
|
|
|
|
assert(round32(-1.3) == -1.0);
|
|
|
|
assert(round32(0.2) == 0.0);
|
|
|
|
assert(round32(1.8) == 2.0);
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.round64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
assert(round64(1.3) == 1.0);
|
|
|
|
assert(round64(-1.3) == -1.0);
|
|
|
|
assert(round64(0.2) == 0.0);
|
|
|
|
assert(round64(1.8) == 2.0);
|
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.round32.special" {
|
|
|
|
assert(round32(0.0) == 0.0);
|
|
|
|
assert(round32(-0.0) == -0.0);
|
|
|
|
assert(math.isPositiveInf(round32(math.inf(f32))));
|
|
|
|
assert(math.isNegativeInf(round32(-math.inf(f32))));
|
|
|
|
assert(math.isNan(round32(math.nan(f32))));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.round64.special" {
|
|
|
|
assert(round64(0.0) == 0.0);
|
|
|
|
assert(round64(-0.0) == -0.0);
|
|
|
|
assert(math.isPositiveInf(round64(math.inf(f64))));
|
|
|
|
assert(math.isNegativeInf(round64(-math.inf(f64))));
|
|
|
|
assert(math.isNan(round64(math.nan(f64))));
|
|
|
|
}
|