Skip to content

Commit b07e665

Browse files
committed
add floor, scalbn; fixes in expf, scalbnf
1 parent 5d17a76 commit b07e665

File tree

7 files changed

+94
-32
lines changed

7 files changed

+94
-32
lines changed

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ impl F32Ext for f32 {
365365
/// NOTE this meant to be a closed extension trait. The only stable way to use this trait is to
366366
/// import it to access its methods.
367367
pub trait F64Ext {
368-
#[cfg(todo)]
369368
fn floor(self) -> Self;
370369

371370
#[cfg(todo)]
@@ -479,7 +478,6 @@ pub trait F64Ext {
479478
}
480479

481480
impl F64Ext for f64 {
482-
#[cfg(todo)]
483481
#[inline]
484482
fn floor(self) -> Self {
485483
floor(self)

src/math/expf.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ const P2 : f32 = -2.7667332906e-3; /* -0xb55215.0p-32 */
1313

1414
#[inline]
1515
pub fn expf(mut x: f32) -> f32 {
16-
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
17-
let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126 /*original 0x1p-149f ??????????? */
16+
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
17+
let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126 /*original 0x1p-149f ??????????? */
1818

19-
let mut hx = x.to_bits() as i32;
19+
let mut hx = x.to_bits();
2020
let sign = (hx >> 31) as i32; /* sign bit of x */
2121
let signb : bool = sign != 0;
2222
hx &= 0x7fffffff; /* high word of |x| */
2323

2424
/* special cases */
25-
if hx >= 0x42aeac50 { /* if |x| >= -87.33655f or NaN */
25+
if hx >= 0x42aeac50 { /* if |x| >= -87.33655f or NaN */
2626
if hx > 0x7f800000 {/* NaN */
2727
return x;
2828
}

src/math/floor.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use core::f64;
2+
3+
const TOINT : f64 = 1. / f64::EPSILON;
4+
5+
#[inline]
6+
pub fn floor(x : f64) -> f64 {
7+
let ui = x.to_bits();
8+
let e = ((ui >> 52) & 0x7ff) as i32;
9+
10+
if (e >= 0x3ff+52) || (x == 0.) {
11+
return x;
12+
}
13+
/* y = int(x) - x, where int(x) is an integer neighbor of x */
14+
let y = if (ui >> 63) != 0 {
15+
x - TOINT + TOINT - x
16+
} else {
17+
x + TOINT - TOINT - x
18+
};
19+
/* special case because of non-nearest rounding modes */
20+
if e <= 0x3ff-1 {
21+
force_eval!(y);
22+
return if (ui >> 63) != 0 { -1. } else { 0. };
23+
}
24+
if y > 0. {
25+
x + y - 1.
26+
} else {
27+
x + y
28+
}
29+
}

src/math/logf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const LG4 : f32 = 0.24279078841; /* 0xf89e26.0p-26 */
88

99
#[inline]
1010
pub fn logf(mut x: f32) -> f32 {
11-
let x1p25 = f32::from_bits(0x4c000000); // 0x1p24f === 2 ^ 25
11+
let x1p25 = f32::from_bits(0x4c000000); // 0x1p25f === 2 ^ 25
1212

1313
let mut ix = x.to_bits();
1414
let mut k = 0i32;

src/math/mod.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,28 @@ mod fabsf;
99
mod fmodf;
1010
mod powf;
1111
mod round;
12+
mod scalbn;
1213
mod scalbnf;
1314
mod sqrtf;
1415
mod logf;
1516
mod expf;
17+
mod floor;
1618

17-
pub use self::fabs::fabs;
18-
pub use self::fabsf::fabsf;
19-
pub use self::fmodf::fmodf;
20-
pub use self::powf::powf;
21-
pub use self::round::round;
22-
pub use self::scalbnf::scalbnf;
23-
pub use self::sqrtf::sqrtf;
24-
pub use self::logf::logf;
25-
pub use self::expf::expf;
19+
//mod service;
20+
21+
pub use self::{
22+
fabs::fabs,
23+
fabsf::fabsf,
24+
fmodf::fmodf,
25+
powf::powf,
26+
round::round,
27+
scalbn::scalbn,
28+
scalbnf::scalbnf,
29+
sqrtf::sqrtf,
30+
logf::logf,
31+
expf::expf,
32+
floor::floor,
33+
};
2634

2735
fn isnanf(x: f32) -> bool {
2836
x.to_bits() & 0x7fffffff > 0x7f800000

src/math/scalbn.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#[inline]
2+
pub fn scalbn(x : f64, mut n: i32) -> f64 {
3+
let x1p1023 = f64::from_bits(0x7fe0000000000000); // 0x1p1023 === 2 ^ 1023
4+
let x1p53 = f64::from_bits(0x4340000000000000); // 0x1p53 === 2 ^ 53
5+
let x1p_1022 = f64::from_bits(0x0010000000000000); // 0x1p-1022 === 2 ^ (-1022)
6+
7+
let mut y = x;
8+
9+
if n > 1023 {
10+
y *= x1p1023;
11+
n -= 1023;
12+
if n > 1023 {
13+
y *= x1p1023;
14+
n -= 1023;
15+
if n > 1023 {
16+
n = 1023;
17+
}
18+
}
19+
} else if n < -1022 {
20+
/* make sure final n < -53 to avoid double
21+
rounding in the subnormal range */
22+
y *= x1p_1022 * x1p53;
23+
n += 1022 - 53;
24+
if n < -1022 {
25+
y *= x1p_1022 * x1p53;
26+
n += 1022 - 53;
27+
if n < -1022 {
28+
n = -1022;
29+
}
30+
}
31+
}
32+
y*f64::from_bits(((0x3ff+n) as u64)<<52)
33+
}

src/math/scalbnf.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
#[inline]
2-
pub fn scalbnf(mut x: f32, mut n: i32) -> f32 {
3-
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
4-
let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126
5-
let x1p24 = f32::from_bits(0x4b800000); // 0x1p24f === 2 ^ 24
6-
7-
let mut y: f32 = x;
8-
2+
pub fn scalbnf(mut x: f32, mut n : i32) -> f32 {
3+
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
4+
let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126
5+
let x1p24 = f32::from_bits(0x4b800000); // 0x1p24f === 2 ^ 24
6+
97
if n > 127 {
10-
y *= x1p127;
8+
x *= x1p127;
119
n -= 127;
1210
if n > 127 {
13-
y *= x1p127;
11+
x *= x1p127;
1412
n -= 127;
1513
if n > 127 {
1614
n = 127;
1715
}
1816
}
1917
} else if n < -126 {
20-
y *= x1p_126;
21-
y *= x1p24;
18+
x *= x1p_126 * x1p24;
2219
n += 126 - 24;
2320
if n < -126 {
24-
y *= x1p_126;
25-
y *= x1p24;
21+
x *= x1p_126 * x1p24;
2622
n += 126 - 24;
2723
if n < -126 {
2824
n = -126;
2925
}
3026
}
3127
}
32-
33-
x = y * f32::from_bits((0x7f + n as u32) << 23);
34-
x
28+
x * f32::from_bits(((0x7f+n) as u32)<<23)
3529
}

0 commit comments

Comments
 (0)