#[inline]
pub const fn roundf(x: f32) -> f32 {
let mut i0 = x.to_bits() as i32;
let j0 = ((i0 >> 23) & 0xff) - 0x7f;
if j0 < 23 {
if j0 < 0 {
i0 &= 0x80000000u32 as i32;
if j0 == -1 {
i0 |= 0x3f800000;
}
} else {
let i = 0x007fffff >> j0;
if (i0 & i) == 0 {
return x;
}
i0 += 0x00400000 >> j0;
i0 &= !i;
}
} else {
return if j0 == 0x80 {
x + x
} else {
x
};
}
f32::from_bits(i0 as u32)
}
#[inline(always)]
pub(crate) fn froundf_finite(x: f32) -> f32 {
#[cfg(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse4.1"
),
target_arch = "aarch64"
))]
{
x.round()
}
#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse4.1"
),
target_arch = "aarch64"
)))]
{
roundf(x)
}
}
#[inline]
pub const fn round(x: f64) -> f64 {
let mut i0: i64 = x.to_bits() as i64;
let j0: i32 = (((i0 >> 52) & 0x7ff) - 0x3ff) as i32;
if j0 < 52 {
if j0 < 0 {
i0 &= 0x8000000000000000u64 as i64;
if j0 == -1 {
i0 |= 0x3ff0000000000000u64 as i64;
}
} else {
let i = (0x000fffffffffffffu64 >> j0) as i64;
if (i0 & i) == 0 {
return x;
}
i0 += (0x0008000000000000u64 >> j0) as i64;
i0 &= !i;
}
} else {
return if j0 == 0x400 {
x + x
} else {
x
};
}
f64::from_bits(i0 as u64)
}
#[inline]
pub(crate) fn fround_finite(x: f64) -> f64 {
#[cfg(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse4.1"
),
target_arch = "aarch64"
))]
{
x.round()
}
#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse4.1"
),
target_arch = "aarch64"
)))]
{
round(x)
}
}
pub(crate) trait CpuRound {
fn cpu_round(self) -> Self;
}
impl CpuRound for f32 {
#[inline(always)]
fn cpu_round(self) -> Self {
froundf_finite(self)
}
}
impl CpuRound for f64 {
#[inline(always)]
fn cpu_round(self) -> Self {
fround_finite(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_roundf() {
assert_eq!(roundf(0f32), 0.0f32.round());
assert_eq!(roundf(1f32), 1.0f32.round());
assert_eq!(roundf(1.2f32), 1.2f32.round());
assert_eq!(roundf(-1.2f32), (-1.2f32).round());
assert_eq!(roundf(-1.6f32), (-1.6f32).round());
assert_eq!(roundf(-1.5f32), (-1.5f32).round());
assert_eq!(roundf(1.6f32), 1.6f32.round());
assert_eq!(roundf(1.5f32), 1.5f32.round());
assert_eq!(roundf(2.5f32), 2.5f32.round());
}
#[test]
fn test_round() {
assert_eq!(round(0.), 0.0f64.round());
assert_eq!(round(1.), 1.0f64.round());
assert_eq!(round(1.2), 1.2f64.round());
assert_eq!(round(-1.2), (-1.2f64).round());
assert_eq!(round(-1.6), (-1.6f64).round());
assert_eq!(round(-1.5), (-1.5f64).round());
assert_eq!(round(1.6), 1.6f64.round());
assert_eq!(round(1.5), 1.5f64.round());
assert_eq!(round(2.5), 2.5f64.round());
}
}