<iso646.h>: Avoid conflicts w/ C++ keywords.
[dragonfly.git] / lib / libm / src / e_sqrtl.c
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  * FreeBSD SVN: 176720 (2008-03-02)
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 #if 0  /* Ignored by gcc, emits warning */
68 #pragma STDC FENV_ACCESS ON
69 #endif
70
71 /*
72  * This is slow, but simple and portable. You should use hardware sqrt
73  * if possible.
74  */
75
76 long double
77 sqrtl(long double x)
78 {
79         union IEEEl2bits u;
80         int k, r;
81         long double lo, xn;
82         fenv_t env;
83
84         u.e = x;
85
86         /* If x = NaN, then sqrt(x) = NaN. */
87         /* If x = Inf, then sqrt(x) = Inf. */
88         /* If x = -Inf, then sqrt(x) = NaN. */
89         if (u.bits.exp == LDBL_MAX_EXP * 2 - 1)
90                 return (x * x + x);
91
92         /* If x = +-0, then sqrt(x) = +-0. */
93         if ((u.bits.manh | u.bits.manl | u.bits.exp) == 0)
94                 return (x);
95
96         /* If x < 0, then raise invalid and return NaN */
97         if (u.bits.sign)
98                 return ((x - x) / (x - x));
99
100         feholdexcept(&env);
101
102         if (u.bits.exp == 0) {
103                 /* Adjust subnormal numbers. */
104                 u.e *= 0x1.0p514;
105                 k = -514;
106         } else {
107                 k = 0;
108         }
109         /*
110          * u.e is a normal number, so break it into u.e = e*2^n where
111          * u.e = (2*e)*2^2k for odd n and u.e = (4*e)*2^2k for even n.
112          */
113         if ((u.bits.exp - 0x3ffe) & 1) {        /* n is odd.     */
114                 k += u.bits.exp - 0x3fff;       /* 2k = n - 1.   */
115                 u.bits.exp = 0x3fff;            /* u.e in [1,2). */
116         } else {
117                 k += u.bits.exp - 0x4000;       /* 2k = n - 2.   */
118                 u.bits.exp = 0x4000;            /* u.e in [2,4). */
119         }
120
121         /*
122          * Newton's iteration.
123          * Split u.e into a high and low part to achieve additional precision.
124          */
125         xn = sqrt(u.e);                 /* 53-bit estimate of sqrtl(x). */
126 #if LDBL_MANT_DIG > 100
127         xn = (xn + (u.e / xn)) * 0.5;   /* 106-bit estimate. */
128 #endif
129         lo = u.e;
130         u.bits.manl = 0;                /* Zero out lower bits. */
131         lo = (lo - u.e) / xn;           /* Low bits divided by xn. */
132         xn = xn + (u.e / xn);           /* High portion of estimate. */
133         u.e = xn + lo;                  /* Combine everything. */
134         u.bits.exp += (k >> 1) - 1;
135
136         feclearexcept(FE_INEXACT);
137         r = fegetround();
138         fesetround(FE_TOWARDZERO);      /* Set to round-toward-zero. */
139         xn = x / u.e;                   /* Chopped quotient (inexact?). */
140
141         if (!fetestexcept(FE_INEXACT)) { /* Quotient is exact. */
142                 if (xn == u.e) {
143                         fesetenv(&env);
144                         return (u.e);
145                 }
146                 /* Round correctly for inputs like x = y**2 - ulp. */
147                 xn = dec(xn);           /* xn = xn - ulp. */
148         }
149
150         if (r == FE_TONEAREST) {
151                 xn = inc(xn);           /* xn = xn + ulp. */
152         } else if (r == FE_UPWARD) {
153                 u.e = inc(u.e);         /* u.e = u.e + ulp. */
154                 xn = inc(xn);           /* xn  = xn + ulp. */
155         }
156         u.e = u.e + xn;                         /* Chopped sum. */
157         feupdateenv(&env);      /* Restore env and raise inexact */
158         u.bits.exp--;
159         return (u.e);
160 }