Skip to content

Commit 541e9d7

Browse files
authored
Rollup merge of rust-lang#141521 - ruancomelli:const-float-rounding, r=RalfJung
Add `const` support for float rounding methods # Add `const` support for float rounding methods This PR makes the following float rounding methods `const`: - `f64::{floor, ceil, trunc, round, round_ties_even}` - and the corresponding methods for `f16`, `f32` and `f128` Tracking issue: rust-lang#141555 ## Procedure I followed rust-lang@c09ed3e as closely as I could in making float methods `const`, and also received great guidance from https://internals.rust-lang.org/t/const-rounding-methods-in-float-types/22957/3?u=ruancomelli. ## Note This is my first code contribution to the Rust project, so please let me know if I missed anything - I'd be more than happy to revise and learn more. Thank you for taking the time to review it!
2 parents d65c6a8 + 0da4f84 commit 541e9d7

File tree

10 files changed

+220
-56
lines changed

10 files changed

+220
-56
lines changed

core/src/intrinsics/mod.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,86 +2212,86 @@ pub unsafe fn fmuladdf128(a: f128, b: f128, c: f128) -> f128;
22122212
/// [`f16::floor`](../../std/primitive.f16.html#method.floor)
22132213
#[rustc_intrinsic]
22142214
#[rustc_nounwind]
2215-
pub unsafe fn floorf16(x: f16) -> f16;
2215+
pub const unsafe fn floorf16(x: f16) -> f16;
22162216
/// Returns the largest integer less than or equal to an `f32`.
22172217
///
22182218
/// The stabilized version of this intrinsic is
22192219
/// [`f32::floor`](../../std/primitive.f32.html#method.floor)
22202220
#[rustc_intrinsic]
22212221
#[rustc_nounwind]
2222-
pub unsafe fn floorf32(x: f32) -> f32;
2222+
pub const unsafe fn floorf32(x: f32) -> f32;
22232223
/// Returns the largest integer less than or equal to an `f64`.
22242224
///
22252225
/// The stabilized version of this intrinsic is
22262226
/// [`f64::floor`](../../std/primitive.f64.html#method.floor)
22272227
#[rustc_intrinsic]
22282228
#[rustc_nounwind]
2229-
pub unsafe fn floorf64(x: f64) -> f64;
2229+
pub const unsafe fn floorf64(x: f64) -> f64;
22302230
/// Returns the largest integer less than or equal to an `f128`.
22312231
///
22322232
/// The stabilized version of this intrinsic is
22332233
/// [`f128::floor`](../../std/primitive.f128.html#method.floor)
22342234
#[rustc_intrinsic]
22352235
#[rustc_nounwind]
2236-
pub unsafe fn floorf128(x: f128) -> f128;
2236+
pub const unsafe fn floorf128(x: f128) -> f128;
22372237

22382238
/// Returns the smallest integer greater than or equal to an `f16`.
22392239
///
22402240
/// The stabilized version of this intrinsic is
22412241
/// [`f16::ceil`](../../std/primitive.f16.html#method.ceil)
22422242
#[rustc_intrinsic]
22432243
#[rustc_nounwind]
2244-
pub unsafe fn ceilf16(x: f16) -> f16;
2244+
pub const unsafe fn ceilf16(x: f16) -> f16;
22452245
/// Returns the smallest integer greater than or equal to an `f32`.
22462246
///
22472247
/// The stabilized version of this intrinsic is
22482248
/// [`f32::ceil`](../../std/primitive.f32.html#method.ceil)
22492249
#[rustc_intrinsic]
22502250
#[rustc_nounwind]
2251-
pub unsafe fn ceilf32(x: f32) -> f32;
2251+
pub const unsafe fn ceilf32(x: f32) -> f32;
22522252
/// Returns the smallest integer greater than or equal to an `f64`.
22532253
///
22542254
/// The stabilized version of this intrinsic is
22552255
/// [`f64::ceil`](../../std/primitive.f64.html#method.ceil)
22562256
#[rustc_intrinsic]
22572257
#[rustc_nounwind]
2258-
pub unsafe fn ceilf64(x: f64) -> f64;
2258+
pub const unsafe fn ceilf64(x: f64) -> f64;
22592259
/// Returns the smallest integer greater than or equal to an `f128`.
22602260
///
22612261
/// The stabilized version of this intrinsic is
22622262
/// [`f128::ceil`](../../std/primitive.f128.html#method.ceil)
22632263
#[rustc_intrinsic]
22642264
#[rustc_nounwind]
2265-
pub unsafe fn ceilf128(x: f128) -> f128;
2265+
pub const unsafe fn ceilf128(x: f128) -> f128;
22662266

22672267
/// Returns the integer part of an `f16`.
22682268
///
22692269
/// The stabilized version of this intrinsic is
22702270
/// [`f16::trunc`](../../std/primitive.f16.html#method.trunc)
22712271
#[rustc_intrinsic]
22722272
#[rustc_nounwind]
2273-
pub unsafe fn truncf16(x: f16) -> f16;
2273+
pub const unsafe fn truncf16(x: f16) -> f16;
22742274
/// Returns the integer part of an `f32`.
22752275
///
22762276
/// The stabilized version of this intrinsic is
22772277
/// [`f32::trunc`](../../std/primitive.f32.html#method.trunc)
22782278
#[rustc_intrinsic]
22792279
#[rustc_nounwind]
2280-
pub unsafe fn truncf32(x: f32) -> f32;
2280+
pub const unsafe fn truncf32(x: f32) -> f32;
22812281
/// Returns the integer part of an `f64`.
22822282
///
22832283
/// The stabilized version of this intrinsic is
22842284
/// [`f64::trunc`](../../std/primitive.f64.html#method.trunc)
22852285
#[rustc_intrinsic]
22862286
#[rustc_nounwind]
2287-
pub unsafe fn truncf64(x: f64) -> f64;
2287+
pub const unsafe fn truncf64(x: f64) -> f64;
22882288
/// Returns the integer part of an `f128`.
22892289
///
22902290
/// The stabilized version of this intrinsic is
22912291
/// [`f128::trunc`](../../std/primitive.f128.html#method.trunc)
22922292
#[rustc_intrinsic]
22932293
#[rustc_nounwind]
2294-
pub unsafe fn truncf128(x: f128) -> f128;
2294+
pub const unsafe fn truncf128(x: f128) -> f128;
22952295

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

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

23142314
/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number with an even
23152315
/// least significant digit.
@@ -2318,7 +2318,7 @@ pub fn round_ties_even_f32(x: f32) -> f32;
23182318
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
23192319
#[rustc_intrinsic]
23202320
#[rustc_nounwind]
2321-
pub fn round_ties_even_f64(x: f64) -> f64;
2321+
pub const fn round_ties_even_f64(x: f64) -> f64;
23222322

23232323
/// Returns the nearest integer to an `f128`. Rounds half-way cases to the number with an even
23242324
/// least significant digit.
@@ -2327,36 +2327,36 @@ pub fn round_ties_even_f64(x: f64) -> f64;
23272327
/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
23282328
#[rustc_intrinsic]
23292329
#[rustc_nounwind]
2330-
pub fn round_ties_even_f128(x: f128) -> f128;
2330+
pub const fn round_ties_even_f128(x: f128) -> f128;
23312331

23322332
/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
23332333
///
23342334
/// The stabilized version of this intrinsic is
23352335
/// [`f16::round`](../../std/primitive.f16.html#method.round)
23362336
#[rustc_intrinsic]
23372337
#[rustc_nounwind]
2338-
pub unsafe fn roundf16(x: f16) -> f16;
2338+
pub const unsafe fn roundf16(x: f16) -> f16;
23392339
/// Returns the nearest integer to an `f32`. Rounds half-way cases away from zero.
23402340
///
23412341
/// The stabilized version of this intrinsic is
23422342
/// [`f32::round`](../../std/primitive.f32.html#method.round)
23432343
#[rustc_intrinsic]
23442344
#[rustc_nounwind]
2345-
pub unsafe fn roundf32(x: f32) -> f32;
2345+
pub const unsafe fn roundf32(x: f32) -> f32;
23462346
/// Returns the nearest integer to an `f64`. Rounds half-way cases away from zero.
23472347
///
23482348
/// The stabilized version of this intrinsic is
23492349
/// [`f64::round`](../../std/primitive.f64.html#method.round)
23502350
#[rustc_intrinsic]
23512351
#[rustc_nounwind]
2352-
pub unsafe fn roundf64(x: f64) -> f64;
2352+
pub const unsafe fn roundf64(x: f64) -> f64;
23532353
/// Returns the nearest integer to an `f128`. Rounds half-way cases away from zero.
23542354
///
23552355
/// The stabilized version of this intrinsic is
23562356
/// [`f128::round`](../../std/primitive.f128.html#method.round)
23572357
#[rustc_intrinsic]
23582358
#[rustc_nounwind]
2359-
pub unsafe fn roundf128(x: f128) -> f128;
2359+
pub const unsafe fn roundf128(x: f128) -> f128;
23602360

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

core/src/num/f128.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,8 +1447,10 @@ impl f128 {
14471447
#[inline]
14481448
#[rustc_allow_incoherent_impl]
14491449
#[unstable(feature = "f128", issue = "116909")]
1450+
#[rustc_const_unstable(feature = "f128", issue = "116909")]
1451+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
14501452
#[must_use = "method returns a new number and does not mutate the original value"]
1451-
pub fn floor(self) -> f128 {
1453+
pub const fn floor(self) -> f128 {
14521454
// SAFETY: intrinsic with no preconditions
14531455
unsafe { intrinsics::floorf128(self) }
14541456
}
@@ -1477,8 +1479,10 @@ impl f128 {
14771479
#[doc(alias = "ceiling")]
14781480
#[rustc_allow_incoherent_impl]
14791481
#[unstable(feature = "f128", issue = "116909")]
1482+
#[rustc_const_unstable(feature = "f128", issue = "116909")]
1483+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
14801484
#[must_use = "method returns a new number and does not mutate the original value"]
1481-
pub fn ceil(self) -> f128 {
1485+
pub const fn ceil(self) -> f128 {
14821486
// SAFETY: intrinsic with no preconditions
14831487
unsafe { intrinsics::ceilf128(self) }
14841488
}
@@ -1513,8 +1517,10 @@ impl f128 {
15131517
#[inline]
15141518
#[rustc_allow_incoherent_impl]
15151519
#[unstable(feature = "f128", issue = "116909")]
1520+
#[rustc_const_unstable(feature = "f128", issue = "116909")]
1521+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
15161522
#[must_use = "method returns a new number and does not mutate the original value"]
1517-
pub fn round(self) -> f128 {
1523+
pub const fn round(self) -> f128 {
15181524
// SAFETY: intrinsic with no preconditions
15191525
unsafe { intrinsics::roundf128(self) }
15201526
}
@@ -1547,8 +1553,10 @@ impl f128 {
15471553
#[inline]
15481554
#[rustc_allow_incoherent_impl]
15491555
#[unstable(feature = "f128", issue = "116909")]
1556+
#[rustc_const_unstable(feature = "f128", issue = "116909")]
1557+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
15501558
#[must_use = "method returns a new number and does not mutate the original value"]
1551-
pub fn round_ties_even(self) -> f128 {
1559+
pub const fn round_ties_even(self) -> f128 {
15521560
intrinsics::round_ties_even_f128(self)
15531561
}
15541562

@@ -1579,8 +1587,10 @@ impl f128 {
15791587
#[doc(alias = "truncate")]
15801588
#[rustc_allow_incoherent_impl]
15811589
#[unstable(feature = "f128", issue = "116909")]
1590+
#[rustc_const_unstable(feature = "f128", issue = "116909")]
1591+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
15821592
#[must_use = "method returns a new number and does not mutate the original value"]
1583-
pub fn trunc(self) -> f128 {
1593+
pub const fn trunc(self) -> f128 {
15841594
// SAFETY: intrinsic with no preconditions
15851595
unsafe { intrinsics::truncf128(self) }
15861596
}
@@ -1610,8 +1620,10 @@ impl f128 {
16101620
#[inline]
16111621
#[rustc_allow_incoherent_impl]
16121622
#[unstable(feature = "f128", issue = "116909")]
1623+
#[rustc_const_unstable(feature = "f128", issue = "116909")]
1624+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
16131625
#[must_use = "method returns a new number and does not mutate the original value"]
1614-
pub fn fract(self) -> f128 {
1626+
pub const fn fract(self) -> f128 {
16151627
self - self.trunc()
16161628
}
16171629

core/src/num/f16.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,8 +1423,10 @@ impl f16 {
14231423
#[inline]
14241424
#[rustc_allow_incoherent_impl]
14251425
#[unstable(feature = "f16", issue = "116909")]
1426+
#[rustc_const_unstable(feature = "f16", issue = "116909")]
1427+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
14261428
#[must_use = "method returns a new number and does not mutate the original value"]
1427-
pub fn floor(self) -> f16 {
1429+
pub const fn floor(self) -> f16 {
14281430
// SAFETY: intrinsic with no preconditions
14291431
unsafe { intrinsics::floorf16(self) }
14301432
}
@@ -1453,8 +1455,10 @@ impl f16 {
14531455
#[doc(alias = "ceiling")]
14541456
#[rustc_allow_incoherent_impl]
14551457
#[unstable(feature = "f16", issue = "116909")]
1458+
#[rustc_const_unstable(feature = "f16", issue = "116909")]
1459+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
14561460
#[must_use = "method returns a new number and does not mutate the original value"]
1457-
pub fn ceil(self) -> f16 {
1461+
pub const fn ceil(self) -> f16 {
14581462
// SAFETY: intrinsic with no preconditions
14591463
unsafe { intrinsics::ceilf16(self) }
14601464
}
@@ -1489,8 +1493,10 @@ impl f16 {
14891493
#[inline]
14901494
#[rustc_allow_incoherent_impl]
14911495
#[unstable(feature = "f16", issue = "116909")]
1496+
#[rustc_const_unstable(feature = "f16", issue = "116909")]
1497+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
14921498
#[must_use = "method returns a new number and does not mutate the original value"]
1493-
pub fn round(self) -> f16 {
1499+
pub const fn round(self) -> f16 {
14941500
// SAFETY: intrinsic with no preconditions
14951501
unsafe { intrinsics::roundf16(self) }
14961502
}
@@ -1523,8 +1529,10 @@ impl f16 {
15231529
#[inline]
15241530
#[rustc_allow_incoherent_impl]
15251531
#[unstable(feature = "f16", issue = "116909")]
1532+
#[rustc_const_unstable(feature = "f16", issue = "116909")]
1533+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
15261534
#[must_use = "method returns a new number and does not mutate the original value"]
1527-
pub fn round_ties_even(self) -> f16 {
1535+
pub const fn round_ties_even(self) -> f16 {
15281536
intrinsics::round_ties_even_f16(self)
15291537
}
15301538

@@ -1555,8 +1563,10 @@ impl f16 {
15551563
#[doc(alias = "truncate")]
15561564
#[rustc_allow_incoherent_impl]
15571565
#[unstable(feature = "f16", issue = "116909")]
1566+
#[rustc_const_unstable(feature = "f16", issue = "116909")]
1567+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
15581568
#[must_use = "method returns a new number and does not mutate the original value"]
1559-
pub fn trunc(self) -> f16 {
1569+
pub const fn trunc(self) -> f16 {
15601570
// SAFETY: intrinsic with no preconditions
15611571
unsafe { intrinsics::truncf16(self) }
15621572
}
@@ -1586,8 +1596,10 @@ impl f16 {
15861596
#[inline]
15871597
#[rustc_allow_incoherent_impl]
15881598
#[unstable(feature = "f16", issue = "116909")]
1599+
#[rustc_const_unstable(feature = "f16", issue = "116909")]
1600+
// #[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
15891601
#[must_use = "method returns a new number and does not mutate the original value"]
1590-
pub fn fract(self) -> f16 {
1602+
pub const fn fract(self) -> f16 {
15911603
self - self.trunc()
15921604
}
15931605

core/src/num/f32.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,8 +1591,9 @@ pub mod math {
15911591
/// [`f32::floor`]: ../../../std/primitive.f32.html#method.floor
15921592
#[inline]
15931593
#[unstable(feature = "core_float_math", issue = "137578")]
1594+
#[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
15941595
#[must_use = "method returns a new number and does not mutate the original value"]
1595-
pub fn floor(x: f32) -> f32 {
1596+
pub const fn floor(x: f32) -> f32 {
15961597
// SAFETY: intrinsic with no preconditions
15971598
unsafe { intrinsics::floorf32(x) }
15981599
}
@@ -1621,7 +1622,8 @@ pub mod math {
16211622
#[doc(alias = "ceiling")]
16221623
#[must_use = "method returns a new number and does not mutate the original value"]
16231624
#[unstable(feature = "core_float_math", issue = "137578")]
1624-
pub fn ceil(x: f32) -> f32 {
1625+
#[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
1626+
pub const fn ceil(x: f32) -> f32 {
16251627
// SAFETY: intrinsic with no preconditions
16261628
unsafe { intrinsics::ceilf32(x) }
16271629
}
@@ -1655,7 +1657,8 @@ pub mod math {
16551657
#[inline]
16561658
#[unstable(feature = "core_float_math", issue = "137578")]
16571659
#[must_use = "method returns a new number and does not mutate the original value"]
1658-
pub fn round(x: f32) -> f32 {
1660+
#[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
1661+
pub const fn round(x: f32) -> f32 {
16591662
// SAFETY: intrinsic with no preconditions
16601663
unsafe { intrinsics::roundf32(x) }
16611664
}
@@ -1688,7 +1691,8 @@ pub mod math {
16881691
#[inline]
16891692
#[unstable(feature = "core_float_math", issue = "137578")]
16901693
#[must_use = "method returns a new number and does not mutate the original value"]
1691-
pub fn round_ties_even(x: f32) -> f32 {
1694+
#[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
1695+
pub const fn round_ties_even(x: f32) -> f32 {
16921696
intrinsics::round_ties_even_f32(x)
16931697
}
16941698

@@ -1718,7 +1722,8 @@ pub mod math {
17181722
#[doc(alias = "truncate")]
17191723
#[must_use = "method returns a new number and does not mutate the original value"]
17201724
#[unstable(feature = "core_float_math", issue = "137578")]
1721-
pub fn trunc(x: f32) -> f32 {
1725+
#[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
1726+
pub const fn trunc(x: f32) -> f32 {
17221727
// SAFETY: intrinsic with no preconditions
17231728
unsafe { intrinsics::truncf32(x) }
17241729
}
@@ -1747,8 +1752,9 @@ pub mod math {
17471752
/// [`f32::fract`]: ../../../std/primitive.f32.html#method.fract
17481753
#[inline]
17491754
#[unstable(feature = "core_float_math", issue = "137578")]
1755+
#[rustc_const_unstable(feature = "const_float_round_methods", issue = "141555")]
17501756
#[must_use = "method returns a new number and does not mutate the original value"]
1751-
pub fn fract(x: f32) -> f32 {
1757+
pub const fn fract(x: f32) -> f32 {
17521758
x - trunc(x)
17531759
}
17541760

0 commit comments

Comments
 (0)