divtc3.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //===-- divtc3.c - Implement __divtc3 -------------------------------------===//
  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 __divtc3 for the compiler_rt library.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #define QUAD_PRECISION
  13. #include "fp_lib.h"
  14. #if defined(CRT_HAS_128BIT) && defined(CRT_HAS_F128)
  15. // Returns: the quotient of (a + ib) / (c + id)
  16. COMPILER_RT_ABI Qcomplex __divtc3(fp_t __a, fp_t __b, fp_t __c, fp_t __d) {
  17. int __ilogbw = 0;
  18. fp_t __logbw = __compiler_rt_logbtf(
  19. __compiler_rt_fmaxtf(crt_fabstf(__c), crt_fabstf(__d)));
  20. if (crt_isfinite(__logbw)) {
  21. __ilogbw = (int)__logbw;
  22. __c = __compiler_rt_scalbntf(__c, -__ilogbw);
  23. __d = __compiler_rt_scalbntf(__d, -__ilogbw);
  24. }
  25. fp_t __denom = __c * __c + __d * __d;
  26. Qcomplex z;
  27. COMPLEXTF_REAL(z) =
  28. __compiler_rt_scalbntf((__a * __c + __b * __d) / __denom, -__ilogbw);
  29. COMPLEXTF_IMAGINARY(z) =
  30. __compiler_rt_scalbntf((__b * __c - __a * __d) / __denom, -__ilogbw);
  31. if (crt_isnan(COMPLEXTF_REAL(z)) && crt_isnan(COMPLEXTF_IMAGINARY(z))) {
  32. if ((__denom == 0.0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
  33. COMPLEXTF_REAL(z) = crt_copysigntf(CRT_INFINITY, __c) * __a;
  34. COMPLEXTF_IMAGINARY(z) = crt_copysigntf(CRT_INFINITY, __c) * __b;
  35. } else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
  36. crt_isfinite(__d)) {
  37. __a = crt_copysigntf(crt_isinf(__a) ? (fp_t)1.0 : (fp_t)0.0, __a);
  38. __b = crt_copysigntf(crt_isinf(__b) ? (fp_t)1.0 : (fp_t)0.0, __b);
  39. COMPLEXTF_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
  40. COMPLEXTF_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
  41. } else if (crt_isinf(__logbw) && __logbw > 0.0 && crt_isfinite(__a) &&
  42. crt_isfinite(__b)) {
  43. __c = crt_copysigntf(crt_isinf(__c) ? (fp_t)1.0 : (fp_t)0.0, __c);
  44. __d = crt_copysigntf(crt_isinf(__d) ? (fp_t)1.0 : (fp_t)0.0, __d);
  45. COMPLEXTF_REAL(z) = 0.0 * (__a * __c + __b * __d);
  46. COMPLEXTF_IMAGINARY(z) = 0.0 * (__b * __c - __a * __d);
  47. }
  48. }
  49. return z;
  50. }
  51. #endif