pub const fn roundf_ties_even(x: f32) -> f32 {
const BIAS: i32 = 0x7f;
const MANT_DIG: i32 = 24;
const MAX_EXP: i32 = 2 * BIAS + 1;
let mut ix: u32 = x.to_bits();
let ux: u32 = ix & 0x7fffffff;
let exponent: i32 = (ux >> (MANT_DIG - 1)) as i32;
if exponent >= BIAS + MANT_DIG - 1 {
if exponent == MAX_EXP {
return x + x;
} else {
return x;
}
} else if exponent >= BIAS {
let int_pos: i32 = (BIAS + MANT_DIG - 1) - exponent;
let half_pos: i32 = int_pos - 1;
let half_bit: u32 = 1u32 << half_pos;
let int_bit: u32 = 1u32 << int_pos;
if (ix & (int_bit | (half_bit - 1))) != 0 {
ix = ix.wrapping_add(half_bit);
}
ix &= !(int_bit - 1);
} else if exponent == BIAS - 1 && ux > 0x3f000000 {
ix = (ix & 0x80000000) | 0x3f800000;
} else {
ix &= 0x80000000;
}
f32::from_bits(ix)
}
pub const fn round_ties_even(x: f64) -> f64 {
let mut ix: u64 = x.to_bits();
let ux = ix & 0x7fffffffffffffffu64;
const BIAS: i32 = 0x3ff;
const MANT_DIG: i32 = 53;
const MAX_EXP: i32 = 2 * BIAS + 1;
let exponent: i32 = (ux >> (MANT_DIG - 1)) as i32;
if exponent >= BIAS + MANT_DIG - 1 {
return if exponent == MAX_EXP {
x + x
} else {
x
};
} else if exponent >= BIAS {
let int_pos: i32 = (BIAS + MANT_DIG - 1) - exponent;
let half_pos: i32 = int_pos - 1;
let half_bit: u64 = 1u64 << half_pos;
let int_bit: u64 = 1u64 << int_pos;
if (ix & (int_bit | (half_bit - 1))) != 0 {
ix = ix.wrapping_add(half_bit);
}
ix &= !(int_bit - 1);
} else if exponent == BIAS - 1 && ux > 0x3fe0000000000000u64 {
ix = (ix & 0x8000000000000000u64) | 0x3ff0000000000000u64;
} else {
ix &= 0x8000000000000000u64;
}
f64::from_bits(ix)
}
pub(crate) trait CpuRoundTiesEven {
fn cpu_round_ties_even(self) -> Self;
}
impl CpuRoundTiesEven for f32 {
#[inline]
fn cpu_round_ties_even(self) -> Self {
#[cfg(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse4.1"
),
target_arch = "aarch64"
))]
{
self.round_ties_even()
}
#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse4.1"
),
target_arch = "aarch64"
)))]
{
roundf_ties_even(self)
}
}
}
impl CpuRoundTiesEven for f64 {
#[inline]
fn cpu_round_ties_even(self) -> Self {
#[cfg(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse4.1"
),
target_arch = "aarch64"
))]
{
self.round_ties_even()
}
#[cfg(not(any(
all(
any(target_arch = "x86", target_arch = "x86_64"),
target_feature = "sse4.1"
),
target_arch = "aarch64"
)))]
{
round_ties_even(self)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_roundf_ties_even() {
assert_eq!(roundf_ties_even(0f32), 0.0f32.round_ties_even());
assert_eq!(roundf_ties_even(0.5f32), 0.5f32.round_ties_even());
assert_eq!(roundf_ties_even(-0.5), (-0.5f32).round_ties_even());
assert_eq!(roundf_ties_even(1f32), 1.0f32.round_ties_even());
assert_eq!(roundf_ties_even(1.2f32), 1.2f32.round_ties_even());
assert_eq!(roundf_ties_even(-1.2f32), (-1.2f32).round_ties_even());
assert_eq!(roundf_ties_even(-1.6f32), (-1.6f32).round_ties_even());
assert_eq!(roundf_ties_even(-1.5f32), (-1.5f32).round_ties_even());
assert_eq!(roundf_ties_even(1.6f32), 1.6f32.round_ties_even());
assert_eq!(roundf_ties_even(1.5f32), 1.5f32.round_ties_even());
assert_eq!(roundf_ties_even(2.5f32), 2.5f32.round_ties_even());
assert_eq!(
roundf_ties_even(f32::INFINITY),
f32::INFINITY.round_ties_even()
);
assert_eq!(
roundf_ties_even(f32::NEG_INFINITY),
f32::NEG_INFINITY.round_ties_even()
);
assert!(roundf_ties_even(f32::NAN).is_nan());
}
#[test]
fn test_round_ties_even() {
assert_eq!(
round_ties_even(5.6916e-320),
(5.6916e-320f64).round_ties_even()
);
assert_eq!(round_ties_even(3f64), 3f64.round_ties_even());
assert_eq!(round_ties_even(2f64), 2f64.round_ties_even());
assert_eq!(round_ties_even(0.), 0.0f64.round_ties_even());
assert_eq!(round_ties_even(0.5), 0.5f64.round_ties_even());
assert_eq!(round_ties_even(-0.5), (-0.5f64).round_ties_even());
assert_eq!(round_ties_even(1.), 1.0f64.round_ties_even());
assert_eq!(round_ties_even(1.2), 1.2f64.round_ties_even());
assert_eq!(round_ties_even(-1.2), (-1.2f64).round_ties_even());
assert_eq!(round_ties_even(-1.6), (-1.6f64).round_ties_even());
assert_eq!(round_ties_even(-1.5), (-1.5f64).round_ties_even());
assert_eq!(round_ties_even(1.6), 1.6f64.round_ties_even());
assert_eq!(round_ties_even(1.5), 1.5f64.round_ties_even());
assert_eq!(round_ties_even(2.5), 2.5f64.round_ties_even());
assert_eq!(round_ties_even(-2.5), (-2.5f64).round_ties_even());
assert_eq!(
round_ties_even(f64::INFINITY),
f64::INFINITY.round_ties_even()
);
assert_eq!(
round_ties_even(f64::NEG_INFINITY),
f64::NEG_INFINITY.round_ties_even()
);
assert!(round_ties_even(f64::NAN).is_nan());
}
}