Skip to content

Commit a9e0c4f

Browse files
bors[bot]jackmott
andcommitted
93: fdimf r=japaric a=jackmott closes rust-lang#47 Co-authored-by: Jack Mott <[email protected]>
2 parents 930d791 + ec6c86c commit a9e0c4f

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
lines changed

src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub trait F32Ext: private::Sealed {
4242

4343
fn trunc(self) -> Self;
4444

45+
fn fdim(self, rhs: Self) -> Self;
46+
4547
#[cfg(todo)]
4648
fn fract(self) -> Self;
4749

@@ -155,6 +157,11 @@ impl F32Ext for f32 {
155157
truncf(self)
156158
}
157159

160+
#[inline]
161+
fn fdim(self, rhs: Self) -> Self {
162+
fdimf(self, rhs)
163+
}
164+
158165
#[cfg(todo)]
159166
#[inline]
160167
fn fract(self) -> Self {
@@ -353,6 +360,8 @@ pub trait F64Ext: private::Sealed {
353360

354361
fn trunc(self) -> Self;
355362

363+
fn fdim(self, rhs: Self) -> Self;
364+
356365
#[cfg(todo)]
357366
fn fract(self) -> Self;
358367

@@ -468,6 +477,10 @@ impl F64Ext for f64 {
468477
trunc(self)
469478
}
470479

480+
#[inline]
481+
fn fdim(self, rhs: Self) -> Self {
482+
fdim(self, rhs)
483+
}
471484
#[cfg(todo)]
472485
#[inline]
473486
fn fract(self) -> Self {

src/math/fdim.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use core::f64;
2+
3+
pub fn fdim(x: f64, y: f64) -> f64 {
4+
if x.is_nan() {
5+
x
6+
} else if y.is_nan() {
7+
y
8+
} else {
9+
if x > y {
10+
x - y
11+
} else {
12+
0.0
13+
}
14+
}
15+
}

src/math/fdimf.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use core::f32;
2+
3+
pub fn fdimf(x: f32, y: f32) -> f32 {
4+
if x.is_nan() {
5+
x
6+
} else if y.is_nan() {
7+
y
8+
} else {
9+
if x > y {
10+
x - y
11+
} else {
12+
0.0
13+
}
14+
}
15+
}

src/math/fmodf.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
use core::f32;
12
use core::u32;
23

3-
use super::isnanf;
4-
54
#[inline]
65
pub fn fmodf(x: f32, y: f32) -> f32 {
76
let mut uxi = x.to_bits();
@@ -11,7 +10,7 @@ pub fn fmodf(x: f32, y: f32) -> f32 {
1110
let sx = uxi & 0x80000000;
1211
let mut i;
1312

14-
if uyi << 1 == 0 || isnanf(y) || ex == 0xff {
13+
if uyi << 1 == 0 || y.is_nan() || ex == 0xff {
1514
return (x * y) / (x * y);
1615
}
1716

src/math/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ mod exp;
1313
mod expf;
1414
mod fabs;
1515
mod fabsf;
16+
mod fdim;
17+
mod fdimf;
1618
mod floor;
1719
mod floorf;
1820
mod fmodf;
@@ -44,6 +46,8 @@ pub use self::exp::exp;
4446
pub use self::expf::expf;
4547
pub use self::fabs::fabs;
4648
pub use self::fabsf::fabsf;
49+
pub use self::fdim::fdim;
50+
pub use self::fdimf::fdimf;
4751
pub use self::floor::floor;
4852
pub use self::floorf::floorf;
4953
pub use self::fmodf::fmodf;
@@ -73,7 +77,3 @@ mod rem_pio2_large;
7377
mod rem_pio2f;
7478

7579
use self::{k_cosf::k_cosf, k_sinf::k_sinf, rem_pio2_large::rem_pio2_large, rem_pio2f::rem_pio2f};
76-
77-
fn isnanf(x: f32) -> bool {
78-
x.to_bits() & 0x7fffffff > 0x7f800000
79-
}

test-generator/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,6 @@ f32_f32! {
662662
// coshf,
663663
// exp2f,
664664
expf,
665-
// fdimf,
666665
log10f,
667666
log1pf,
668667
log2f,
@@ -679,6 +678,7 @@ f32_f32! {
679678
// With signature `fn(f32, f32) -> f32`
680679
f32f32_f32! {
681680
// atan2f,
681+
fdimf,
682682
hypotf,
683683
fmodf,
684684
powf,
@@ -724,7 +724,7 @@ f64_f64! {
724724
// With signature `fn(f64, f64) -> f64`
725725
f64f64_f64! {
726726
// atan2,
727-
// fdim,
727+
fdim,
728728
// fmod,
729729
hypot,
730730
// pow,

0 commit comments

Comments
 (0)