Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add round_to_even to floating point types #82273

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
truncf64(flt) -> f64 => trunc,
roundf32(flt) -> f32 => roundf,
roundf64(flt) -> f64 => round,
roundevenf32(flt) -> f32 => roundevenf,
roundevenf64(flt) -> f64 => roundeven,
Comment on lines +500 to +501
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, this will cause calls to the roundeven and roundevenf libc functions. But those are non-standard, and not available on all libc implementations.

cc @bjorn3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cranelift doesn't have an instruction for this. If this ever becomes a problem, I can request one.


// trigonometry
sinf32(flt) -> f32 => sinf,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ impl CodegenCx<'b, 'tcx> {
ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
ifn!("llvm.roundeven.f32", fn(t_f32) -> t_f32);
ifn!("llvm.roundeven.f64", fn(t_f64) -> t_f64);
Comment on lines +576 to +577
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nagisa Can you confirm it's okay to add these llvm intrinsics?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It is documented here. How available it is across different backends is a different question, however – a quick look suggests these lower to native instructions on aarch64 and x86 only (and maybe amdgpu). Everything else will lower to libm calls.


ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ fn get_simple_intrinsic(cx: &CodegenCx<'ll, '_>, name: Symbol) -> Option<&'ll Va
sym::nearbyintf64 => "llvm.nearbyint.f64",
sym::roundf32 => "llvm.round.f32",
sym::roundf64 => "llvm.round.f64",
sym::roundevenf32 => "llvm.roundeven.f32",
sym::roundevenf64 => "llvm.roundeven.f64",
_ => return None,
};
Some(cx.get_intrinsic(&llvm_name))
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,8 @@ symbols! {
rlib,
rotate_left,
rotate_right,
roundevenf32,
roundevenf64,
roundf32,
roundf64,
rt,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_typeck/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
sym::nearbyintf64 => (0, vec![tcx.types.f64], tcx.types.f64),
sym::roundf32 => (0, vec![tcx.types.f32], tcx.types.f32),
sym::roundf64 => (0, vec![tcx.types.f64], tcx.types.f64),
sym::roundevenf32 => (0, vec![tcx.types.f32], tcx.types.f32),
sym::roundevenf64 => (0, vec![tcx.types.f64], tcx.types.f64),

sym::volatile_load | sym::unaligned_volatile_load => {
(1, vec![tcx.mk_imm_ptr(param(0))], param(0))
Expand Down
15 changes: 15 additions & 0 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,21 @@ extern "rust-intrinsic" {
/// [`f64::round`](../../std/primitive.f64.html#method.round)
pub fn roundf64(x: f64) -> f64;

/// Returns the nearest integer to an `f32`. Rounds half-way to the nearest even
/// integer.
///
/// The version of this intrinsic in the standard libary is
/// [`f32::round`](../../std/primitive.f32.html#method.round_to_even)
#[cfg(not(bootstrap))]
pub fn roundevenf32(x: f32) -> f32;
/// Returns the nearest integer to an `f64`. Rounds half-way to the nearest even
/// integer.
///
/// The version of this intrinsic in the standard libary is
/// [`f64::round`](../../std/primitive.f64.html#method.round_to_even)
#[cfg(not(bootstrap))]
pub fn roundevenf64(x: f64) -> f64;

/// Float addition that allows optimizations based on algebraic rules.
/// May assume inputs are finite.
///
Expand Down
21 changes: 21 additions & 0 deletions library/std/src/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ impl f32 {
unsafe { intrinsics::roundf32(self) }
}

/// Returns the nearest integer to a number. Round half-way cases to the
/// nearest even integer.
///
/// # Examples
///
/// ```
/// # #![cfg_attr(not(bootstrap), feature(round_to_even))]
/// let f = 3.3_f32;
/// let g = -3.3_f32;
///
/// assert_eq!(f.round_to_even(), 3.0);
/// assert_eq!(g.round_to_even(), -3.0);
/// ```
#[cfg(not(bootstrap))]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "round_to_even", issue = "none")]
#[inline]
pub fn round_to_even(self) -> f32 {
unsafe { intrinsics::roundevenf32(self) }
}

/// Returns the integer part of a number.
///
/// # Examples
Expand Down
19 changes: 19 additions & 0 deletions library/std/src/f32/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,31 @@ fn test_round() {
assert_approx_eq!(1.3f32.round(), 1.0f32);
assert_approx_eq!(1.5f32.round(), 2.0f32);
assert_approx_eq!(1.7f32.round(), 2.0f32);
assert_approx_eq!(2.5f32.round(), 3.0f32);
assert_approx_eq!(0.0f32.round(), 0.0f32);
assert_approx_eq!((-0.0f32).round(), -0.0f32);
assert_approx_eq!((-1.0f32).round(), -1.0f32);
assert_approx_eq!((-1.3f32).round(), -1.0f32);
assert_approx_eq!((-1.5f32).round(), -2.0f32);
assert_approx_eq!((-1.7f32).round(), -2.0f32);
assert_approx_eq!((-2.5f32).round(), -3.0f32);
}

#[test]
#[cfg(not(bootstrap))]
fn test_round_to_even() {
assert_approx_eq!(1.0f32.round_to_even(), 1.0f32);
assert_approx_eq!(1.3f32.round_to_even(), 1.0f32);
assert_approx_eq!(1.5f32.round_to_even(), 2.0f32);
assert_approx_eq!(1.7f32.round_to_even(), 2.0f32);
assert_approx_eq!(2.5f32.round_to_even(), 2.0f32);
assert_approx_eq!(0.0f32.round_to_even(), 0.0f32);
assert_approx_eq!((-0.0f32).round_to_even(), -0.0f32);
assert_approx_eq!((-1.0f32).round_to_even(), -1.0f32);
assert_approx_eq!((-1.3f32).round_to_even(), -1.0f32);
assert_approx_eq!((-1.5f32).round_to_even(), -2.0f32);
assert_approx_eq!((-1.7f32).round_to_even(), -2.0f32);
assert_approx_eq!((-2.5f32).round_to_even(), -2.0f32);
}

#[test]
Expand Down
21 changes: 21 additions & 0 deletions library/std/src/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ impl f64 {
unsafe { intrinsics::roundf64(self) }
}

/// Returns the nearest integer to a number. Round half-way cases to the
/// nearest even integer.
///
/// # Examples
///
/// ```
/// # #![cfg_attr(not(bootstrap), feature(round_to_even))]
/// let f = 3.3_f64;
/// let g = -3.3_f64;
///
/// assert_eq!(f.round_to_even(), 3.0);
/// assert_eq!(g.round_to_even(), -3.0);
/// ```
#[cfg(not(bootstrap))]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "round_to_even", issue = "none")]
#[inline]
pub fn round_to_even(self) -> f64 {
unsafe { intrinsics::roundevenf64(self) }
}

/// Returns the integer part of a number.
///
/// # Examples
Expand Down
19 changes: 19 additions & 0 deletions library/std/src/f64/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,31 @@ fn test_round() {
assert_approx_eq!(1.3f64.round(), 1.0f64);
assert_approx_eq!(1.5f64.round(), 2.0f64);
assert_approx_eq!(1.7f64.round(), 2.0f64);
assert_approx_eq!(2.5f64.round(), 3.0f64);
assert_approx_eq!(0.0f64.round(), 0.0f64);
assert_approx_eq!((-0.0f64).round(), -0.0f64);
assert_approx_eq!((-1.0f64).round(), -1.0f64);
assert_approx_eq!((-1.3f64).round(), -1.0f64);
assert_approx_eq!((-1.5f64).round(), -2.0f64);
assert_approx_eq!((-1.7f64).round(), -2.0f64);
assert_approx_eq!((-2.5f64).round(), -3.0f64);
}

#[test]
#[cfg(not(bootstrap))]
fn test_round_to_even() {
assert_approx_eq!(1.0f64.round_to_even(), 1.0f64);
assert_approx_eq!(1.3f64.round_to_even(), 1.0f64);
assert_approx_eq!(1.5f64.round_to_even(), 2.0f64);
assert_approx_eq!(1.7f64.round_to_even(), 2.0f64);
assert_approx_eq!(2.5f64.round_to_even(), 2.0f64);
assert_approx_eq!(0.0f64.round_to_even(), 0.0f64);
assert_approx_eq!((-0.0f64).round_to_even(), -0.0f64);
assert_approx_eq!((-1.0f64).round_to_even(), -1.0f64);
assert_approx_eq!((-1.3f64).round_to_even(), -1.0f64);
assert_approx_eq!((-1.5f64).round_to_even(), -2.0f64);
assert_approx_eq!((-1.7f64).round_to_even(), -2.0f64);
assert_approx_eq!((-2.5f64).round_to_even(), -2.0f64);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
#![feature(ptr_internals)]
#![feature(raw)]
#![feature(ready_macro)]
#![cfg_attr(not(bootstrap), feature(round_to_even))]
#![feature(rustc_attrs)]
#![feature(rustc_private)]
#![feature(shrink_to)]
Expand Down