blob: 75c46acc0e85616d14a1e893e4b26b509c372455 [file]
use super::generic;
/// Round `x` to the nearest integer, breaking ties toward even.
#[cfg(f16_enabled)]
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
pub fn rintf16(x: f16) -> f16 {
select_implementation! {
name: rintf16,
use_arch: all(target_arch = "aarch64", target_feature = "fp16"),
args: x,
}
generic::rint_status(x).val
}
/// Round `x` to the nearest integer, breaking ties toward even.
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
pub fn rintf(x: f32) -> f32 {
select_implementation! {
name: rintf,
use_arch: any(
all(target_arch = "aarch64", target_feature = "neon"),
all(target_arch = "wasm32", intrinsics_enabled),
),
args: x,
}
generic::rint_status(x).val
}
/// Round `x` to the nearest integer, breaking ties toward even.
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
pub fn rint(x: f64) -> f64 {
select_implementation! {
name: rint,
use_arch: any(
all(target_arch = "aarch64", target_feature = "neon"),
all(target_arch = "wasm32", intrinsics_enabled),
),
use_arch_required: x86_no_sse2,
args: x,
}
generic::rint_status(x).val
}
/// Round `x` to the nearest integer, breaking ties toward even.
#[cfg(f128_enabled)]
#[cfg_attr(assert_no_panic, no_panic::no_panic)]
pub fn rintf128(x: f128) -> f128 {
generic::rint_status(x).val
}
#[cfg(test)]
mod tests {
use super::*;
use crate::support::{Float, FpResult, Hex, Status};
macro_rules! cases {
($f:ty) => {
[
// roundtrip
(0.0, 0.0, Status::OK),
(-0.0, -0.0, Status::OK),
(1.0, 1.0, Status::OK),
(-1.0, -1.0, Status::OK),
(<$f>::INFINITY, <$f>::INFINITY, Status::OK),
(<$f>::NEG_INFINITY, <$f>::NEG_INFINITY, Status::OK),
// with rounding
(0.1, 0.0, Status::INEXACT),
(-0.1, -0.0, Status::INEXACT),
(0.5, 0.0, Status::INEXACT),
(-0.5, -0.0, Status::INEXACT),
(0.9, 1.0, Status::INEXACT),
(-0.9, -1.0, Status::INEXACT),
(1.1, 1.0, Status::INEXACT),
(-1.1, -1.0, Status::INEXACT),
(1.5, 2.0, Status::INEXACT),
(-1.5, -2.0, Status::INEXACT),
(1.9, 2.0, Status::INEXACT),
(-1.9, -2.0, Status::INEXACT),
(2.5, 2.0, Status::INEXACT),
(-2.5, -2.0, Status::INEXACT),
(3.5, 4.0, Status::INEXACT),
(-3.5, -4.0, Status::INEXACT),
]
};
}
#[track_caller]
fn check<F: Float>(f: fn(F) -> F, cases: &[(F, F, Status)]) {
for &(x, exp_res, exp_stat) in cases {
let FpResult { val, status } = generic::rint_status(x);
assert_biteq!(val, exp_res, "generic::rint_status({x:?}) ({})", Hex(x));
assert_eq!(
status,
exp_stat,
"{x:?} {} -> {exp_res:?} {}",
Hex(x),
Hex(exp_res)
);
let val = f(x);
assert_biteq!(val, exp_res, "rint({x:?}) ({})", Hex(x));
}
}
#[test]
#[cfg(f16_enabled)]
fn check_f16() {
check::<f16>(rintf16, &cases!(f16));
check::<f16>(
rintf16,
&[
(hf16!("0x1p10"), hf16!("0x1p10"), Status::OK),
(hf16!("-0x1p10"), hf16!("-0x1p10"), Status::OK),
],
);
}
#[test]
fn check_f32() {
check::<f32>(rintf, &cases!(f32));
check::<f32>(
rintf,
&[
(hf32!("0x1p23"), hf32!("0x1p23"), Status::OK),
(hf32!("-0x1p23"), hf32!("-0x1p23"), Status::OK),
],
);
}
#[test]
fn check_f64() {
check::<f64>(rint, &cases!(f64));
check::<f64>(
rint,
&[
(hf64!("0x1p52"), hf64!("0x1p52"), Status::OK),
(hf64!("-0x1p52"), hf64!("-0x1p52"), Status::OK),
],
);
}
#[test]
#[cfg(f128_enabled)]
fn check_f128() {
check::<f128>(rintf128, &cases!(f128));
check::<f128>(
rintf128,
&[
(hf128!("0x1p112"), hf128!("0x1p112"), Status::OK),
(hf128!("-0x1p112"), hf128!("-0x1p112"), Status::OK),
],
);
}
}