divxc3.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===-- divxc3.c - Implement __divxc3 -------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements __divxc3 for the compiler_rt library.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #if !_ARCH_PPC
  13. #include "int_lib.h"
  14. #include "int_math.h"
  15. // Returns: the quotient of (a + ib) / (c + id)
  16. COMPILER_RT_ABI Lcomplex __divxc3(xf_float __a, xf_float __b, xf_float __c,
  17. xf_float __d) {
  18. int __ilogbw = 0;
  19. xf_float __logbw = crt_logbl(crt_fmaxl(crt_fabsl(__c), crt_fabsl(__d)));
  20. if (crt_isfinite(__logbw)) {
  21. __ilogbw = (int)__logbw;
  22. __c = crt_scalbnl(__c, -__ilogbw);
  23. __d = crt_scalbnl(__d, -__ilogbw);
  24. }
  25. xf_float __denom = __c * __c + __d * __d;
  26. Lcomplex z;
  27. COMPLEX_REAL(z) = crt_scalbnl((__a * __c + __b * __d) / __denom, -__ilogbw);
  28. COMPLEX_IMAGINARY(z) =
  29. crt_scalbnl((__b * __c - __a * __d) / __denom, -__ilogbw);
  30. if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
  31. if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
  32. COMPLEX_REAL(z) = crt_copysignl(CRT_INFINITY, __c) * __a;
  33. COMPLEX_IMAGINARY(z) = crt_copysignl(CRT_INFINITY, __c) * __b;
  34. } else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
  35. crt_isfinite(__d)) {
  36. __a = crt_copysignl(crt_isinf(__a) ? 1 : 0, __a);
  37. __b = crt_copysignl(crt_isinf(__b) ? 1 : 0, __b);
  38. COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
  39. COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
  40. } else if (crt_isinf(__logbw) && __logbw > 0 && crt_isfinite(__a) &&
  41. crt_isfinite(__b)) {
  42. __c = crt_copysignl(crt_isinf(__c) ? 1 : 0, __c);
  43. __d = crt_copysignl(crt_isinf(__d) ? 1 : 0, __d);
  44. COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
  45. COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
  46. }
  47. }
  48. return z;
  49. }
  50. #endif