Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement f{16,32,64,128}::{erf,erfc} (#![feature(float_erf)]) #136324

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions library/std/src/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,7 @@ impl f128 {
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
// #[unstable(feature = "float_gamma", issue = "99842")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn gamma(self) -> f128 {
unsafe { cmath::tgammaf128(self) }
Expand Down Expand Up @@ -1260,10 +1261,83 @@ impl f128 {
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
// #[unstable(feature = "float_gamma", issue = "99842")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn ln_gamma(self) -> (f128, i32) {
let mut signgamp: i32 = 0;
let x = unsafe { cmath::lgammaf128_r(self, &mut signgamp) };
(x, signgamp)
}

/// Error function.
///
/// # 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.
///
/// This function currently corresponds to the `erff128` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// #![feature(float_erf)]
/// # #[cfg(reliable_f128_math)] {
/// /// The error function relates what percent of a normal distribution lies
/// /// within `x` standard deviations (scaled by `1/sqrt(2)`).
/// fn within_standard_deviations(x: f128) -> f128 {
/// (x * std::f128::consts::FRAC_1_SQRT_2).erf() * 100.0
/// }
///
/// // 68% of a normal distribution is within one standard deviation
/// assert!((within_standard_deviations(1.0) - 68.269).abs() < 0.01);
/// // 95% of a normal distribution is within two standard deviations
/// assert!((within_standard_deviations(2.0) - 95.450).abs() < 0.01);
/// // 99.7% of a normal distribution is within three standard deviations
/// assert!((within_standard_deviations(3.0) - 99.730).abs() < 0.01);
/// # }
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "f128", issue = "116909")]
// #[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erf(self) -> f128 {
unsafe { cmath::erff128(self) }
}

/// Complementary error function.
///
/// # 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.
///
/// This function currently corresponds to the `erfcf128` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// #![feature(float_erf)]
/// # #[cfg(reliable_f128_math)] {
/// let x: f128 = 0.123;
///
/// let one = x.erf() + x.erfc();
/// let abs_difference = (one - 1.0).abs();
///
/// assert!(abs_difference <= f128::EPSILON);
/// # }
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "f128", issue = "116909")]
// #[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erfc(self) -> f128 {
unsafe { cmath::erfcf128(self) }
}
}
72 changes: 72 additions & 0 deletions library/std/src/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,7 @@ impl f16 {
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f16", issue = "116909")]
// #[unstable(feature = "float_gamma", issue = "99842")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn gamma(self) -> f16 {
(unsafe { cmath::tgammaf(self as f32) }) as f16
Expand Down Expand Up @@ -1258,10 +1259,81 @@ impl f16 {
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f16", issue = "116909")]
// #[unstable(feature = "float_gamma", issue = "99842")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn ln_gamma(self) -> (f16, i32) {
let mut signgamp: i32 = 0;
let x = (unsafe { cmath::lgammaf_r(self as f32, &mut signgamp) }) as f16;
(x, signgamp)
}

/// Error function.
///
/// # 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.
///
/// This function currently corresponds to the `erff` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(f16)]
/// #![feature(float_erf)]
/// # #[cfg(reliable_f16_math)] {
/// /// The error function relates what percent of a normal distribution lies
/// /// within `x` standard deviations (scaled by `1/sqrt(2)`).
/// fn within_standard_deviations(x: f16) -> f16 {
/// (x * std::f16::consts::FRAC_1_SQRT_2).erf() * 100.0
/// }
///
/// // 68% of a normal distribution is within one standard deviation
/// assert!((within_standard_deviations(1.0) - 68.269).abs() < 0.1);
/// // 95% of a normal distribution is within two standard deviations
/// assert!((within_standard_deviations(2.0) - 95.450).abs() < 0.1);
/// // 99.7% of a normal distribution is within three standard deviations
/// assert!((within_standard_deviations(3.0) - 99.730).abs() < 0.1);
/// # }
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "f16", issue = "116909")]
// #[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erf(self) -> f16 {
(unsafe { cmath::erff(self as f32) }) as f16
}

/// Complementary error function.
///
/// # 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.
///
/// This function currently corresponds to the `erfcf` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(f16)]
/// #![feature(float_erf)]
/// let x: f16 = 0.123;
///
/// let one = x.erf() + x.erfc();
/// let abs_difference = (one - 1.0).abs();
///
/// assert!(abs_difference <= f16::EPSILON);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "f16", issue = "116909")]
// #[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erfc(self) -> f16 {
(unsafe { cmath::erfcf(self as f32) }) as f16
}
}
64 changes: 64 additions & 0 deletions library/std/src/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,4 +1151,68 @@ impl f32 {
let x = unsafe { cmath::lgammaf_r(self, &mut signgamp) };
(x, signgamp)
}

/// Error function.
///
/// # 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.
///
/// This function currently corresponds to the `erff` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(float_erf)]
/// /// The error function relates what percent of a normal distribution lies
/// /// within `x` standard deviations (scaled by `1/sqrt(2)`).
/// fn within_standard_deviations(x: f32) -> f32 {
/// (x * std::f32::consts::FRAC_1_SQRT_2).erf() * 100.0
/// }
///
/// // 68% of a normal distribution is within one standard deviation
/// assert!((within_standard_deviations(1.0) - 68.269).abs() < 0.01);
/// // 95% of a normal distribution is within two standard deviations
/// assert!((within_standard_deviations(2.0) - 95.450).abs() < 0.01);
/// // 99.7% of a normal distribution is within three standard deviations
/// assert!((within_standard_deviations(3.0) - 99.730).abs() < 0.01);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erf(self) -> f32 {
unsafe { cmath::erff(self) }
}

/// Complementary error function.
///
/// # 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.
///
/// This function currently corresponds to the `erfcf` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(float_erf)]
/// let x: f32 = 0.123;
///
/// let one = x.erf() + x.erfc();
/// let abs_difference = (one - 1.0).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erfc(self) -> f32 {
unsafe { cmath::erfcf(self) }
}
}
64 changes: 64 additions & 0 deletions library/std/src/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,4 +1151,68 @@ impl f64 {
let x = unsafe { cmath::lgamma_r(self, &mut signgamp) };
(x, signgamp)
}

/// Error function.
///
/// # 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.
///
/// This function currently corresponds to the `erf` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(float_erf)]
/// /// The error function relates what percent of a normal distribution lies
/// /// within `x` standard deviations (scaled by `1/sqrt(2)`).
/// fn within_standard_deviations(x: f64) -> f64 {
/// (x * std::f64::consts::FRAC_1_SQRT_2).erf() * 100.0
/// }
///
/// // 68% of a normal distribution is within one standard deviation
/// assert!((within_standard_deviations(1.0) - 68.269).abs() < 0.01);
/// // 95% of a normal distribution is within two standard deviations
/// assert!((within_standard_deviations(2.0) - 95.450).abs() < 0.01);
/// // 99.7% of a normal distribution is within three standard deviations
/// assert!((within_standard_deviations(3.0) - 99.730).abs() < 0.01);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erf(self) -> f64 {
unsafe { cmath::erf(self) }
}

/// Complementary error function.
///
/// # 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.
///
/// This function currently corresponds to the `erfc` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(float_erf)]
/// let x: f64 = 0.123;
///
/// let one = x.erf() + x.erfc();
/// let abs_difference = (one - 1.0).abs();
///
/// assert!(abs_difference <= f64::EPSILON);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erfc(self) -> f64 {
unsafe { cmath::erfc(self) }
}
}
6 changes: 6 additions & 0 deletions library/std/src/sys/cmath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ unsafe extern "C" {
pub fn lgamma_r(n: f64, s: &mut i32) -> f64;
#[cfg(not(target_os = "aix"))]
pub fn lgammaf_r(n: f32, s: &mut i32) -> f32;
pub fn erf(n: f64) -> f64;
pub fn erff(n: f32) -> f32;
pub fn erfc(n: f64) -> f64;
pub fn erfcf(n: f32) -> f32;

pub fn acosf128(n: f128) -> f128;
pub fn asinf128(n: f128) -> f128;
Expand All @@ -43,6 +47,8 @@ unsafe extern "C" {
pub fn tanhf128(n: f128) -> f128;
pub fn tgammaf128(n: f128) -> f128;
pub fn lgammaf128_r(n: f128, s: &mut i32) -> f128;
pub fn erff128(n: f128) -> f128;
pub fn erfcf128(n: f128) -> f128;

cfg_if::cfg_if! {
if #[cfg(not(all(target_os = "windows", target_env = "msvc", target_arch = "x86")))] {
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![feature(rustc_private)]
#![feature(cell_update)]
#![feature(float_gamma)]
#![feature(float_erf)]
#![feature(map_try_insert)]
#![feature(never_type)]
#![feature(try_blocks)]
Expand Down
8 changes: 8 additions & 0 deletions src/tools/miri/src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
| "log1pf"
| "expm1f"
| "tgammaf"
| "erff"
| "erfcf"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add calls to this function in src/tools/miri/tests/pass/float.rs.

=> {
let [f] = this.check_shim(abi, Conv::C , link_name, args)?;
let f = this.read_scalar(f)?.to_f32()?;
Expand All @@ -759,6 +761,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
"log1pf" => f_host.ln_1p(),
"expm1f" => f_host.exp_m1(),
"tgammaf" => f_host.gamma(),
"erff" => f_host.erf(),
"erfcf" => f_host.erfc(),
_ => bug!(),
};
let res = res.to_soft();
Expand Down Expand Up @@ -799,6 +803,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
| "log1p"
| "expm1"
| "tgamma"
| "erf"
| "erfc"
=> {
let [f] = this.check_shim(abi, Conv::C , link_name, args)?;
let f = this.read_scalar(f)?.to_f64()?;
Expand All @@ -816,6 +822,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
"log1p" => f_host.ln_1p(),
"expm1" => f_host.exp_m1(),
"tgamma" => f_host.gamma(),
"erf" => f_host.erf(),
"erfc" => f_host.erfc(),
_ => bug!(),
};
let res = res.to_soft();
Expand Down
Loading
Loading