Fix i_to_f test

When casting from `fN` to `iX`, `fN::{,NEG_}INFINITY` always saturates to to `[iu]X::{MAX,MIN}`, which can sometimes be further away from the original integer that was casted to `fN` than the next closest value to zero, `fN::{MAX,MIN}`. This PR fixes the test by manually calculating the integer value that is 1 ULP above/below `fN::{MAX,MIN}`, therefore allowing the test to pass in this case.
This commit is contained in:
Bee
2026-09-22 18:22:14 +01:00
parent 5e99de02f0
commit b61fc1c685
3 changed files with 74 additions and 48 deletions

View File

@@ -41,13 +41,37 @@ mod i_to_f {
);
let f1: $f_ty = $fn(x);
if f1.is_nan() {
panic!("int->float cannot result in a NaN: {}({x})", stringify!($fn));
}
#[cfg($sys_available)] {
// If `f1` is infinite, calculate the integer that is 1 ULP after/before
// $f_ty::MAX/$f_ty::MIN, saturating as needed. There's no need to
// handle NaN as the result of a conversion can never be NaN.
let as_int = |f: $f_ty| {
if f.is_infinite() {
// For infinity, calculate the float that is half of the desired
// 1-ulp-after-max/-before-min (and therefore representable as
// $f_ty), then multiply by two after converting to an integer.
let bits = <$f_ty>::EXP_MASK - <$f_ty>::IMPLICIT_BIT;
let bits = if f.is_sign_negative() {
bits | <$f_ty as Float>::SIGN_MASK
} else {
bits
};
// float->int casts saturate, so saturate the multiply to match.
(<$f_ty>::from_bits(bits) as $i_ty).saturating_mul(2)
} else {
f as $i_ty
}
};
// This makes sure that the conversion produced the best rounding possible, and does
// this independent of `x as $into` rounding correctly.
// This assumes that float to integer conversion is correct.
let y_minus_ulp = <$f_ty>::from_bits(f1.to_bits().wrapping_sub(1)) as $i_ty;
let y = f1 as $i_ty;
let y_plus_ulp = <$f_ty>::from_bits(f1.to_bits().wrapping_add(1)) as $i_ty;
let y_minus_ulp = as_int(f1.next_down());
let y = as_int(f1);
let y_plus_ulp = as_int(f1.next_up());
let error_minus = <$i_ty as Int>::abs_diff(y_minus_ulp, x);
let error = <$i_ty as Int>::abs_diff(y, x);
let error_plus = <$i_ty as Int>::abs_diff(y_plus_ulp, x);

View File

@@ -11,35 +11,11 @@ use crate::{Int, MinInt};
/// Extension to `libm`'s `Float` trait with methods that are useful for tests but not
/// needed in `libm` itself.
pub trait FloatExt: Float {
/// The minimum subnormal number.
const TINY_BITS: Self::Int = Self::Int::ONE;
/// Retrieve additional constants for this float type.
fn consts() -> Consts<Self> {
Consts::new()
}
/// Increment by one ULP, saturating at infinity.
fn next_up(self) -> Self {
let bits = self.to_bits();
if self.is_nan() || bits == Self::INFINITY.to_bits() {
return self;
}
let abs = self.abs().to_bits();
let next_bits = if abs == Self::Int::ZERO {
// Next up from 0 is the smallest subnormal
Self::TINY_BITS
} else if bits == abs {
// Positive: counting up is more positive
bits + Self::Int::ONE
} else {
// Negative: counting down is more positive
bits - Self::Int::ONE
};
Self::from_bits(next_bits)
}
/// A faster way to effectively call `next_up` `n` times.
fn n_up(self, n: Self::Int) -> Self {
let bits = self.to_bits();
@@ -70,27 +46,6 @@ pub trait FloatExt: Float {
Self::from_bits(next_bits)
}
/// Decrement by one ULP, saturating at negative infinity.
fn next_down(self) -> Self {
let bits = self.to_bits();
if self.is_nan() || bits == Self::NEG_INFINITY.to_bits() {
return self;
}
let abs = self.abs().to_bits();
let next_bits = if abs == Self::Int::ZERO {
// Next up from 0 is the smallest negative subnormal
Self::TINY_BITS | Self::SIGN_MASK
} else if bits == abs {
// Positive: counting down is more negative
bits - Self::Int::ONE
} else {
// Negative: counting up is more negative
bits + Self::Int::ONE
};
Self::from_bits(next_bits)
}
/// A faster way to effectively call `next_down` `n` times.
fn n_down(self, n: Self::Int) -> Self {
let bits = self.to_bits();

View File

@@ -111,6 +111,9 @@ pub trait Float:
/// A mask for the top bit of the significand, useful for NaN ops.
const SIG_TOP_BIT: Self::Int;
/// The minimum subnormal number.
const TINY_BITS: Self::Int = Self::Int::ONE;
/// Returns `self` transmuted to `Self::Int`
fn to_bits(self) -> Self::Int;
@@ -241,6 +244,50 @@ pub trait Float:
// <https://github.com/llvm/llvm-project/issues/32650>).
self * Self::ONE
}
/// Increment by one ULP, saturating at infinity.
fn next_up(self) -> Self {
// FIXME: Replace with `next_up` from `core` once `libm` MSRV reaches 1.86.
let bits = self.to_bits();
if self.is_nan() || bits == Self::INFINITY.to_bits() {
return self;
}
let abs = self.abs().to_bits();
let next_bits = if abs == Self::Int::ZERO {
// Next up from 0 is the smallest subnormal
Self::TINY_BITS
} else if bits == abs {
// Positive: counting up is more positive
bits + Self::Int::ONE
} else {
// Negative: counting down is more positive
bits - Self::Int::ONE
};
Self::from_bits(next_bits)
}
/// Decrement by one ULP, saturating at negative infinity.
fn next_down(self) -> Self {
// FIXME: Replace with `next_down` from `core` once `libm` MSRV reaches 1.86.
let bits = self.to_bits();
if self.is_nan() || bits == Self::NEG_INFINITY.to_bits() {
return self;
}
let abs = self.abs().to_bits();
let next_bits = if abs == Self::Int::ZERO {
// Next up from 0 is the smallest negative subnormal
Self::TINY_BITS | Self::SIGN_MASK
} else if bits == abs {
// Positive: counting down is more negative
bits - Self::Int::ONE
} else {
// Negative: counting up is more negative
bits + Self::Int::ONE
};
Self::from_bits(next_bits)
}
}
/// Access the associated `Int` type from a float (helper to avoid ambiguous associated types).