pow.rs 742 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. use int::Int;
  2. use float::Float;
  3. trait Pow: Float {
  4. /// Returns `a` raised to the power `b`
  5. fn pow(self, mut b: i32) -> Self {
  6. let mut a = self;
  7. let recip = b < 0;
  8. let mut r = Self::ONE;
  9. loop {
  10. if (b & 1) != 0 {
  11. r *= a;
  12. }
  13. b = b.aborting_div(2);
  14. if b == 0 {
  15. break;
  16. }
  17. a *= a;
  18. }
  19. if recip {
  20. Self::ONE / r
  21. } else {
  22. r
  23. }
  24. }
  25. }
  26. impl Pow for f32 {}
  27. impl Pow for f64 {}
  28. intrinsics! {
  29. pub extern "C" fn __powisf2(a: f32, b: i32) -> f32 {
  30. a.pow(b)
  31. }
  32. pub extern "C" fn __powidf2(a: f64, b: i32) -> f64 {
  33. a.pow(b)
  34. }
  35. }