compiler-builtins-zynq/src/float/pow.rs

41 lines
742 B
Rust
Raw Normal View History

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;
let recip = b < 0;
2017-09-14 07:59:02 +08:00
let mut r = Self::ONE;
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);
if b == 0 {
break;
2016-10-01 06:15:44 +08:00
}
a *= a;
2016-10-01 06:15:44 +08:00
}
if recip {
2017-09-14 07:59:02 +08:00
Self::ONE / r
} 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 {}
intrinsics! {
pub extern "C" fn __powisf2(a: f32, b: i32) -> f32 {
2017-09-14 07:59:02 +08:00
a.pow(b)
}
pub extern "C" fn __powidf2(a: f64, b: i32) -> f64 {
2017-09-14 07:59:02 +08:00
a.pow(b)
}
}