- /*
- 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;
- }
- fesetround(round);
- if (round == FE_TONEAREST)
- return dadd(hi, dadd(lo1, lo2));
- return hi + (lo1 + lo2);
-}
-#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;
- uint64_t hibits, lobits;
-
- sum = dd_add(a, b);
- if (sum.lo != 0) {
- EXTRACT_WORD64(hibits, sum.hi);
- if ((hibits & 1) == 0) {
- /* hibits += (int)copysign(1.0, sum.hi * sum.lo) */
- EXTRACT_WORD64(lobits, sum.lo);
- hibits += 1 - ((hibits ^ lobits) >> 62);
- INSERT_WORD64(sum.hi, hibits);
- }
- }
- return (sum.hi);
-}
-
-/*
- * Compute ldexp(a+b, scale) with a single rounding error. It is assumed
- * that the result will be subnormal, and care is taken to ensure that
- * double rounding does not occur.
- */
-static inline double add_and_denormalize(double a, double b, int scale)
-{
- struct dd sum;
- uint64_t hibits, lobits;
- int bits_lost;
-
- sum = dd_add(a, b);
-
- /*
- * If we are losing at least two bits of accuracy to denormalization,
- * then the first lost bit becomes a round bit, and we adjust the
- * lowest bit of sum.hi to make it a sticky bit summarizing all the
- * bits in sum.lo. With the sticky bit adjusted, the hardware will
- * break any ties in the correct direction.
- *
- * If we are losing only one bit to denormalization, however, we must
- * break the ties manually.
- */
- if (sum.lo != 0) {
- EXTRACT_WORD64(hibits, sum.hi);
- bits_lost = -((int)(hibits >> 52) & 0x7ff) - scale + 1;
- if (bits_lost != 1 ^ (int)(hibits & 1)) {
- /* hibits += (int)copysign(1.0, sum.hi * sum.lo) */
- EXTRACT_WORD64(lobits, sum.lo);
- hibits += 1 - (((hibits ^ lobits) >> 62) & 2);
- INSERT_WORD64(sum.hi, hibits);