Skip to content

Commit

Permalink
Initial implementation of core_float_math
Browse files Browse the repository at this point in the history
Since [1], `compiler-builtins` makes a certain set of math symbols
weakly available on all platforms. This means we can begin exposing some
of the related functions in `core`, so begin this process here.

It is not possible to provide inherent methods in both `core` and `std`
while giving them different stability gates, so standalone functions are
added instead. This provides a way to experiment with the functionality
while unstable; once it is time to stabilize, they can be converted to
inherent.

For `f16` and `f128`, everything is unstable so we can move the inherent
methods.

The following are included to start:

* floor
* ceil
* round
* round_ties_even
* trunc
* fract
* mul_add
* div_euclid
* rem_euclid
* powi
* sqrt
* abs_sub
* cbrt

These mirror the set of functions that we have in `compiler-builtins`
since [1].

Tracking issue: rust-lang#137578

[1]: rust-lang/compiler-builtins#763
  • Loading branch information
tgross35 committed Mar 7, 2025
1 parent 98a4878 commit bcb843c
Show file tree
Hide file tree
Showing 12 changed files with 1,642 additions and 800 deletions.
377 changes: 377 additions & 0 deletions library/core/src/num/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,3 +1366,380 @@ impl f128 {
unsafe { intrinsics::copysignf128(self, sign) }
}
}

// Functions in this module fall into `core_float_math`
// FIXME(f16_f128): all doctests must be gated to platforms that have `long double` === `_Float128`
// due to https://github.com/llvm/llvm-project/issues/44744. aarch64 linux matches this.
// #[unstable(feature = "core_float_math", issue = "137578")]
#[cfg(not(test))]
impl f128 {
/// Returns the largest integer less than or equal to `self`.
///
/// This function always returns the precise result.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let f = 3.7_f128;
/// let g = 3.0_f128;
/// let h = -3.7_f128;
///
/// assert_eq!(f.floor(), 3.0);
/// assert_eq!(g.floor(), 3.0);
/// assert_eq!(h.floor(), -4.0);
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn floor(self) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::floorf128(self) }
}

/// Returns the smallest integer greater than or equal to `self`.
///
/// This function always returns the precise result.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let f = 3.01_f128;
/// let g = 4.0_f128;
///
/// assert_eq!(f.ceil(), 4.0);
/// assert_eq!(g.ceil(), 4.0);
/// # }
/// ```
#[inline]
#[doc(alias = "ceiling")]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn ceil(self) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::ceilf128(self) }
}

/// Returns the nearest integer to `self`. If a value is half-way between two
/// integers, round away from `0.0`.
///
/// This function always returns the precise result.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let f = 3.3_f128;
/// let g = -3.3_f128;
/// let h = -3.7_f128;
/// let i = 3.5_f128;
/// let j = 4.5_f128;
///
/// assert_eq!(f.round(), 3.0);
/// assert_eq!(g.round(), -3.0);
/// assert_eq!(h.round(), -4.0);
/// assert_eq!(i.round(), 4.0);
/// assert_eq!(j.round(), 5.0);
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn round(self) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::roundf128(self) }
}

/// Returns the nearest integer to a number. Rounds half-way cases to the number
/// with an even least significant digit.
///
/// This function always returns the precise result.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let f = 3.3_f128;
/// let g = -3.3_f128;
/// let h = 3.5_f128;
/// let i = 4.5_f128;
///
/// assert_eq!(f.round_ties_even(), 3.0);
/// assert_eq!(g.round_ties_even(), -3.0);
/// assert_eq!(h.round_ties_even(), 4.0);
/// assert_eq!(i.round_ties_even(), 4.0);
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn round_ties_even(self) -> f128 {
intrinsics::round_ties_even_f128(self)
}

/// Returns the integer part of `self`.
/// This means that non-integer numbers are always truncated towards zero.
///
/// This function always returns the precise result.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let f = 3.7_f128;
/// let g = 3.0_f128;
/// let h = -3.7_f128;
///
/// assert_eq!(f.trunc(), 3.0);
/// assert_eq!(g.trunc(), 3.0);
/// assert_eq!(h.trunc(), -3.0);
/// # }
/// ```
#[inline]
#[doc(alias = "truncate")]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn trunc(self) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::truncf128(self) }
}

/// Returns the fractional part of `self`.
///
/// This function always returns the precise result.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let x = 3.6_f128;
/// let y = -3.6_f128;
/// let abs_difference_x = (x.fract() - 0.6).abs();
/// let abs_difference_y = (y.fract() - (-0.6)).abs();
///
/// assert!(abs_difference_x <= f128::EPSILON);
/// assert!(abs_difference_y <= f128::EPSILON);
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn fract(self) -> f128 {
self - self.trunc()
}

/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
/// error, yielding a more accurate result than an unfused multiply-add.
///
/// Using `mul_add` *may* be more performant than an unfused multiply-add if
/// the target architecture has a dedicated `fma` CPU instruction. However,
/// this is not always true, and will be heavily dependant on designing
/// algorithms with specific target hardware in mind.
///
/// # Precision
///
/// The result of this operation is guaranteed to be the rounded
/// infinite-precision result. It is specified by IEEE 754 as
/// `fusedMultiplyAdd` and guaranteed not to change.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let m = 10.0_f128;
/// let x = 4.0_f128;
/// let b = 60.0_f128;
///
/// assert_eq!(m.mul_add(x, b), 100.0);
/// assert_eq!(m * x + b, 100.0);
///
/// let one_plus_eps = 1.0_f128 + f128::EPSILON;
/// let one_minus_eps = 1.0_f128 - f128::EPSILON;
/// let minus_one = -1.0_f128;
///
/// // The exact result (1 + eps) * (1 - eps) = 1 - eps * eps.
/// assert_eq!(one_plus_eps.mul_add(one_minus_eps, minus_one), -f128::EPSILON * f128::EPSILON);
/// // Different rounding with the non-fused multiply and add.
/// assert_eq!(one_plus_eps * one_minus_eps + minus_one, 0.0);
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[doc(alias = "fmaf128", alias = "fusedMultiplyAdd")]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn mul_add(self, a: f128, b: f128) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::fmaf128(self, a, b) }
}

/// Calculates Euclidean division, the matching method for `rem_euclid`.
///
/// This computes the integer `n` such that
/// `self = n * rhs + self.rem_euclid(rhs)`.
/// In other words, the result is `self / rhs` rounded to the integer `n`
/// such that `self >= n * rhs`.
///
/// # Precision
///
/// The result of this operation is guaranteed to be the rounded
/// infinite-precision result.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let a: f128 = 7.0;
/// let b = 4.0;
/// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
/// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
/// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
/// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn div_euclid(self, rhs: f128) -> f128 {
let q = (self / rhs).trunc();
if self % rhs < 0.0 {
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
}
q
}

/// Calculates the least nonnegative remainder of `self (mod rhs)`.
///
/// In particular, the return value `r` satisfies `0.0 <= r < rhs.abs()` in
/// most cases. However, due to a floating point round-off error it can
/// result in `r == rhs.abs()`, violating the mathematical definition, if
/// `self` is much smaller than `rhs.abs()` in magnitude and `self < 0.0`.
/// This result is not an element of the function's codomain, but it is the
/// closest floating point number in the real numbers and thus fulfills the
/// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
/// approximately.
///
/// # Precision
///
/// The result of this operation is guaranteed to be the rounded
/// infinite-precision result.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let a: f128 = 7.0;
/// let b = 4.0;
/// assert_eq!(a.rem_euclid(b), 3.0);
/// assert_eq!((-a).rem_euclid(b), 1.0);
/// assert_eq!(a.rem_euclid(-b), 3.0);
/// assert_eq!((-a).rem_euclid(-b), 1.0);
/// // limitation due to round-off error
/// assert!((-f128::EPSILON).rem_euclid(3.0) != 0.0);
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[doc(alias = "modulo", alias = "mod")]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn rem_euclid(self, rhs: f128) -> f128 {
let r = self % rhs;
if r < 0.0 { r + rhs.abs() } else { r }
}

/// Raises a number to an integer power.
///
/// Using this function is generally faster than using `powf`.
/// It might have a different sequence of rounding operations than `powf`,
/// so the results are not guaranteed to agree.
///
/// # Unspecified precision
///
/// The precision of this function is non-deterministic. This means it varies by platform,
/// Rust version, and can even differ within the same execution from one invocation to the next.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let x = 2.0_f128;
/// let abs_difference = (x.powi(2) - (x * x)).abs();
/// assert!(abs_difference <= f128::EPSILON);
///
/// assert_eq!(f128::powi(f128::NAN, 0), 1.0);
/// # }
/// ```
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn powi(self, n: i32) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::powif128(self, n) }
}

/// Returns the square root of a number.
///
/// Returns NaN if `self` is a negative number other than `-0.0`.
///
/// # Precision
///
/// The result of this operation is guaranteed to be the rounded
/// infinite-precision result. It is specified by IEEE 754 as `squareRoot`
/// and guaranteed not to change.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// # #[cfg(all(target_arch = "aarch64", target_os = "linux"))] {
///
/// let positive = 4.0_f128;
/// let negative = -4.0_f128;
/// let negative_zero = -0.0_f128;
///
/// assert_eq!(positive.sqrt(), 2.0);
/// assert!(negative.sqrt().is_nan());
/// assert!(negative_zero.sqrt() == negative_zero);
/// # }
/// ```
#[inline]
#[doc(alias = "squareRoot")]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn sqrt(self) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::sqrtf128(self) }
}
}
Loading

0 comments on commit bcb843c

Please sign in to comment.