darwin-ldouble.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Borrowed from GCC 4.2.2 (which still was GPL v2+)
  3. */
  4. /* 128-bit long double support routines for Darwin.
  5. Copyright (C) 1993, 2003, 2004, 2005, 2006, 2007
  6. Free Software Foundation, Inc.
  7. This file is part of GCC.
  8. GCC is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 2, or (at your option) any later
  11. version.
  12. In addition to the permissions in the GNU General Public License, the
  13. Free Software Foundation gives you unlimited permission to link the
  14. compiled version of this file into combinations with other programs,
  15. and to distribute those combinations without any restriction coming
  16. from the use of this file. (The General Public License restrictions
  17. do apply in other respects; for example, they cover modification of
  18. the file, and distribution when not linked into a combine
  19. executable.)
  20. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  21. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  22. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  23. for more details.
  24. You should have received a copy of the GNU General Public License
  25. along with GCC; see the file COPYING. If not, write to the Free
  26. Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
  27. 02110-1301, USA. */
  28. /*
  29. * Implementations of floating-point long double basic arithmetic
  30. * functions called by the IBM C compiler when generating code for
  31. * PowerPC platforms. In particular, the following functions are
  32. * implemented: __gcc_qadd, __gcc_qsub, __gcc_qmul, and __gcc_qdiv.
  33. * Double-double algorithms are based on the paper "Doubled-Precision
  34. * IEEE Standard 754 Floating-Point Arithmetic" by W. Kahan, February 26,
  35. * 1987. An alternative published reference is "Software for
  36. * Doubled-Precision Floating-Point Computations", by Seppo Linnainmaa,
  37. * ACM TOMS vol 7 no 3, September 1981, pages 272-283.
  38. */
  39. /*
  40. * Each long double is made up of two IEEE doubles. The value of the
  41. * long double is the sum of the values of the two parts. The most
  42. * significant part is required to be the value of the long double
  43. * rounded to the nearest double, as specified by IEEE. For Inf
  44. * values, the least significant part is required to be one of +0.0 or
  45. * -0.0. No other requirements are made; so, for example, 1.0 may be
  46. * represented as (1.0, +0.0) or (1.0, -0.0), and the low part of a
  47. * NaN is don't-care.
  48. *
  49. * This code currently assumes big-endian.
  50. */
  51. #define fabs(x) __builtin_fabs(x)
  52. #define isless(x, y) __builtin_isless(x, y)
  53. #define inf() __builtin_inf()
  54. #define unlikely(x) __builtin_expect((x), 0)
  55. #define nonfinite(a) unlikely(!isless(fabs(a), inf()))
  56. typedef union {
  57. long double ldval;
  58. double dval[2];
  59. } longDblUnion;
  60. /* Add two 'long double' values and return the result. */
  61. long double __gcc_qadd(double a, double aa, double c, double cc)
  62. {
  63. longDblUnion x;
  64. double z, q, zz, xh;
  65. z = a + c;
  66. if (nonfinite(z)) {
  67. z = cc + aa + c + a;
  68. if (nonfinite(z))
  69. return z;
  70. x.dval[0] = z; /* Will always be DBL_MAX. */
  71. zz = aa + cc;
  72. if (fabs(a) > fabs(c))
  73. x.dval[1] = a - z + c + zz;
  74. else
  75. x.dval[1] = c - z + a + zz;
  76. } else {
  77. q = a - z;
  78. zz = q + c + (a - (q + z)) + aa + cc;
  79. /* Keep -0 result. */
  80. if (zz == 0.0)
  81. return z;
  82. xh = z + zz;
  83. if (nonfinite(xh))
  84. return xh;
  85. x.dval[0] = xh;
  86. x.dval[1] = z - xh + zz;
  87. }
  88. return x.ldval;
  89. }
  90. long double __gcc_qsub(double a, double b, double c, double d)
  91. {
  92. return __gcc_qadd(a, b, -c, -d);
  93. }
  94. long double __gcc_qmul(double a, double b, double c, double d)
  95. {
  96. longDblUnion z;
  97. double t, tau, u, v, w;
  98. t = a * c; /* Highest order double term. */
  99. if (unlikely(t == 0) /* Preserve -0. */
  100. || nonfinite(t))
  101. return t;
  102. /* Sum terms of two highest orders. */
  103. /* Use fused multiply-add to get low part of a * c. */
  104. #ifndef __NO_FPRS__
  105. asm("fmsub %0,%1,%2,%3" : "=f"(tau) : "f"(a), "f"(c), "f"(t));
  106. #else
  107. tau = fmsub(a, c, t);
  108. #endif
  109. v = a * d;
  110. w = b * c;
  111. tau += v + w; /* Add in other second-order terms. */
  112. u = t + tau;
  113. /* Construct long double result. */
  114. if (nonfinite(u))
  115. return u;
  116. z.dval[0] = u;
  117. z.dval[1] = (t - u) + tau;
  118. return z.ldval;
  119. }