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

Commit f9e6cd7

Browse files
committed
Add a generic version of fdim
1 parent 85a2553 commit f9e6cd7

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

libm/etc/function-definitions.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,15 @@
289289
"fdim": {
290290
"sources": [
291291
"src/libm_helper.rs",
292-
"src/math/fdim.rs"
292+
"src/math/fdim.rs",
293+
"src/math/generic/fdim.rs"
293294
],
294295
"type": "f64"
295296
},
296297
"fdimf": {
297298
"sources": [
298-
"src/math/fdimf.rs"
299+
"src/math/fdimf.rs",
300+
"src/math/generic/fdim.rs"
299301
],
300302
"type": "f32"
301303
},

libm/src/math/fdim.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use core::f64;
2-
31
/// Positive difference (f64)
42
///
53
/// Determines the positive difference between arguments, returning:
@@ -10,13 +8,5 @@ use core::f64;
108
/// A range error may occur.
119
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
1210
pub fn fdim(x: f64, y: f64) -> f64 {
13-
if x.is_nan() {
14-
x
15-
} else if y.is_nan() {
16-
y
17-
} else if x > y {
18-
x - y
19-
} else {
20-
0.0
21-
}
11+
super::generic::fdim(x, y)
2212
}

libm/src/math/fdimf.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use core::f32;
2-
31
/// Positive difference (f32)
42
///
53
/// Determines the positive difference between arguments, returning:
@@ -10,13 +8,5 @@ use core::f32;
108
/// A range error may occur.
119
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
1210
pub fn fdimf(x: f32, y: f32) -> f32 {
13-
if x.is_nan() {
14-
x
15-
} else if y.is_nan() {
16-
y
17-
} else if x > y {
18-
x - y
19-
} else {
20-
0.0
21-
}
11+
super::generic::fdim(x, y)
2212
}

libm/src/math/generic/fdim.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 fdim<F: Float>(x: F, y: F) -> F {
4+
if x.is_nan() {
5+
x
6+
} else if y.is_nan() {
7+
y
8+
} else if x > y {
9+
x - y
10+
} else {
11+
F::ZERO
12+
}
13+
}

libm/src/math/generic/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
mod copysign;
22
mod fabs;
3+
mod fdim;
34
mod trunc;
45

56
pub use copysign::copysign;
67
pub use fabs::fabs;
8+
pub use fdim::fdim;
79
pub use trunc::trunc;

0 commit comments

Comments
 (0)