divdc3.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===-- divdc3.c - Implement __divdc3 -------------------------------------===//
  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 __divdc3 for the compiler_rt library.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #define DOUBLE_PRECISION
  13. #include "fp_lib.h"
  14. #include "int_lib.h"
  15. #include "int_math.h"
  16. // Returns: the quotient of (a + ib) / (c + id)
  17. COMPILER_RT_ABI Dcomplex __divdc3(double __a, double __b, double __c,
  18. double __d) {
  19. int __ilogbw = 0;
  20. double __logbw = __compiler_rt_logb(__compiler_rt_fmax(crt_fabs(__c),
  21. crt_fabs(__d)));
  22. if (crt_isfinite(__logbw)) {
  23. __ilogbw = (int)__logbw;
  24. __c = __compiler_rt_scalbn(__c, -__ilogbw);
  25. __d = __compiler_rt_scalbn(__d, -__ilogbw);
  26. }
  27. double __denom = __c * __c + __d * __d;
  28. Dcomplex z;
  29. COMPLEX_REAL(z) =
  30. __compiler_rt_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
  31. COMPLEX_IMAGINARY(z) =
  32. __compiler_rt_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
  33. if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
  34. if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
  35. COMPLEX_REAL(z) = crt_copysign(CRT_INFINITY, __c) * __a;
  36. COMPLEX_IMAGINARY(z) = crt_copysign(CRT_INFINITY, __c) * __b;
  37. } else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
  38. crt_isfinite(__d)) {
  39. __a = crt_copysign(crt_isinf(__a) ? 1.0 : 0.0, __a);
  40. __b = crt_copysign(crt_isinf(__b) ? 1.0 : 0.0, __b);
  41. COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
  42. COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
  43. } else if (crt_isinf(__logbw) && __logbw > 0.0 && crt_isfinite(__a) &&
  44. crt_isfinite(__b)) {
  45. __c = crt_copysign(crt_isinf(__c) ? 1.0 : 0.0, __c);
  46. __d = crt_copysign(crt_isinf(__d) ? 1.0 : 0.0, __d);
  47. COMPLEX_REAL(z) = 0.0 * (__a * __c + __b * __d);
  48. COMPLEX_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);
  49. }
  50. }
  51. return z;
  52. }