gcc_qdiv.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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_qdiv(long double x, long double y);
  5. // This file implements the PowerPC 128-bit double-double division operation.
  6. // This implementation is shamelessly cribbed from Apple's DDRT, circa 1993(!)
  7. #include "DD.h"
  8. long double __gcc_qdiv(long double a, long double b) {
  9. static const uint32_t infinityHi = UINT32_C(0x7ff00000);
  10. DD dst = {.ld = a}, src = {.ld = b};
  11. register double x = dst.s.hi, x1 = dst.s.lo, y = src.s.hi, y1 = src.s.lo;
  12. double yHi, yLo, qHi, qLo;
  13. double yq, tmp, q;
  14. q = x / y;
  15. // Detect special cases
  16. if (q == 0.0) {
  17. dst.s.hi = q;
  18. dst.s.lo = 0.0;
  19. return dst.ld;
  20. }
  21. const doublebits qBits = {.d = q};
  22. if (((uint32_t)(qBits.x >> 32) & infinityHi) == infinityHi) {
  23. dst.s.hi = q;
  24. dst.s.lo = 0.0;
  25. return dst.ld;
  26. }
  27. yHi = high26bits(y);
  28. qHi = high26bits(q);
  29. yq = y * q;
  30. yLo = y - yHi;
  31. qLo = q - qHi;
  32. tmp = LOWORDER(yq, yHi, yLo, qHi, qLo);
  33. tmp = (x - yq) - tmp;
  34. tmp = ((tmp + x1) - y1 * q) / y;
  35. x = q + tmp;
  36. dst.s.lo = (q - x) + tmp;
  37. dst.s.hi = x;
  38. return dst.ld;
  39. }