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