10000 gh-100239: Handle NaN and zero division in guards by Eclips4 · Pull Request #128963 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-100239: Handle NaN and zero division in guards #128963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Convers 8000 ations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Address Irit's review
  • Loading branch information
Eclips4 committed Jan 19, 2025
commit 127cb30e1b0c25b2472a2547ccea409883cefecb
30 changes: 17 additions & 13 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,22 +1384,26 @@ def float_lhs(arg):

def binary_op_nan():
def compactlong_lhs(arg):
42 + arg
42 - arg
42 * arg
42 / arg
return (
42 + arg,
42 - arg,
42 * arg,
42 / arg,
)
def compactlong_rhs(arg):
arg + 42
arg - 42
arg * 42
arg / 42

compactlong_lhs(1.0)
return (
arg + 42,
arg - 42,
arg * 2,
arg / 42,
)
nan = float('nan')
self.assertEqual(compactlong_lhs(1.0), (43.0, 41.0, 42.0, 42.0))
for _ in range(100):
compactlong_lhs(float('nan'))
compactlong_rhs(1.0)
self.assertTrue(all(filter(lambda x: x is nan, compactlong_lhs(nan))))
self.assertEqual(compactlong_rhs(42.0), (84.0, 0.0, 84.0, 1.0))
for _ in range(100):
compactlong_rhs(float('nan'))
self.assertTrue(all(filter(lambda x: x is nan, compactlong_rhs(nan))))

self.assert_no_opcode(compactlong_lhs, "BINARY_OP_EXTEND")
self.assert_no_opcode(compactlong_rhs, "BINARY_OP_EXTEND")
Expand Down
14 changes: 7 additions & 7 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2421,14 +2421,14 @@ float_compactlong_guard(PyObject *lhs, PyObject *rhs)
{
return (
PyFloat_CheckExact(lhs) &&
!isnan(PyFloat_AsDouble(lhs)) &&
PyLong_CheckExact(rhs) && E0F9 ;
_PyLong_IsCompact((PyLongObject *)rhs) &&
!isnan(PyFloat_AsDouble(lhs))
_PyLong_IsCompact((PyLongObject *)rhs)
);
}

static inline int
float_compactlong_guard_true_div(PyObject *lhs, PyObject *rhs)
nonzero_float_compactlong_guard(PyObject *lhs, PyObject *rhs)
{
return (
float_compactlong_guard(lhs, rhs) && !PyLong_IsZero(rhs)
Expand All @@ -2455,15 +2455,15 @@ static inline int
compactlong_float_guard(PyObject *lhs, PyObject *rhs)
{
return (
PyFloat_CheckExact(rhs) &&
PyLong_CheckExact(lhs) &&
_PyLong_IsCompact((PyLongObject *)lhs) &&
PyFloat_CheckExact(rhs) &&
!isnan(PyFloat_AsDouble(rhs))
);
}

static inline int
compactlong_float_guard_true_div(PyObject *lhs, PyObject *rhs)
nonzero_compactlong_float_guard(PyObject *lhs, PyObject *rhs)
{
return (
compactlong_float_guard(lhs, rhs) && PyFloat_AsDouble(rhs) != 0.0
Expand All @@ -2487,14 +2487,14 @@ LONG_FLOAT_ACTION(compactlong_float_true_div, /)
static _PyBinaryOpSpecializationDescr float_compactlong_specs[NB_OPARG_LAST+1] = {
[NB_ADD] = {float_compactlong_guard, float_compactlong_add},
[NB_SUBTRACT] = {float_compactlong_guard, float_compactlong_subtract},
[NB_TRUE_DIVIDE] = {float_compactlong_guard_true_div, float_compactlong_true_div},
[NB_TRUE_DIVIDE] = {nonzero_float_compactlong_guard, float_compactlong_true_div},
[NB_MULTIPLY] = {float_compactlong_guard, float_compactlong_multiply},
};

static _PyBinaryOpSpecializationDescr compactlong_float_specs[NB_OPARG_LAST+1] = {
[NB_ADD] = {compactlong_float_guard, compactlong_float_add},
[NB_SUBTRACT] = {compactlong_float_guard, compactlong_float_subtract},
[NB_TRUE_DIVIDE] = {compactlong_float_guard_true_div, compactlong_float_true_div},
[NB_TRUE_DIVIDE] = {nonzero_compactlong_float_guard, compactlong_float_true_div},
[NB_MULTIPLY] = {compactlong_float_guard, compactlong_float_multiply},
};

Expand Down
Loading
0