1 /*-
2  * Copyright (c) 2007 Steven G. Kargl
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  */
26 
27 
28 #ifndef _DOUBLE_DOUBLE_FLOAT
29 /* Return (x + ulp) for normal positive x. Assumes no overflow. */
30 static inline long double
inc(long double x)31 inc(long double x)
32 {
33 	union IEEEl2bits u;
34 
35 	u.e = x;
36 	if (++u.bits.manl == 0) {
37 		if (++u.bits.manh == 0) {
38 			u.bits.exp++;
39 			u.bits.manh |= LDBL_NBIT;
40 		}
41 	}
42 	return (u.e);
43 }
44 
45 /* Return (x - ulp) for normal positive x. Assumes no underflow. */
46 static inline long double
dec(long double x)47 dec(long double x)
48 {
49 	union IEEEl2bits u;
50 
51 	u.e = x;
52 	if (u.bits.manl-- == 0) {
53 		if (u.bits.manh-- == LDBL_NBIT) {
54 			u.bits.exp--;
55 			u.bits.manh |= LDBL_NBIT;
56 		}
57 	}
58 	return (u.e);
59 }
60 
61 #ifndef __GNUC__
62 #pragma STDC FENV_ACCESS ON
63 #endif
64 
65 #endif
66 
67 /*
68  * This is slow, but simple and portable. You should use hardware sqrt
69  * if possible.
70  */
71 
72 #define BIAS    (LDBL_MAX_EXP-1)
73 
74 long double
sqrtl(long double x)75 sqrtl(long double x)
76 {
77 	union IEEEl2bits u;
78 	int k;
79 	long double lo, xn;
80 	fenv_t env;
81 
82 	u.e = x;
83 
84 	/* If x = NaN, then sqrt(x) = qNaN. */
85 	/* If x = Inf, then sqrt(x) = Inf. */
86 	/* If x = -Inf, then sqrt(x) = sNaN. */
87 	if (u.bits.exp == LDBL_INF_NAN_EXP) {
88                 if (u.bits.sign && u.bits.manh == LDBL_NBIT_INF && u.bits.manl == 0)
89                         return __math_invalidl(x);
90                 return x + x;
91         }
92 
93 	/* If x = +-0, then sqrt(x) = +-0. */
94 	if ((u.bits.manh | u.bits.manl | u.bits.exp) == 0)
95 		return (x);
96 
97 	/* If x < 0, then raise invalid and return NaN */
98 	if (u.bits.sign)
99                 return __math_invalidl(x);
100 
101 	feholdexcept(&env);
102 
103 	if (u.bits.exp == 0) {
104 		/* Adjust subnormal numbers. */
105 		u.e *= 0x1.0p514L;
106 		k = -514;
107 	} else {
108 		k = 0;
109 	}
110 	/*
111 	 * u.e is a normal number, so break it into u.e = e*2^n where
112 	 * u.e = (2*e)*2^2k for odd n and u.e = (4*e)*2^2k for even n.
113 	 */
114 	if ((u.bits.exp - (BIAS-1)) & 1) {	/* n is odd.     */
115 		k += u.bits.exp - BIAS;	/* 2k = n - 1.   */
116 #ifdef _DOUBLE_DOUBLE_FLOAT
117                 u.dbits.dl = scalbn(u.dbits.dl, BIAS - u.bits.exp);
118 #endif
119 		u.bits.exp = BIAS;		/* u.e in [1,2). */
120 	} else {
121                 k += u.bits.exp - (BIAS + 1);	/* 2k = n - 2.   */
122 #ifdef _DOUBLE_DOUBLE_FLOAT
123                 u.dbits.dl = scalbn(u.dbits.dl, (BIAS + 1) - u.bits.exp);
124 #endif
125 		u.bits.exp = (BIAS + 1);	/* u.e in [2,4). */
126 	}
127 
128 	/*
129 	 * Newton's iteration.
130 	 * Split u.e into a high and low part to achieve additional precision.
131 	 */
132 	xn = (long double)sqrt((double)u.e);			/* 53-bit estimate of sqrtl(x). */
133 	xn = (xn + (u.e / xn)) * 0.5L;	/* 106-bit estimate. */
134 
135 	lo = u.e;
136 #ifdef _DOUBLE_DOUBLE_FLOAT
137         u.dbits.dl = 0.0;               /* Zero out lower double */
138 #else
139 	u.bits.manl = 0;		/* Zero out lower bits. */
140 #endif
141 	lo = (lo - u.e) / xn;		/* Low bits divided by xn. */
142 	xn = xn + (u.e / xn);		/* High portion of estimate. */
143 	u.e = xn + lo;			/* Combine everything. */
144 
145 	u.bits.exp += (k >> 1) - 1;
146 #ifdef _DOUBLE_DOUBLE_FLOAT
147         u.dbits.dl = scalbn(u.dbits.dl, (k>>1) -1);
148 #endif
149 
150 #if defined(FE_INEXACT) && defined(FE_TOWARDZERO) && defined(FE_TONEAREST) && defined(FE_UPWARD) && !defined(_DOUBLE_DOUBLE_FLOAT)
151         {
152                 int r;
153                 feclearexcept(FE_INEXACT);
154                 r = fegetround();
155                 fesetround(FE_TOWARDZERO);	/* Set to round-toward-zero. */
156                 xn = x / u.e;			/* Chopped quotient (inexact?). */
157 
158                 if (!fetestexcept(FE_INEXACT)) { /* Quotient is exact. */
159                         if (xn == u.e) {
160                                 fesetenv(&env);
161                                 return (u.e);
162                         }
163                         /* Round correctly for inputs like x = y**2 - ulp. */
164                         xn = dec(xn);		/* xn = xn - ulp. */
165                 }
166 
167                 if (r == FE_TONEAREST) {
168                         xn = inc(xn);		/* xn = xn + ulp. */
169                 } else if (r == FE_UPWARD) {
170                         u.e = inc(u.e);		/* u.e = u.e + ulp. */
171                         xn = inc(xn);		/* xn  = xn + ulp. */
172                 }
173                 u.e = u.e + xn;				/* Chopped sum. */
174                 feupdateenv(&env);	/* Restore env and raise inexact */
175                 u.bits.exp--;
176         }
177 #endif
178 	return (u.e);
179 }
180