Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7abf8d4

Browse files
committed
The SSE2 detection and enabling could potentially cause
problems for binary distributions of Python in situations where the build machine has SSE2 but the target machine does not. Therefore, don't enable SSE2 instructions automatically on x86.
1 parent 153c70f commit 7abf8d4

7 files changed

Lines changed: 47 additions & 280 deletions

File tree

Include/pyport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ extern "C" {
469469
the FPU is using 53-bit precision. Here are macros that force this. See
470470
Python/pystrtod.c for an example of their use. */
471471

472-
#ifdef USING_X87_FPU
472+
#ifdef HAVE_GCC_ASM_FOR_X87
473473
#define _Py_SET_53BIT_PRECISION_HEADER \
474474
unsigned short old_387controlword, new_387controlword
475475
#define _Py_SET_53BIT_PRECISION_START \

Misc/NEWS

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ Core and Builtins
3131
value: str(1e11 + 0.5). (This minor issue has existed in 2.x for a
3232
long time.)
3333

34-
- On x86, SSE2 instructions for floating-point are automatically
35-
detected and, where possible, enabled on platforms using the gcc
36-
compiler. As a consequence, some arithmetic operations may have
37-
different (more accurate!) results on some platforms, and
38-
cross-platform consistency of Python arithmetic should be improved.
39-
This applies particularly to Linux/x86.
40-
4134
- Issue #1580: On most platforms, use a 'short' float repr: for a
4235
finite float x, repr(x) now outputs a string based on the shortest
4336
sequence of decimal digits that rounds to x. Previous behaviour was

Objects/floatobject.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
#define MAX(x, y) ((x) < (y) ? (y) : (x))
1616
#define MIN(x, y) ((x) < (y) ? (x) : (y))
1717

18+
/* ascii character tests (as opposed to locale tests) */
19+
#define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
20+
(c) == '\r' || (c) == '\t' || (c) == '\v')
21+
#define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
22+
1823
#ifdef HAVE_IEEEFP_H
1924
#include <ieeefp.h>
2025
#endif
@@ -188,7 +193,7 @@ PyFloat_FromString(PyObject *v)
188193
}
189194

190195
last = s + len;
191-
while (*s && isspace(Py_CHARMASK(*s)))
196+
while (*s && ISSPACE(Py_CHARMASK(*s)))
192197
s++;
193198
if (*s == '\0') {
194199
PyErr_SetString(PyExc_ValueError, "empty string for float()");
@@ -245,7 +250,7 @@ PyFloat_FromString(PyObject *v)
245250
}
246251
/* Since end != s, the platform made *some* kind of sense out
247252
of the input. Trust it. */
248-
while (*end && isspace(Py_CHARMASK(*end)))
253+
while (*end && ISSPACE(Py_CHARMASK(*end)))
249254
end++;
250255
if (*end != '\0') {
251256
PyOS_snprintf(buffer, sizeof(buffer),
@@ -1275,7 +1280,7 @@ float_fromhex(PyObject *cls, PyObject *arg)
12751280
********************/
12761281

12771282
/* leading whitespace and optional sign */
1278-
while (isspace(Py_CHARMASK(*s)))
1283+
while (ISSPACE(Py_CHARMASK(*s)))
12791284
s++;
12801285
if (*s == '-') {
12811286
s++;
@@ -1299,6 +1304,7 @@ float_fromhex(PyObject *cls, PyObject *arg)
12991304
s_store = s;
13001305
if (*s == '0') {
13011306
s++;
1307+
if (*s == 'x' || *s == 'X')
13021308
if (tolower(*s) == (int)'x')
13031309
s++;
13041310
else
@@ -1345,7 +1351,7 @@ float_fromhex(PyObject *cls, PyObject *arg)
13451351
exp = 0;
13461352

13471353
/* optional trailing whitespace leading to the end of the string */
1348-
while (isspace(Py_CHARMASK(*s)))
1354+
while (ISSPACE(Py_CHARMASK(*s)))
13491355
s++;
13501356
if (s != s_end)
13511357
goto parse_error;

Python/pymath.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ double _Py_force_double(double x)
1313
}
1414
#endif
1515

16-
#ifdef USING_X87_FPU
17-
# ifdef HAVE_GCC_ASM_FOR_X87
16+
#ifdef HAVE_GCC_ASM_FOR_X87
1817

1918
/* inline assembly for getting and setting the 387 FPU control word on
2019
gcc/x86 */
@@ -29,9 +28,6 @@ void _Py_set_387controlword(unsigned short cw) {
2928
__asm__ __volatile__ ("fldcw %0" : : "m" (cw));
3029
}
3130

32-
# else
33-
# error "Unable to get and set x87 control word"
34-
# endif
3531
#endif
3632

3733

configure

Lines changed: 16 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#! /bin/sh
2-
# From configure.in Revision: 71663 .
2+
# From configure.in Revision: 71704 .
33
# Guess values for system-dependent variables and create Makefiles.
44
# Generated by GNU Autoconf 2.61 for python 3.1.
55
#
@@ -21827,174 +21827,21 @@ _ACEOF
2182721827

2182821828
fi
2182921829

21830-
# David Gay's code in Python/dtoa.c requires that the FPU uses 53-bit
21830+
# The short float repr introduced in Python 3.1 requires the
21831+
# correctly-rounded string <-> double conversion functions in
21832+
# Python/dtoa.c, which in turn require that the FPU uses 53-bit
2183121833
# rounding; this is a particular problem on x86, where the x87 FPU has
2183221834
# a default rounding precision of 64 bits. For gcc/x86, we try to fix
21833-
# this by:
21834-
#
21835-
# (1) using the SSE2 instruction set when available (it usually is
21836-
# on modern machines)
21837-
# (2) using inline assembler to get and set the x87 FPU control word
21838-
# otherwise.
21839-
#
21840-
# On AMD64 (aka x86-64), gcc automatically enables use of SSE2
21841-
# instructions, so we don't bother trying to detect.
21835+
# this by using inline assembler to get and set the x87 FPU control
21836+
# word.
2184221837

2184321838
if test "$GCC" = yes && test -n "`$CC -dM -E - </dev/null | grep i386`"
2184421839
then
21845-
# determine whether we're already using the SSE2 instruction set for math
21846-
# (e.g., this is true by default on OS X/x86)
21847-
{ echo "$as_me:$LINENO: checking whether SSE2 instructions are already enabled for math" >&5
21848-
echo $ECHO_N "checking whether SSE2 instructions are already enabled for math... $ECHO_C" >&6; }
21849-
if test -n "`$CC -dM -E - </dev/null | grep __SSE2_MATH__`"
21850-
then
21851-
ac_sse2_enabled=yes
21852-
else
21853-
ac_sse2_enabled=no
21854-
fi
21855-
{ echo "$as_me:$LINENO: result: $ac_sse2_enabled" >&5
21856-
echo "${ECHO_T}$ac_sse2_enabled" >&6; }
21857-
21858-
# if we're not using SSE2 already, we need to either enable it
21859-
# (when available), or use inline assembler to get and set the
21860-
# 387 control word.
21861-
if test $ac_sse2_enabled = no
21862-
then
21863-
# Check cpuid for SSE2 availability. Bits 25 and 26 of edx tell
21864-
# us about SSE and SSE2 respectively.
21865-
{ echo "$as_me:$LINENO: checking whether SSE2 instructions are available on this CPU" >&5
21866-
echo $ECHO_N "checking whether SSE2 instructions are available on this CPU... $ECHO_C" >&6; }
21867-
if test "$cross_compiling" = yes; then
21868-
ac_cv_cpu_has_sse2=no
21869-
else
21870-
cat >conftest.$ac_ext <<_ACEOF
21871-
/* confdefs.h. */
21872-
_ACEOF
21873-
cat confdefs.h >>conftest.$ac_ext
21874-
cat >>conftest.$ac_ext <<_ACEOF
21875-
/* end confdefs.h. */
21876-
21877-
int main() {
21878-
unsigned int ax, bx, cx, dx, func;
21879-
func = 1U;
21880-
__asm__ __volatile__ (
21881-
"pushl %%ebx\n\t" /* don't clobber ebx */
21882-
"cpuid\n\t"
21883-
"movl %%ebx, %1\n\t"
21884-
"popl %%ebx"
21885-
: "=a" (ax), "=r" (bx), "=c" (cx), "=d" (dx)
21886-
: "a" (func)
21887-
: "cc" );
21888-
if ((dx & (1U << 25)) && (dx & (1U << 26)))
21889-
return 0;
21890-
else
21891-
return 1;
21892-
}
21893-
21894-
_ACEOF
21895-
rm -f conftest$ac_exeext
21896-
if { (ac_try="$ac_link"
21897-
case "(($ac_try" in
21898-
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
21899-
*) ac_try_echo=$ac_try;;
21900-
esac
21901-
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
21902-
(eval "$ac_link") 2>&5
21903-
ac_status=$?
21904-
echo "$as_me:$LINENO: \$? = $ac_status" >&5
21905-
(exit $ac_status); } && { ac_try='./conftest$ac_exeext'
21906-
{ (case "(($ac_try" in
21907-
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
21908-
*) ac_try_echo=$ac_try;;
21909-
esac
21910-
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
21911-
(eval "$ac_try") 2>&5
21912-
ac_status=$?
21913-
echo "$as_me:$LINENO: \$? = $ac_status" >&5
21914-
(exit $ac_status); }; }; then
21915-
ac_cv_cpu_has_sse2=yes
21916-
else
21917-
echo "$as_me: program exited with status $ac_status" >&5
21918-
echo "$as_me: failed program was:" >&5
21919-
sed 's/^/| /' conftest.$ac_ext >&5
21920-
21921-
( exit $ac_status )
21922-
ac_cv_cpu_has_sse2=no
21923-
fi
21924-
rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
21925-
fi
21926-
21927-
21928-
{ echo "$as_me:$LINENO: result: $ac_cv_cpu_has_sse2" >&5
21929-
echo "${ECHO_T}$ac_cv_cpu_has_sse2" >&6; }
21930-
21931-
# determine whether gcc accepts options to turn on SSE2
21932-
{ echo "$as_me:$LINENO: checking whether $CC accepts -msse2 -mfpmath=sse" >&5
21933-
echo $ECHO_N "checking whether $CC accepts -msse2 -mfpmath=sse... $ECHO_C" >&6; }
21934-
ac_save_cc="$CC"
21935-
CC="$CC -msse2 -mfpmath=sse"
21936-
if test "$cross_compiling" = yes; then
21937-
ac_cv_msse2_ok=no
21938-
else
21939-
cat >conftest.$ac_ext <<_ACEOF
21940-
/* confdefs.h. */
21941-
_ACEOF
21942-
cat confdefs.h >>conftest.$ac_ext
21943-
cat >>conftest.$ac_ext <<_ACEOF
21944-
/* end confdefs.h. */
21945-
int main() { return 0; }
21946-
_ACEOF
21947-
rm -f conftest$ac_exeext
21948-
if { (ac_try="$ac_link"
21949-
case "(($ac_try" in
21950-
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
21951-
*) ac_try_echo=$ac_try;;
21952-
esac
21953-
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
21954-
(eval "$ac_link") 2>&5
21955-
ac_status=$?
21956-
echo "$as_me:$LINENO: \$? = $ac_status" >&5
21957-
(exit $ac_status); } && { ac_try='./conftest$ac_exeext'
21958-
{ (case "(($ac_try" in
21959-
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
21960-
*) ac_try_echo=$ac_try;;
21961-
esac
21962-
eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
21963-
(eval "$ac_try") 2>&5
21964-
ac_status=$?
21965-
echo "$as_me:$LINENO: \$? = $ac_status" >&5
21966-
(exit $ac_status); }; }; then
21967-
ac_cv_msse2_ok=yes
21968-
else
21969-
echo "$as_me: program exited with status $ac_status" >&5
21970-
echo "$as_me: failed program was:" >&5
21971-
sed 's/^/| /' conftest.$ac_ext >&5
21972-
21973-
( exit $ac_status )
21974-
ac_cv_msse2_ok=no
21975-
fi
21976-
rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
21977-
fi
21978-
21979-
21980-
CC="$ac_save_cc"
21981-
{ echo "$as_me:$LINENO: result: $ac_cv_msse2_ok" >&5
21982-
echo "${ECHO_T}$ac_cv_msse2_ok" >&6; }
21983-
21984-
if test $ac_cv_cpu_has_sse2 = yes && test $ac_cv_msse2_ok = yes
21985-
then
21986-
BASECFLAGS="$BASECFLAGS -msse2 -mfpmath=sse"
21987-
else
21988-
# SSE2 doesn't appear to be available. Check that it's okay
21989-
# to use gcc inline assembler to get and set x87 control word
21990-
21991-
cat >>confdefs.h <<\_ACEOF
21992-
#define USING_X87_FPU 1
21993-
_ACEOF
21994-
21995-
{ echo "$as_me:$LINENO: checking whether we can use gcc inline assembler to get and set x87 control word" >&5
21840+
# Check that it's okay to use gcc inline assembler to get and set
21841+
# x87 control word
21842+
{ echo "$as_me:$LINENO: checking whether we can use gcc inline assembler to get and set x87 control word" >&5
2199621843
echo $ECHO_N "checking whether we can use gcc inline assembler to get and set x87 control word... $ECHO_C" >&6; }
21997-
cat >conftest.$ac_ext <<_ACEOF
21844+
cat >conftest.$ac_ext <<_ACEOF
2199821845
/* confdefs.h. */
2199921846
_ACEOF
2200021847
cat confdefs.h >>conftest.$ac_ext
@@ -22005,9 +21852,9 @@ int
2200521852
main ()
2200621853
{
2200721854

22008-
unsigned short cw;
22009-
__asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
22010-
__asm__ __volatile__ ("fldcw %0" : : "m" (cw));
21855+
unsigned short cw;
21856+
__asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
21857+
__asm__ __volatile__ ("fldcw %0" : : "m" (cw));
2201121858

2201221859
;
2201321860
return 0;
@@ -22039,17 +21886,15 @@ sed 's/^/| /' conftest.$ac_ext >&5
2203921886
fi
2204021887

2204121888
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
22042-
{ echo "$as_me:$LINENO: result: $have_gcc_asm_for_x87" >&5
21889+
{ echo "$as_me:$LINENO: result: $have_gcc_asm_for_x87" >&5
2204321890
echo "${ECHO_T}$have_gcc_asm_for_x87" >&6; }
22044-
if test "$have_gcc_asm_for_x87" = yes
22045-
then
21891+
if test "$have_gcc_asm_for_x87" = yes
21892+
then
2204621893

2204721894
cat >>confdefs.h <<\_ACEOF
2204821895
#define HAVE_GCC_ASM_FOR_X87 1
2204921896
_ACEOF
2205021897

22051-
fi
22052-
fi
2205321898
fi
2205421899
fi
2205521900

0 commit comments

Comments
 (0)