/contrib/compiler-rt/lib/divsc3.c

https://github.com/okuoku/freebsd-head · C · 59 lines · 42 code · 3 blank · 14 comment · 17 complexity · e925d1117c65cbc34e453d7873fdac66 MD5 · raw file

  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 <math.h>
  16. #include <complex.h>
  17. /* Returns: the quotient of (a + ib) / (c + id) */
  18. float _Complex
  19. __divsc3(float __a, float __b, float __c, float __d)
  20. {
  21. int __ilogbw = 0;
  22. float __logbw = logbf(fmaxf(fabsf(__c), fabsf(__d)));
  23. if (isfinite(__logbw))
  24. {
  25. __ilogbw = (int)__logbw;
  26. __c = scalbnf(__c, -__ilogbw);
  27. __d = scalbnf(__d, -__ilogbw);
  28. }
  29. float __denom = __c * __c + __d * __d;
  30. float _Complex z;
  31. __real__ z = scalbnf((__a * __c + __b * __d) / __denom, -__ilogbw);
  32. __imag__ z = scalbnf((__b * __c - __a * __d) / __denom, -__ilogbw);
  33. if (isnan(__real__ z) && isnan(__imag__ z))
  34. {
  35. if ((__denom == 0) && (!isnan(__a) || !isnan(__b)))
  36. {
  37. __real__ z = copysignf(INFINITY, __c) * __a;
  38. __imag__ z = copysignf(INFINITY, __c) * __b;
  39. }
  40. else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
  41. {
  42. __a = copysignf(isinf(__a) ? 1 : 0, __a);
  43. __b = copysignf(isinf(__b) ? 1 : 0, __b);
  44. __real__ z = INFINITY * (__a * __c + __b * __d);
  45. __imag__ z = INFINITY * (__b * __c - __a * __d);
  46. }
  47. else if (isinf(__logbw) && __logbw > 0 && isfinite(__a) && isfinite(__b))
  48. {
  49. __c = copysignf(isinf(__c) ? 1 : 0, __c);
  50. __d = copysignf(isinf(__d) ? 1 : 0, __d);
  51. __real__ z = 0 * (__a * __c + __b * __d);
  52. __imag__ z = 0 * (__b * __c - __a * __d);
  53. }
  54. }
  55. return z;
  56. }