xref: /openbsd-src/lib/libm/src/e_sqrtl.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
1*2f2c0062Sguenther /*	$OpenBSD: e_sqrtl.c,v 1.3 2016/09/12 19:47:02 guenther Exp $	*/
2390c8400Smartynas /*-
3390c8400Smartynas  * Copyright (c) 2007 Steven G. Kargl
4390c8400Smartynas  * All rights reserved.
5390c8400Smartynas  *
6390c8400Smartynas  * Redistribution and use in source and binary forms, with or without
7390c8400Smartynas  * modification, are permitted provided that the following conditions
8390c8400Smartynas  * are met:
9390c8400Smartynas  * 1. Redistributions of source code must retain the above copyright
10390c8400Smartynas  *    notice unmodified, this list of conditions, and the following
11390c8400Smartynas  *    disclaimer.
12390c8400Smartynas  * 2. Redistributions in binary form must reproduce the above copyright
13390c8400Smartynas  *    notice, this list of conditions and the following disclaimer in the
14390c8400Smartynas  *    documentation and/or other materials provided with the distribution.
15390c8400Smartynas  *
16390c8400Smartynas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17390c8400Smartynas  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18390c8400Smartynas  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19390c8400Smartynas  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20390c8400Smartynas  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21390c8400Smartynas  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22390c8400Smartynas  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23390c8400Smartynas  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24390c8400Smartynas  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25390c8400Smartynas  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26390c8400Smartynas  */
27390c8400Smartynas 
28390c8400Smartynas #include <sys/types.h>
29e65fc0a0Sguenther #include <machine/ieee.h>	/* for struct ieee_ext */
30e65fc0a0Sguenther #include <fenv.h>
31390c8400Smartynas #include <float.h>
32390c8400Smartynas #include <math.h>
33390c8400Smartynas 
34390c8400Smartynas #ifdef EXT_IMPLICIT_NBIT
35390c8400Smartynas #define	LDBL_NBIT	0
36390c8400Smartynas #else /* EXT_IMPLICIT_NBIT */
37390c8400Smartynas #define	LDBL_NBIT	0x80000000
38390c8400Smartynas #endif /* EXT_IMPLICIT_NBIT */
39390c8400Smartynas 
40390c8400Smartynas /* Return (x + ulp) for normal positive x. Assumes no overflow. */
41390c8400Smartynas static inline long double
inc(long double x)42390c8400Smartynas inc(long double x)
43390c8400Smartynas {
44390c8400Smartynas 	struct ieee_ext *p = (struct ieee_ext *)&x;
45390c8400Smartynas 
46390c8400Smartynas #ifdef EXT_FRACHMBITS
47390c8400Smartynas 	uint64_t frach;
48390c8400Smartynas 
49390c8400Smartynas 	frach = ((uint64_t)p->ext_frach << EXT_FRACHMBITS) | p->ext_frachm;
50390c8400Smartynas 	frach++;
51390c8400Smartynas 	p->ext_frach = frach >> EXT_FRACHMBITS;
52390c8400Smartynas 	p->ext_frachm = frach & 0x00000000ffffffff;
53390c8400Smartynas #else /* EXT_FRACHMBITS */
54390c8400Smartynas 	uint32_t frach;
55390c8400Smartynas 
56390c8400Smartynas 	p->ext_frach++;
57390c8400Smartynas 	frach = p->ext_frach;
58390c8400Smartynas #endif /* EXT_FRACHMBITS */
59390c8400Smartynas 
60390c8400Smartynas 	if (frach == 0) {
61390c8400Smartynas 
62390c8400Smartynas #ifdef EXT_FRACLMBITS
63390c8400Smartynas 		uint64_t fracl;
64390c8400Smartynas 
65390c8400Smartynas 		fracl = ((uint64_t)p->ext_fraclm << EXT_FRACLBITS) |
66390c8400Smartynas 			p->ext_fracl;
67390c8400Smartynas 		fracl++;
68390c8400Smartynas 		p->ext_fraclm = fracl >> EXT_FRACLBITS;
69390c8400Smartynas 		p->ext_fracl = fracl & 0x00000000ffffffff;
70390c8400Smartynas #else /* EXT_FRACLMBITS */
71390c8400Smartynas 		uint32_t fracl;
72390c8400Smartynas 
73390c8400Smartynas 		p->ext_fracl++;
74390c8400Smartynas 		fracl = p->ext_fracl;
75390c8400Smartynas #endif /* EXT_FRACLMBITS */
76390c8400Smartynas 
77390c8400Smartynas 		if (fracl == 0) {
78390c8400Smartynas 			p->ext_exp++;
79390c8400Smartynas 			p->ext_frach |= LDBL_NBIT;
80390c8400Smartynas 		}
81390c8400Smartynas 	}
82390c8400Smartynas 
83390c8400Smartynas 	return x;
84390c8400Smartynas }
85390c8400Smartynas 
86390c8400Smartynas /* Return (x - ulp) for normal positive x. Assumes no underflow. */
87390c8400Smartynas static inline long double
dec(long double x)88390c8400Smartynas dec(long double x)
89390c8400Smartynas {
90390c8400Smartynas 	struct ieee_ext *p = (struct ieee_ext *)&x;
91390c8400Smartynas 
92390c8400Smartynas #ifdef EXT_FRACLMBITS
93390c8400Smartynas 	uint64_t fracl;
94390c8400Smartynas 
95390c8400Smartynas 	fracl = ((uint64_t)p->ext_fraclm << EXT_FRACLBITS) | p->ext_fracl;
96390c8400Smartynas 	fracl--;
97390c8400Smartynas 	p->ext_fraclm = fracl >> EXT_FRACLBITS;
98390c8400Smartynas 	p->ext_fracl = fracl & 0x00000000ffffffff;
99390c8400Smartynas #else /* EXT_FRACLMBITS */
100390c8400Smartynas 	uint32_t fracl;
101390c8400Smartynas 
102390c8400Smartynas 	p->ext_fracl--;
103390c8400Smartynas 	fracl = p->ext_fracl;
104390c8400Smartynas #endif /* EXT_FRACLMBITS */
105390c8400Smartynas 
106390c8400Smartynas 	if (fracl == 0) {
107390c8400Smartynas 
108390c8400Smartynas #ifdef EXT_FRACHMBITS
109390c8400Smartynas 		uint64_t frach;
110390c8400Smartynas 
111390c8400Smartynas 		frach = ((uint64_t)p->ext_frach << EXT_FRACHMBITS) |
112390c8400Smartynas 			p->ext_frachm;
113390c8400Smartynas 		frach--;
114390c8400Smartynas 		p->ext_frach = frach >> EXT_FRACHMBITS;
115390c8400Smartynas 		p->ext_frachm = frach & 0x00000000ffffffff;
116390c8400Smartynas #else /* EXT_FRACHMBITS */
117390c8400Smartynas 		uint32_t frach;
118390c8400Smartynas 
119390c8400Smartynas 		p->ext_frach--;
120390c8400Smartynas 		frach = p->ext_frach;
121390c8400Smartynas #endif /* EXT_FRACHMBITS */
122390c8400Smartynas 
123390c8400Smartynas 		if (frach == LDBL_NBIT) {
124390c8400Smartynas 			p->ext_exp--;
125390c8400Smartynas 			p->ext_frach |= LDBL_NBIT;
126390c8400Smartynas 		}
127390c8400Smartynas 	}
128390c8400Smartynas 
129390c8400Smartynas 	return x;
130390c8400Smartynas }
131390c8400Smartynas 
132390c8400Smartynas /*
133390c8400Smartynas  * This is slow, but simple and portable. You should use hardware sqrt
134390c8400Smartynas  * if possible.
135390c8400Smartynas  */
136390c8400Smartynas 
137390c8400Smartynas long double
sqrtl(long double x)138390c8400Smartynas sqrtl(long double x)
139390c8400Smartynas {
140390c8400Smartynas 	union {
141390c8400Smartynas 		long double e;
142390c8400Smartynas 		struct ieee_ext bits;
143390c8400Smartynas 	} u;
144390c8400Smartynas 	int k, r;
145390c8400Smartynas 	long double lo, xn;
146390c8400Smartynas 
147390c8400Smartynas 	u.e = x;
148390c8400Smartynas 
149390c8400Smartynas 	/* If x = NaN, then sqrt(x) = NaN. */
150390c8400Smartynas 	/* If x = Inf, then sqrt(x) = Inf. */
151390c8400Smartynas 	/* If x = -Inf, then sqrt(x) = NaN. */
152390c8400Smartynas 	if (u.bits.ext_exp == LDBL_MAX_EXP * 2 - 1)
153390c8400Smartynas 		return (x * x + x);
154390c8400Smartynas 
155390c8400Smartynas 	/* If x = +-0, then sqrt(x) = +-0. */
156390c8400Smartynas 	if ((u.bits.ext_frach
157390c8400Smartynas #ifdef EXT_FRACHMBITS
158390c8400Smartynas 		| u.bits.ext_frachm
159390c8400Smartynas #endif /* EXT_FRACHMBITS */
160390c8400Smartynas #ifdef EXT_FRACLMBITS
161390c8400Smartynas 		| u.bits.ext_fraclm
162390c8400Smartynas #endif /* EXT_FRACLMBITS */
163390c8400Smartynas 		| u.bits.ext_fracl | u.bits.ext_exp) == 0)
164390c8400Smartynas 		return (x);
165390c8400Smartynas 
166390c8400Smartynas 	/* If x < 0, then raise invalid and return NaN */
167390c8400Smartynas 	if (u.bits.ext_sign)
168390c8400Smartynas 		return ((x - x) / (x - x));
169390c8400Smartynas 
170390c8400Smartynas 	if (u.bits.ext_exp == 0) {
171390c8400Smartynas 		/* Adjust subnormal numbers. */
172390c8400Smartynas 		u.e *= 0x1.0p514;
173390c8400Smartynas 		k = -514;
174390c8400Smartynas 	} else {
175390c8400Smartynas 		k = 0;
176390c8400Smartynas 	}
177390c8400Smartynas 	/*
178390c8400Smartynas 	 * u.e is a normal number, so break it into u.e = e*2^n where
179390c8400Smartynas 	 * u.e = (2*e)*2^2k for odd n and u.e = (4*e)*2^2k for even n.
180390c8400Smartynas 	 */
181390c8400Smartynas 	if ((u.bits.ext_exp - 0x3ffe) & 1) {	/* n is odd.     */
182390c8400Smartynas 		k += u.bits.ext_exp - 0x3fff;	/* 2k = n - 1.   */
183390c8400Smartynas 		u.bits.ext_exp = 0x3fff;	/* u.e in [1,2). */
184390c8400Smartynas 	} else {
185390c8400Smartynas 		k += u.bits.ext_exp - 0x4000;	/* 2k = n - 2.   */
186390c8400Smartynas 		u.bits.ext_exp = 0x4000;	/* u.e in [2,4). */
187390c8400Smartynas 	}
188390c8400Smartynas 
189390c8400Smartynas 	/*
190390c8400Smartynas 	 * Newton's iteration.
191390c8400Smartynas 	 * Split u.e into a high and low part to achieve additional precision.
192390c8400Smartynas 	 */
193390c8400Smartynas 	xn = sqrt(u.e);			/* 53-bit estimate of sqrtl(x). */
194390c8400Smartynas #if LDBL_MANT_DIG > 100
195390c8400Smartynas 	xn = (xn + (u.e / xn)) * 0.5;	/* 106-bit estimate. */
196390c8400Smartynas #endif
197390c8400Smartynas 	lo = u.e;
198390c8400Smartynas 	u.bits.ext_fracl = 0;		/* Zero out lower bits. */
199390c8400Smartynas #ifdef EXT_FRACLMBITS
200390c8400Smartynas 	u.bits.ext_fraclm = 0;
201390c8400Smartynas #endif /* EXT_FRACLMBITS */
202390c8400Smartynas 	lo = (lo - u.e) / xn;		/* Low bits divided by xn. */
203390c8400Smartynas 	xn = xn + (u.e / xn);		/* High portion of estimate. */
204390c8400Smartynas 	u.e = xn + lo;			/* Combine everything. */
205390c8400Smartynas 	u.bits.ext_exp += (k >> 1) - 1;
206390c8400Smartynas 
207e65fc0a0Sguenther 	feclearexcept(FE_INEXACT);
208e65fc0a0Sguenther 	r = fegetround();
209e65fc0a0Sguenther 	fesetround(FE_TOWARDZERO);	/* Set to round-toward-zero. */
210390c8400Smartynas 	xn = x / u.e;			/* Chopped quotient (inexact?). */
211390c8400Smartynas 
212e65fc0a0Sguenther 	if (!fetestexcept(FE_INEXACT)) { /* Quotient is exact. */
213390c8400Smartynas 		if (xn == u.e) {
214e65fc0a0Sguenther 			fesetround(r);
215390c8400Smartynas 			return (u.e);
216390c8400Smartynas 		}
217390c8400Smartynas 		/* Round correctly for inputs like x = y**2 - ulp. */
218390c8400Smartynas 		xn = dec(xn);		/* xn = xn - ulp. */
219390c8400Smartynas 	}
220390c8400Smartynas 
221e65fc0a0Sguenther 	if (r == FE_TONEAREST) {
222390c8400Smartynas 		xn = inc(xn);		/* xn = xn + ulp. */
223e65fc0a0Sguenther 	} else if (r == FE_UPWARD) {
224390c8400Smartynas 		u.e = inc(u.e);		/* u.e = u.e + ulp. */
225390c8400Smartynas 		xn = inc(xn);		/* xn  = xn + ulp. */
226390c8400Smartynas 	}
227390c8400Smartynas 	u.e = u.e + xn;			/* Chopped sum. */
228e65fc0a0Sguenther 	fesetround(r);			/* Restore env and raise inexact */
229390c8400Smartynas 	u.bits.ext_exp--;
230390c8400Smartynas 	return (u.e);
231390c8400Smartynas }
232*2f2c0062Sguenther DEF_STD(sqrtl);
233