divsc3.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*===-- divsc3.c - Implement __divsc3 -------------------------------------===
  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 __divsc3 for the compiler_rt library.
  11. *
  12. *===----------------------------------------------------------------------===
  13. */
  14. #include "int_lib.h"
  15. #include "int_math.h"
  16. /* Returns: the quotient of (a + ib) / (c + id) */
  17. COMPILER_RT_ABI Fcomplex
  18. __divsc3(float __a, float __b, float __c, float __d)
  19. {
  20. int __ilogbw = 0;
  21. float __logbw = crt_logbf(crt_fmaxf(crt_fabsf(__c), crt_fabsf(__d)));
  22. if (crt_isfinite(__logbw))
  23. {
  24. __ilogbw = (int)__logbw;
  25. __c = crt_scalbnf(__c, -__ilogbw);
  26. __d = crt_scalbnf(__d, -__ilogbw);
  27. }
  28. float __denom = __c * __c + __d * __d;
  29. Fcomplex z;
  30. COMPLEX_REAL(z) = crt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
  31. COMPLEX_IMAGINARY(z) = crt_scalbnf((__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_copysignf(CRT_INFINITY, __c) * __a;
  37. COMPLEX_IMAGINARY(z) = crt_copysignf(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_copysignf(crt_isinf(__a) ? 1 : 0, __a);
  43. __b = crt_copysignf(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_copysignf(crt_isinf(__c) ? 1 : 0, __c);
  51. __d = crt_copysignf(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. }