divxc3.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* ===-- divxc3.c - Implement __divxc3 -------------------------------------===
  2. *
  3. * The LLVM Compiler Infrastructure
  4. *
  5. * This file is dual licensed under the MIT and the University of Illinois Open
  6. * Source Licenses. See LICENSE.TXT for details.
  7. *
  8. * ===----------------------------------------------------------------------===
  9. *
  10. * This file implements __divxc3 for the compiler_rt library.
  11. *
  12. */
  13. #if !_ARCH_PPC
  14. #include "int_lib.h"
  15. #include "int_math.h"
  16. /* Returns: the quotient of (a + ib) / (c + id) */
  17. COMPILER_RT_ABI Lcomplex
  18. __divxc3(long double __a, long double __b, long double __c, long double __d)
  19. {
  20. int __ilogbw = 0;
  21. long double __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
  22. if (crt_isfinite(__logbw))
  23. {
  24. __ilogbw = (int)__logbw;
  25. __c = crt_scalbnl(__c, -__ilogbw);
  26. __d = crt_scalbnl(__d, -__ilogbw);
  27. }
  28. long double __denom = __c * __c + __d * __d;
  29. Lcomplex z;
  30. COMPLEX_REAL(z) = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
  31. COMPLEX_IMAGINARY(z) = crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
  32. if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z)))
  33. {
  34. if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b)))
  35. {
  36. COMPLEX_REAL(z) = crt_copysignl(CRT_INFINITY, __c) * __a;
  37. COMPLEX_IMAGINARY(z) = crt_copysignl(CRT_INFINITY, __c) * __b;
  38. }
  39. else if ((crt_isinf(__a) || crt_isinf(__b)) &&
  40. crt_isfinite(__c) && crt_isfinite(__d))
  41. {
  42. __a = crt_copysignl(crt_isinf(__a) ? 1 : 0, __a);
  43. __b = crt_copysignl(crt_isinf(__b) ? 1 : 0, __b);
  44. COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
  45. COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
  46. }
  47. else if (crt_isinf(__logbw) && __logbw > 0 &&
  48. crt_isfinite(__a) && crt_isfinite(__b))
  49. {
  50. __c = crt_copysignl(crt_isinf(__c) ? 1 : 0, __c);
  51. __d = crt_copysignl(crt_isinf(__d) ? 1 : 0, __d);
  52. COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
  53. COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
  54. }
  55. }
  56. return z;
  57. }
  58. #endif