Skip to content

Commit 835fe38

Browse files
author
mark.dickinson
committed
Issue #4575: fix Py_IS_INFINITY macro to work correctly on x87 FPUs.
It now forces its argument to double before testing for infinity. git-svn-id: http://svn.python.org/projects/python/trunk@69459 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent d9dece9 commit 835fe38

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 2.7 alpha 1
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 #4978: Passing keyword arguments as unicode strings is now allowed.
1619

1720
- Issue 1242657: the __len__() and __length_hint__() calls in several tools

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)