Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add const support for float rounding methods
Add const support for the float rounding methods floor, ceil, trunc,
fract, round and round_ties_even.
This works by moving the calculation logic from

     src/tools/miri/src/intrinsics/mod.rs

into

     compiler/rustc_const_eval/src/interpret/intrinsics.rs.

All relevant method definitions were adjusted to include the `const`
keyword for all supported float types: f16, f32, f64 and f128.

The constness is hidden behind the feature gate

     feature(const_float_round_methods)

which is tracked in

     #141555

This commit is a squash of the following commits:
- test: add tests that we expect to pass when float rounding becomes const
- feat: make float rounding methods `const`
- fix: replace `rustc_allow_const_fn_unstable(core_intrinsics)` attribute with `#[rustc_const_unstable(feature = "f128", issue = "116909")]` in `library/core/src/num/f128.rs`
- revert: undo update to `library/stdarch`
- refactor: replace multiple `float_<mode>_intrinsic` rounding methods with a single, parametrized one
- fix: add `#[cfg(not(bootstrap))]` to new const method tests
- test: add extra sign tests to check `+0.0` and `-0.0`
- revert: undo accidental changes to `round` docs
- fix: gate `const` float round method behind `const_float_round_methods`
- fix: remove unnecessary `#![feature(const_float_methods)]`
- fix: remove unnecessary `#![feature(const_float_methods)]` [2]
- revert: undo changes to `tests/ui/consts/const-eval/float_methods.rs`
- fix: adjust after rebase
- test: fix float tests
- test: add tests for `fract`
- chore: add commented-out `const_float_round_methods` feature gates to `f16` and `f128`
- fix: adjust NaN when rounding floats
- chore: add FIXME comment for de-duplicating float tests
- test: remove unnecessary test file `tests/ui/consts/const-eval/float_methods.rs`
- test: fix tests after upstream simplification of how float tests are run
  • Loading branch information
ruancomelli committed May 31, 2025
commit f8e97badb26f1436d062b3bfdd4f50adc41b843a
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 @@ -2212,86 +2212,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 @@ -2300,7 +2300,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 @@ -2309,7 +2309,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;

/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number with an even
/// least significant digit.
Expand All @@ -2318,7 +2318,7 @@ pub fn round_ties_even_f32(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;

/// Returns the nearest integer to an `f128`. Rounds half-way cases to the number with an even
/// least significant digit.
Expand All @@ -2327,36 +2327,36 @@ pub fn round_ties_even_f64(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