File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change 1
1
use int:: Int ;
2
+ use float:: Float ;
2
3
3
- /// Returns `a` raised to the power `b`
4
- macro_rules! pow {
5
- ( $a : expr , $ b: expr ) => ( {
6
- let ( mut a, mut b ) = ( $a , $b ) ;
4
+ trait Pow : Float {
5
+ /// Returns `a` raised to the power `b`
6
+ fn pow ( self , mut b : i32 ) -> Self {
7
+ let mut a = self ;
7
8
let recip = b < 0 ;
8
- let mut r = 1.0 ;
9
+ let mut r = Self :: ONE ;
9
10
loop {
10
11
if ( b & 1 ) != 0 {
11
12
r *= a;
@@ -18,19 +19,22 @@ macro_rules! pow {
18
19
}
19
20
20
21
if recip {
21
- 1.0 / r
22
+ Self :: ONE / r
22
23
} else {
23
24
r
24
25
}
25
- } )
26
+ }
26
27
}
27
28
29
+ impl Pow for f32 { }
30
+ impl Pow for f64 { }
31
+
28
32
intrinsics ! {
29
33
pub extern "C" fn __powisf2( a: f32 , b: i32 ) -> f32 {
30
- pow! ( a , b)
34
+ a . pow( b)
31
35
}
32
36
33
37
pub extern "C" fn __powidf2( a: f64 , b: i32 ) -> f64 {
34
- pow! ( a , b)
38
+ a . pow( b)
35
39
}
36
40
}
You can’t perform that action at this time.
0 commit comments