Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify the "small" int case
  • Loading branch information
brandtbucher committed Jan 13, 2022
commit 78cc5c44070fa7e524f7482630e1c1efd3e12cc0
73 changes: 37 additions & 36 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,42 +868,43 @@ static const binaryfunc binary_ops[] = {
_Py_OPCODE(*next_instr) == STORE_FAST__LOAD_FAST) && \
GETLOCAL(_Py_OPARG(*next_instr)) == (O)) \

#define BINARY_OP_FAST_INT(OP) \
do { \
PyObject *lhs = SECOND(); \
PyObject *rhs = TOP(); \
DEOPT_IF(!PyLong_CheckExact(lhs), BINARY_OP); \
DEOPT_IF(!PyLong_CheckExact(rhs), BINARY_OP); \
DEOPT_IF(1 < Py_ABS(Py_SIZE(lhs)), BINARY_OP); \
DEOPT_IF(1 < Py_ABS(Py_SIZE(rhs)), BINARY_OP); \
STAT_INC(BINARY_OP, hit); \
stwodigits l = Py_SIZE(lhs) * ((PyLongObject *)lhs)->ob_digit[0]; \
stwodigits r = Py_SIZE(rhs) * ((PyLongObject *)rhs)->ob_digit[0]; \
stwodigits i = l OP r; \
Py_DECREF(rhs); \
STACK_SHRINK(1); \
if (-_PY_NSMALLNEGINTS <= i && i < _PY_NSMALLPOSINTS) { \
Py_DECREF(lhs); \
i += _PY_NSMALLNEGINTS; \
PyObject *res = (PyObject *)&_PyLong_SMALL_INTS[i]; \
Py_INCREF(res); \
SET_TOP(res); \
DISPATCH(); \
} \
bool inplace = NEXT_OP_STORES(lhs); \
if (Py_ABS(i) < PyLong_BASE && Py_REFCNT(lhs) == inplace + 1) { \
assert(l < -_PY_NSMALLNEGINTS || _PY_NSMALLPOSINTS <= l); \
((PyLongObject *)lhs)->ob_digit[0] = Py_ABS(i); \
Py_SET_SIZE(lhs, i < 0 ? -1 : 1); \
DISPATCH(); \
} \
Py_DECREF(lhs); \
PyObject *res = PyLong_FromLong(i); \
SET_TOP(res); \
if (res == NULL) { \
goto error; \
} \
DISPATCH(); \
#define BINARY_OP_FAST_INT(OP) \
do { \
PyObject *lhs = SECOND(); \
PyObject *rhs = TOP(); \
DEOPT_IF(!PyLong_CheckExact(lhs), BINARY_OP); \
DEOPT_IF(!PyLong_CheckExact(rhs), BINARY_OP); \
DEOPT_IF(1 < Py_ABS(Py_SIZE(lhs)), BINARY_OP); \
DEOPT_IF(1 < Py_ABS(Py_SIZE(rhs)), BINARY_OP); \
STAT_INC(BINARY_OP, hit); \
stwodigits l = Py_SIZE(lhs) * ((PyLongObject *)lhs)->ob_digit[0]; \
stwodigits r = Py_SIZE(rhs) * ((PyLongObject *)rhs)->ob_digit[0]; \
stwodigits i = l OP r; \
Py_DECREF(rhs); \
STACK_SHRINK(1); \
if (-_PY_NSMALLNEGINTS <= i && i < _PY_NSMALLPOSINTS) { \
Py_DECREF(lhs); \
PyLongObject *res = &_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + i]; \
Py_INCREF(res); \
SET_TOP((PyObject *)res); \
DISPATCH(); \
} \
bool inplace = NEXT_OP_STORES(lhs); \
if (Py_ABS(i) < PyLong_BASE && Py_REFCNT(lhs) == inplace + 1) { \
assert(lhs < (PyObject *)&_PyLong_SMALL_INTS[0] || \
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
lhs >= (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + \
_PY_NSMALLPOSINTS]); \
((PyLongObject *)lhs)->ob_digit[0] = Py_ABS(i); \
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
Py_SET_SIZE(lhs, i < 0 ? -1 : 1); \
DISPATCH(); \
} \
Py_DECREF(lhs); \
PyObject *res = PyLong_FromLong(i); \
SET_TOP(res); \
if (res == NULL) { \
goto error; \
} \
DISPATCH(); \
} while (0)

#define BINARY_OP_FAST_FLOAT(OP) \
Expand Down