Skip to content

Commit

Permalink
Rollup merge of rust-lang#46926 - varkor:contrib-9, r=joshtriplett
Browse files Browse the repository at this point in the history
Add LLVM `minnum`/`maxnum` intrinsics

LLVM’s intrinsics `minnum` and `maxnum` are now used for `min` and
`max` on `f32` and `f64`. This resolves rust-lang#18384.
  • Loading branch information
kennytm committed Dec 23, 2017
2 parents f196e9b + a060814 commit 5d9fe6f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 36 deletions.
54 changes: 54 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,44 @@ 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 {
// 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 x < y || y != y { x } else { y }) * 1.0
}
/// Returns the minimum of two `f64` values.
#[cfg(stage0)]
pub unsafe fn minnumf64(x: f64, y: f64) -> f64 {
// Identical to the `f32` case.
(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 {
// 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 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 {
// Identical to the `f32` case.
(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

0 comments on commit 5d9fe6f

Please sign in to comment.