File tree 5 files changed +54
-6
lines changed
5 files changed +54
-6
lines changed Original file line number Diff line number Diff line change @@ -44,7 +44,6 @@ pub trait F32Ext {
44
44
#[ cfg( todo) ]
45
45
fn round ( self ) -> Self ;
46
46
47
- #[ cfg( todo) ]
48
47
fn trunc ( self ) -> Self ;
49
48
50
49
#[ cfg( todo) ]
@@ -163,7 +162,6 @@ impl F32Ext for f32 {
163
162
roundf ( self )
164
163
}
165
164
166
- #[ cfg( todo) ]
167
165
#[ inline]
168
166
fn trunc ( self ) -> Self {
169
167
truncf ( self )
@@ -372,7 +370,6 @@ pub trait F64Ext {
372
370
373
371
fn round ( self ) -> Self ;
374
372
375
- #[ cfg( todo) ]
376
373
fn trunc ( self ) -> Self ;
377
374
378
375
#[ cfg( todo) ]
@@ -494,7 +491,6 @@ impl F64Ext for f64 {
494
491
round ( self )
495
492
}
496
493
497
- #[ cfg( todo) ]
498
494
#[ inline]
499
495
fn trunc ( self ) -> Self {
500
496
trunc ( self )
Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ mod sqrtf;
15
15
mod logf;
16
16
mod expf;
17
17
mod floor;
18
+ mod trunc;
19
+ mod truncf;
18
20
19
21
//mod service;
20
22
@@ -30,6 +32,8 @@ pub use self::{
30
32
logf:: logf,
31
33
expf:: expf,
32
34
floor:: floor,
35
+ trunc:: trunc,
36
+ truncf:: truncf,
33
37
} ;
34
38
35
39
fn isnanf ( x : f32 ) -> bool {
Original file line number Diff line number Diff line change
1
+ use core:: f64;
2
+
3
+ #[ inline]
4
+ pub fn trunc ( x : f64 ) -> f64 {
5
+ let x1p120 = f64:: from_bits ( 0x4770000000000000 ) ; // 0x1p120f === 2 ^ 120
6
+
7
+ let mut i: u64 = x. to_bits ( ) ;
8
+ let mut e: i64 = ( i >> 52 & 0x7ff ) as i64 - 0x3ff + 12 ;
9
+ let m: u64 ;
10
+
11
+ if e >= 52 + 12 {
12
+ return x;
13
+ }
14
+ if e < 12 {
15
+ e = 1 ;
16
+ }
17
+ m = -1i64 as u64 >> e;
18
+ if ( i & m) == 0 {
19
+ return x;
20
+ }
21
+ force_eval ! ( x + x1p120) ;
22
+ i &= !m;
23
+ f64:: from_bits ( i)
24
+ }
Original file line number Diff line number Diff line change
1
+ use core:: f32;
2
+
3
+ #[ inline]
4
+ pub fn truncf ( x : f32 ) -> f32 {
5
+ let x1p120 = f32:: from_bits ( 0x7b800000 ) ; // 0x1p120f === 2 ^ 120
6
+
7
+ let mut i: u32 = x. to_bits ( ) ;
8
+ let mut e: i32 = ( i >> 23 & 0xff ) as i32 - 0x7f + 9 ;
9
+ let m: u32 ;
10
+
11
+ if e >= 23 + 9 {
12
+ return x;
13
+ }
14
+ if e < 9 {
15
+ e = 1 ;
16
+ }
17
+ m = -1i32 as u32 >> e;
18
+ if ( i & m) == 0 {
19
+ return x;
20
+ }
21
+ force_eval ! ( x + x1p120) ;
22
+ i &= !m;
23
+ f32:: from_bits ( i)
24
+ }
Original file line number Diff line number Diff line change @@ -562,7 +562,7 @@ fn main() -> Result<(), Box<Error>> {
562
562
f32_f32 ! {
563
563
// acosf,
564
564
// floorf,
565
- // truncf
565
+ truncf,
566
566
// asinf,
567
567
// atanf,
568
568
// cbrtf,
@@ -625,7 +625,7 @@ f64_f64! {
625
625
// sqrt,
626
626
// tan,
627
627
// tanh,
628
- // trunc,
628
+ trunc,
629
629
fabs,
630
630
}
631
631
You can’t perform that action at this time.
0 commit comments