Skip to content

Add LLVM minnum/maxnum intrinsics #46926

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,19 @@ extern "rust-intrinsic" {
/// Returns the absolute value of an `f64`.
pub fn fabsf64(x: f64) -> f64;

/// Returns the minimum of two `f32` values.
#[cfg(not(stage0))]
pub fn minnumf32(x: f32, y: f32) -> f32;
/// Returns the minimum of two `f64` values.
#[cfg(not(stage0))]
pub fn minnumf64(x: f64, y: f64) -> f64;
/// Returns the maximum of two `f32` values.
#[cfg(not(stage0))]
pub fn maxnumf32(x: f32, y: f32) -> f32;
/// Returns the maximum of two `f64` values.
#[cfg(not(stage0))]
pub fn maxnumf64(x: f64, y: f64) -> f64;

/// Copies the sign from `y` to `x` for `f32` values.
pub fn copysignf32(x: f32, y: f32) -> f32;
/// Copies the sign from `y` to `x` for `f64` values.
Expand Down Expand Up @@ -1393,3 +1406,25 @@ extern "rust-intrinsic" {
#[cfg(not(stage0))]
pub fn nontemporal_store<T>(ptr: *mut T, val: T);
}

// Simple bootstrap implementations for stage0 compilation
/// Returns the minimum of two `f32` values.
#[cfg(stage0)]
pub unsafe fn minnumf32(x: f32, y: f32) -> f32 {
(if x < y || y != y { x } else { y }) * 1.0
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't you copy the deleted comments from libcore at least once here?

Copy link
Member

Choose a reason for hiding this comment

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

Good call. Even though these are only used for bootstrapping, they still ought to have that correctness warning.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, I was wondering whether they were necessary if just used for bootstrapping. I'll add them back!

}
/// Returns the minimum of two `f64` values.
#[cfg(stage0)]
pub unsafe fn minnumf64(x: f64, y: f64) -> f64 {
(if x < y || y != y { x } else { y }) * 1.0
}
/// Returns the maximum of two `f32` values.
#[cfg(stage0)]
pub unsafe fn maxnumf32(x: f32, y: f32) -> f32 {
(if x < y || x != x { y } else { x }) * 1.0
}
/// Returns the maximum of two `f64` values.
#[cfg(stage0)]
pub unsafe fn maxnumf64(x: f64, y: f64) -> f64 {
(if x < y || x != x { y } else { x }) * 1.0
}
20 changes: 2 additions & 18 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,28 +250,12 @@ impl Float for f32 {
/// Returns the maximum of the two numbers.
#[inline]
fn max(self, other: f32) -> f32 {
// IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
// is either x or y, canonicalized (this means results might differ among implementations).
// When either x or y is a signalingNaN, then the result is according to 6.2.
//
// Since we do not support sNaN in Rust yet, we do not need to handle them.
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
// multiplying by 1.0. Should switch to the `canonicalize` when it works.
(if self < other || self.is_nan() { other } else { self }) * 1.0
unsafe { intrinsics::maxnumf32(self, other) }
}

/// Returns the minimum of the two numbers.
#[inline]
fn min(self, other: f32) -> f32 {
// IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
// is either x or y, canonicalized (this means results might differ among implementations).
// When either x or y is a signalingNaN, then the result is according to 6.2.
//
// Since we do not support sNaN in Rust yet, we do not need to handle them.
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
// multiplying by 1.0. Should switch to the `canonicalize` when it works.
(if self < other || other.is_nan() { self } else { other }) * 1.0
unsafe { intrinsics::minnumf32(self, other) }
}
}
20 changes: 2 additions & 18 deletions src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,28 +248,12 @@ impl Float for f64 {
/// Returns the maximum of the two numbers.
#[inline]
fn max(self, other: f64) -> f64 {
// IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
// is either x or y, canonicalized (this means results might differ among implementations).
// When either x or y is a signalingNaN, then the result is according to 6.2.
//
// Since we do not support sNaN in Rust yet, we do not need to handle them.
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
// multiplying by 1.0. Should switch to the `canonicalize` when it works.
(if self < other || self.is_nan() { other } else { self }) * 1.0
unsafe { intrinsics::maxnumf64(self, other) }
}

/// Returns the minimum of the two numbers.
#[inline]
fn min(self, other: f64) -> f64 {
// IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
// is either x or y, canonicalized (this means results might differ among implementations).
// When either x or y is a signalingNaN, then the result is according to 6.2.
//
// Since we do not support sNaN in Rust yet, we do not need to handle them.
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
// multiplying by 1.0. Should switch to the `canonicalize` when it works.
(if self < other || other.is_nan() { self } else { other }) * 1.0
unsafe { intrinsics::minnumf64(self, other) }
}
}
5 changes: 5 additions & 0 deletions src/librustc_trans/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,11 @@ fn declare_intrinsic(ccx: &CrateContext, key: &str) -> Option<ValueRef> {
ifn!("llvm.fabs.f32", fn(t_f32) -> t_f32);
ifn!("llvm.fabs.f64", fn(t_f64) -> t_f64);

ifn!("llvm.minnum.f32", fn(t_f32, t_f32) -> t_f32);
ifn!("llvm.minnum.f64", fn(t_f64, t_f64) -> t_f64);
ifn!("llvm.maxnum.f32", fn(t_f32, t_f32) -> t_f32);
ifn!("llvm.maxnum.f64", fn(t_f64, t_f64) -> t_f64);

ifn!("llvm.floor.f32", fn(t_f32) -> t_f32);
ifn!("llvm.floor.f64", fn(t_f64) -> t_f64);
ifn!("llvm.ceil.f32", fn(t_f32) -> t_f32);
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_trans/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ fn get_simple_intrinsic(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
"fmaf64" => "llvm.fma.f64",
"fabsf32" => "llvm.fabs.f32",
"fabsf64" => "llvm.fabs.f64",
"minnumf32" => "llvm.minnum.f32",
"minnumf64" => "llvm.minnum.f64",
"maxnumf32" => "llvm.maxnum.f32",
"maxnumf64" => "llvm.maxnum.f64",
"copysignf32" => "llvm.copysign.f32",
"copysignf64" => "llvm.copysign.f64",
"floorf32" => "llvm.floor.f32",
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_typeck/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}
"fabsf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
"fabsf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
"minnumf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
"minnumf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
"maxnumf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
"maxnumf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
"copysignf32" => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
"copysignf64" => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
"floorf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
Expand Down