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