2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2007 Steven G. Kargl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 #pragma STDC FENV_ACCESS ON
70 * This is slow, but simple and portable. You should use hardware sqrt
84 /* If x = NaN, then sqrt(x) = NaN. */
85 /* If x = Inf, then sqrt(x) = Inf. */
86 /* If x = -Inf, then sqrt(x) = NaN. */
87 if (u.bits.exp == LDBL_MAX_EXP * 2 - 1)
90 /* If x = +-0, then sqrt(x) = +-0. */
91 if ((u.bits.manh | u.bits.manl | u.bits.exp) == 0)
94 /* If x < 0, then raise invalid and return NaN */
96 return ((x - x) / (x - x));
100 if (u.bits.exp == 0) {
101 /* Adjust subnormal numbers. */
108 * u.e is a normal number, so break it into u.e = e*2^n where
109 * u.e = (2*e)*2^2k for odd n and u.e = (4*e)*2^2k for even n.
111 if ((u.bits.exp - 0x3ffe) & 1) { /* n is odd. */
112 k += u.bits.exp - 0x3fff; /* 2k = n - 1. */
113 u.bits.exp = 0x3fff; /* u.e in [1,2). */
115 k += u.bits.exp - 0x4000; /* 2k = n - 2. */
116 u.bits.exp = 0x4000; /* u.e in [2,4). */
120 * Newton's iteration.
121 * Split u.e into a high and low part to achieve additional precision.
123 xn = sqrt(u.e); /* 53-bit estimate of sqrtl(x). */
124 #if LDBL_MANT_DIG > 100
125 xn = (xn + (u.e / xn)) * 0.5; /* 106-bit estimate. */
128 u.bits.manl = 0; /* Zero out lower bits. */
129 lo = (lo - u.e) / xn; /* Low bits divided by xn. */
130 xn = xn + (u.e / xn); /* High portion of estimate. */
131 u.e = xn + lo; /* Combine everything. */
132 u.bits.exp += (k >> 1) - 1;
134 feclearexcept(FE_INEXACT);
136 fesetround(FE_TOWARDZERO); /* Set to round-toward-zero. */
137 xn = x / u.e; /* Chopped quotient (inexact?). */
139 if (!fetestexcept(FE_INEXACT)) { /* Quotient is exact. */
144 /* Round correctly for inputs like x = y**2 - ulp. */
145 xn = dec(xn); /* xn = xn - ulp. */
148 if (r == FE_TONEAREST) {
149 xn = inc(xn); /* xn = xn + ulp. */
150 } else if (r == FE_UPWARD) {
151 u.e = inc(u.e); /* u.e = u.e + ulp. */
152 xn = inc(xn); /* xn = xn + ulp. */
154 u.e = u.e + xn; /* Chopped sum. */
155 feupdateenv(&env); /* Restore env and raise inexact */