Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / common / sqrtl.c
blob9976f35e73e5bde9798d279ddd69622783aa3527
1 /*
2 (C) Copyright IBM Corp. 2009
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 * Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 * 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 * Neither the name of IBM nor the names of its contributors may be
15 used to endorse or promote products derived from this software without
16 specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE.
31 #include <math.h>
32 #include "local.h"
34 #ifdef _LDBL_EQ_DBL
35 /* On platforms where long double is as wide as double. */
36 long double
37 sqrtl (long double x)
39 return sqrt(x);
42 #else
44 /* This code is based upon the version in the BSD math's library.
45 That code is...
47 * Copyright (c) 2007 Steven G. Kargl
48 * All rights reserved.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 * 1. Redistributions of source code must retain the above copyright
54 * notice unmodified, this list of conditions, and the following
55 * disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
60 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
61 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
62 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
64 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
65 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 #include <float.h>
72 #include "ieeefp.h"
74 #ifndef LDBL_NBIT
75 #define LDBL_NBIT 0
76 #endif
78 #ifndef LDBL_MAX_EXP
79 #define LDBL_MAX_EXP DBL_MAX_EXP
80 #endif
82 /* Return (x + ulp) for normal positive x. Assumes no overflow. */
84 static inline long double
85 inc (long double x)
87 union ieee_ext_u ux = { .extu_ld = x, };
89 if (++ux.extu_ext.ext_fracl == 0)
91 if (++ux.extu_ext.ext_frach == 0)
93 ux.extu_ext.ext_exp++;
94 ux.extu_ext.ext_frach |= LDBL_NBIT;
98 return ux.extu_ld;
101 /* Return (x - ulp) for normal positive x. Assumes no underflow. */
103 static inline long double
104 dec (long double x)
106 union ieee_ext_u ux = { .extu_ld = x, };
108 if (ux.extu_ext.ext_fracl-- == 0)
110 if (ux.extu_ext.ext_frach-- == LDBL_NBIT)
112 ux.extu_ext.ext_exp--;
113 ux.extu_ext.ext_frach |= LDBL_NBIT;
117 return ux.extu_ld;
120 /* This is slow, but simple and portable. */
122 long double
123 sqrtl (long double x)
125 union ieee_ext_u ux = { .extu_ld = x, };
126 int k;
127 long double lo, xn;
129 /* If x = NaN, then sqrt(x) = NaN. */
130 /* If x = Inf, then sqrt(x) = Inf. */
131 /* If x = -Inf, then sqrt(x) = NaN. */
132 if (ux.extu_ext.ext_exp == LDBL_MAX_EXP * 2 - 1)
133 return (x * x + x);
135 /* If x = +-0, then sqrt(x) = +-0. */
136 if (x == 0.0L || x == -0.0L)
137 return x;
139 /* If x < 0, then raise invalid and return NaN. */
140 if (ux.extu_ext.ext_sign)
141 return ((x - x) / (x - x));
143 if (ux.extu_ext.ext_exp == 0)
145 /* Adjust subnormal numbers. */
146 ux.extu_ld *= 0x1.0p514;
147 k = -514;
149 else
150 k = 0;
152 /* ux.extu_ld is a normal number, so break it into ux.extu_ld = e*2^n where
153 ux.extu_ld = (2*e)*2^2k for odd n and ux.extu_ld = (4*e)*2^2k for even n. */
155 if ((ux.extu_ext.ext_exp - EXT_EXP_BIAS) & 1)
157 /* n is even. */
158 k += ux.extu_ext.ext_exp - EXT_EXP_BIAS - 1; /* 2k = n - 2. */
159 ux.extu_ext.ext_exp = EXT_EXP_BIAS + 1; /* ux.extu_ld in [2,4). */
161 else
163 k += ux.extu_ext.ext_exp - EXT_EXP_BIAS; /* 2k = n - 1. */
164 ux.extu_ext.ext_exp = EXT_EXP_BIAS; /* ux.extu_ld in [1,2). */
167 /* Newton's iteration.
168 Split ux.extu_ld into a high and low part to achieve additional precision. */
170 xn = sqrt ((double) ux.extu_ld); /* 53-bit estimate of sqrtl(x). */
172 #if LDBL_MANT_DIG > 100
173 xn = (xn + (ux.extu_ld / xn)) * 0.5; /* 106-bit estimate. */
174 #endif
176 lo = ux.extu_ld;
177 ux.extu_ext.ext_fracl = 0; /* Zero out lower bits. */
178 lo = (lo - ux.extu_ld) / xn; /* Low bits divided by xn. */
179 xn = xn + (ux.extu_ld / xn); /* High portion of estimate. */
180 ux.extu_ld = xn + lo; /* Combine everything. */
181 ux.extu_ext.ext_exp += (k >> 1) - 1;
183 xn = x / ux.extu_ld; /* Chopped quotient (inexact?). */
185 /* For simplicity we round to nearest. */
186 xn = inc (xn); /* xn = xn + ulp. */
188 ux.extu_ld = ux.extu_ld + xn; /* Chopped sum. */
189 ux.extu_ext.ext_exp--;
191 return ux.extu_ld;
193 #endif /* ! _LDBL_EQ_DBL */