Skip to content

Commit 74bb7ed

Browse files
committed
feat: make float rounding methods const
1 parent 796794b commit 74bb7ed

File tree

11 files changed

+243
-118
lines changed

11 files changed

+243
-118
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,37 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
518518
sym::fabsf64 => self.float_abs_intrinsic::<Double>(args, dest)?,
519519
sym::fabsf128 => self.float_abs_intrinsic::<Quad>(args, dest)?,
520520

521+
sym::floorf16 => self.float_floor_intrinsic::<Half>(args, dest)?,
522+
sym::floorf32 => self.float_floor_intrinsic::<Single>(args, dest)?,
523+
sym::floorf64 => self.float_floor_intrinsic::<Double>(args, dest)?,
524+
sym::floorf128 => self.float_floor_intrinsic::<Quad>(args, dest)?,
525+
526+
sym::ceilf16 => self.float_ceil_intrinsic::<Half>(args, dest)?,
527+
sym::ceilf32 => self.float_ceil_intrinsic::<Single>(args, dest)?,
528+
sym::ceilf64 => self.float_ceil_intrinsic::<Double>(args, dest)?,
529+
sym::ceilf128 => self.float_ceil_intrinsic::<Quad>(args, dest)?,
530+
531+
sym::truncf16 => self.float_trunc_intrinsic::<Half>(args, dest)?,
532+
sym::truncf32 => self.float_trunc_intrinsic::<Single>(args, dest)?,
533+
sym::truncf64 => self.float_trunc_intrinsic::<Double>(args, dest)?,
534+
sym::truncf128 => self.float_trunc_intrinsic::<Quad>(args, dest)?,
535+
536+
sym::roundf16 => self.float_round_intrinsic::<Half>(args, dest)?,
537+
sym::roundf32 => self.float_round_intrinsic::<Single>(args, dest)?,
538+
sym::roundf64 => self.float_round_intrinsic::<Double>(args, dest)?,
539+
sym::roundf128 => self.float_round_intrinsic::<Quad>(args, dest)?,
540+
541+
sym::round_ties_even_f16 => self.float_round_ties_even_intrinsic::<Half>(args, dest)?,
542+
sym::round_ties_even_f32 => {
543+
self.float_round_ties_even_intrinsic::<Single>(args, dest)?
544+
}
545+
sym::round_ties_even_f64 => {
546+
self.float_round_ties_even_intrinsic::<Double>(args, dest)?
547+
}
548+
sym::round_ties_even_f128 => {
549+
self.float_round_ties_even_intrinsic::<Quad>(args, dest)?
550+
}
551+
521552
// Unsupported intrinsic: skip the return_to_block below.
522553
_ => return interp_ok(false),
523554
}
@@ -900,4 +931,74 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
900931
self.write_scalar(x.abs(), dest)?;
901932
interp_ok(())
902933
}
934+
935+
fn float_floor_intrinsic<F>(
936+
&mut self,
937+
args: &[OpTy<'tcx, M::Provenance>],
938+
dest: &MPlaceTy<'tcx, M::Provenance>,
939+
) -> InterpResult<'tcx, ()>
940+
where
941+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
942+
{
943+
let x: F = self.read_scalar(&args[0])?.to_float()?;
944+
let res = x.round_to_integral(rustc_apfloat::Round::TowardNegative).value;
945+
self.write_scalar(res, dest)?;
946+
interp_ok(())
947+
}
948+
949+
fn float_ceil_intrinsic<F>(
950+
&mut self,
951+
args: &[OpTy<'tcx, M::Provenance>],
952+
dest: &MPlaceTy<'tcx, M::Provenance>,
953+
) -> InterpResult<'tcx, ()>
954+
where
955+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
956+
{
957+
let x: F = self.read_scalar(&args[0])?.to_float()?;
958+
let res = x.round_to_integral(rustc_apfloat::Round::TowardPositive).value;
959+
self.write_scalar(res, dest)?;
960+
interp_ok(())
961+
}
962+
963+
fn float_trunc_intrinsic<F>(
964+
&mut self,
965+
args: &[OpTy<'tcx, M::Provenance>],
966+
dest: &MPlaceTy<'tcx, M::Provenance>,
967+
) -> InterpResult<'tcx, ()>
968+
where
969+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
970+
{
971+
let x: F = self.read_scalar(&args[0])?.to_float()?;
972+
let res = x.round_to_integral(rustc_apfloat::Round::TowardZero).value;
973+
self.write_scalar(res, dest)?;
974+
interp_ok(())
975+
}
976+
977+
fn float_round_intrinsic<F>(
978+
&mut self,
979+
args: &[OpTy<'tcx, M::Provenance>],
980+
dest: &MPlaceTy<'tcx, M::Provenance>,
981+
) -> InterpResult<'tcx, ()>
982+
where
983+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
984+
{
985+
let x: F = self.read_scalar(&args[0])?.to_float()?;
986+
let res = x.round_to_integral(rustc_apfloat::Round::NearestTiesToAway).value;
987+
self.write_scalar(res, dest)?;
988+
interp_ok(())
989+
}
990+
991+
fn float_round_ties_even_intrinsic<F>(
992+
&mut self,
993+
args: &[OpTy<'tcx, M::Provenance>],
994+
dest: &MPlaceTy<'tcx, M::Provenance>,
995+
) -> InterpResult<'tcx, ()>
996+
where
997+
F: rustc_apfloat::Float + rustc_apfloat::FloatConvert<F> + Into<Scalar<M::Provenance>>,
998+
{
999+
let x: F = self.read_scalar(&args[0])?.to_float()?;
1000+
let res = x.round_to_integral(rustc_apfloat::Round::NearestTiesToEven).value;
1001+
self.write_scalar(res, dest)?;
1002+
interp_ok(())
1003+
}
9031004
}

library/core/src/intrinsics/mod.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,86 +2198,86 @@ pub unsafe fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
21982198
/// [`f16::floor`](../../std/primitive.f16.html#method.floor)
21992199
#[rustc_intrinsic]
22002200
#[rustc_nounwind]
2201-
pub unsafe fn floorf16(x: f16) -> f16;
2201+
pub const unsafe fn floorf16(x: f16) -> f16;
22022202
/// Returns the largest integer less than or equal to an `f32`.
22032203
///
22042204
/// The stabilized version of this intrinsic is
22052205
/// [`f32::floor`](../../std/primitive.f32.html#method.floor)
22062206
#[rustc_intrinsic]
22072207
#[rustc_nounwind]
2208-
pub unsafe fn floorf32(x: f32) -> f32;
2208+
pub const unsafe fn floorf32(x: f32) -> f32;
22092209
/// Returns the largest integer less than or equal to an `f64`.
22102210
///
22112211
/// The stabilized version of this intrinsic is
22122212
/// [`f64::floor`](../../std/primitive.f64.html#method.floor)
22132213
#[rustc_intrinsic]
22142214
#[rustc_nounwind]
2215-
pub unsafe fn floorf64(x: f64) -> f64;
2215+
pub const unsafe fn floorf64(x: f64) -> f64;
22162216
/// Returns the largest integer less than or equal to an `f128`.
22172217
///
22182218
/// The stabilized version of this intrinsic is
22192219
/// [`f128::floor`](../../std/primitive.f128.html#method.floor)
22202220
#[rustc_intrinsic]
22212221
#[rustc_nounwind]
2222-
pub unsafe fn floorf128(x: f128) -> f128;
2222+
pub const unsafe fn floorf128(x: f128) -> f128;
22232223

22242224
/// Returns the smallest integer greater than or equal to an `f16`.
22252225
///
22262226
/// The stabilized version of this intrinsic is
22272227
/// [`f16::ceil`](../../std/primitive.f16.html#method.ceil)
22282228
#[rustc_intrinsic]
22292229
#[rustc_nounwind]
2230-
pub unsafe fn ceilf16(x: f16) -> f16;
2230+
pub const unsafe fn ceilf16(x: f16) -> f16;
22312231
/// Returns the smallest integer greater than or equal to an `f32`.
22322232
///
22332233
/// The stabilized version of this intrinsic is
22342234
/// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
22352235
#[rustc_intrinsic]
22362236
#[rustc_nounwind]
2237-
pub unsafe fn ceilf32(x: f32) -> f32;
2237+
pub const unsafe fn ceilf32(x: f32) -> f32;
22382238
/// Returns the smallest integer greater than or equal to an `f64`.
22392239
///
22402240
/// The stabilized version of this intrinsic is
22412241
/// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
22422242
#[rustc_intrinsic]
22432243
#[rustc_nounwind]
2244-
pub unsafe fn ceilf64(x: f64) -> f64;
2244+
pub const unsafe fn ceilf64(x: f64) -> f64;
22452245
/// Returns the smallest integer greater than or equal to an `f128`.
22462246
///
22472247
/// The stabilized version of this intrinsic is
22482248
/// [`f128::ceil`](../../std/primitive.f128.html#method.ceil)
22492249
#[rustc_intrinsic]
22502250
#[rustc_nounwind]
2251-
pub unsafe fn ceilf128(x: f128) -> f128;
2251+
pub const unsafe fn ceilf128(x: f128) -> f128;
22522252

22532253
/// Returns the integer part of an `f16`.
22542254
///
22552255
/// The stabilized version of this intrinsic is
22562256
/// [`f16::trunc`](../../std/primitive.f16.html#method.trunc)
22572257
#[rustc_intrinsic]
22582258
#[rustc_nounwind]
2259-
pub unsafe fn truncf16(x: f16) -> f16;
2259+
pub const unsafe fn truncf16(x: f16) -> f16;
22602260
/// Returns the integer part of an `f32`.
22612261
///
22622262
/// The stabilized version of this intrinsic is
22632263
/// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
22642264
#[rustc_intrinsic]
22652265
#[rustc_nounwind]
2266-
pub unsafe fn truncf32(x: f32) -> f32;
2266+
pub const unsafe fn truncf32(x: f32) -> f32;
22672267
/// Returns the integer part of an `f64`.
22682268
///
22692269
/// The stabilized version of this intrinsic is
22702270
/// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
22712271
#[rustc_intrinsic]
22722272
#[rustc_nounwind]
2273-
pub unsafe fn truncf64(x: f64) -> f64;
2273+
pub const unsafe fn truncf64(x: f64) -> f64;
22742274
/// Returns the integer part of an `f128`.
22752275
///
22762276
/// The stabilized version of this intrinsic is
22772277
/// [`f128::trunc`](../../std/primitive.f128.html#method.trunc)
22782278
#[rustc_intrinsic]
22792279
#[rustc_nounwind]
2280-
pub unsafe fn truncf128(x: f128) -> f128;
2280+
pub const unsafe fn truncf128(x: f128) -> f128;
22812281

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

22912291
/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number with an even
22922292
/// least significant digit.
@@ -2295,10 +2295,11 @@ pub fn round_ties_even_f16(x: f16) -> f16;
22952295
/// [`f32::round_ties_even`](../../std/primitive.f32.html#method.round_ties_even)
22962296
#[rustc_intrinsic]
22972297
#[rustc_nounwind]
2298-
pub fn round_ties_even_f32(x: f32) -> f32;
2298+
pub const fn round_ties_even_f32(x: f32) -> f32;
22992299

23002300
/// Provided for compatibility with stdarch. DO NOT USE.
23012301
#[inline(always)]
2302+
#[rustc_allow_const_fn_unstable(core_intrinsics)]
23022303
pub unsafe fn rintf32(x: f32) -> f32 {
23032304
round_ties_even_f32(x)
23042305
}
@@ -2310,11 +2311,12 @@ pub unsafe fn rintf32(x: f32) -> f32 {
23102311
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
23112312
#[rustc_intrinsic]
23122313
#[rustc_nounwind]
2313-
pub fn round_ties_even_f64(x: f64) -> f64;
2314+
pub const fn round_ties_even_f64(x: f64) -> f64;
23142315

23152316
/// Provided for compatibility with stdarch. DO NOT USE.
23162317
#[inline(always)]
2317-
pub unsafe fn rintf64(x: f64) -> f64 {
2318+
#[rustc_allow_const_fn_unstable(core_intrinsics)]
2319+
pub const unsafe fn rintf64(x: f64) -> f64 {
23182320
round_ties_even_f64(x)
23192321
}
23202322

@@ -2325,36 +2327,36 @@ pub unsafe fn rintf64(x: f64) -> f64 {
23252327
/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
23262328
#[rustc_intrinsic]
23272329
#[rustc_nounwind]
2328-
pub fn round_ties_even_f128(x: f128) -> f128;
2330+
pub const fn round_ties_even_f128(x: f128) -> f128;
23292331

23302332
/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
23312333
///
23322334
/// The stabilized version of this intrinsic is
23332335
/// [`f16::round`](../../std/primitive.f16.html#method.round)
23342336
#[rustc_intrinsic]
23352337
#[rustc_nounwind]
2336-
pub unsafe fn roundf16(x: f16) -> f16;
2338+
pub const unsafe fn roundf16(x: f16) -> f16;
23372339
/// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
23382340
///
23392341
/// The stabilized version of this intrinsic is
23402342
/// [`f32::round`](../../std/primitive.f32.html#method.round)
23412343
#[rustc_intrinsic]
23422344
#[rustc_nounwind]
2343-
pub unsafe fn roundf32(x: f32) -> f32;
2345+
pub const unsafe fn roundf32(x: f32) -> f32;
23442346
/// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
23452347
///
23462348
/// The stabilized version of this intrinsic is
23472349
/// [`f64::round`](../../std/primitive.f64.html#method.round)
23482350
#[rustc_intrinsic]
23492351
#[rustc_nounwind]
2350-
pub unsafe fn roundf64(x: f64) -> f64;
2352+
pub const unsafe fn roundf64(x: f64) -> f64;
23512353
/// Returns the nearest integer to an `f128`. Rounds half-way cases away from zero.
23522354
///
23532355
/// The stabilized version of this intrinsic is
23542356
/// [`f128::round`](../../std/primitive.f128.html#method.round)
23552357
#[rustc_intrinsic]
23562358
#[rustc_nounwind]
2357-
pub unsafe fn roundf128(x: f128) -> f128;
2359+
pub const unsafe fn roundf128(x: f128) -> f128;
23582360

23592361
/// Float addition that allows optimizations based on algebraic rules.
23602362
/// May assume inputs are finite.

library/core/src/num/f128.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,8 @@ impl f128 {
14481448
#[rustc_allow_incoherent_impl]
14491449
#[unstable(feature = "f128", issue = "116909")]
14501450
#[must_use = "method returns a new number and does not mutate the original value"]
1451-
pub fn floor(self) -> f128 {
1451+
#[rustc_allow_const_fn_unstable(core_intrinsics)]
1452+
pub const fn floor(self) -> f128 {
14521453
// SAFETY: intrinsic with no preconditions
14531454
unsafe { intrinsics::floorf128(self) }
14541455
}
@@ -1478,7 +1479,8 @@ impl f128 {
14781479
#[rustc_allow_incoherent_impl]
14791480
#[unstable(feature = "f128", issue = "116909")]
14801481
#[must_use = "method returns a new number and does not mutate the original value"]
1481-
pub fn ceil(self) -> f128 {
1482+
#[rustc_allow_const_fn_unstable(core_intrinsics)]
1483+
pub const fn ceil(self) -> f128 {
14821484
// SAFETY: intrinsic with no preconditions
14831485
unsafe { intrinsics::ceilf128(self) }
14841486
}
@@ -1514,7 +1516,8 @@ impl f128 {
15141516
#[rustc_allow_incoherent_impl]
15151517
#[unstable(feature = "f128", issue = "116909")]
15161518
#[must_use = "method returns a new number and does not mutate the original value"]
1517-
pub fn round(self) -> f128 {
1519+
#[rustc_allow_const_fn_unstable(core_intrinsics)]
1520+
pub const fn round(self) -> f128 {
15181521
// SAFETY: intrinsic with no preconditions
15191522
unsafe { intrinsics::roundf128(self) }
15201523
}
@@ -1548,7 +1551,8 @@ impl f128 {
15481551
#[rustc_allow_incoherent_impl]
15491552
#[unstable(feature = "f128", issue = "116909")]
15501553
#[must_use = "method returns a new number and does not mutate the original value"]
1551-
pub fn round_ties_even(self) -> f128 {
1554+
#[rustc_allow_const_fn_unstable(core_intrinsics)]
1555+
pub const fn round_ties_even(self) -> f128 {
15521556
intrinsics::round_ties_even_f128(self)
15531557
}
15541558

@@ -1580,7 +1584,8 @@ impl f128 {
15801584
#[rustc_allow_incoherent_impl]
15811585
#[unstable(feature = "f128", issue = "116909")]
15821586
#[must_use = "method returns a new number and does not mutate the original value"]
1583-
pub fn trunc(self) -> f128 {
1587+
#[rustc_allow_const_fn_unstable(core_intrinsics)]
1588+
pub const fn trunc(self) -> f128 {
15841589
// SAFETY: intrinsic with no preconditions
15851590
unsafe { intrinsics::truncf128(self) }
15861591
}
@@ -1611,7 +1616,7 @@ impl f128 {
16111616
#[rustc_allow_incoherent_impl]
16121617
#[unstable(feature = "f128", issue = "116909")]
16131618
#[must_use = "method returns a new number and does not mutate the original value"]
1614-
pub fn fract(self) -> f128 {
1619+
pub const fn fract(self) -> f128 {
16151620
self - self.trunc()
16161621
}
16171622

0 commit comments

Comments
 (0)