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

Commit 72956e3

Browse files
committed
Add a generic version of fmin and fmax
These can be used for `fmin`, `fminf`, `fmax`, and `fmaxf`. No changes to the implementation are made, so [1] is not fixed. [1]: https://github.com/rust-lang/libm/issues/439
1 parent 71606c6 commit 72956e3

File tree

8 files changed

+47
-40
lines changed

8 files changed

+47
-40
lines changed

libm/etc/function-definitions.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,26 +379,30 @@
379379
"fmax": {
380380
"sources": [
381381
"src/libm_helper.rs",
382-
"src/math/fmax.rs"
382+
"src/math/fmax.rs",
383+
"src/math/generic/fmax.rs"
383384
],
384385
"type": "f64"
385386
},
386387
"fmaxf": {
387388
"sources": [
388-
"src/math/fmaxf.rs"
389+
"src/math/fmaxf.rs",
390+
"src/math/generic/fmax.rs"
389391
],
390392
"type": "f32"
391393
},
392394
"fmin": {
393395
"sources": [
394396
"src/libm_helper.rs",
395-
"src/math/fmin.rs"
397+
"src/math/fmin.rs",
398+
"src/math/generic/fmin.rs"
396399
],
397400
"type": "f64"
398401
},
399402
"fminf": {
400403
"sources": [
401-
"src/math/fminf.rs"
404+
"src/math/fminf.rs",
405+
"src/math/generic/fmin.rs"
402406
],
403407
"type": "f32"
404408
},

libm/src/math/fmax.rs

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

libm/src/math/fmaxf.rs

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

libm/src/math/fmin.rs

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

libm/src/math/fminf.rs

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

libm/src/math/generic/fmax.rs

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

libm/src/math/generic/fmin.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use super::super::Float;
2+
3+
pub fn fmin<F: Float>(x: F, y: F) -> F {
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 }) * F::ONE
13+
}

libm/src/math/generic/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ mod copysign;
33
mod fabs;
44
mod fdim;
55
mod floor;
6+
mod fmax;
7+
mod fmin;
68
mod rint;
79
mod round;
810
mod scalbn;
@@ -14,6 +16,8 @@ pub use copysign::copysign;
1416
pub use fabs::fabs;
1517
pub use fdim::fdim;
1618
pub use floor::floor;
19+
pub use fmax::fmax;
20+
pub use fmin::fmin;
1721
pub use rint::rint;
1822
pub use round::round;
1923
pub use scalbn::scalbn;

0 commit comments

Comments
 (0)