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