2 * Copyright (c) 2007 Steven G. Kargl
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * FreeBSD SVN: 176720 (2008-03-02)
35 /* Return (x + ulp) for normal positive x. Assumes no overflow. */
36 static inline long double
42 if (++u.bits.manl == 0) {
43 if (++u.bits.manh == 0) {
45 u.bits.manh |= LDBL_NBIT;
51 /* Return (x - ulp) for normal positive x. Assumes no underflow. */
52 static inline long double
58 if (u.bits.manl-- == 0) {
59 if (u.bits.manh-- == LDBL_NBIT) {
61 u.bits.manh |= LDBL_NBIT;
67 #if 0 /* Ignored by gcc, emits warning */
68 #pragma STDC FENV_ACCESS ON
72 * This is slow, but simple and portable. You should use hardware sqrt
86 /* If x = NaN, then sqrt(x) = NaN. */
87 /* If x = Inf, then sqrt(x) = Inf. */
88 /* If x = -Inf, then sqrt(x) = NaN. */
89 if (u.bits.exp == LDBL_MAX_EXP * 2 - 1)
92 /* If x = +-0, then sqrt(x) = +-0. */
93 if ((u.bits.manh | u.bits.manl | u.bits.exp) == 0)
96 /* If x < 0, then raise invalid and return NaN */
98 return ((x - x) / (x - x));
102 if (u.bits.exp == 0) {
103 /* Adjust subnormal numbers. */
110 * u.e is a normal number, so break it into u.e = e*2^n where
111 * u.e = (2*e)*2^2k for odd n and u.e = (4*e)*2^2k for even n.
113 if ((u.bits.exp - 0x3ffe) & 1) { /* n is odd. */
114 k += u.bits.exp - 0x3fff; /* 2k = n - 1. */
115 u.bits.exp = 0x3fff; /* u.e in [1,2). */
117 k += u.bits.exp - 0x4000; /* 2k = n - 2. */
118 u.bits.exp = 0x4000; /* u.e in [2,4). */
122 * Newton's iteration.
123 * Split u.e into a high and low part to achieve additional precision.
125 xn = sqrt(u.e); /* 53-bit estimate of sqrtl(x). */
126 #if LDBL_MANT_DIG > 100
127 xn = (xn + (u.e / xn)) * 0.5; /* 106-bit estimate. */
130 u.bits.manl = 0; /* Zero out lower bits. */
131 lo = (lo - u.e) / xn; /* Low bits divided by xn. */
132 xn = xn + (u.e / xn); /* High portion of estimate. */
133 u.e = xn + lo; /* Combine everything. */
134 u.bits.exp += (k >> 1) - 1;
136 feclearexcept(FE_INEXACT);
138 fesetround(FE_TOWARDZERO); /* Set to round-toward-zero. */
139 xn = x / u.e; /* Chopped quotient (inexact?). */
141 if (!fetestexcept(FE_INEXACT)) { /* Quotient is exact. */
146 /* Round correctly for inputs like x = y**2 - ulp. */
147 xn = dec(xn); /* xn = xn - ulp. */
150 if (r == FE_TONEAREST) {
151 xn = inc(xn); /* xn = xn + ulp. */
152 } else if (r == FE_UPWARD) {
153 u.e = inc(u.e); /* u.e = u.e + ulp. */
154 xn = inc(xn); /* xn = xn + ulp. */
156 u.e = u.e + xn; /* Chopped sum. */
157 feupdateenv(&env); /* Restore env and raise inexact */