2017-06-24 12:31:54 +08:00
|
|
|
use int::Int;
|
2017-09-14 07:59:02 +08:00
|
|
|
use float::Float;
|
2017-06-24 12:31:54 +08:00
|
|
|
|
2017-09-14 07:59:02 +08:00
|
|
|
trait Pow: Float {
|
|
|
|
/// Returns `a` raised to the power `b`
|
|
|
|
fn pow(self, mut b: i32) -> Self {
|
|
|
|
let mut a = self;
|
2017-06-24 02:05:25 +08:00
|
|
|
let recip = b < 0;
|
2017-09-14 07:59:02 +08:00
|
|
|
let mut r = Self::ONE;
|
2017-06-24 02:05:25 +08:00
|
|
|
loop {
|
|
|
|
if (b & 1) != 0 {
|
|
|
|
r *= a;
|
2016-10-01 06:15:44 +08:00
|
|
|
}
|
2017-06-24 12:31:54 +08:00
|
|
|
b = b.aborting_div(2);
|
2017-06-24 02:05:25 +08:00
|
|
|
if b == 0 {
|
|
|
|
break;
|
2016-10-01 06:15:44 +08:00
|
|
|
}
|
2017-06-24 02:05:25 +08:00
|
|
|
a *= a;
|
2016-10-01 06:15:44 +08:00
|
|
|
}
|
2017-06-24 02:05:25 +08:00
|
|
|
|
|
|
|
if recip {
|
2017-09-14 07:59:02 +08:00
|
|
|
Self::ONE / r
|
2017-06-24 02:05:25 +08:00
|
|
|
} else {
|
|
|
|
r
|
|
|
|
}
|
2017-09-14 07:59:02 +08:00
|
|
|
}
|
2016-10-01 06:15:44 +08:00
|
|
|
}
|
|
|
|
|
2017-09-14 07:59:02 +08:00
|
|
|
impl Pow for f32 {}
|
|
|
|
impl Pow for f64 {}
|
|
|
|
|
2017-06-24 02:05:25 +08:00
|
|
|
intrinsics! {
|
|
|
|
pub extern "C" fn __powisf2(a: f32, b: i32) -> f32 {
|
2017-09-14 07:59:02 +08:00
|
|
|
a.pow(b)
|
2017-06-24 02:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub extern "C" fn __powidf2(a: f64, b: i32) -> f64 {
|
2017-09-14 07:59:02 +08:00
|
|
|
a.pow(b)
|
2017-06-24 02:05:25 +08:00
|
|
|
}
|
|
|
|
}
|