xref: /relibc/openlibm/src/e_sqrtl.c (revision 7e5585aaca6c7e52cf4608f26bdcac06d0f1ef6c)
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 #include "cdefs-compat.h"
28 //__FBSDID("$FreeBSD: src/lib/msun/src/e_sqrtl.c,v 1.1 2008/03/02 01:47:58 das Exp $");
29 
30 #include <float.h>
31 #include <openlibm_fenv.h>
32 #include <openlibm_math.h>
33 
34 #include "fpmath.h"
35 #include "math_private.h"
36 
37 /* Return (x + ulp) for normal positive x. Assumes no overflow. */
38 static inline long double
39 inc(long double x)
40 {
41 	union IEEEl2bits u;
42 
43 	u.e = x;
44 	if (++u.bits.manl == 0) {
45 		if (++u.bits.manh == 0) {
46 			u.bits.exp++;
47 			u.bits.manh |= LDBL_NBIT;
48 		}
49 	}
50 	return (u.e);
51 }
52 
53 /* Return (x - ulp) for normal positive x. Assumes no underflow. */
54 static inline long double
55 dec(long double x)
56 {
57 	union IEEEl2bits u;
58 
59 	u.e = x;
60 	if (u.bits.manl-- == 0) {
61 		if (u.bits.manh-- == LDBL_NBIT) {
62 			u.bits.exp--;
63 			u.bits.manh |= LDBL_NBIT;
64 		}
65 	}
66 	return (u.e);
67 }
68 
69 #ifndef __GNUC__
70 #pragma STDC FENV_ACCESS ON
71 #endif
72 
73 /*
74  * This is slow, but simple and portable. You should use hardware sqrt
75  * if possible.
76  */
77 
78 DLLEXPORT long double
79 sqrtl(long double x)
80 {
81 	union IEEEl2bits u;
82 	int k, r;
83 	long double lo, xn;
84 	fenv_t env;
85 
86 	u.e = x;
87 
88 	/* If x = NaN, then sqrt(x) = NaN. */
89 	/* If x = Inf, then sqrt(x) = Inf. */
90 	/* If x = -Inf, then sqrt(x) = NaN. */
91 	if (u.bits.exp == LDBL_MAX_EXP * 2 - 1)
92 		return (x * x + x);
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 ((x - x) / (x - x));
101 
102 	feholdexcept(&env);
103 
104 	if (u.bits.exp == 0) {
105 		/* Adjust subnormal numbers. */
106 		u.e *= 0x1.0p514;
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 - 0x3ffe) & 1) {	/* n is odd.     */
116 		k += u.bits.exp - 0x3fff;	/* 2k = n - 1.   */
117 		u.bits.exp = 0x3fff;		/* u.e in [1,2). */
118 	} else {
119 		k += u.bits.exp - 0x4000;	/* 2k = n - 2.   */
120 		u.bits.exp = 0x4000;		/* u.e in [2,4). */
121 	}
122 
123 	/*
124 	 * Newton's iteration.
125 	 * Split u.e into a high and low part to achieve additional precision.
126 	 */
127 	xn = sqrt(u.e);			/* 53-bit estimate of sqrtl(x). */
128 #if LDBL_MANT_DIG > 100
129 	xn = (xn + (u.e / xn)) * 0.5;	/* 106-bit estimate. */
130 #endif
131 	lo = u.e;
132 	u.bits.manl = 0;		/* Zero out lower bits. */
133 	lo = (lo - u.e) / xn;		/* Low bits divided by xn. */
134 	xn = xn + (u.e / xn);		/* High portion of estimate. */
135 	u.e = xn + lo;			/* Combine everything. */
136 	u.bits.exp += (k >> 1) - 1;
137 
138 	feclearexcept(FE_INEXACT);
139 	r = fegetround();
140 	fesetround(FE_TOWARDZERO);	/* Set to round-toward-zero. */
141 	xn = x / u.e;			/* Chopped quotient (inexact?). */
142 
143 	if (!fetestexcept(FE_INEXACT)) { /* Quotient is exact. */
144 		if (xn == u.e) {
145 			fesetenv(&env);
146 			return (u.e);
147 		}
148 		/* Round correctly for inputs like x = y**2 - ulp. */
149 		xn = dec(xn);		/* xn = xn - ulp. */
150 	}
151 
152 	if (r == FE_TONEAREST) {
153 		xn = inc(xn);		/* xn = xn + ulp. */
154 	} else if (r == FE_UPWARD) {
155 		u.e = inc(u.e);		/* u.e = u.e + ulp. */
156 		xn = inc(xn);		/* xn  = xn + ulp. */
157 	}
158 	u.e = u.e + xn;				/* Chopped sum. */
159 	feupdateenv(&env);	/* Restore env and raise inexact */
160 	u.bits.exp--;
161 	return (u.e);
162 }
163