Skip to content

Commit 33e45d6

Browse files
authored
Rollup merge of rust-lang#136543 - RalfJung:round-ties-even, r=tgross35
intrinsics: unify rint, roundeven, nearbyint in a single round_ties_even intrinsic LLVM has three intrinsics here that all do the same thing (when used in the default FP environment). There's no reason Rust needs to copy that historically-grown mess -- let's just have one intrinsic and leave it up to the LLVM backend to decide how to lower that. Suggested by `@hanna-kruppe` in rust-lang#136459; Cc `@tgross35` try-job: test-various
2 parents 3abc694 + 4eb3105 commit 33e45d6

File tree

5 files changed

+89
-116
lines changed

5 files changed

+89
-116
lines changed

core/src/intrinsics/mod.rs

+85-112
Original file line numberDiff line numberDiff line change
@@ -2731,110 +2731,124 @@ pub unsafe fn truncf128(_x: f128) -> f128 {
27312731
unreachable!()
27322732
}
27332733

2734-
/// Returns the nearest integer to an `f16`. Changing the rounding mode is not possible in Rust,
2735-
/// so this rounds half-way cases to the number with an even least significant digit.
2736-
///
2737-
/// May raise an inexact floating-point exception if the argument is not an integer.
2738-
/// However, Rust assumes floating-point exceptions cannot be observed, so these exceptions
2739-
/// cannot actually be utilized from Rust code.
2740-
/// In other words, this intrinsic is equivalent in behavior to `nearbyintf16` and `roundevenf16`.
2734+
/// Returns the nearest integer to an `f16`. Rounds half-way cases to the number with an even
2735+
/// least significant digit.
27412736
///
27422737
/// The stabilized version of this intrinsic is
27432738
/// [`f16::round_ties_even`](../../std/primitive.f16.html#method.round_ties_even)
27442739
#[rustc_intrinsic]
27452740
#[rustc_intrinsic_must_be_overridden]
27462741
#[rustc_nounwind]
2747-
pub unsafe fn rintf16(_x: f16) -> f16 {
2742+
#[cfg(not(bootstrap))]
2743+
pub fn round_ties_even_f16(_x: f16) -> f16 {
27482744
unreachable!()
27492745
}
2750-
/// Returns the nearest integer to an `f32`. Changing the rounding mode is not possible in Rust,
2751-
/// so this rounds half-way cases to the number with an even least significant digit.
2752-
///
2753-
/// May raise an inexact floating-point exception if the argument is not an integer.
2754-
/// However, Rust assumes floating-point exceptions cannot be observed, so these exceptions
2755-
/// cannot actually be utilized from Rust code.
2756-
/// In other words, this intrinsic is equivalent in behavior to `nearbyintf32` and `roundevenf32`.
2746+
2747+
/// To be removed on next bootstrap bump.
2748+
#[cfg(bootstrap)]
2749+
pub fn round_ties_even_f16(x: f16) -> f16 {
2750+
#[rustc_intrinsic]
2751+
#[rustc_intrinsic_must_be_overridden]
2752+
#[rustc_nounwind]
2753+
unsafe fn rintf16(_x: f16) -> f16 {
2754+
unreachable!()
2755+
}
2756+
2757+
// SAFETY: this intrinsic isn't actually unsafe
2758+
unsafe { rintf16(x) }
2759+
}
2760+
2761+
/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number with an even
2762+
/// least significant digit.
27572763
///
27582764
/// The stabilized version of this intrinsic is
27592765
/// [`f32::round_ties_even`](../../std/primitive.f32.html#method.round_ties_even)
27602766
#[rustc_intrinsic]
27612767
#[rustc_intrinsic_must_be_overridden]
27622768
#[rustc_nounwind]
2763-
pub unsafe fn rintf32(_x: f32) -> f32 {
2769+
#[cfg(not(bootstrap))]
2770+
pub fn round_ties_even_f32(_x: f32) -> f32 {
27642771
unreachable!()
27652772
}
2766-
/// Returns the nearest integer to an `f64`. Changing the rounding mode is not possible in Rust,
2767-
/// so this rounds half-way cases to the number with an even least significant digit.
2768-
///
2769-
/// May raise an inexact floating-point exception if the argument is not an integer.
2770-
/// However, Rust assumes floating-point exceptions cannot be observed, so these exceptions
2771-
/// cannot actually be utilized from Rust code.
2772-
/// In other words, this intrinsic is equivalent in behavior to `nearbyintf64` and `roundevenf64`.
2773-
///
2774-
/// The stabilized version of this intrinsic is
2775-
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
2776-
#[rustc_intrinsic]
2777-
#[rustc_intrinsic_must_be_overridden]
2778-
#[rustc_nounwind]
2779-
pub unsafe fn rintf64(_x: f64) -> f64 {
2780-
unreachable!()
2773+
2774+
/// To be removed on next bootstrap bump.
2775+
#[cfg(bootstrap)]
2776+
pub fn round_ties_even_f32(x: f32) -> f32 {
2777+
#[rustc_intrinsic]
2778+
#[rustc_intrinsic_must_be_overridden]
2779+
#[rustc_nounwind]
2780+
unsafe fn rintf32(_x: f32) -> f32 {
2781+
unreachable!()
2782+
}
2783+
2784+
// SAFETY: this intrinsic isn't actually unsafe
2785+
unsafe { rintf32(x) }
27812786
}
2782-
/// Returns the nearest integer to an `f128`. Changing the rounding mode is not possible in Rust,
2783-
/// so this rounds half-way cases to the number with an even least significant digit.
2784-
///
2785-
/// May raise an inexact floating-point exception if the argument is not an integer.
2786-
/// However, Rust assumes floating-point exceptions cannot be observed, so these exceptions
2787-
/// cannot actually be utilized from Rust code.
2788-
/// In other words, this intrinsic is equivalent in behavior to `nearbyintf128` and `roundevenf128`.
2787+
2788+
/// Provided for compatibility with stdarch. DO NOT USE.
2789+
#[inline(always)]
2790+
pub unsafe fn rintf32(x: f32) -> f32 {
2791+
round_ties_even_f32(x)
2792+
}
2793+
2794+
/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number with an even
2795+
/// least significant digit.
27892796
///
27902797
/// The stabilized version of this intrinsic is
2791-
/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
2798+
/// [`f64::round_ties_even`](../../std/primitive.f64.html#method.round_ties_even)
27922799
#[rustc_intrinsic]
27932800
#[rustc_intrinsic_must_be_overridden]
27942801
#[rustc_nounwind]
2795-
pub unsafe fn rintf128(_x: f128) -> f128 {
2802+
#[cfg(not(bootstrap))]
2803+
pub fn round_ties_even_f64(_x: f64) -> f64 {
27962804
unreachable!()
27972805
}
27982806

2799-
/// Returns the nearest integer to an `f16`. Changing the rounding mode is not possible in Rust,
2800-
/// so this rounds half-way cases to the number with an even least significant digit.
2801-
///
2802-
/// This intrinsic does not have a stable counterpart.
2803-
#[rustc_intrinsic]
2804-
#[rustc_intrinsic_must_be_overridden]
2805-
#[rustc_nounwind]
2806-
pub unsafe fn nearbyintf16(_x: f16) -> f16 {
2807-
unreachable!()
2807+
/// To be removed on next bootstrap bump.
2808+
#[cfg(bootstrap)]
2809+
pub fn round_ties_even_f64(x: f64) -> f64 {
2810+
#[rustc_intrinsic]
2811+
#[rustc_intrinsic_must_be_overridden]
2812+
#[rustc_nounwind]
2813+
unsafe fn rintf64(_x: f64) -> f64 {
2814+
unreachable!()
2815+
}
2816+
2817+
// SAFETY: this intrinsic isn't actually unsafe
2818+
unsafe { rintf64(x) }
28082819
}
2809-
/// Returns the nearest integer to an `f32`. Changing the rounding mode is not possible in Rust,
2810-
/// so this rounds half-way cases to the number with an even least significant digit.
2811-
///
2812-
/// This intrinsic does not have a stable counterpart.
2813-
#[rustc_intrinsic]
2814-
#[rustc_intrinsic_must_be_overridden]
2815-
#[rustc_nounwind]
2816-
pub unsafe fn nearbyintf32(_x: f32) -> f32 {
2817-
unreachable!()
2820+
2821+
/// Provided for compatibility with stdarch. DO NOT USE.
2822+
#[inline(always)]
2823+
pub unsafe fn rintf64(x: f64) -> f64 {
2824+
round_ties_even_f64(x)
28182825
}
2819-
/// Returns the nearest integer to an `f64`. Changing the rounding mode is not possible in Rust,
2820-
/// so this rounds half-way cases to the number with an even least significant digit.
2826+
2827+
/// Returns the nearest integer to an `f128`. Rounds half-way cases to the number with an even
2828+
/// least significant digit.
28212829
///
2822-
/// This intrinsic does not have a stable counterpart.
2830+
/// The stabilized version of this intrinsic is
2831+
/// [`f128::round_ties_even`](../../std/primitive.f128.html#method.round_ties_even)
28232832
#[rustc_intrinsic]
28242833
#[rustc_intrinsic_must_be_overridden]
28252834
#[rustc_nounwind]
2826-
pub unsafe fn nearbyintf64(_x: f64) -> f64 {
2835+
#[cfg(not(bootstrap))]
2836+
pub fn round_ties_even_f128(_x: f128) -> f128 {
28272837
unreachable!()
28282838
}
2829-
/// Returns the nearest integer to an `f128`. Changing the rounding mode is not possible in Rust,
2830-
/// so this rounds half-way cases to the number with an even least significant digit.
2831-
///
2832-
/// This intrinsic does not have a stable counterpart.
2833-
#[rustc_intrinsic]
2834-
#[rustc_intrinsic_must_be_overridden]
2835-
#[rustc_nounwind]
2836-
pub unsafe fn nearbyintf128(_x: f128) -> f128 {
2837-
unreachable!()
2839+
2840+
/// To be removed on next bootstrap bump.
2841+
#[cfg(bootstrap)]
2842+
pub fn round_ties_even_f128(x: f128) -> f128 {
2843+
#[rustc_intrinsic]
2844+
#[rustc_intrinsic_must_be_overridden]
2845+
#[rustc_nounwind]
2846+
unsafe fn rintf128(_x: f128) -> f128 {
2847+
unreachable!()
2848+
}
2849+
2850+
// SAFETY: this intrinsic isn't actually unsafe
2851+
unsafe { rintf128(x) }
28382852
}
28392853

28402854
/// Returns the nearest integer to an `f16`. Rounds half-way cases away from zero.
@@ -2878,47 +2892,6 @@ pub unsafe fn roundf128(_x: f128) -> f128 {
28782892
unreachable!()
28792893
}
28802894

2881-
/// Returns the nearest integer to an `f16`. Rounds half-way cases to the number
2882-
/// with an even least significant digit.
2883-
///
2884-
/// This intrinsic does not have a stable counterpart.
2885-
#[rustc_intrinsic]
2886-
#[rustc_intrinsic_must_be_overridden]
2887-
#[rustc_nounwind]
2888-
pub unsafe fn roundevenf16(_x: f16) -> f16 {
2889-
unreachable!()
2890-
}
2891-
/// Returns the nearest integer to an `f32`. Rounds half-way cases to the number
2892-
/// with an even least significant digit.
2893-
///
2894-
/// This intrinsic does not have a stable counterpart.
2895-
#[rustc_intrinsic]
2896-
#[rustc_intrinsic_must_be_overridden]
2897-
#[rustc_nounwind]
2898-
pub unsafe fn roundevenf32(_x: f32) -> f32 {
2899-
unreachable!()
2900-
}
2901-
/// Returns the nearest integer to an `f64`. Rounds half-way cases to the number
2902-
/// with an even least significant digit.
2903-
///
2904-
/// This intrinsic does not have a stable counterpart.
2905-
#[rustc_intrinsic]
2906-
#[rustc_intrinsic_must_be_overridden]
2907-
#[rustc_nounwind]
2908-
pub unsafe fn roundevenf64(_x: f64) -> f64 {
2909-
unreachable!()
2910-
}
2911-
/// Returns the nearest integer to an `f128`. Rounds half-way cases to the number
2912-
/// with an even least significant digit.
2913-
///
2914-
/// This intrinsic does not have a stable counterpart.
2915-
#[rustc_intrinsic]
2916-
#[rustc_intrinsic_must_be_overridden]
2917-
#[rustc_nounwind]
2918-
pub unsafe fn roundevenf128(_x: f128) -> f128 {
2919-
unreachable!()
2920-
}
2921-
29222895
/// Float addition that allows optimizations based on algebraic rules.
29232896
/// May assume inputs are finite.
29242897
///

std/src/f128.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl f128 {
126126
#[unstable(feature = "f128", issue = "116909")]
127127
#[must_use = "method returns a new number and does not mutate the original value"]
128128
pub fn round_ties_even(self) -> f128 {
129-
unsafe { intrinsics::rintf128(self) }
129+
intrinsics::round_ties_even_f128(self)
130130
}
131131

132132
/// Returns the integer part of `self`.

std/src/f16.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl f16 {
126126
#[unstable(feature = "f16", issue = "116909")]
127127
#[must_use = "method returns a new number and does not mutate the original value"]
128128
pub fn round_ties_even(self) -> f16 {
129-
unsafe { intrinsics::rintf16(self) }
129+
intrinsics::round_ties_even_f16(self)
130130
}
131131

132132
/// Returns the integer part of `self`.

std/src/f32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl f32 {
122122
#[stable(feature = "round_ties_even", since = "1.77.0")]
123123
#[inline]
124124
pub fn round_ties_even(self) -> f32 {
125-
unsafe { intrinsics::rintf32(self) }
125+
intrinsics::round_ties_even_f32(self)
126126
}
127127

128128
/// Returns the integer part of `self`.

std/src/f64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl f64 {
122122
#[stable(feature = "round_ties_even", since = "1.77.0")]
123123
#[inline]
124124
pub fn round_ties_even(self) -> f64 {
125-
unsafe { intrinsics::rintf64(self) }
125+
intrinsics::round_ties_even_f64(self)
126126
}
127127

128128
/// Returns the integer part of `self`.

0 commit comments

Comments
 (0)