Don't use a macro for pow calculation
parent
8313cec597
commit
72ed4c8bce
|
@ -1,11 +1,12 @@
|
||||||
use int::Int;
|
use int::Int;
|
||||||
|
use float::Float;
|
||||||
|
|
||||||
/// Returns `a` raised to the power `b`
|
trait Pow: Float {
|
||||||
macro_rules! pow {
|
/// Returns `a` raised to the power `b`
|
||||||
($a: expr, $b: expr) => ({
|
fn pow(self, mut b: i32) -> Self {
|
||||||
let (mut a, mut b) = ($a, $b);
|
let mut a = self;
|
||||||
let recip = b < 0;
|
let recip = b < 0;
|
||||||
let mut r = 1.0;
|
let mut r = Self::ONE;
|
||||||
loop {
|
loop {
|
||||||
if (b & 1) != 0 {
|
if (b & 1) != 0 {
|
||||||
r *= a;
|
r *= a;
|
||||||
|
@ -18,19 +19,22 @@ macro_rules! pow {
|
||||||
}
|
}
|
||||||
|
|
||||||
if recip {
|
if recip {
|
||||||
1.0 / r
|
Self::ONE / r
|
||||||
} else {
|
} else {
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Pow for f32 {}
|
||||||
|
impl Pow for f64 {}
|
||||||
|
|
||||||
intrinsics! {
|
intrinsics! {
|
||||||
pub extern "C" fn __powisf2(a: f32, b: i32) -> f32 {
|
pub extern "C" fn __powisf2(a: f32, b: i32) -> f32 {
|
||||||
pow!(a, b)
|
a.pow(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub extern "C" fn __powidf2(a: f64, b: i32) -> f64 {
|
pub extern "C" fn __powidf2(a: f64, b: i32) -> f64 {
|
||||||
pow!(a, b)
|
a.pow(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue