- /*
- ez <= exy - 12
- the 12 extra bits (1guard, 11round+sticky) are needed so with
- lo = dadd(lo1, lo2)
- elo <= ehi - 11, and we use the last 10 bits in adjust so
- dadd(hi, lo)
- gives correct result when rounded to double
- */
- hi = xy;
- lo2 = z;
- }
- /*
- the result is stored before return for correct precision and exceptions
-
- one corner case is when the underflow flag should be raised because
- the precise result is an inexact subnormal double, but the calculated
- long double result is an exact subnormal double
- (so rounding to double does not raise exceptions)
-
- in nearest rounding mode dadd takes care of this: the last bit of the
- result is adjusted so rounding sees an inexact value when it should
-
- in non-nearest rounding mode fenv is used for the workaround
- */
- fesetround(round);
- if (round == FE_TONEAREST)
- z = dadd(hi, dadd(lo1, lo2));
- else {
-#if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
- int e = fetestexcept(FE_INEXACT);
- feclearexcept(FE_INEXACT);
-#endif
- z = hi + (lo1 + lo2);
-#if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
- if (getexp(z) < 0x3fff-1022 && fetestexcept(FE_INEXACT))
- feraiseexcept(FE_UNDERFLOW);
- else if (e)
- feraiseexcept(FE_INEXACT);
-#endif
- }
- return z;
-}
-#else
-/* origin: FreeBSD /usr/src/lib/msun/src/s_fma.c */
-/*-
- * Copyright (c) 2005-2011 David Schultz <das@FreeBSD.ORG>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/*
- * A struct dd represents a floating-point number with twice the precision
- * of a double. We maintain the invariant that "hi" stores the 53 high-order
- * bits of the result.
- */
-struct dd {
- double hi;
- double lo;
-};
-
-/*
- * Compute a+b exactly, returning the exact result in a struct dd. We assume
- * that both a and b are finite, but make no assumptions about their relative
- * magnitudes.
- */
-static inline struct dd dd_add(double a, double b)
-{
- struct dd ret;
- double s;
-
- ret.hi = a + b;
- s = ret.hi - a;
- ret.lo = (a - (ret.hi - s)) + (b - s);
- return (ret);
-}
-
-/*
- * Compute a+b, with a small tweak: The least significant bit of the
- * result is adjusted into a sticky bit summarizing all the bits that
- * were lost to rounding. This adjustment negates the effects of double
- * rounding when the result is added to another number with a higher
- * exponent. For an explanation of round and sticky bits, see any reference
- * on FPU design, e.g.,
- *
- * J. Coonen. An Implementation Guide to a Proposed Standard for
- * Floating-Point Arithmetic. Computer, vol. 13, no. 1, Jan 1980.
- */
-static inline double add_adjusted(double a, double b)
-{
- struct dd sum;
- union {double f; uint64_t i;} uhi, ulo;
-
- sum = dd_add(a, b);
- if (sum.lo != 0) {
- uhi.f = sum.hi;
- if ((uhi.i & 1) == 0) {
- /* hibits += (int)copysign(1.0, sum.hi * sum.lo) */
- ulo.f = sum.lo;
- uhi.i += 1 - ((uhi.i ^ ulo.i) >> 62);
- sum.hi = uhi.f;