Skip to content

feat: add const support for float rounding methods #141521

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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
56ce2f3
test: add tests that we expect to pass when float rounding becomes const
ruancomelli May 19, 2025
8de1103
feat: make float rounding methods `const`
ruancomelli May 24, 2025
0b1d30e
fix: replace `rustc_allow_const_fn_unstable(core_intrinsics)` attribu…
ruancomelli May 24, 2025
ca8a8db
revert: undo update to `library/stdarch`
ruancomelli May 24, 2025
1113a19
refactor: replace multiple `float_<mode>_intrinsic` rounding methods …
ruancomelli May 24, 2025
d1bb3f0
revert: undo changes to `rint` functions
ruancomelli May 25, 2025
b7fb0df
fix: add `#[cfg(not(bootstrap))]` to new const method tests
ruancomelli May 25, 2025
0e24d5c
test: add extra sign tests to check `+0.0` and `-0.0`
ruancomelli May 25, 2025
52ce976
revert: undo accidental changes to `round` docs
ruancomelli May 25, 2025
b186fd1
fix: gate `const` float round method behind `const_float_round_methods`
ruancomelli May 25, 2025
0caa691
fix: remove unnecessary `#![feature(const_float_methods)]`
ruancomelli May 25, 2025
74e3628
fix: remove unnecessary `#![feature(const_float_methods)]` [2]
ruancomelli May 25, 2025
3af13ac
revert: undo changes to `tests/ui/consts/const-eval/float_methods.rs`
ruancomelli May 27, 2025
e257f2f
fix: adjust after rebase
ruancomelli May 27, 2025
ca73cae
test: fix float tests
ruancomelli May 27, 2025
f8930f4
test: add tests for `fract`
ruancomelli May 27, 2025
c24e404
chore: add commented-out `const_float_round_methods` feature gates to…
ruancomelli May 28, 2025
85056fd
fix: adjust NaN when rounding floats
ruancomelli May 28, 2025
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
113 changes: 113 additions & 0 deletions compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,103 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
sym::fabsf64 => self.float_abs_intrinsic::<Double>(args, dest)?,
sym::fabsf128 => self.float_abs_intrinsic::<Quad>(args, dest)?,

sym::floorf16 => self.float_round_intrinsic::<Half>(
args,
dest,
rustc_apfloat::Round::TowardNegative,
)?,
sym::floorf32 => self.float_round_intrinsic::<Single>(
args,
dest,
rustc_apfloat::Round::TowardNegative,
)?,
sym::floorf64 => self.float_round_intrinsic::<Double>(
args,
dest,
rustc_apfloat::Round::TowardNegative,
)?,
sym::floorf128 => self.float_round_intrinsic::<Quad>(
args,
dest,
rustc_apfloat::Round::TowardNegative,
)?,

sym::ceilf16 => self.float_round_intrinsic::<Half>(
args,
dest,
rustc_apfloat::Round::TowardPositive,
)?,
sym::ceilf32 => self.float_round_intrinsic::<Single>(
args,
dest,
rustc_apfloat::Round::TowardPositive,
)?,
sym::ceilf64 => self.float_round_intrinsic::<Double>(
args,
dest,
rustc_apfloat::Round::TowardPositive,
)?,
sym::ceilf128 => self.float_round_intrinsic::<Quad>(
args,
dest,
rustc_apfloat::Round::TowardPositive,
)?,

sym::truncf16 => {
self.float_round_intrinsic::<Half>(args, dest, rustc_apfloat::Round::TowardZero)?
}
sym::truncf32 => {
self.float_round_intrinsic::<Single>(args, dest, rustc_apfloat::Round::TowardZero)?
}
sym::truncf64 => {
self.float_round_intrinsic::<Double>(args, dest, rustc_apfloat::Round::TowardZero)?
}
sym::truncf128 => {
self.float_round_intrinsic::<Quad>(args, dest, rustc_apfloat::Round::TowardZero)?
}

sym::roundf16 => self.float_round_intrinsic::<Half>(
args,
dest,
rustc_apfloat::Round::NearestTiesToAway,
)?,
sym::roundf32 => self.float_round_intrinsic::<Single>(
args,
dest,
rustc_apfloat::Round::NearestTiesToAway,
)?,
sym::roundf64 => self.float_round_intrinsic::<Double>(
args,
dest,
rustc_apfloat::Round::NearestTiesToAway,
)?,
sym::roundf128 => self.float_round_intrinsic::<Quad>(
args,
dest,
rustc_apfloat::Round::NearestTiesToAway,
)?,

sym::round_ties_even_f16 => self.float_round_intrinsic::<Half>(
args,
dest,
rustc_apfloat::Round::NearestTiesToEven,
)?,
sym::round_ties_even_f32 => self.float_round_intrinsic::<Single>(
args,
dest,
rustc_apfloat::Round::NearestTiesToEven,
)?,
sym::round_ties_even_f64 => self.float_round_intrinsic::<Double>(
args,
dest,
rustc_apfloat::Round::NearestTiesToEven,
)?,
sym::round_ties_even_f128 => self.float_round_intrinsic::<Quad>(
args,
dest,
rustc_apfloat::Round::NearestTiesToEven,
)?,

// Unsupported intrinsic: skip the return_to_block below.
_ => return interp_ok(false),
}
Expand Down Expand Up @@ -900,4 +997,20 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
self.write_scalar(x.abs(), dest)?;
interp_ok(())
}

fn float_round_intrinsic<F>(
&mut self,
args: &[OpTy<'tcx, M::Provenance>],
dest: &PlaceTy<'tcx, M::Provenance>,
mode: rustc_apfloat::Round,
) -> InterpResult<'tcx, ()>
where
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
{
let x: F = self.read_scalar(&args[0])?.to_float()?;
let res = x.round_to_integral(mode).value;
let res = self.adjust_nan(res, &[x]);
self.write_scalar(res, dest)?;
interp_ok(())
}
}
40 changes: 20 additions & 20 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,86 +2186,86 @@ pub unsafe fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
/// [`f16::floor`](../../std/primitive.f16.html#method.floor)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn floorf16(x: f16) -> f16;
pub const unsafe fn floorf16(x: f16) -> f16;
/// Returns the largest integer less than or equal to an `f32`.
///
/// The stabilized version of this intrinsic is
/// [`f32::floor`](../../std/primitive.f32.html#method.floor)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn floorf32(x: f32) -> f32;
pub const unsafe fn floorf32(x: f32) -> f32;
/// Returns the largest integer less than or equal to an `f64`.
///
/// The stabilized version of this intrinsic is
/// [`f64::floor`](../../std/primitive.f64.html#method.floor)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn floorf64(x: f64) -> f64;
pub const unsafe fn floorf64(x: f64) -> f64;
/// Returns the largest integer less than or equal to an `f128`.
///
/// The stabilized version of this intrinsic is
/// [`f128::floor`](../../std/primitive.f128.html#method.floor)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn floorf128(x: f128) -> f128;
pub const unsafe fn floorf128(x: f128) -> f128;

/// Returns the smallest integer greater than or equal to an `f16`.
///
/// The stabilized version of this intrinsic is
/// [`f16::ceil`](../../std/primitive.f16.html#method.ceil)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn ceilf16(x: f16) -> f16;
pub const unsafe fn ceilf16(x: f16) -> f16;
/// Returns the smallest integer greater than or equal to an `f32`.
///
/// The stabilized version of this intrinsic is
/// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn ceilf32(x: f32) -> f32;
pub const unsafe fn ceilf32(x: f32) -> f32;
/// Returns the smallest integer greater than or equal to an `f64`.
///
/// The stabilized version of this intrinsic is
/// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn ceilf64(x: f64) -> f64;
pub const unsafe fn ceilf64(x: f64) -> f64;
/// Returns the smallest integer greater than or equal to an `f128`.
///
/// The stabilized version of this intrinsic is
/// [`f128::ceil`](../../std/primitive.f128.html#method.ceil)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn ceilf128(x: f128) -> f128;
pub const unsafe fn ceilf128(x: f128) -> f128;

/// Returns the integer part of an `f16`.
///
/// The stabilized version of this intrinsic is
/// [`f16::trunc`](../../std/primitive.f16.html#method.trunc)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn truncf16(x: f16) -> f16;
pub const unsafe fn truncf16(x: f16) -> f16;
/// Returns the integer part of an `f32`.
///
/// The stabilized version of this intrinsic is
/// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn truncf32(x: f32) -> f32;
pub const unsafe fn truncf32(x: f32) -> f32;
/// Returns the integer part of an `f64`.
///
/// The stabilized version of this intrinsic is
/// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn truncf64(x: f64) -> f64;
pub const unsafe fn truncf64(x: f64) -> f64;
/// Returns the integer part of an `f128`.
///
/// The stabilized version of this intrinsic is
/// [`f128::trunc`](../../std/primitive.f128.html#method.trunc)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn truncf128(x: f128) -> f128;
pub const unsafe fn truncf128(x: f128) -> f128;

/// Returns the nearest integer to an `f16`. Rounds half-way cases to the number with an even
/// least significant digit.
Expand All @@ -2274,7 +2274,7 @@ pub unsafe fn truncf128(x: f128) -> f128;
/// [`f16::round_ties_even`](../../std/primitive.f16.html#method.round_ties_even)
#[rustc_intrinsic]
#[rustc_nounwind]
pub fn round_ties_even_f16(x: f16) -> f16;
pub const fn round_ties_even_f16(x: f16) -> f16;

/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number with an even
/// least significant digit.
Expand All @@ -2283,7 +2283,7 @@ pub fn round_ties_even_f16(x: f16) -> f16;
/// [`f32::round_ties_even`](../../std/primitive.f32.html#method.round_ties_even)
#[rustc_intrinsic]
#[rustc_nounwind]
pub fn round_ties_even_f32(x: f32) -> f32;
pub const fn round_ties_even_f32(x: f32) -> f32;

/// Provided for compatibility with stdarch. DO NOT USE.
#[inline(always)]
Expand All @@ -2298,7 +2298,7 @@ pub unsafe fn rintf32(x: f32) -> f32 {
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
#[rustc_intrinsic]
#[rustc_nounwind]
pub fn round_ties_even_f64(x: f64) -> f64;
pub const fn round_ties_even_f64(x: f64) -> f64;

/// Provided for compatibility with stdarch. DO NOT USE.
#[inline(always)]
Expand All @@ -2313,36 +2313,36 @@ pub unsafe fn rintf64(x: f64) -> f64 {
/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
#[rustc_intrinsic]
#[rustc_nounwind]
pub fn round_ties_even_f128(x: f128) -> f128;
pub const fn round_ties_even_f128(x: f128) -> f128;

/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
///
/// The stabilized version of this intrinsic is
/// [`f16::round`](../../std/primitive.f16.html#method.round)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn roundf16(x: f16) -> f16;
pub const unsafe fn roundf16(x: f16) -> f16;
/// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
///
/// The stabilized version of this intrinsic is
/// [`f32::round`](../../std/primitive.f32.html#method.round)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn roundf32(x: f32) -> f32;
pub const unsafe fn roundf32(x: f32) -> f32;
/// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
///
/// The stabilized version of this intrinsic is
/// [`f64::round`](../../std/primitive.f64.html#method.round)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn roundf64(x: f64) -> f64;
pub const unsafe fn roundf64(x: f64) -> f64;
/// Returns the nearest integer to an `f128`. Rounds half-way cases away from zero.
///
/// The stabilized version of this intrinsic is
/// [`f128::round`](../../std/primitive.f128.html#method.round)
#[rustc_intrinsic]
#[rustc_nounwind]
pub unsafe fn roundf128(x: f128) -> f128;
pub const unsafe fn roundf128(x: f128) -> f128;

/// Float addition that allows optimizations based on algebraic rules.
/// May assume inputs are finite.
Expand Down
24 changes: 18 additions & 6 deletions library/core/src/num/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,8 +1447,10 @@ impl f128 {
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[rustc_const_unstable(feature = "f128", issue = "116909")]
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn floor(self) -> f128 {
pub const fn floor(self) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::floorf128(self) }
}
Expand Down Expand Up @@ -1477,8 +1479,10 @@ impl f128 {
#[doc(alias = "ceiling")]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[rustc_const_unstable(feature = "f128", issue = "116909")]
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn ceil(self) -> f128 {
pub const fn ceil(self) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::ceilf128(self) }
}
Expand Down Expand Up @@ -1513,8 +1517,10 @@ impl f128 {
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[rustc_const_unstable(feature = "f128", issue = "116909")]
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn round(self) -> f128 {
pub const fn round(self) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::roundf128(self) }
}
Expand Down Expand Up @@ -1547,8 +1553,10 @@ impl f128 {
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[rustc_const_unstable(feature = "f128", issue = "116909")]
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn round_ties_even(self) -> f128 {
pub const fn round_ties_even(self) -> f128 {
intrinsics::round_ties_even_f128(self)
}

Expand Down Expand Up @@ -1579,8 +1587,10 @@ impl f128 {
#[doc(alias = "truncate")]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[rustc_const_unstable(feature = "f128", issue = "116909")]
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn trunc(self) -> f128 {
pub const fn trunc(self) -> f128 {
// SAFETY: intrinsic with no preconditions
unsafe { intrinsics::truncf128(self) }
}
Expand Down Expand Up @@ -1610,8 +1620,10 @@ impl f128 {
#[inline]
#[rustc_allow_incoherent_impl]
#[unstable(feature = "f128", issue = "116909")]
#[rustc_const_unstable(feature = "f128", issue = "116909")]
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn fract(self) -> f128 {
pub const fn fract(self) -> f128 {
self - self.trunc()
}

Expand Down
Loading
Loading