pow.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. macro_rules! pow {
  2. ($intrinsic:ident: $fty:ty, $ity:ident) => {
  3. /// Returns `a` raised to the power `b`
  4. #[cfg_attr(not(test), no_mangle)]
  5. pub extern "C" fn $intrinsic(a: $fty, b: $ity) -> $fty {
  6. let (mut a, mut b) = (a, b);
  7. let recip = b < 0;
  8. let mut r: $fty = 1.0;
  9. loop {
  10. if (b & 1) != 0 {
  11. r *= a;
  12. }
  13. b = sdiv!($ity, b, 2);
  14. if b == 0 {
  15. break;
  16. }
  17. a *= a;
  18. }
  19. if recip {
  20. 1.0 / r
  21. } else {
  22. r
  23. }
  24. }
  25. }
  26. }
  27. pow!(__powisf2: f32, i32);
  28. pow!(__powidf2: f64, i32);
  29. #[cfg(test)]
  30. mod tests {
  31. use qc::{I32, F32, F64};
  32. check! {
  33. fn __powisf2(f: extern "C" fn(f32, i32) -> f32,
  34. a: F32,
  35. b: I32) -> Option<F32> {
  36. Some(F32(f(a.0, b.0)))
  37. }
  38. fn __powidf2(f: extern "C" fn(f64, i32) -> f64,
  39. a: F64,
  40. b: I32) -> Option<F64> {
  41. Some(F64(f(a.0, b.0)))
  42. }
  43. }
  44. }