s_fmal.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*-
  2. * Copyright (c) 2005-2011 David Schultz <das@FreeBSD.ORG>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. #include "cdefs-compat.h"
  27. //__FBSDID("$FreeBSD: src/lib/msun/src/s_fmal.c,v 1.7 2011/10/21 06:30:43 das Exp $");
  28. #include <fenv.h>
  29. #include <float.h>
  30. #include <openlibm.h>
  31. #include "math_private.h"
  32. #include "fpmath.h"
  33. /*
  34. * A struct dd represents a floating-point number with twice the precision
  35. * of a long double. We maintain the invariant that "hi" stores the high-order
  36. * bits of the result.
  37. */
  38. struct dd {
  39. long double hi;
  40. long double lo;
  41. };
  42. /*
  43. * Compute a+b exactly, returning the exact result in a struct dd. We assume
  44. * that both a and b are finite, but make no assumptions about their relative
  45. * magnitudes.
  46. */
  47. static inline struct dd
  48. dd_add(long double a, long double b)
  49. {
  50. struct dd ret;
  51. long double s;
  52. ret.hi = a + b;
  53. s = ret.hi - a;
  54. ret.lo = (a - (ret.hi - s)) + (b - s);
  55. return (ret);
  56. }
  57. /*
  58. * Compute a+b, with a small tweak: The least significant bit of the
  59. * result is adjusted into a sticky bit summarizing all the bits that
  60. * were lost to rounding. This adjustment negates the effects of double
  61. * rounding when the result is added to another number with a higher
  62. * exponent. For an explanation of round and sticky bits, see any reference
  63. * on FPU design, e.g.,
  64. *
  65. * J. Coonen. An Implementation Guide to a Proposed Standard for
  66. * Floating-Point Arithmetic. Computer, vol. 13, no. 1, Jan 1980.
  67. */
  68. static inline long double
  69. add_adjusted(long double a, long double b)
  70. {
  71. struct dd sum;
  72. union IEEEl2bits u;
  73. sum = dd_add(a, b);
  74. if (sum.lo != 0) {
  75. u.e = sum.hi;
  76. if ((u.bits.manl & 1) == 0)
  77. sum.hi = nextafterl(sum.hi, INFINITY * sum.lo);
  78. }
  79. return (sum.hi);
  80. }
  81. /*
  82. * Compute ldexp(a+b, scale) with a single rounding error. It is assumed
  83. * that the result will be subnormal, and care is taken to ensure that
  84. * double rounding does not occur.
  85. */
  86. static inline long double
  87. add_and_denormalize(long double a, long double b, int scale)
  88. {
  89. struct dd sum;
  90. int bits_lost;
  91. union IEEEl2bits u;
  92. sum = dd_add(a, b);
  93. /*
  94. * If we are losing at least two bits of accuracy to denormalization,
  95. * then the first lost bit becomes a round bit, and we adjust the
  96. * lowest bit of sum.hi to make it a sticky bit summarizing all the
  97. * bits in sum.lo. With the sticky bit adjusted, the hardware will
  98. * break any ties in the correct direction.
  99. *
  100. * If we are losing only one bit to denormalization, however, we must
  101. * break the ties manually.
  102. */
  103. if (sum.lo != 0) {
  104. u.e = sum.hi;
  105. bits_lost = -u.bits.exp - scale + 1;
  106. if ((bits_lost != 1) ^ (int)(u.bits.manl & 1))
  107. sum.hi = nextafterl(sum.hi, INFINITY * sum.lo);
  108. }
  109. return (ldexp(sum.hi, scale));
  110. }
  111. /*
  112. * Compute a*b exactly, returning the exact result in a struct dd. We assume
  113. * that both a and b are normalized, so no underflow or overflow will occur.
  114. * The current rounding mode must be round-to-nearest.
  115. */
  116. static inline struct dd
  117. dd_mul(long double a, long double b)
  118. {
  119. #if LDBL_MANT_DIG == 64
  120. static const long double split = 0x1p32L + 1.0;
  121. #elif LDBL_MANT_DIG == 113
  122. static const long double split = 0x1p57L + 1.0;
  123. #endif
  124. struct dd ret;
  125. long double ha, hb, la, lb, p, q;
  126. p = a * split;
  127. ha = a - p;
  128. ha += p;
  129. la = a - ha;
  130. p = b * split;
  131. hb = b - p;
  132. hb += p;
  133. lb = b - hb;
  134. p = ha * hb;
  135. q = ha * lb + la * hb;
  136. ret.hi = p + q;
  137. ret.lo = p - ret.hi + q + la * lb;
  138. return (ret);
  139. }
  140. /*
  141. * Fused multiply-add: Compute x * y + z with a single rounding error.
  142. *
  143. * We use scaling to avoid overflow/underflow, along with the
  144. * canonical precision-doubling technique adapted from:
  145. *
  146. * Dekker, T. A Floating-Point Technique for Extending the
  147. * Available Precision. Numer. Math. 18, 224-242 (1971).
  148. */
  149. DLLEXPORT long double
  150. fmal(long double x, long double y, long double z)
  151. {
  152. long double xs, ys, zs, adj;
  153. struct dd xy, r;
  154. int oround;
  155. int ex, ey, ez;
  156. int spread;
  157. /*
  158. * Handle special cases. The order of operations and the particular
  159. * return values here are crucial in handling special cases involving
  160. * infinities, NaNs, overflows, and signed zeroes correctly.
  161. */
  162. if (x == 0.0 || y == 0.0)
  163. return (x * y + z);
  164. if (z == 0.0)
  165. return (x * y);
  166. if (!isfinite(x) || !isfinite(y))
  167. return (x * y + z);
  168. if (!isfinite(z))
  169. return (z);
  170. xs = frexpl(x, &ex);
  171. ys = frexpl(y, &ey);
  172. zs = frexpl(z, &ez);
  173. oround = fegetround();
  174. spread = ex + ey - ez;
  175. /*
  176. * If x * y and z are many orders of magnitude apart, the scaling
  177. * will overflow, so we handle these cases specially. Rounding
  178. * modes other than FE_TONEAREST are painful.
  179. */
  180. if (spread < -LDBL_MANT_DIG) {
  181. feraiseexcept(FE_INEXACT);
  182. if (!isnormal(z))
  183. feraiseexcept(FE_UNDERFLOW);
  184. switch (oround) {
  185. case FE_TONEAREST:
  186. return (z);
  187. case FE_TOWARDZERO:
  188. if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0))
  189. return (z);
  190. else
  191. return (nextafterl(z, 0));
  192. case FE_DOWNWARD:
  193. if ((x > 0.0) ^ (y < 0.0))
  194. return (z);
  195. else
  196. return (nextafterl(z, -INFINITY));
  197. default: /* FE_UPWARD */
  198. if ((x > 0.0) ^ (y < 0.0))
  199. return (nextafterl(z, INFINITY));
  200. else
  201. return (z);
  202. }
  203. }
  204. if (spread <= LDBL_MANT_DIG * 2)
  205. zs = ldexpl(zs, -spread);
  206. else
  207. zs = copysignl(LDBL_MIN, zs);
  208. fesetround(FE_TONEAREST);
  209. /*
  210. * Basic approach for round-to-nearest:
  211. *
  212. * (xy.hi, xy.lo) = x * y (exact)
  213. * (r.hi, r.lo) = xy.hi + z (exact)
  214. * adj = xy.lo + r.lo (inexact; low bit is sticky)
  215. * result = r.hi + adj (correctly rounded)
  216. */
  217. xy = dd_mul(xs, ys);
  218. r = dd_add(xy.hi, zs);
  219. spread = ex + ey;
  220. if (r.hi == 0.0) {
  221. /*
  222. * When the addends cancel to 0, ensure that the result has
  223. * the correct sign.
  224. */
  225. fesetround(oround);
  226. volatile long double vzs = zs; /* XXX gcc CSE bug workaround */
  227. return (xy.hi + vzs + ldexpl(xy.lo, spread));
  228. }
  229. if (oround != FE_TONEAREST) {
  230. /*
  231. * There is no need to worry about double rounding in directed
  232. * rounding modes.
  233. */
  234. fesetround(oround);
  235. adj = r.lo + xy.lo;
  236. return (ldexpl(r.hi + adj, spread));
  237. }
  238. adj = add_adjusted(r.lo, xy.lo);
  239. if (spread + ilogbl(r.hi) > -16383)
  240. return (ldexpl(r.hi + adj, spread));
  241. else
  242. return (add_and_denormalize(r.hi, adj, spread));
  243. }