Skip to content

Commit 87ec085

Browse files
committed
Merged revisions 69459 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r69459 | mark.dickinson | 2009-02-09 14:18:43 +0000 (Mon, 09 Feb 2009) | 3 lines Issue #4575: fix Py_IS_INFINITY macro to work correctly on x87 FPUs. It now forces its argument to double before testing for infinity. ........
1 parent ae09899 commit 87ec085

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

Include/pymath.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ extern double copysign(double, double);
7777
#define Py_MATH_E 2.7182818284590452354
7878
#endif
7979

80+
/* On x86, Py_FORCE_DOUBLE forces a floating-point number out of an x87 FPU
81+
register and into a 64-bit memory location, rounding from extended
82+
precision to double precision in the process. On other platforms it does
83+
nothing. */
84+
85+
/* we take double rounding as evidence of x87 usage */
86+
#ifndef Py_FORCE_DOUBLE
87+
# ifdef X87_DOUBLE_ROUNDING
88+
PyAPI_FUNC(double) _Py_force_double(double);
89+
# define Py_FORCE_DOUBLE(X) (_Py_force_double(X))
90+
# else
91+
# define Py_FORCE_DOUBLE(X) (X)
92+
# endif
93+
#endif
94+
8095
/* Py_IS_NAN(X)
8196
* Return 1 if float or double arg is a NaN, else 0.
8297
* Caution:
@@ -101,14 +116,18 @@ extern double copysign(double, double);
101116
* This implementation may set the underflow flag if |X| is very small;
102117
* it really can't be implemented correctly (& easily) before C99.
103118
* Override in pyconfig.h if you have a better spelling on your platform.
119+
* Py_FORCE_DOUBLE is used to avoid getting false negatives from a
120+
* non-infinite value v sitting in an 80-bit x87 register such that
121+
* v becomes infinite when spilled from the register to 64-bit memory.
104122
* Note: PC/pyconfig.h defines Py_IS_INFINITY as _isinf
105123
*/
106124
#ifndef Py_IS_INFINITY
107-
#if defined HAVE_DECL_ISINF && HAVE_DECL_ISINF == 1
108-
#define Py_IS_INFINITY(X) isinf(X)
109-
#else
110-
#define Py_IS_INFINITY(X) ((X) && (X)*0.5 == (X))
111-
#endif
125+
# if defined HAVE_DECL_ISINF && HAVE_DECL_ISINF == 1
126+
# define Py_IS_INFINITY(X) isinf(X)
127+
# else
128+
# define Py_IS_INFINITY(X) ((X) && \
129+
(Py_FORCE_DOUBLE(X)*0.5 == Py_FORCE_DOUBLE(X)))
130+
# endif
112131
#endif
113132

114133
/* Py_IS_FINITE(X)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.1 alpha 0
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #4575: Fix Py_IS_INFINITY macro to work correctly on x87 FPUs:
16+
it now forces its argument to double before testing for infinity.
17+
1518
- Issue #5137: Make len() correctly raise a TypeError when a __len__ method
1619
returns a non-number type.
1720

Python/pymath.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
#include "Python.h"
22

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)
9+
{
10+
volatile double y;
11+
y = x;
12+
return y;
13+
}
14+
#endif
15+
316
#ifndef HAVE_HYPOT
417
double hypot(double x, double y)
518
{

0 commit comments

Comments
 (0)