2019-05-01 18:15:57 +12:00
|
|
|
// Ported from musl, which is licensed under the MIT license:
|
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
|
2017-06-20 23:01:04 +12:00
|
|
|
//
|
2019-05-01 18:15:57 +12:00
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/roundf.c
|
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/round.c
|
2017-06-20 23:01:04 +12:00
|
|
|
|
2019-02-08 18:18:47 -05:00
|
|
|
const expect = std.testing.expect;
|
2019-03-02 16:46:04 -05:00
|
|
|
const std = @import("../std.zig");
|
2017-12-23 22:08:53 -05:00
|
|
|
const math = std.math;
|
2017-06-16 20:26:10 +12:00
|
|
|
|
2019-05-01 18:15:57 +12:00
|
|
|
/// Returns x rounded to the nearest integer, rounding half away from zero.
|
|
|
|
|
///
|
|
|
|
|
/// Special Cases:
|
|
|
|
|
/// - round(+-0) = +-0
|
|
|
|
|
/// - round(+-inf) = +-inf
|
|
|
|
|
/// - round(nan) = nan
|
2020-07-11 14:09:04 +03:00
|
|
|
pub fn round(x: anytype) @TypeOf(x) {
|
2019-12-09 21:56:19 +01:00
|
|
|
const T = @TypeOf(x);
|
2017-12-22 00:50:30 -05:00
|
|
|
return switch (T) {
|
2017-12-22 13:14:07 -05:00
|
|
|
f32 => round32(x),
|
|
|
|
|
f64 => round64(x),
|
2020-06-17 18:18:45 +02:00
|
|
|
f128 => round128(x),
|
2017-06-16 20:26:10 +12:00
|
|
|
else => @compileError("round not implemented for " ++ @typeName(T)),
|
2017-12-22 00:50:30 -05:00
|
|
|
};
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|
|
|
|
|
|
2018-01-25 04:10:11 -05:00
|
|
|
fn round32(x_: f32) f32 {
|
2017-06-16 20:26:10 +12:00
|
|
|
var x = x_;
|
|
|
|
|
const u = @bitCast(u32, x);
|
|
|
|
|
const e = (u >> 23) & 0xFF;
|
|
|
|
|
var y: f32 = undefined;
|
|
|
|
|
|
2018-05-10 00:29:49 -04:00
|
|
|
if (e >= 0x7F + 23) {
|
2017-06-16 20:26:10 +12:00
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
if (u >> 31 != 0) {
|
|
|
|
|
x = -x;
|
|
|
|
|
}
|
2018-05-10 00:29:49 -04:00
|
|
|
if (e < 0x7F - 1) {
|
2020-08-26 01:48:51 +02:00
|
|
|
math.doNotOptimizeAway(x + math.f32_toint);
|
2017-06-16 20:26:10 +12:00
|
|
|
return 0 * @bitCast(f32, u);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 21:42:09 +12:00
|
|
|
y = x + math.f32_toint - math.f32_toint - x;
|
2017-06-16 20:26:10 +12: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-22 00:50:30 -05:00
|
|
|
return -y;
|
2017-06-16 20:26:10 +12:00
|
|
|
} else {
|
2017-12-22 00:50:30 -05:00
|
|
|
return y;
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 04:10:11 -05:00
|
|
|
fn round64(x_: f64) f64 {
|
2017-06-16 20:26:10 +12:00
|
|
|
var x = x_;
|
|
|
|
|
const u = @bitCast(u64, x);
|
|
|
|
|
const e = (u >> 52) & 0x7FF;
|
|
|
|
|
var y: f64 = undefined;
|
|
|
|
|
|
2018-05-10 00:29:49 -04:00
|
|
|
if (e >= 0x3FF + 52) {
|
2017-06-16 20:26:10 +12:00
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
if (u >> 63 != 0) {
|
|
|
|
|
x = -x;
|
|
|
|
|
}
|
2018-05-10 00:29:49 -04:00
|
|
|
if (e < 0x3ff - 1) {
|
2020-08-26 01:48:51 +02:00
|
|
|
math.doNotOptimizeAway(x + math.f64_toint);
|
2017-06-16 20:26:10 +12:00
|
|
|
return 0 * @bitCast(f64, u);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 21:42:09 +12:00
|
|
|
y = x + math.f64_toint - math.f64_toint - x;
|
2017-06-16 20:26:10 +12: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-22 00:50:30 -05:00
|
|
|
return -y;
|
2017-06-16 20:26:10 +12:00
|
|
|
} else {
|
2017-12-22 00:50:30 -05:00
|
|
|
return y;
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 18:18:45 +02:00
|
|
|
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) {
|
2020-08-26 01:48:51 +02:00
|
|
|
math.doNotOptimizeAway(x + math.f64_toint);
|
2020-06-17 18:18:45 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 14:36:33 -04:00
|
|
|
test "math.round" {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(round(@as(f32, 1.3)) == round32(1.3));
|
|
|
|
|
try expect(round(@as(f64, 1.3)) == round64(1.3));
|
|
|
|
|
try expect(round(@as(f128, 1.3)) == round128(1.3));
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 14:36:33 -04:00
|
|
|
test "math.round32" {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(round32(1.3) == 1.0);
|
|
|
|
|
try expect(round32(-1.3) == -1.0);
|
|
|
|
|
try expect(round32(0.2) == 0.0);
|
|
|
|
|
try expect(round32(1.8) == 2.0);
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|
|
|
|
|
|
2017-06-19 14:36:33 -04:00
|
|
|
test "math.round64" {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(round64(1.3) == 1.0);
|
|
|
|
|
try expect(round64(-1.3) == -1.0);
|
|
|
|
|
try expect(round64(0.2) == 0.0);
|
|
|
|
|
try expect(round64(1.8) == 2.0);
|
2017-06-16 20:26:10 +12:00
|
|
|
}
|
2017-06-20 23:01:04 +12:00
|
|
|
|
2020-06-17 18:18:45 +02:00
|
|
|
test "math.round128" {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(round128(1.3) == 1.0);
|
|
|
|
|
try expect(round128(-1.3) == -1.0);
|
|
|
|
|
try expect(round128(0.2) == 0.0);
|
|
|
|
|
try expect(round128(1.8) == 2.0);
|
2020-06-17 18:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-20 23:01:04 +12:00
|
|
|
test "math.round32.special" {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(round32(0.0) == 0.0);
|
|
|
|
|
try expect(round32(-0.0) == -0.0);
|
|
|
|
|
try expect(math.isPositiveInf(round32(math.inf(f32))));
|
|
|
|
|
try expect(math.isNegativeInf(round32(-math.inf(f32))));
|
|
|
|
|
try expect(math.isNan(round32(math.nan(f32))));
|
2017-06-20 23:01:04 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test "math.round64.special" {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(round64(0.0) == 0.0);
|
|
|
|
|
try expect(round64(-0.0) == -0.0);
|
|
|
|
|
try expect(math.isPositiveInf(round64(math.inf(f64))));
|
|
|
|
|
try expect(math.isNegativeInf(round64(-math.inf(f64))));
|
|
|
|
|
try expect(math.isNan(round64(math.nan(f64))));
|
2017-06-20 23:01:04 +12:00
|
|
|
}
|
2020-06-17 18:18:45 +02:00
|
|
|
|
|
|
|
|
test "math.round128.special" {
|
2021-05-04 20:47:26 +03:00
|
|
|
try expect(round128(0.0) == 0.0);
|
|
|
|
|
try expect(round128(-0.0) == -0.0);
|
|
|
|
|
try expect(math.isPositiveInf(round128(math.inf(f128))));
|
|
|
|
|
try expect(math.isNegativeInf(round128(-math.inf(f128))));
|
|
|
|
|
try expect(math.isNan(round128(math.nan(f128))));
|
2020-06-17 18:18:45 +02:00
|
|
|
}
|