Skip to content

Commit e912046

Browse files
committed
feat: make float rounding methods const
1 parent 9fd6d8d commit e912046

File tree

12 files changed

+244
-124
lines changed

12 files changed

+244
-124
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
@@ -2197,86 +2197,86 @@ pub unsafe fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
21972197
/// [`f16::floor`](../../std/primitive.f16.html#method.floor)
21982198
#[rustc_intrinsic]
21992199
#[rustc_nounwind]
2200-
pub unsafe fn floorf16(x: f16) -> f16;
2200+
pub const unsafe fn floorf16(x: f16) -> f16;
22012201
/// Returns the largest integer less than or equal to an `f32`.
22022202
///
22032203
/// The stabilized version of this intrinsic is
22042204
/// [`f32::floor`](../../std/primitive.f32.html#method.floor)
22052205
#[rustc_intrinsic]
22062206
#[rustc_nounwind]
2207-
pub unsafe fn floorf32(x: f32) -> f32;
2207+
pub const unsafe fn floorf32(x: f32) -> f32;
22082208
/// Returns the largest integer less than or equal to an `f64`.
22092209
///
22102210
/// The stabilized version of this intrinsic is
22112211
/// [`f64::floor`](../../std/primitive.f64.html#method.floor)
22122212
#[rustc_intrinsic]
22132213
#[rustc_nounwind]
2214-
pub unsafe fn floorf64(x: f64) -> f64;
2214+
pub const unsafe fn floorf64(x: f64) -> f64;
22152215
/// Returns the largest integer less than or equal to an `f128`.
22162216
///
22172217
/// The stabilized version of this intrinsic is
22182218
/// [`f128::floor`](../../std/primitive.f128.html#method.floor)
22192219
#[rustc_intrinsic]
22202220
#[rustc_nounwind]
2221-
pub unsafe fn floorf128(x: f128) -> f128;
2221+
pub const unsafe fn floorf128(x: f128) -> f128;
22222222

22232223
/// Returns the smallest integer greater than or equal to an `f16`.
22242224
///
22252225
/// The stabilized version of this intrinsic is
22262226
/// [`f16::ceil`](../../std/primitive.f16.html#method.ceil)
22272227
#[rustc_intrinsic]
22282228
#[rustc_nounwind]
2229-
pub unsafe fn ceilf16(x: f16) -> f16;
2229+
pub const unsafe fn ceilf16(x: f16) -> f16;
22302230
/// Returns the smallest integer greater than or equal to an `f32`.
22312231
///
22322232
/// The stabilized version of this intrinsic is
22332233
/// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
22342234
#[rustc_intrinsic]
22352235
#[rustc_nounwind]
2236-
pub unsafe fn ceilf32(x: f32) -> f32;
2236+
pub const unsafe fn ceilf32(x: f32) -> f32;
22372237
/// Returns the smallest integer greater than or equal to an `f64`.
22382238
///
22392239
/// The stabilized version of this intrinsic is
22402240
/// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
22412241
#[rustc_intrinsic]
22422242
#[rustc_nounwind]
2243-
pub unsafe fn ceilf64(x: f64) -> f64;
2243+
pub const unsafe fn ceilf64(x: f64) -> f64;
22442244
/// Returns the smallest integer greater than or equal to an `f128`.
22452245
///
22462246
/// The stabilized version of this intrinsic is
22472247
/// [`f128::ceil`](../../std/primitive.f128.html#method.ceil)
22482248
#[rustc_intrinsic]
22492249
#[rustc_nounwind]
2250-
pub unsafe fn ceilf128(x: f128) -> f128;
2250+
pub const unsafe fn ceilf128(x: f128) -> f128;
22512251

22522252
/// Returns the integer part of an `f16`.
22532253
///
22542254
/// The stabilized version of this intrinsic is
22552255
/// [`f16::trunc`](../../std/primitive.f16.html#method.trunc)
22562256
#[rustc_intrinsic]
22572257
#[rustc_nounwind]
2258-
pub unsafe fn truncf16(x: f16) -> f16;
2258+
pub const unsafe fn truncf16(x: f16) -> f16;
22592259
/// Returns the integer part of an `f32`.
22602260
///
22612261
/// The stabilized version of this intrinsic is
22622262
/// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
22632263
#[rustc_intrinsic]
22642264
#[rustc_nounwind]
2265-
pub unsafe fn truncf32(x: f32) -> f32;
2265+
pub const unsafe fn truncf32(x: f32) -> f32;
22662266
/// Returns the integer part of an `f64`.
22672267
///
22682268
/// The stabilized version of this intrinsic is
22692269
/// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
22702270
#[rustc_intrinsic]
22712271
#[rustc_nounwind]
2272-
pub unsafe fn truncf64(x: f64) -> f64;
2272+
pub const unsafe fn truncf64(x: f64) -> f64;
22732273
/// Returns the integer part of an `f128`.
22742274
///
22752275
/// The stabilized version of this intrinsic is
22762276
/// [`f128::trunc`](../../std/primitive.f128.html#method.trunc)
22772277
#[rustc_intrinsic]
22782278
#[rustc_nounwind]
2279-
pub unsafe fn truncf128(x: f128) -> f128;
2279+
pub const unsafe fn truncf128(x: f128) -> f128;
22802280

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

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

22992299
/// Provided for compatibility with stdarch. DO NOT USE.
23002300
#[inline(always)]
2301+
#[rustc_allow_const_fn_unstable(core_intrinsics)]
23012302
pub unsafe fn rintf32(x: f32) -> f32 {
23022303
round_ties_even_f32(x)
23032304
}
@@ -2309,11 +2310,12 @@ pub unsafe fn rintf32(x: f32) -> f32 {
23092310
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
23102311
#[rustc_intrinsic]
23112312
#[rustc_nounwind]
2312-
pub fn round_ties_even_f64(x: f64) -> f64;
2313+
pub const fn round_ties_even_f64(x: f64) -> f64;
23132314

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

@@ -2324,36 +2326,36 @@ pub unsafe fn rintf64(x: f64) -> f64 {
23242326
/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
23252327
#[rustc_intrinsic]
23262328
#[rustc_nounwind]
2327-
pub fn round_ties_even_f128(x: f128) -> f128;
2329+
pub const fn round_ties_even_f128(x: f128) -> f128;
23282330

23292331
/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
23302332
///
23312333
/// The stabilized version of this intrinsic is
23322334
/// [`f16::round`](../../std/primitive.f16.html#method.round)
23332335
#[rustc_intrinsic]
23342336
#[rustc_nounwind]
2335-
pub unsafe fn roundf16(x: f16) -> f16;
2337+
pub const unsafe fn roundf16(x: f16) -> f16;
23362338
/// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
23372339
///
23382340
/// The stabilized version of this intrinsic is
23392341
/// [`f32::round`](../../std/primitive.f32.html#method.round)
23402342
#[rustc_intrinsic]
23412343
#[rustc_nounwind]
2342-
pub unsafe fn roundf32(x: f32) -> f32;
2344+
pub const unsafe fn roundf32(x: f32) -> f32;
23432345
/// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
23442346
///
23452347
/// The stabilized version of this intrinsic is
23462348
/// [`f64::round`](../../std/primitive.f64.html#method.round)
23472349
#[rustc_intrinsic]
23482350
#[rustc_nounwind]
2349-
pub unsafe fn roundf64(x: f64) -> f64;
2351+
pub const unsafe fn roundf64(x: f64) -> f64;
23502352
/// Returns the nearest integer to an `f128`. Rounds half-way cases away from zero.
23512353
///
23522354
/// The stabilized version of this intrinsic is
23532355
/// [`f128::round`](../../std/primitive.f128.html#method.round)
23542356
#[rustc_intrinsic]
23552357
#[rustc_nounwind]
2356-
pub unsafe fn roundf128(x: f128) -> f128;
2358+
pub const unsafe fn roundf128(x: f128) -> f128;
23572359

23582360
/// Float addition that allows optimizations based on algebraic rules.
23592361
/// 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)