multc3.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* ===-- multc3.c - Implement __multc3 -------------------------------------===
  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 __multc3 for the compiler_rt library.
  11. *
  12. * ===----------------------------------------------------------------------===
  13. */
  14. #include "int_lib.h"
  15. #include "int_math.h"
  16. /* Returns: the product of a + ib and c + id */
  17. COMPILER_RT_ABI long double _Complex
  18. __multc3(long double a, long double b, long double c, long double d)
  19. {
  20. long double ac = a * c;
  21. long double bd = b * d;
  22. long double ad = a * d;
  23. long double bc = b * c;
  24. long double _Complex z;
  25. __real__ z = ac - bd;
  26. __imag__ z = ad + bc;
  27. if (crt_isnan(__real__ z) && crt_isnan(__imag__ z)) {
  28. int recalc = 0;
  29. if (crt_isinf(a) || crt_isinf(b)) {
  30. a = crt_copysignl(crt_isinf(a) ? 1 : 0, a);
  31. b = crt_copysignl(crt_isinf(b) ? 1 : 0, b);
  32. if (crt_isnan(c))
  33. c = crt_copysignl(0, c);
  34. if (crt_isnan(d))
  35. d = crt_copysignl(0, d);
  36. recalc = 1;
  37. }
  38. if (crt_isinf(c) || crt_isinf(d)) {
  39. c = crt_copysignl(crt_isinf(c) ? 1 : 0, c);
  40. d = crt_copysignl(crt_isinf(d) ? 1 : 0, d);
  41. if (crt_isnan(a))
  42. a = crt_copysignl(0, a);
  43. if (crt_isnan(b))
  44. b = crt_copysignl(0, b);
  45. recalc = 1;
  46. }
  47. if (!recalc && (crt_isinf(ac) || crt_isinf(bd) ||
  48. crt_isinf(ad) || crt_isinf(bc))) {
  49. if (crt_isnan(a))
  50. a = crt_copysignl(0, a);
  51. if (crt_isnan(b))
  52. b = crt_copysignl(0, b);
  53. if (crt_isnan(c))
  54. c = crt_copysignl(0, c);
  55. if (crt_isnan(d))
  56. d = crt_copysignl(0, d);
  57. recalc = 1;
  58. }
  59. if (recalc) {
  60. __real__ z = CRT_INFINITY * (a * c - b * d);
  61. __imag__ z = CRT_INFINITY * (a * d + b * c);
  62. }
  63. }
  64. return z;
  65. }