divsc3.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //===-- divsc3.c - Implement __divsc3 -------------------------------------===//
  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 __divsc3 for the compiler_rt library.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #define SINGLE_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 Fcomplex __divsc3(float __a, float __b, float __c, float __d) {
  18. int __ilogbw = 0;
  19. float __logbw =
  20. __compiler_rt_logbf(__compiler_rt_fmaxf(crt_fabsf(__c), crt_fabsf(__d)));
  21. if (crt_isfinite(__logbw)) {
  22. __ilogbw = (int)__logbw;
  23. __c = __compiler_rt_scalbnf(__c, -__ilogbw);
  24. __d = __compiler_rt_scalbnf(__d, -__ilogbw);
  25. }
  26. float __denom = __c * __c + __d * __d;
  27. Fcomplex z;
  28. COMPLEX_REAL(z) =
  29. __compiler_rt_scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
  30. COMPLEX_IMAGINARY(z) =
  31. __compiler_rt_scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
  32. if (crt_isnan(COMPLEX_REAL(z)) && crt_isnan(COMPLEX_IMAGINARY(z))) {
  33. if ((__denom == 0) && (!crt_isnan(__a) || !crt_isnan(__b))) {
  34. COMPLEX_REAL(z) = crt_copysignf(CRT_INFINITY, __c) * __a;
  35. COMPLEX_IMAGINARY(z) = crt_copysignf(CRT_INFINITY, __c) * __b;
  36. } else if ((crt_isinf(__a) || crt_isinf(__b)) && crt_isfinite(__c) &&
  37. crt_isfinite(__d)) {
  38. __a = crt_copysignf(crt_isinf(__a) ? 1 : 0, __a);
  39. __b = crt_copysignf(crt_isinf(__b) ? 1 : 0, __b);
  40. COMPLEX_REAL(z) = CRT_INFINITY * (__a * __c + __b * __d);
  41. COMPLEX_IMAGINARY(z) = CRT_INFINITY * (__b * __c - __a * __d);
  42. } else if (crt_isinf(__logbw) && __logbw > 0 && crt_isfinite(__a) &&
  43. crt_isfinite(__b)) {
  44. __c = crt_copysignf(crt_isinf(__c) ? 1 : 0, __c);
  45. __d = crt_copysignf(crt_isinf(__d) ? 1 : 0, __d);
  46. COMPLEX_REAL(z) = 0 * (__a * __c + __b * __d);
  47. COMPLEX_IMAGINARY(z) = 0 * (__b * __c - __a * __d);
  48. }
  49. }
  50. return z;
  51. }