File tree 4 files changed +40
-5
lines changed
4 files changed +40
-5
lines changed Original file line number Diff line number Diff line change @@ -40,7 +40,6 @@ pub trait F32Ext: private::Sealed {
40
40
#[ cfg( todo) ]
41
41
fn ceil ( self ) -> Self ;
42
42
43
- #[ cfg( todo) ]
44
43
fn round ( self ) -> Self ;
45
44
46
45
fn trunc ( self ) -> Self ;
@@ -154,7 +153,6 @@ impl F32Ext for f32 {
154
153
ceilf ( self )
155
154
}
156
155
157
- #[ cfg( todo) ]
158
156
#[ inline]
159
157
fn round ( self ) -> Self {
160
158
roundf ( self )
Original file line number Diff line number Diff line change 1
1
macro_rules! force_eval {
2
2
( $e: expr) => {
3
- unsafe { :: core:: ptr:: read_volatile( & $e) ; }
4
- }
3
+ unsafe {
4
+ :: core:: ptr:: read_volatile( & $e) ;
5
+ }
6
+ } ;
5
7
}
6
8
7
9
mod fabs;
8
10
mod fabsf;
9
11
mod fmodf;
10
12
mod powf;
11
13
mod round;
14
+ mod roundf;
12
15
mod scalbn;
13
16
mod scalbnf;
14
17
mod sqrt;
@@ -29,6 +32,7 @@ pub use self::{
29
32
fmodf:: fmodf,
30
33
powf:: powf,
31
34
round:: round,
35
+ roundf:: roundf,
32
36
scalbn:: scalbn,
33
37
scalbnf:: scalbnf,
34
38
sqrt:: sqrt,
Original file line number Diff line number Diff line change
1
+ use core:: f32;
2
+
3
+ const TOINT : f32 = 1.0 / f32:: EPSILON ;
4
+
5
+ pub fn roundf ( mut x : f32 ) -> f32 {
6
+ let i = x. to_bits ( ) ;
7
+ let e: u32 = i >> 23 & 0xff ;
8
+ let mut y: f32 ;
9
+
10
+ if e >= 0x7f + 23 {
11
+ return x;
12
+ }
13
+ if i >> 31 != 0 {
14
+ x = -x;
15
+ }
16
+ if e < 0x7f - 1 {
17
+ force_eval ! ( x + TOINT ) ;
18
+ return 0.0 * x;
19
+ }
20
+ y = x + TOINT - TOINT - x;
21
+ if y > 0.5f32 {
22
+ y = y + x - 1.0 ;
23
+ } else if y <= -0.5f32 {
24
+ y = y + x + 1.0 ;
25
+ } else {
26
+ y = y + x;
27
+ }
28
+ if i >> 31 != 0 {
29
+ -y
30
+ } else {
31
+ y
32
+ }
33
+ }
Original file line number Diff line number Diff line change @@ -666,7 +666,7 @@ f32_f32! {
666
666
// log10f,
667
667
// log2f,
668
668
logf,
669
- // roundf,
669
+ roundf,
670
670
// sinf,
671
671
// sinhf,
672
672
// tanf,
You can’t perform that action at this time.
0 commit comments