Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 01bee72

Browse files
authored
Merge pull request #180 from varkor/min-max
Implement min, minf, max, maxf
2 parents 9bbab43 + 03b46a9 commit 01bee72

File tree

7 files changed

+95
-0
lines changed

7 files changed

+95
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased]
77

8+
### Added
9+
10+
- minf
11+
- fmin
12+
- fmaxf
13+
- fmax
14+
815
## [v0.1.2] - 2018-07-18
916

1017
### Added

src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ pub trait F32Ext: private::Sealed + Sized {
136136
fn acosh(self) -> Self;
137137

138138
fn atanh(self) -> Self;
139+
140+
fn min(self, other: Self) -> Self;
141+
142+
fn max(self, other: Self) -> Self;
139143
}
140144

141145
impl F32Ext for f32 {
@@ -327,6 +331,16 @@ impl F32Ext for f32 {
327331
fn atanh(self) -> Self {
328332
atanhf(self)
329333
}
334+
335+
#[inline]
336+
fn min(self, other: Self) -> Self {
337+
fminf(self, other)
338+
}
339+
340+
#[inline]
341+
fn max(self, other: Self) -> Self {
342+
fmaxf(self, other)
343+
}
330344
}
331345

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

412426
fn atanh(self) -> Self;
427+
428+
fn min(self, other: Self) -> Self;
429+
430+
fn max(self, other: Self) -> Self;
413431
}
414432

415433
impl F64Ext for f64 {
@@ -601,6 +619,16 @@ impl F64Ext for f64 {
601619
fn atanh(self) -> Self {
602620
atanh(self)
603621
}
622+
623+
#[inline]
624+
fn min(self, other: Self) -> Self {
625+
fmin(self, other)
626+
}
627+
628+
#[inline]
629+
fn max(self, other: Self) -> Self {
630+
fmax(self, other)
631+
}
604632
}
605633

606634
mod private {

src/math/fmax.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[inline]
2+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
3+
pub fn fmax(x: f64, y: f64) -> f64 {
4+
// IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
5+
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
6+
// is either x or y, canonicalized (this means results might differ among implementations).
7+
// When either x or y is a signalingNaN, then the result is according to 6.2.
8+
//
9+
// Since we do not support sNaN in Rust yet, we do not need to handle them.
10+
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
11+
// multiplying by 1.0. Should switch to the `canonicalize` when it works.
12+
(if x.is_nan() || x < y { y } else { x }) * 1.0
13+
}

src/math/fmaxf.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[inline]
2+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
3+
pub fn fmaxf(x: f32, y: f32) -> f32 {
4+
// IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
5+
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
6+
// is either x or y, canonicalized (this means results might differ among implementations).
7+
// When either x or y is a signalingNaN, then the result is according to 6.2.
8+
//
9+
// Since we do not support sNaN in Rust yet, we do not need to handle them.
10+
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
11+
// multiplying by 1.0. Should switch to the `canonicalize` when it works.
12+
(if x.is_nan() || x < y { y } else { x }) * 1.0
13+
}

src/math/fmin.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[inline]
2+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
3+
pub fn fmin(x: f64, y: f64) -> f64 {
4+
// IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the
5+
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
6+
// is either x or y, canonicalized (this means results might differ among implementations).
7+
// When either x or y is a signalingNaN, then the result is according to 6.2.
8+
//
9+
// Since we do not support sNaN in Rust yet, we do not need to handle them.
10+
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
11+
// multiplying by 1.0. Should switch to the `canonicalize` when it works.
12+
(if y.is_nan() || x < y { x } else { y }) * 1.0
13+
}

src/math/fminf.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[inline]
2+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
3+
pub fn fminf(x: f32, y: f32) -> f32 {
4+
// IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the
5+
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
6+
// is either x or y, canonicalized (this means results might differ among implementations).
7+
// When either x or y is a signalingNaN, then the result is according to 6.2.
8+
//
9+
// Since we do not support sNaN in Rust yet, we do not need to handle them.
10+
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
11+
// multiplying by 1.0. Should switch to the `canonicalize` when it works.
12+
(if y.is_nan() || x < y { x } else { y }) * 1.0
13+
}

src/math/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ mod floor;
112112
mod floorf;
113113
mod fma;
114114
mod fmaf;
115+
mod fmax;
116+
mod fmaxf;
117+
mod fmin;
118+
mod fminf;
115119
mod fmod;
116120
mod fmodf;
117121
mod frexp;
@@ -212,6 +216,10 @@ pub use self::floor::floor;
212216
pub use self::floorf::floorf;
213217
pub use self::fma::fma;
214218
pub use self::fmaf::fmaf;
219+
pub use self::fmax::fmax;
220+
pub use self::fmaxf::fmaxf;
221+
pub use self::fmin::fmin;
222+
pub use self::fminf::fminf;
215223
pub use self::fmod::fmod;
216224
pub use self::fmodf::fmodf;
217225
pub use self::frexp::frexp;

0 commit comments

Comments
 (0)