xref: /minix3/lib/libm/src/e_sqrtl.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*-
2*84d9c625SLionel Sambuc  * Copyright (c) 2007 Steven G. Kargl
3*84d9c625SLionel Sambuc  * All rights reserved.
4*84d9c625SLionel Sambuc  *
5*84d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
6*84d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
7*84d9c625SLionel Sambuc  * are met:
8*84d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
9*84d9c625SLionel Sambuc  *    notice unmodified, this list of conditions, and the following
10*84d9c625SLionel Sambuc  *    disclaimer.
11*84d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
12*84d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
13*84d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
14*84d9c625SLionel Sambuc  *
15*84d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*84d9c625SLionel Sambuc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*84d9c625SLionel Sambuc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*84d9c625SLionel Sambuc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*84d9c625SLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*84d9c625SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*84d9c625SLionel Sambuc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*84d9c625SLionel Sambuc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*84d9c625SLionel Sambuc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*84d9c625SLionel Sambuc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*84d9c625SLionel Sambuc  */
26*84d9c625SLionel Sambuc 
27*84d9c625SLionel Sambuc #include <sys/cdefs.h>
28*84d9c625SLionel Sambuc #if 0
29*84d9c625SLionel Sambuc __FBSDID("$FreeBSD: head/lib/msun/src/e_sqrtl.c 176720 2008-03-02 01:47:58Z das $");
30*84d9c625SLionel Sambuc #endif
31*84d9c625SLionel Sambuc __RCSID("$NetBSD: e_sqrtl.c,v 1.4 2013/11/22 20:15:06 martin Exp $");
32*84d9c625SLionel Sambuc 
33*84d9c625SLionel Sambuc #include <machine/ieee.h>
34*84d9c625SLionel Sambuc #include <float.h>
35*84d9c625SLionel Sambuc 
36*84d9c625SLionel Sambuc #include "math.h"
37*84d9c625SLionel Sambuc #include "math_private.h"
38*84d9c625SLionel Sambuc 
39*84d9c625SLionel Sambuc #ifdef __HAVE_LONG_DOUBLE
40*84d9c625SLionel Sambuc 
41*84d9c625SLionel Sambuc #ifdef HAVE_FENV_H
42*84d9c625SLionel Sambuc #include <fenv.h>
43*84d9c625SLionel Sambuc #endif
44*84d9c625SLionel Sambuc 
45*84d9c625SLionel Sambuc #ifdef LDBL_IMPLICIT_NBIT
46*84d9c625SLionel Sambuc #define	LDBL_NBIT	0
47*84d9c625SLionel Sambuc #endif
48*84d9c625SLionel Sambuc 
49*84d9c625SLionel Sambuc #ifdef HAVE_FENV_H
50*84d9c625SLionel Sambuc 
51*84d9c625SLionel Sambuc /* Return (x + ulp) for normal positive x. Assumes no overflow. */
52*84d9c625SLionel Sambuc static inline long double
inc(long double x)53*84d9c625SLionel Sambuc inc(long double x)
54*84d9c625SLionel Sambuc {
55*84d9c625SLionel Sambuc 	union ieee_ext_u ux = { .extu_ld = x, };
56*84d9c625SLionel Sambuc 
57*84d9c625SLionel Sambuc 	if (++ux.extu_fracl == 0) {
58*84d9c625SLionel Sambuc 		if (++ux.extu_frach == 0) {
59*84d9c625SLionel Sambuc 			ux.extu_exp++;
60*84d9c625SLionel Sambuc 			ux.extu_frach |= LDBL_NBIT;
61*84d9c625SLionel Sambuc 		}
62*84d9c625SLionel Sambuc 	}
63*84d9c625SLionel Sambuc 	return (ux.extu_ld);
64*84d9c625SLionel Sambuc }
65*84d9c625SLionel Sambuc 
66*84d9c625SLionel Sambuc /* Return (x - ulp) for normal positive x. Assumes no underflow. */
67*84d9c625SLionel Sambuc static inline long double
dec(long double x)68*84d9c625SLionel Sambuc dec(long double x)
69*84d9c625SLionel Sambuc {
70*84d9c625SLionel Sambuc 	union ieee_ext_u ux = { .extu_ld = x, };
71*84d9c625SLionel Sambuc 
72*84d9c625SLionel Sambuc 	if (ux.extu_fracl-- == 0) {
73*84d9c625SLionel Sambuc 		if (ux.extu_frach-- == LDBL_NBIT) {
74*84d9c625SLionel Sambuc 			ux.extu_exp--;
75*84d9c625SLionel Sambuc 			ux.extu_frach |= LDBL_NBIT;
76*84d9c625SLionel Sambuc 		}
77*84d9c625SLionel Sambuc 	}
78*84d9c625SLionel Sambuc 	return (ux.extu_ld);
79*84d9c625SLionel Sambuc }
80*84d9c625SLionel Sambuc 
81*84d9c625SLionel Sambuc /*
82*84d9c625SLionel Sambuc  * This is slow, but simple and portable. You should use hardware sqrt
83*84d9c625SLionel Sambuc  * if possible.
84*84d9c625SLionel Sambuc  */
85*84d9c625SLionel Sambuc 
86*84d9c625SLionel Sambuc long double
__ieee754_sqrtl(long double x)87*84d9c625SLionel Sambuc __ieee754_sqrtl(long double x)
88*84d9c625SLionel Sambuc {
89*84d9c625SLionel Sambuc 	union ieee_ext_u ux = { .extu_ld = x, };
90*84d9c625SLionel Sambuc 	int k, r;
91*84d9c625SLionel Sambuc 	long double lo, xn;
92*84d9c625SLionel Sambuc 	fenv_t env;
93*84d9c625SLionel Sambuc 
94*84d9c625SLionel Sambuc 	/* If x = NaN, then sqrt(x) = NaN. */
95*84d9c625SLionel Sambuc 	/* If x = Inf, then sqrt(x) = Inf. */
96*84d9c625SLionel Sambuc 	/* If x = -Inf, then sqrt(x) = NaN. */
97*84d9c625SLionel Sambuc 	if (ux.extu_exp == LDBL_MAX_EXP * 2 - 1)
98*84d9c625SLionel Sambuc 		return (x * x + x);
99*84d9c625SLionel Sambuc 
100*84d9c625SLionel Sambuc 	/* If x = +-0, then sqrt(x) = +-0. */
101*84d9c625SLionel Sambuc 	if ((ux.extu_frach | ux.extu_fracl | ux.extu_exp) == 0)
102*84d9c625SLionel Sambuc 		return (x);
103*84d9c625SLionel Sambuc 
104*84d9c625SLionel Sambuc 	/* If x < 0, then raise invalid and return NaN */
105*84d9c625SLionel Sambuc 	if (ux.extu_sign)
106*84d9c625SLionel Sambuc 		return ((x - x) / (x - x));
107*84d9c625SLionel Sambuc 
108*84d9c625SLionel Sambuc 	feholdexcept(&env);
109*84d9c625SLionel Sambuc 
110*84d9c625SLionel Sambuc 	if (ux.extu_exp == 0) {
111*84d9c625SLionel Sambuc 		/* Adjust subnormal numbers. */
112*84d9c625SLionel Sambuc 		ux.extu_ld *= 0x1.0p514;
113*84d9c625SLionel Sambuc 		k = -514;
114*84d9c625SLionel Sambuc 	} else {
115*84d9c625SLionel Sambuc 		k = 0;
116*84d9c625SLionel Sambuc 	}
117*84d9c625SLionel Sambuc 	/*
118*84d9c625SLionel Sambuc 	 * ux.extu_ld is a normal number, so break it into ux.extu_ld = e*2^n where
119*84d9c625SLionel Sambuc 	 * ux.extu_ld = (2*e)*2^2k for odd n and ux.extu_ld = (4*e)*2^2k for even n.
120*84d9c625SLionel Sambuc 	 */
121*84d9c625SLionel Sambuc 	if ((ux.extu_exp - EXT_EXP_BIAS) & 1) {	/* n is even.     */
122*84d9c625SLionel Sambuc 		k += ux.extu_exp - EXT_EXP_BIAS - 1; /* 2k = n - 2.   */
123*84d9c625SLionel Sambuc 		ux.extu_exp = EXT_EXP_BIAS + 1;	/* ux.extu_ld in [2,4). */
124*84d9c625SLionel Sambuc 	} else {
125*84d9c625SLionel Sambuc 		k += ux.extu_exp - EXT_EXP_BIAS;	/* 2k = n - 1.   */
126*84d9c625SLionel Sambuc 		ux.extu_exp = EXT_EXP_BIAS;	/* ux.extu_ld in [1,2). */
127*84d9c625SLionel Sambuc 	}
128*84d9c625SLionel Sambuc 
129*84d9c625SLionel Sambuc 	/*
130*84d9c625SLionel Sambuc 	 * Newton's iteration.
131*84d9c625SLionel Sambuc 	 * Split ux.extu_ld into a high and low part to achieve additional precision.
132*84d9c625SLionel Sambuc 	 */
133*84d9c625SLionel Sambuc 	xn = sqrt(ux.extu_ld);			/* 53-bit estimate of sqrtl(x). */
134*84d9c625SLionel Sambuc #if LDBL_MANT_DIG > 100
135*84d9c625SLionel Sambuc 	xn = (xn + (ux.extu_ld / xn)) * 0.5;	/* 106-bit estimate. */
136*84d9c625SLionel Sambuc #endif
137*84d9c625SLionel Sambuc 	lo = ux.extu_ld;
138*84d9c625SLionel Sambuc 	ux.extu_fracl = 0;		/* Zero out lower bits. */
139*84d9c625SLionel Sambuc 	lo = (lo - ux.extu_ld) / xn;	/* Low bits divided by xn. */
140*84d9c625SLionel Sambuc 	xn = xn + (ux.extu_ld / xn);	/* High portion of estimate. */
141*84d9c625SLionel Sambuc 	ux.extu_ld = xn + lo;		/* Combine everything. */
142*84d9c625SLionel Sambuc 	ux.extu_exp += (k >> 1) - 1;
143*84d9c625SLionel Sambuc 
144*84d9c625SLionel Sambuc 	feclearexcept(FE_INEXACT);
145*84d9c625SLionel Sambuc 	r = fegetround();
146*84d9c625SLionel Sambuc 	fesetround(FE_TOWARDZERO);	/* Set to round-toward-zero. */
147*84d9c625SLionel Sambuc 	xn = x / ux.extu_ld;		/* Chopped quotient (inexact?). */
148*84d9c625SLionel Sambuc 
149*84d9c625SLionel Sambuc 	if (!fetestexcept(FE_INEXACT)) { /* Quotient is exact. */
150*84d9c625SLionel Sambuc 		if (xn == ux.extu_ld) {
151*84d9c625SLionel Sambuc 			fesetenv(&env);
152*84d9c625SLionel Sambuc 			return (ux.extu_ld);
153*84d9c625SLionel Sambuc 		}
154*84d9c625SLionel Sambuc 		/* Round correctly for inputs like x = y**2 - ulp. */
155*84d9c625SLionel Sambuc 		xn = dec(xn);		/* xn = xn - ulp. */
156*84d9c625SLionel Sambuc 	}
157*84d9c625SLionel Sambuc 
158*84d9c625SLionel Sambuc 	if (r == FE_TONEAREST) {
159*84d9c625SLionel Sambuc 		xn = inc(xn);		/* xn = xn + ulp. */
160*84d9c625SLionel Sambuc 	} else if (r == FE_UPWARD) {
161*84d9c625SLionel Sambuc 		ux.extu_ld = inc(ux.extu_ld);	/* ux.extu_ld = ux.extu_ld + ulp. */
162*84d9c625SLionel Sambuc 		xn = inc(xn);		/* xn  = xn + ulp. */
163*84d9c625SLionel Sambuc 	}
164*84d9c625SLionel Sambuc 	ux.extu_ld = ux.extu_ld + xn;		/* Chopped sum. */
165*84d9c625SLionel Sambuc 	feupdateenv(&env);	/* Restore env and raise inexact */
166*84d9c625SLionel Sambuc 	ux.extu_exp--;
167*84d9c625SLionel Sambuc 	return (ux.extu_ld);
168*84d9c625SLionel Sambuc }
169*84d9c625SLionel Sambuc 
170*84d9c625SLionel Sambuc #else
171*84d9c625SLionel Sambuc 
172*84d9c625SLionel Sambuc /*
173*84d9c625SLionel Sambuc  * No fenv support:
174*84d9c625SLionel Sambuc  * poor man's version: just use double
175*84d9c625SLionel Sambuc  */
176*84d9c625SLionel Sambuc long double
__ieee754_sqrtl(long double x)177*84d9c625SLionel Sambuc __ieee754_sqrtl(long double x)
178*84d9c625SLionel Sambuc {
179*84d9c625SLionel Sambuc 	return __ieee754_sqrt((double)x);
180*84d9c625SLionel Sambuc }
181*84d9c625SLionel Sambuc 
182*84d9c625SLionel Sambuc #endif
183*84d9c625SLionel Sambuc 
184*84d9c625SLionel Sambuc #endif
185