diff options
| author | Trevor Gross <tmgross@umich.edu> | 2024-06-18 18:25:07 -0500 |
|---|---|---|
| committer | Trevor Gross <tmgross@umich.edu> | 2024-06-25 01:32:36 -0400 |
| commit | 6e2d934a881cc79c14cf7b92e7f57a13426b7c26 (patch) | |
| tree | 4b320a11b8970dec4e81035999acdbd8291edd3a /library/std/src | |
| parent | 0eee0557d076e24c716740c168428f6d12cdf566 (diff) | |
| download | rust-6e2d934a881cc79c14cf7b92e7f57a13426b7c26.tar.gz rust-6e2d934a881cc79c14cf7b92e7f57a13426b7c26.zip | |
Add more `f16` and `f128` library functions and constants
This adds everything that was directly or transitively blocked on const arithmetic for these types, which was recently merged. Since const arithmetic is recent, most of these need to be gated by `bootstrap`. Anything that relies on intrinsics that are still missing is excluded.
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/f128.rs | 30 | ||||
| -rw-r--r-- | library/std/src/f16.rs | 29 |
2 files changed, 59 insertions, 0 deletions
diff --git a/library/std/src/f128.rs b/library/std/src/f128.rs index 491235a872e..0591c6f517b 100644 --- a/library/std/src/f128.rs +++ b/library/std/src/f128.rs @@ -32,4 +32,34 @@ impl f128 { pub fn powi(self, n: i32) -> f128 { unsafe { intrinsics::powif128(self, n) } } + + /// Computes the absolute value of `self`. + /// + /// This function always returns the precise result. + /// + /// # Examples + /// + /// ``` + /// #![feature(f128)] + /// # #[cfg(reliable_f128)] { // FIXME(f16_f128): reliable_f128 + /// + /// let x = 3.5_f128; + /// let y = -3.5_f128; + /// + /// assert_eq!(x.abs(), x); + /// assert_eq!(y.abs(), -y); + /// + /// assert!(f128::NAN.abs().is_nan()); + /// # } + /// ``` + #[inline] + #[cfg(not(bootstrap))] + #[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 abs(self) -> Self { + // FIXME(f16_f128): replace with `intrinsics::fabsf128` when available + // We don't do this now because LLVM has lowering bugs for f128 math. + Self::from_bits(self.to_bits() & !(1 << 127)) + } } diff --git a/library/std/src/f16.rs b/library/std/src/f16.rs index 1cb655ffabd..d4851862299 100644 --- a/library/std/src/f16.rs +++ b/library/std/src/f16.rs @@ -32,4 +32,33 @@ impl f16 { pub fn powi(self, n: i32) -> f16 { unsafe { intrinsics::powif16(self, n) } } + + /// Computes the absolute value of `self`. + /// + /// This function always returns the precise result. + /// + /// # Examples + /// + /// ``` + /// #![feature(f16)] + /// # #[cfg(reliable_f16)] { + /// + /// let x = 3.5_f16; + /// let y = -3.5_f16; + /// + /// assert_eq!(x.abs(), x); + /// assert_eq!(y.abs(), -y); + /// + /// assert!(f16::NAN.abs().is_nan()); + /// # } + /// ``` + #[inline] + #[cfg(not(bootstrap))] + #[rustc_allow_incoherent_impl] + #[unstable(feature = "f16", issue = "116909")] + #[must_use = "method returns a new number and does not mutate the original value"] + pub fn abs(self) -> Self { + // FIXME(f16_f128): replace with `intrinsics::fabsf16` when available + Self::from_bits(self.to_bits() & !(1 << 15)) + } } |
