File tree 7 files changed +94
-32
lines changed
7 files changed +94
-32
lines changed Original file line number Diff line number Diff line change @@ -365,7 +365,6 @@ impl F32Ext for f32 {
365
365
/// NOTE this meant to be a closed extension trait. The only stable way to use this trait is to
366
366
/// import it to access its methods.
367
367
pub trait F64Ext {
368
- #[ cfg( todo) ]
369
368
fn floor ( self ) -> Self ;
370
369
371
370
#[ cfg( todo) ]
@@ -479,7 +478,6 @@ pub trait F64Ext {
479
478
}
480
479
481
480
impl F64Ext for f64 {
482
- #[ cfg( todo) ]
483
481
#[ inline]
484
482
fn floor ( self ) -> Self {
485
483
floor ( self )
Original file line number Diff line number Diff line change @@ -13,16 +13,16 @@ const P2 : f32 = -2.7667332906e-3; /* -0xb55215.0p-32 */
13
13
14
14
#[ inline]
15
15
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 ??????????? */
18
18
19
- let mut hx = x. to_bits ( ) as i32 ;
19
+ let mut hx = x. to_bits ( ) ;
20
20
let sign = ( hx >> 31 ) as i32 ; /* sign bit of x */
21
21
let signb : bool = sign != 0 ;
22
22
hx &= 0x7fffffff ; /* high word of |x| */
23
23
24
24
/* special cases */
25
- if hx >= 0x42aeac50 { /* if |x| >= -87.33655f or NaN */
25
+ if hx >= 0x42aeac50 { /* if |x| >= -87.33655f or NaN */
26
26
if hx > 0x7f800000 { /* NaN */
27
27
return x;
28
28
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ const LG4 : f32 = 0.24279078841; /* 0xf89e26.0p-26 */
8
8
9
9
#[ inline]
10
10
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
12
12
13
13
let mut ix = x. to_bits ( ) ;
14
14
let mut k = 0i32 ;
Original file line number Diff line number Diff line change @@ -9,20 +9,28 @@ mod fabsf;
9
9
mod fmodf;
10
10
mod powf;
11
11
mod round;
12
+ mod scalbn;
12
13
mod scalbnf;
13
14
mod sqrtf;
14
15
mod logf;
15
16
mod expf;
17
+ mod floor;
16
18
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
+ } ;
26
34
27
35
fn isnanf ( x : f32 ) -> bool {
28
36
x. to_bits ( ) & 0x7fffffff > 0x7f800000
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
#[ 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
+
9
7
if n > 127 {
10
- y *= x1p127;
8
+ x *= x1p127;
11
9
n -= 127 ;
12
10
if n > 127 {
13
- y *= x1p127;
11
+ x *= x1p127;
14
12
n -= 127 ;
15
13
if n > 127 {
16
14
n = 127 ;
17
15
}
18
16
}
19
17
} else if n < -126 {
20
- y *= x1p_126;
21
- y *= x1p24;
18
+ x *= x1p_126 * x1p24;
22
19
n += 126 - 24 ;
23
20
if n < -126 {
24
- y *= x1p_126;
25
- y *= x1p24;
21
+ x *= x1p_126 * x1p24;
26
22
n += 126 - 24 ;
27
23
if n < -126 {
28
24
n = -126 ;
29
25
}
30
26
}
31
27
}
32
-
33
- x = y * f32:: from_bits ( ( 0x7f + n as u32 ) << 23 ) ;
34
- x
28
+ x * f32:: from_bits ( ( ( 0x7f +n) as u32 ) <<23 )
35
29
}
You can’t perform that action at this time.
0 commit comments