This repository was archived by the owner on Apr 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 8 files changed +79
-89
lines changed Expand file tree Collapse file tree 8 files changed +79
-89
lines changed Original file line number Diff line number Diff line change 466
466
"frexp" : {
467
467
"sources" : [
468
468
" src/libm_helper.rs" ,
469
- " src/math/frexp.rs"
469
+ " src/math/frexp.rs" ,
470
+ " src/math/generic/frexp.rs"
470
471
],
471
472
"type" : " f64"
472
473
},
473
474
"frexpf" : {
474
475
"sources" : [
475
- " src/math/frexpf.rs"
476
+ " src/math/frexpf.rs" ,
477
+ " src/math/generic/frexp.rs"
476
478
],
477
479
"type" : " f32"
478
480
},
492
494
"ilogb" : {
493
495
"sources" : [
494
496
" src/libm_helper.rs" ,
497
+ " src/math/generic/ilogb.rs" ,
495
498
" src/math/ilogb.rs"
496
499
],
497
500
"type" : " f64"
498
501
},
499
502
"ilogbf" : {
500
503
"sources" : [
504
+ " src/math/generic/ilogb.rs" ,
501
505
" src/math/ilogbf.rs"
502
506
],
503
507
"type" : " f32"
Original file line number Diff line number Diff line change
1
+ /// Decompose a float into a normalized value within the range `[0.5, 1)`, and a power of 2.
2
+ ///
3
+ /// That is, `x * 2^p` will represent the input value.
4
+ #[ cfg_attr( all( test, assert_no_panic) , no_panic:: no_panic) ]
1
5
pub fn frexp ( x : f64 ) -> ( f64 , i32 ) {
2
- let mut y = x. to_bits ( ) ;
3
- let ee = ( ( y >> 52 ) & 0x7ff ) as i32 ;
4
-
5
- if ee == 0 {
6
- if x != 0.0 {
7
- let x1p64 = f64:: from_bits ( 0x43f0000000000000 ) ;
8
- let ( x, e) = frexp ( x * x1p64) ;
9
- return ( x, e - 64 ) ;
10
- }
11
- return ( x, 0 ) ;
12
- } else if ee == 0x7ff {
13
- return ( x, 0 ) ;
14
- }
15
-
16
- let e = ee - 0x3fe ;
17
- y &= 0x800fffffffffffff ;
18
- y |= 0x3fe0000000000000 ;
19
- return ( f64:: from_bits ( y) , e) ;
6
+ super :: generic:: frexp ( x)
20
7
}
Original file line number Diff line number Diff line change
1
+ /// Decompose a float into a normalized value within the range `[0.5, 1)`, and a power of 2.
2
+ ///
3
+ /// That is, `x * 2^p` will represent the input value.
4
+ #[ cfg_attr( all( test, assert_no_panic) , no_panic:: no_panic) ]
1
5
pub fn frexpf ( x : f32 ) -> ( f32 , i32 ) {
2
- let mut y = x. to_bits ( ) ;
3
- let ee: i32 = ( ( y >> 23 ) & 0xff ) as i32 ;
4
-
5
- if ee == 0 {
6
- if x != 0.0 {
7
- let x1p64 = f32:: from_bits ( 0x5f800000 ) ;
8
- let ( x, e) = frexpf ( x * x1p64) ;
9
- return ( x, e - 64 ) ;
10
- } else {
11
- return ( x, 0 ) ;
12
- }
13
- } else if ee == 0xff {
14
- return ( x, 0 ) ;
15
- }
16
-
17
- let e = ee - 0x7e ;
18
- y &= 0x807fffff ;
19
- y |= 0x3f000000 ;
20
- ( f32:: from_bits ( y) , e)
6
+ super :: generic:: frexp ( x)
21
7
}
Original file line number Diff line number Diff line change
1
+ use super :: super :: { CastFrom , Float , MinInt } ;
2
+
3
+ pub fn frexp < F : Float > ( x : F ) -> ( F , i32 ) {
4
+ let mut ix = x. to_bits ( ) ;
5
+ let ee = x. exp ( ) ;
6
+
7
+ if ee == 0 {
8
+ if x != F :: ZERO {
9
+ // normalize via multiplication; 1p64 for `f64`
10
+ let magic = F :: from_parts ( false , F :: EXP_BIAS + F :: BITS , F :: Int :: ZERO ) ;
11
+ magic. to_bits ( ) ;
12
+
13
+ let ( x, e) = frexp ( x * magic) ;
14
+ return ( x, e - F :: BITS as i32 ) ;
15
+ }
16
+ return ( x, 0 ) ;
17
+ } else if ee == F :: EXP_SAT {
18
+ return ( x, 0 ) ;
19
+ }
20
+
21
+ let e = ee as i32 - ( F :: EXP_BIAS as i32 - 1 ) ;
22
+ ix &= F :: SIGN_MASK | F :: SIG_MASK ;
23
+ ix |= F :: Int :: cast_from ( F :: EXP_BIAS - 1 ) << F :: SIG_BITS ;
24
+ ( F :: from_bits ( ix) , e)
25
+ }
Original file line number Diff line number Diff line change
1
+ use super :: super :: { Float , MinInt } ;
2
+
3
+ const FP_ILOGBNAN : i32 = i32:: MIN ;
4
+ const FP_ILOGB0 : i32 = FP_ILOGBNAN ;
5
+
6
+ pub fn ilogb < F : Float > ( x : F ) -> i32 {
7
+ let zero = F :: Int :: ZERO ;
8
+ let mut i = x. to_bits ( ) ;
9
+ let e = x. exp ( ) as i32 ;
10
+
11
+ if e == 0 {
12
+ i <<= F :: EXP_BITS + 1 ;
13
+ if i == F :: Int :: ZERO {
14
+ force_eval ! ( 0.0 / 0.0 ) ;
15
+ return FP_ILOGB0 ;
16
+ }
17
+ /* subnormal x */
18
+ let mut e = -( F :: EXP_BIAS as i32 ) ;
19
+ while i >> ( F :: BITS - 1 ) == zero {
20
+ e -= 1 ;
21
+ i <<= 1 ;
22
+ }
23
+ e
24
+ } else if e == F :: EXP_SAT as i32 {
25
+ force_eval ! ( 0.0 / 0.0 ) ;
26
+ if i << ( F :: EXP_BITS + 1 ) != zero { FP_ILOGBNAN } else { i32:: MAX }
27
+ } else {
28
+ e - F :: EXP_BIAS as i32
29
+ }
30
+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ mod floor;
6
6
mod fmax;
7
7
mod fmin;
8
8
mod fmod;
9
+ mod frexp;
10
+ mod ilogb;
9
11
mod rint;
10
12
mod round;
11
13
mod scalbn;
@@ -20,6 +22,8 @@ pub use floor::floor;
20
22
pub use fmax:: fmax;
21
23
pub use fmin:: fmin;
22
24
pub use fmod:: fmod;
25
+ pub use frexp:: frexp;
26
+ pub use ilogb:: ilogb;
23
27
pub use rint:: rint;
24
28
pub use round:: round;
25
29
pub use scalbn:: scalbn;
Original file line number Diff line number Diff line change 1
- const FP_ILOGBNAN : i32 = -1 - 0x7fffffff ;
2
- const FP_ILOGB0 : i32 = FP_ILOGBNAN ;
3
-
1
+ /// Extract the binary exponent of `x`.
4
2
#[ cfg_attr( all( test, assert_no_panic) , no_panic:: no_panic) ]
5
3
pub fn ilogb ( x : f64 ) -> i32 {
6
- let mut i: u64 = x. to_bits ( ) ;
7
- let e = ( ( i >> 52 ) & 0x7ff ) as i32 ;
8
-
9
- if e == 0 {
10
- i <<= 12 ;
11
- if i == 0 {
12
- force_eval ! ( 0.0 / 0.0 ) ;
13
- return FP_ILOGB0 ;
14
- }
15
- /* subnormal x */
16
- let mut e = -0x3ff ;
17
- while ( i >> 63 ) == 0 {
18
- e -= 1 ;
19
- i <<= 1 ;
20
- }
21
- e
22
- } else if e == 0x7ff {
23
- force_eval ! ( 0.0 / 0.0 ) ;
24
- if ( i << 12 ) != 0 { FP_ILOGBNAN } else { i32:: MAX }
25
- } else {
26
- e - 0x3ff
27
- }
4
+ super :: generic:: ilogb ( x)
28
5
}
Original file line number Diff line number Diff line change 1
- const FP_ILOGBNAN : i32 = -1 - 0x7fffffff ;
2
- const FP_ILOGB0 : i32 = FP_ILOGBNAN ;
3
-
1
+ /// Extract the binary exponent of `x`.
4
2
#[ cfg_attr( all( test, assert_no_panic) , no_panic:: no_panic) ]
5
3
pub fn ilogbf ( x : f32 ) -> i32 {
6
- let mut i = x. to_bits ( ) ;
7
- let e = ( ( i >> 23 ) & 0xff ) as i32 ;
8
-
9
- if e == 0 {
10
- i <<= 9 ;
11
- if i == 0 {
12
- force_eval ! ( 0.0 / 0.0 ) ;
13
- return FP_ILOGB0 ;
14
- }
15
- /* subnormal x */
16
- let mut e = -0x7f ;
17
- while ( i >> 31 ) == 0 {
18
- e -= 1 ;
19
- i <<= 1 ;
20
- }
21
- e
22
- } else if e == 0xff {
23
- force_eval ! ( 0.0 / 0.0 ) ;
24
- if ( i << 9 ) != 0 { FP_ILOGBNAN } else { i32:: MAX }
25
- } else {
26
- e - 0x7f
27
- }
4
+ super :: generic:: ilogb ( x)
28
5
}
You can’t perform that action at this time.
0 commit comments