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