2022-06-18 02:10:00 +01:00
|
|
|
//! Ported from musl, which is licensed under the MIT license:
|
|
|
|
//! https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
|
|
|
|
//!
|
|
|
|
//! https://git.musl-libc.org/cgit/musl/tree/src/math/roundf.c
|
|
|
|
//! https://git.musl-libc.org/cgit/musl/tree/src/math/round.c
|
2022-04-26 18:13:55 +01:00
|
|
|
|
|
|
|
const std = @import("std");
|
2022-06-10 09:25:59 +01:00
|
|
|
const builtin = @import("builtin");
|
2022-04-26 18:13:55 +01:00
|
|
|
const math = std.math;
|
|
|
|
const expect = std.testing.expect;
|
2022-06-10 09:25:59 +01:00
|
|
|
const arch = builtin.cpu.arch;
|
2022-06-18 02:10:00 +01:00
|
|
|
const common = @import("common.zig");
|
|
|
|
|
|
|
|
pub const panic = common.panic;
|
2022-06-10 09:25:59 +01:00
|
|
|
|
|
|
|
comptime {
|
2022-12-28 13:57:17 +00:00
|
|
|
@export(__roundh, .{ .name = "__roundh", .linkage = common.linkage, .visibility = common.visibility });
|
|
|
|
@export(roundf, .{ .name = "roundf", .linkage = common.linkage, .visibility = common.visibility });
|
|
|
|
@export(round, .{ .name = "round", .linkage = common.linkage, .visibility = common.visibility });
|
|
|
|
@export(__roundx, .{ .name = "__roundx", .linkage = common.linkage, .visibility = common.visibility });
|
2022-10-22 21:31:09 +01:00
|
|
|
if (common.want_ppc_abi) {
|
2022-12-28 13:57:17 +00:00
|
|
|
@export(roundq, .{ .name = "roundf128", .linkage = common.linkage, .visibility = common.visibility });
|
2022-10-22 21:31:09 +01:00
|
|
|
}
|
2022-12-28 13:57:17 +00:00
|
|
|
@export(roundq, .{ .name = "roundq", .linkage = common.linkage, .visibility = common.visibility });
|
|
|
|
@export(roundl, .{ .name = "roundl", .linkage = common.linkage, .visibility = common.visibility });
|
2022-06-10 09:25:59 +01:00
|
|
|
}
|
2022-04-26 18:13:55 +01:00
|
|
|
|
|
|
|
pub fn __roundh(x: f16) callconv(.C) f16 {
|
|
|
|
// TODO: more efficient implementation
|
2023-07-24 11:34:16 +01:00
|
|
|
return @floatCast(roundf(x));
|
2022-04-26 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn roundf(x_: f32) callconv(.C) f32 {
|
|
|
|
const f32_toint = 1.0 / math.floatEps(f32);
|
|
|
|
|
|
|
|
var x = x_;
|
2023-07-24 11:34:16 +01:00
|
|
|
const u: u32 = @bitCast(x);
|
2022-04-26 18:13:55 +01:00
|
|
|
const e = (u >> 23) & 0xFF;
|
|
|
|
var y: f32 = undefined;
|
|
|
|
|
|
|
|
if (e >= 0x7F + 23) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
if (u >> 31 != 0) {
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
if (e < 0x7F - 1) {
|
|
|
|
math.doNotOptimizeAway(x + f32_toint);
|
2023-06-22 18:46:56 +01:00
|
|
|
return 0 * @as(f32, @bitCast(u));
|
2022-04-26 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
y = x + f32_toint - f32_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 >> 31 != 0) {
|
|
|
|
return -y;
|
|
|
|
} else {
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn round(x_: f64) callconv(.C) f64 {
|
|
|
|
const f64_toint = 1.0 / math.floatEps(f64);
|
|
|
|
|
|
|
|
var x = x_;
|
2023-07-24 11:34:16 +01:00
|
|
|
const u: u64 = @bitCast(x);
|
2022-04-26 18:13:55 +01:00
|
|
|
const e = (u >> 52) & 0x7FF;
|
|
|
|
var y: f64 = undefined;
|
|
|
|
|
|
|
|
if (e >= 0x3FF + 52) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
if (u >> 63 != 0) {
|
|
|
|
x = -x;
|
|
|
|
}
|
|
|
|
if (e < 0x3ff - 1) {
|
|
|
|
math.doNotOptimizeAway(x + f64_toint);
|
2023-06-22 18:46:56 +01:00
|
|
|
return 0 * @as(f64, @bitCast(u));
|
2022-04-26 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
y = x + f64_toint - f64_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 >> 63 != 0) {
|
|
|
|
return -y;
|
|
|
|
} else {
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn __roundx(x: f80) callconv(.C) f80 {
|
|
|
|
// TODO: more efficient implementation
|
2023-07-24 11:34:16 +01:00
|
|
|
return @floatCast(roundq(x));
|
2022-04-26 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn roundq(x_: f128) callconv(.C) f128 {
|
|
|
|
const f128_toint = 1.0 / math.floatEps(f128);
|
|
|
|
|
|
|
|
var x = x_;
|
2023-07-24 11:34:16 +01:00
|
|
|
const u: u128 = @bitCast(x);
|
2022-04-26 18:13:55 +01:00
|
|
|
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.doNotOptimizeAway(x + f128_toint);
|
2023-06-22 18:46:56 +01:00
|
|
|
return 0 * @as(f128, @bitCast(u));
|
2022-04-26 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
y = x + f128_toint - 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 21:06:21 +01:00
|
|
|
pub fn roundl(x: c_longdouble) callconv(.C) c_longdouble {
|
|
|
|
switch (@typeInfo(c_longdouble).Float.bits) {
|
|
|
|
16 => return __roundh(x),
|
|
|
|
32 => return roundf(x),
|
|
|
|
64 => return round(x),
|
|
|
|
80 => return __roundx(x),
|
|
|
|
128 => return roundq(x),
|
|
|
|
else => @compileError("unreachable"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 18:13:55 +01:00
|
|
|
test "round32" {
|
|
|
|
try expect(roundf(1.3) == 1.0);
|
|
|
|
try expect(roundf(-1.3) == -1.0);
|
|
|
|
try expect(roundf(0.2) == 0.0);
|
|
|
|
try expect(roundf(1.8) == 2.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "round64" {
|
|
|
|
try expect(round(1.3) == 1.0);
|
|
|
|
try expect(round(-1.3) == -1.0);
|
|
|
|
try expect(round(0.2) == 0.0);
|
|
|
|
try expect(round(1.8) == 2.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "round128" {
|
|
|
|
try expect(roundq(1.3) == 1.0);
|
|
|
|
try expect(roundq(-1.3) == -1.0);
|
|
|
|
try expect(roundq(0.2) == 0.0);
|
|
|
|
try expect(roundq(1.8) == 2.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "round32.special" {
|
|
|
|
try expect(roundf(0.0) == 0.0);
|
|
|
|
try expect(roundf(-0.0) == -0.0);
|
|
|
|
try expect(math.isPositiveInf(roundf(math.inf(f32))));
|
|
|
|
try expect(math.isNegativeInf(roundf(-math.inf(f32))));
|
|
|
|
try expect(math.isNan(roundf(math.nan(f32))));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "round64.special" {
|
|
|
|
try expect(round(0.0) == 0.0);
|
|
|
|
try expect(round(-0.0) == -0.0);
|
|
|
|
try expect(math.isPositiveInf(round(math.inf(f64))));
|
|
|
|
try expect(math.isNegativeInf(round(-math.inf(f64))));
|
|
|
|
try expect(math.isNan(round(math.nan(f64))));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "round128.special" {
|
|
|
|
try expect(roundq(0.0) == 0.0);
|
|
|
|
try expect(roundq(-0.0) == -0.0);
|
|
|
|
try expect(math.isPositiveInf(roundq(math.inf(f128))));
|
|
|
|
try expect(math.isNegativeInf(roundq(-math.inf(f128))));
|
|
|
|
try expect(math.isNan(roundq(math.nan(f128))));
|
|
|
|
}
|