gcc_qadd.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  2. // See https://llvm.org/LICENSE.txt for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. // long double __gcc_qadd(long double x, long double y);
  5. // This file implements the PowerPC 128-bit double-double add operation.
  6. // This implementation is shamelessly cribbed from Apple's DDRT, circa 1993(!)
  7. #include "DD.h"
  8. long double __gcc_qadd(long double x, long double y) {
  9. static const uint32_t infinityHi = UINT32_C(0x7ff00000);
  10. DD dst = {.ld = x}, src = {.ld = y};
  11. register double A = dst.s.hi, a = dst.s.lo, B = src.s.hi, b = src.s.lo;
  12. // If both operands are zero:
  13. if ((A == 0.0) && (B == 0.0)) {
  14. dst.s.hi = A + B;
  15. dst.s.lo = 0.0;
  16. return dst.ld;
  17. }
  18. // If either operand is NaN or infinity:
  19. const doublebits abits = {.d = A};
  20. const doublebits bbits = {.d = B};
  21. if ((((uint32_t)(abits.x >> 32) & infinityHi) == infinityHi) ||
  22. (((uint32_t)(bbits.x >> 32) & infinityHi) == infinityHi)) {
  23. dst.s.hi = A + B;
  24. dst.s.lo = 0.0;
  25. return dst.ld;
  26. }
  27. // If the computation overflows:
  28. // This may be playing things a little bit fast and loose, but it will do for
  29. // a start.
  30. const double testForOverflow = A + (B + (a + b));
  31. const doublebits testbits = {.d = testForOverflow};
  32. if (((uint32_t)(testbits.x >> 32) & infinityHi) == infinityHi) {
  33. dst.s.hi = testForOverflow;
  34. dst.s.lo = 0.0;
  35. return dst.ld;
  36. }
  37. double H, h;
  38. double T, t;
  39. double W, w;
  40. double Y;
  41. H = B + (A - (A + B));
  42. T = b + (a - (a + b));
  43. h = A + (B - (A + B));
  44. t = a + (b - (a + b));
  45. if (local_fabs(A) <= local_fabs(B))
  46. w = (a + b) + h;
  47. else
  48. w = (a + b) + H;
  49. W = (A + B) + w;
  50. Y = (A + B) - W;
  51. Y += w;
  52. if (local_fabs(a) <= local_fabs(b))
  53. w = t + Y;
  54. else
  55. w = T + Y;
  56. dst.s.hi = Y = W + w;
  57. dst.s.lo = (W - Y) + w;
  58. return dst.ld;
  59. }