blob: 2e049be8704339a9a09e49be3bfd5c0e76960a17 [file] [log] [blame]
Christian Heimes53876d92008-04-19 00:31:39 +00001#include "Python.h"
2
Mark Dickinson87ec0852009-02-09 17:15:59 +00003#ifdef X87_DOUBLE_ROUNDING
4/* On x86 platforms using an x87 FPU, this function is called from the
5 Py_FORCE_DOUBLE macro (defined in pymath.h) to force a floating-point
6 number out of an 80-bit x87 FPU register and into a 64-bit memory location,
7 thus rounding from extended precision to double precision. */
8double _Py_force_double(double x)
9{
10 volatile double y;
11 y = x;
12 return y;
13}
14#endif
15
Mark Dickinsonb08a53a2009-04-16 19:52:09 +000016#ifdef USING_X87_FPU
17# ifdef HAVE_GCC_ASM_FOR_X87
18
19/* inline assembly for getting and setting the 387 FPU control word on
20 gcc/x86 */
21
22unsigned short _Py_get_387controlword(void) {
23 unsigned short cw;
24 __asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
25 return cw;
26}
27
28void _Py_set_387controlword(unsigned short cw) {
29 __asm__ __volatile__ ("fldcw %0" : : "m" (cw));
30}
31
32# else
33# error "Unable to get and set x87 control word"
34# endif
35#endif
36
37
Christian Heimes53876d92008-04-19 00:31:39 +000038#ifndef HAVE_HYPOT
39double hypot(double x, double y)
40{
41 double yx;
42
43 x = fabs(x);
44 y = fabs(y);
45 if (x < y) {
46 double temp = x;
47 x = y;
48 y = temp;
49 }
50 if (x == 0.)
51 return 0.;
52 else {
53 yx = y/x;
54 return x*sqrt(1.+yx*yx);
55 }
56}
57#endif /* HAVE_HYPOT */
58
59#ifndef HAVE_COPYSIGN
60static double
61copysign(double x, double y)
62{
63 /* use atan2 to distinguish -0. from 0. */
64 if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
65 return fabs(x);
66 } else {
67 return -fabs(x);
68 }
69}
70#endif /* HAVE_COPYSIGN */
71
72#ifndef HAVE_LOG1P
Andrew MacIntyre45612572008-09-22 14:49:01 +000073#include <float.h>
74
Christian Heimes53876d92008-04-19 00:31:39 +000075double
76log1p(double x)
77{
78 /* For x small, we use the following approach. Let y be the nearest
79 float to 1+x, then
80
81 1+x = y * (1 - (y-1-x)/y)
82
83 so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny,
84 the second term is well approximated by (y-1-x)/y. If abs(x) >=
85 DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
86 then y-1-x will be exactly representable, and is computed exactly
87 by (y-1)-x.
88
89 If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
90 round-to-nearest then this method is slightly dangerous: 1+x could
91 be rounded up to 1+DBL_EPSILON instead of down to 1, and in that
92 case y-1-x will not be exactly representable any more and the
93 result can be off by many ulps. But this is easily fixed: for a
94 floating-point number |x| < DBL_EPSILON/2., the closest
95 floating-point number to log(1+x) is exactly x.
96 */
97
98 double y;
99 if (fabs(x) < DBL_EPSILON/2.) {
100 return x;
101 } else if (-0.5 <= x && x <= 1.) {
102 /* WARNING: it's possible than an overeager compiler
103 will incorrectly optimize the following two lines
104 to the equivalent of "return log(1.+x)". If this
105 happens, then results from log1p will be inaccurate
106 for small x. */
107 y = 1.+x;
108 return log(y)-((y-1.)-x)/y;
109 } else {
110 /* NaNs and infinities should end up here */
111 return log(1.+x);
112 }
113}
114#endif /* HAVE_LOG1P */
115
116/*
117 * ====================================================
118 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
119 *
120 * Developed at SunPro, a Sun Microsystems, Inc. business.
121 * Permission to use, copy, modify, and distribute this
122 * software is freely granted, provided that this notice
123 * is preserved.
124 * ====================================================
125 */
126
127static const double ln2 = 6.93147180559945286227E-01;
128static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */
129static const double two_pow_p28 = 268435456.0; /* 2**28 */
130static const double zero = 0.0;
131
132/* asinh(x)
133 * Method :
134 * Based on
135 * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
136 * we have
137 * asinh(x) := x if 1+x*x=1,
138 * := sign(x)*(log(x)+ln2)) for large |x|, else
139 * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
140 * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
141 */
142
143#ifndef HAVE_ASINH
144double
145asinh(double x)
146{
147 double w;
148 double absx = fabs(x);
149
150 if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {
151 return x+x;
152 }
153 if (absx < two_pow_m28) { /* |x| < 2**-28 */
154 return x; /* return x inexact except 0 */
155 }
156 if (absx > two_pow_p28) { /* |x| > 2**28 */
157 w = log(absx)+ln2;
158 }
159 else if (absx > 2.0) { /* 2 < |x| < 2**28 */
160 w = log(2.0*absx + 1.0 / (sqrt(x*x + 1.0) + absx));
161 }
162 else { /* 2**-28 <= |x| < 2= */
163 double t = x*x;
164 w = log1p(absx + t / (1.0 + sqrt(1.0 + t)));
165 }
166 return copysign(w, x);
167
168}
169#endif /* HAVE_ASINH */
170
171/* acosh(x)
172 * Method :
173 * Based on
174 * acosh(x) = log [ x + sqrt(x*x-1) ]
175 * we have
176 * acosh(x) := log(x)+ln2, if x is large; else
177 * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
178 * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
179 *
180 * Special cases:
181 * acosh(x) is NaN with signal if x<1.
182 * acosh(NaN) is NaN without signal.
183 */
184
185#ifndef HAVE_ACOSH
186double
187acosh(double x)
188{
189 if (Py_IS_NAN(x)) {
190 return x+x;
191 }
192 if (x < 1.) { /* x < 1; return a signaling NaN */
193 errno = EDOM;
194#ifdef Py_NAN
195 return Py_NAN;
196#else
197 return (x-x)/(x-x);
198#endif
199 }
200 else if (x >= two_pow_p28) { /* x > 2**28 */
201 if (Py_IS_INFINITY(x)) {
202 return x+x;
203 } else {
204 return log(x)+ln2; /* acosh(huge)=log(2x) */
205 }
206 }
207 else if (x == 1.) {
208 return 0.0; /* acosh(1) = 0 */
209 }
210 else if (x > 2.) { /* 2 < x < 2**28 */
211 double t = x*x;
212 return log(2.0*x - 1.0 / (x + sqrt(t - 1.0)));
213 }
214 else { /* 1 < x <= 2 */
215 double t = x - 1.0;
216 return log1p(t + sqrt(2.0*t + t*t));
217 }
218}
219#endif /* HAVE_ACOSH */
220
221/* atanh(x)
222 * Method :
223 * 1.Reduced x to positive by atanh(-x) = -atanh(x)
224 * 2.For x>=0.5
225 * 1 2x x
226 * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
227 * 2 1 - x 1 - x
228 *
229 * For x<0.5
230 * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
231 *
232 * Special cases:
233 * atanh(x) is NaN if |x| >= 1 with signal;
234 * atanh(NaN) is that NaN with no signal;
235 *
236 */
237
238#ifndef HAVE_ATANH
239double
240atanh(double x)
241{
242 double absx;
243 double t;
244
245 if (Py_IS_NAN(x)) {
246 return x+x;
247 }
248 absx = fabs(x);
249 if (absx >= 1.) { /* |x| >= 1 */
250 errno = EDOM;
251#ifdef Py_NAN
252 return Py_NAN;
253#else
254 return x/zero;
255#endif
256 }
257 if (absx < two_pow_m28) { /* |x| < 2**-28 */
258 return x;
259 }
260 if (absx < 0.5) { /* |x| < 0.5 */
261 t = absx+absx;
262 t = 0.5 * log1p(t + t*absx / (1.0 - absx));
263 }
264 else { /* 0.5 <= |x| <= 1.0 */
265 t = 0.5 * log1p((absx + absx) / (1.0 - absx));
266 }
267 return copysign(t, x);
268}
269#endif /* HAVE_ATANH */