3 #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. */
8 double _Py_force_double(double x
)
16 #ifdef HAVE_GCC_ASM_FOR_X87
18 /* inline assembly for getting and setting the 387 FPU control word on
21 unsigned short _Py_get_387controlword(void) {
23 __asm__
__volatile__ ("fnstcw %0" : "=m" (cw
));
27 void _Py_set_387controlword(unsigned short cw
) {
28 __asm__
__volatile__ ("fldcw %0" : : "m" (cw
));
35 double hypot(double x
, double y
)
50 return x
*sqrt(1.+yx
*yx
);
53 #endif /* HAVE_HYPOT */
57 copysign(double x
, double y
)
59 /* use atan2 to distinguish -0. from 0. */
60 if (y
> 0. || (y
== 0. && atan2(y
, -1.) > 0.)) {
66 #endif /* HAVE_COPYSIGN */
77 return copysign(y
, x
);
79 #endif /* HAVE_ROUND */
87 /* For x small, we use the following approach. Let y be the nearest
90 1+x = y * (1 - (y-1-x)/y)
92 so log(1+x) = log(y) + log(1-(y-1-x)/y). Since (y-1-x)/y is tiny,
93 the second term is well approximated by (y-1-x)/y. If abs(x) >=
94 DBL_EPSILON/2 or the rounding-mode is some form of round-to-nearest
95 then y-1-x will be exactly representable, and is computed exactly
98 If abs(x) < DBL_EPSILON/2 and the rounding mode is not known to be
99 round-to-nearest then this method is slightly dangerous: 1+x could
100 be rounded up to 1+DBL_EPSILON instead of down to 1, and in that
101 case y-1-x will not be exactly representable any more and the
102 result can be off by many ulps. But this is easily fixed: for a
103 floating-point number |x| < DBL_EPSILON/2., the closest
104 floating-point number to log(1+x) is exactly x.
108 if (fabs(x
) < DBL_EPSILON
/2.) {
110 } else if (-0.5 <= x
&& x
<= 1.) {
111 /* WARNING: it's possible than an overeager compiler
112 will incorrectly optimize the following two lines
113 to the equivalent of "return log(1.+x)". If this
114 happens, then results from log1p will be inaccurate
117 return log(y
)-((y
-1.)-x
)/y
;
119 /* NaNs and infinities should end up here */
123 #endif /* HAVE_LOG1P */
126 * ====================================================
127 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
129 * Developed at SunPro, a Sun Microsystems, Inc. business.
130 * Permission to use, copy, modify, and distribute this
131 * software is freely granted, provided that this notice
133 * ====================================================
136 static const double ln2
= 6.93147180559945286227E-01;
137 static const double two_pow_m28
= 3.7252902984619141E-09; /* 2**-28 */
138 static const double two_pow_p28
= 268435456.0; /* 2**28 */
139 static const double zero
= 0.0;
144 * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
146 * asinh(x) := x if 1+x*x=1,
147 * := sign(x)*(log(x)+ln2)) for large |x|, else
148 * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
149 * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
157 double absx
= fabs(x
);
159 if (Py_IS_NAN(x
) || Py_IS_INFINITY(x
)) {
162 if (absx
< two_pow_m28
) { /* |x| < 2**-28 */
163 return x
; /* return x inexact except 0 */
165 if (absx
> two_pow_p28
) { /* |x| > 2**28 */
168 else if (absx
> 2.0) { /* 2 < |x| < 2**28 */
169 w
= log(2.0*absx
+ 1.0 / (sqrt(x
*x
+ 1.0) + absx
));
171 else { /* 2**-28 <= |x| < 2= */
173 w
= log1p(absx
+ t
/ (1.0 + sqrt(1.0 + t
)));
175 return copysign(w
, x
);
178 #endif /* HAVE_ASINH */
183 * acosh(x) = log [ x + sqrt(x*x-1) ]
185 * acosh(x) := log(x)+ln2, if x is large; else
186 * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
187 * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
190 * acosh(x) is NaN with signal if x<1.
191 * acosh(NaN) is NaN without signal.
201 if (x
< 1.) { /* x < 1; return a signaling NaN */
209 else if (x
>= two_pow_p28
) { /* x > 2**28 */
210 if (Py_IS_INFINITY(x
)) {
213 return log(x
)+ln2
; /* acosh(huge)=log(2x) */
217 return 0.0; /* acosh(1) = 0 */
219 else if (x
> 2.) { /* 2 < x < 2**28 */
221 return log(2.0*x
- 1.0 / (x
+ sqrt(t
- 1.0)));
223 else { /* 1 < x <= 2 */
225 return log1p(t
+ sqrt(2.0*t
+ t
*t
));
228 #endif /* HAVE_ACOSH */
232 * 1.Reduced x to positive by atanh(-x) = -atanh(x)
235 * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
239 * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
242 * atanh(x) is NaN if |x| >= 1 with signal;
243 * atanh(NaN) is that NaN with no signal;
258 if (absx
>= 1.) { /* |x| >= 1 */
266 if (absx
< two_pow_m28
) { /* |x| < 2**-28 */
269 if (absx
< 0.5) { /* |x| < 0.5 */
271 t
= 0.5 * log1p(t
+ t
*absx
/ (1.0 - absx
));
273 else { /* 0.5 <= |x| <= 1.0 */
274 t
= 0.5 * log1p((absx
+ absx
) / (1.0 - absx
));
276 return copysign(t
, x
);
278 #endif /* HAVE_ATANH */