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