Debian Codegraph @

rpython/translator/c/src/ll_math.c

raw symbols SCIP
1 /* Definitions of some C99 math library functions, for those platforms
2 that don't implement these functions already. */
3
4 #include <errno.h>
5
6 /* The following macros are copied from CPython header files */
7
8 #ifdef _WIN32
9 #include <float.h>
10 #include <math.h>
11 #endif
12
13 #include "src/ll_math.h"
14
15 #ifdef _MSC_VER
16 #define PyPy_IS_NAN _isnan
17 #define PyPy_IS_INFINITY(X) (!_finite(X) && !_isnan(X))
18 #else
19 #define PyPy_IS_NAN(X) ((X) != (X))
20 #define Py_FORCE_DOUBLE(X) ((double)(X))
21 #define PyPy_IS_INFINITY(X) ((X) && \
22 (Py_FORCE_DOUBLE(X)*0.5 == Py_FORCE_DOUBLE(X)))
23 #endif
24 #define PyPy_NAN (HUGE_VAL * 0.)
25
26 /* The following copyright notice applies to the original
27 implementations of acosh, asinh and atanh. */
28
29 /*
30 * ====================================================
31 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
32 *
33 * Developed at SunPro, a Sun Microsystems, Inc. business.
34 * Permission to use, copy, modify, and distribute this
35 * software is freely granted, provided that this notice
36 * is preserved.
37 * ====================================================
38 */
39
40 RPY_EXTERN double _pypy_math_log1p(double x);
41
42 static const double ln2 = 6.93147180559945286227E-01;
43 static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */
44 static const double two_pow_p28 = 268435456.0; /* 2**28 */
45 static const double zero = 0.0;
46
47 /* acosh(x)
48 * Method :
49 * Based on
50 * acosh(x) = log [ x + sqrt(x*x-1) ]
51 * we have
52 * acosh(x) := log(x)+ln2, if x is large; else
53 * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
54 * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
55 *
56 * Special cases:
57 * acosh(x) is NaN with signal if x<1.
58 * acosh(NaN) is NaN without signal.
59 */
60
61 double
62 _pypy_math_acosh(double x)
63 {
64 if (PyPy_IS_NAN(x)) {
65 return x+x;
66 }
67 if (x < 1.) { /* x < 1; return a signaling NaN */
68 errno = EDOM;
69 #ifdef PyPy_NAN
70 return PyPy_NAN;
71 #else
72 return (x-x)/(x-x);
73 #endif
74 }
75 else if (x >= two_pow_p28) { /* x > 2**28 */
76 if (PyPy_IS_INFINITY(x)) {
77 return x+x;
78 } else {
79 return log(x)+ln2; /* acosh(huge)=log(2x) */
80 }
81 }
82 else if (x == 1.) {
83 return 0.0; /* acosh(1) = 0 */
84 }
85 else if (x > 2.) { /* 2 < x < 2**28 */
86 double t = x*x;
87 return log(2.0*x - 1.0 / (x + sqrt(t - 1.0)));
88 }
89 else { /* 1 < x <= 2 */
90 double t = x - 1.0;
91 return _pypy_math_log1p(t + sqrt(2.0*t + t*t));
92 }
93 }
94
95
96 /* asinh(x)
97 * Method :
98 * Based on
99 * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
100 * we have
101 * asinh(x) := x if 1+x*x=1,
102 * := sign(x)*(log(x)+ln2)) for large |x|, else
103 * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
104 * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
105 */
106
107 double
108 _pypy_math_asinh(double x)
109 {
110 double w;
111 double absx = fabs(x);
112
113 if (PyPy_IS_NAN(x) || PyPy_IS_INFINITY(x)) {
114 return x+x;
115 }
116 if (absx < two_pow_m28) { /* |x| < 2**-28 */
117 return x; /* return x inexact except 0 */
118 }
119 if (absx > two_pow_p28) { /* |x| > 2**28 */
120 w = log(absx)+ln2;
121 }
122 else if (absx > 2.0) { /* 2 < |x| < 2**28 */
123 w = log(2.0*absx + 1.0 / (sqrt(x*x + 1.0) + absx));
124 }
125 else { /* 2**-28 <= |x| < 2= */
126 double t = x*x;
127 w = _pypy_math_log1p(absx + t / (1.0 + sqrt(1.0 + t)));
128 }
129 return copysign(w, x);
130
131 }
132
133 /* atanh(x)
134 * Method :
135 * 1.Reduced x to positive by atanh(-x) = -atanh(x)
136 * 2.For x>=0.5
137 * 1 2x x
138 * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
139 * 2 1 - x 1 - x
140 *
141 * For x<0.5
142 * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
143 *
144 * Special cases:
145 * atanh(x) is NaN if |x| >= 1 with signal;
146 * atanh(NaN) is that NaN with no signal;
147 *
148 */
149
150 double
151 _pypy_math_atanh(double x)
152 {
153 double absx;
154 double t;
155
156 if (PyPy_IS_NAN(x)) {
157 return x+x;
158 }
159 absx = fabs(x);
160 if (absx >= 1.) { /* |x| >= 1 */
161 errno = EDOM;
162 #ifdef PyPy_NAN
163 return PyPy_NAN;
164 #else
165 return x/zero;
166 #endif
167 }
168 if (absx < two_pow_m28) { /* |x| < 2**-28 */
169 return x;
170 }
171 if (absx < 0.5) { /* |x| < 0.5 */
172 t = absx+absx;
173 t = 0.5 * _pypy_math_log1p(t + t*absx / (1.0 - absx));
174 }
175 else { /* 0.5 <= |x| <= 1.0 */
176 t = 0.5 * _pypy_math_log1p((absx + absx) / (1.0 - absx));
177 }
178 return copysign(t, x);
179 }
180
181 /* Mathematically, expm1(x) = exp(x) - 1. The expm1 function is designed
182 to avoid the significant loss of precision that arises from direct
183 evaluation of the expression exp(x) - 1, for x near 0. */
184
185 double
186 _pypy_math_expm1(double x)
187 {
188 /* For abs(x) >= log(2), it's safe to evaluate exp(x) - 1 directly; this
189 also works fine for infinities and nans.
190
191 For smaller x, we can use a method due to Kahan that achieves close to
192 full accuracy.
193 */
194
195 if (fabs(x) < 0.7) {
196 double u;
197 u = exp(x);
198 if (u == 1.0)
199 return x;
200 else
201 return (u - 1.0) * x / log(u);
202 }
203 else
204 return exp(x) - 1.0;
205 }
206
207 /* log1p(x) = log(1+x). The log1p function is designed to avoid the
208 significant loss of precision that arises from direct evaluation when x is
209 small. */
210
211 double
212 _pypy_math_log1p(double x)
213 {
214 /* For x small, we use the following approach. Let y be the nearest float
215 to 1+x, then
216
217 1+x = y * (1 - (y-1-x)/y)
218
219 so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny, the
220 second term is well approximated by (y-1-x)/y. If abs(x) >=
221 DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
222 then y-1-x will be exactly representable, and is computed exactly by
223 (y-1)-x.
224
225 If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
226 round-to-nearest then this method is slightly dangerous: 1+x could be
227 rounded up to 1+DBL_EPSILON instead of down to 1, and in that case
228 y-1-x will not be exactly representable any more and the result can be
229 off by many ulps. But this is easily fixed: for a floating-point
230 number |x| < DBL_EPSILON/2., the closest floating-point number to
231 log(1+x) is exactly x.
232 */
233
234 double y;
235 if (fabs(x) < DBL_EPSILON/2.) {
236 return x;
237 } else if (-0.5 <= x && x <= 1.) {
238 /* WARNING: it's possible than an overeager compiler
239 will incorrectly optimize the following two lines
240 to the equivalent of "return log(1.+x)". If this
241 happens, then results from log1p will be inaccurate
242 for small x. */
243 y = 1.+x;
244 return log(y)-((y-1.)-x)/y;
245 } else {
246 /* NaNs and infinities should end up here */
247 return log(1.+x);
248 }
249 }