Skip to content

Commit 72ed4c8

Browse files
committed
Don't use a macro for pow calculation
1 parent 8313cec commit 72ed4c8

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/float/pow.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use int::Int;
2+
use float::Float;
23

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;
78
let recip = b < 0;
8-
let mut r = 1.0;
9+
let mut r = Self::ONE;
910
loop {
1011
if (b & 1) != 0 {
1112
r *= a;
@@ -18,19 +19,22 @@ macro_rules! pow {
1819
}
1920

2021
if recip {
21-
1.0 / r
22+
Self::ONE / r
2223
} else {
2324
r
2425
}
25-
})
26+
}
2627
}
2728

29+
impl Pow for f32 {}
30+
impl Pow for f64 {}
31+
2832
intrinsics! {
2933
pub extern "C" fn __powisf2(a: f32, b: i32) -> f32 {
30-
pow!(a, b)
34+
a.pow(b)
3135
}
3236

3337
pub extern "C" fn __powidf2(a: f64, b: i32) -> f64 {
34-
pow!(a, b)
38+
a.pow(b)
3539
}
3640
}

0 commit comments

Comments
 (0)