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

Implement min, minf, max, maxf #180

Merged
merged 6 commits into from
Jun 5, 2019
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- minf
- fmin
- fmaxf
- fmax

## [v0.1.2] - 2018-07-18

### Added
Expand Down
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ pub trait F32Ext: private::Sealed + Sized {
fn acosh(self) -> Self;

fn atanh(self) -> Self;

fn min(self, other: Self) -> Self;

fn max(self, other: Self) -> Self;
}

impl F32Ext for f32 {
Expand Down Expand Up @@ -327,6 +331,16 @@ impl F32Ext for f32 {
fn atanh(self) -> Self {
atanhf(self)
}

#[inline]
fn min(self, other: Self) -> Self {
fminf(self, other)
}

#[inline]
fn max(self, other: Self) -> Self {
fmaxf(self, other)
}
}

/// Math support for `f64`
Expand Down Expand Up @@ -410,6 +424,10 @@ pub trait F64Ext: private::Sealed + Sized {
fn acosh(self) -> Self;

fn atanh(self) -> Self;

fn min(self, other: Self) -> Self;

fn max(self, other: Self) -> Self;
}

impl F64Ext for f64 {
Expand Down Expand Up @@ -601,6 +619,16 @@ impl F64Ext for f64 {
fn atanh(self) -> Self {
atanh(self)
}

#[inline]
fn min(self, other: Self) -> Self {
fmin(self, other)
}

#[inline]
fn max(self, other: Self) -> Self {
fmax(self, other)
}
}

mod private {
Expand Down
13 changes: 13 additions & 0 deletions src/math/fmax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[inline]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmax(x: f64, y: 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 x.is_nan() || x < y { y } else { x }) * 1.0
}
13 changes: 13 additions & 0 deletions src/math/fmaxf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[inline]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmaxf(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.is_nan() || x < y { y } else { x }) * 1.0
}
13 changes: 13 additions & 0 deletions src/math/fmin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[inline]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fmin(x: f64, y: 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 y.is_nan() || x < y { x } else { y }) * 1.0
}
13 changes: 13 additions & 0 deletions src/math/fminf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[inline]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn fminf(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 y.is_nan() || x < y { x } else { y }) * 1.0
}
8 changes: 8 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ mod floor;
mod floorf;
mod fma;
mod fmaf;
mod fmax;
mod fmaxf;
mod fmin;
mod fminf;
mod fmod;
mod fmodf;
mod frexp;
Expand Down Expand Up @@ -212,6 +216,10 @@ pub use self::floor::floor;
pub use self::floorf::floorf;
pub use self::fma::fma;
pub use self::fmaf::fmaf;
pub use self::fmax::fmax;
pub use self::fmaxf::fmaxf;
pub use self::fmin::fmin;
pub use self::fminf::fminf;
pub use self::fmod::fmod;
pub use self::fmodf::fmodf;
pub use self::frexp::frexp;
Expand Down