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
Clean things up a bit
  • Loading branch information
brandtbucher committed Jan 12, 2022
commit f7187d9133fa8a6e178b37a03da0ebdbca26792f
131 changes: 71 additions & 60 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,62 +863,73 @@ static const binaryfunc binary_ops[] = {
[NB_INPLACE_XOR] = PyNumber_InPlaceXor,
};

#define BINARY_OP_INT_FAST(NAME, 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); \
int op = GET_CACHE()->adaptive.original_oparg; \
if (Py_ABS(i) < PyLong_BASE && \
(_PY_NSMALLPOSINTS <= i || i < -_PY_NSMALLNEGINTS) && \
(_PY_NSMALLPOSINTS <= l || l < -_PY_NSMALLNEGINTS) && \
Py_REFCNT(lhs) == 1 + (op == NB_INPLACE_ ## NAME)) \
{ \
assert(i); \
((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 NEXT_OP_STORES(O) \
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
((_Py_OPCODE(*next_instr) == STORE_FAST || \
_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); \
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_FLOAT_FAST(NAME, OP) \
do { \
PyObject *lhs = SECOND(); \
PyObject *rhs = TOP(); \
DEOPT_IF(!PyFloat_CheckExact(lhs), BINARY_OP); \
DEOPT_IF(!PyFloat_CheckExact(rhs), BINARY_OP); \
STAT_INC(BINARY_OP, hit); \
double d = PyFloat_AS_DOUBLE(lhs) OP PyFloat_AS_DOUBLE(rhs); \
Py_DECREF(rhs); \
STACK_SHRINK(1); \
int op = GET_CACHE()->adaptive.original_oparg; \
if (Py_REFCNT(lhs) == 1 + (op == NB_INPLACE_ ## NAME)) { \
PyFloat_AS_DOUBLE(lhs) = d; \
DISPATCH(); \
} \
Py_DECREF(lhs); \
PyObject *res = PyFloat_FromDouble(d); \
SET_TOP(res); \
if (res == NULL) { \
goto error; \
} \
DISPATCH(); \
#define BINARY_OP_FAST_FLOAT(OP) \
do { \
PyObject *lhs = SECOND(); \
PyObject *rhs = TOP(); \
DEOPT_IF(!PyFloat_CheckExact(lhs), BINARY_OP); \
DEOPT_IF(!PyFloat_CheckExact(rhs), BINARY_OP); \
STAT_INC(BINARY_OP, hit); \
double l = PyFloat_AS_DOUBLE(lhs); \
double r = PyFloat_AS_DOUBLE(rhs); \
double d = l OP r; \
Py_DECREF(rhs); \
STACK_SHRINK(1); \
bool inplace = NEXT_OP_STORES(lhs); \
if (Py_REFCNT(lhs) == inplace + 1) { \
PyFloat_AS_DOUBLE(lhs) = d; \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably (see PEP 674) Victor won't like this use of a macro as a target. :-)

Nevertheless, this whole macro is so nice and straightforward compared to ints!

DISPATCH(); \
} \
Py_DECREF(lhs); \
Comment thread
brandtbucher marked this conversation as resolved.
Outdated
PyObject *res = PyFloat_FromDouble(d); \
SET_TOP(res); \
if (res == NULL) { \
goto error; \
} \
DISPATCH(); \
} while (0)

// PEP 634: Structural Pattern Matching
Expand Down Expand Up @@ -2111,19 +2122,19 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
}

TARGET(BINARY_OP_MULTIPLY_INT) {
BINARY_OP_INT_FAST(MULTIPLY, *);
BINARY_OP_FAST_INT(*);
}

TARGET(BINARY_OP_MULTIPLY_FLOAT) {
BINARY_OP_FLOAT_FAST(MULTIPLY, *);
BINARY_OP_FAST_FLOAT(*);
}

TARGET(BINARY_OP_SUBTRACT_INT) {
BINARY_OP_INT_FAST(SUBTRACT, -);
BINARY_OP_FAST_INT(-);
}

TARGET(BINARY_OP_SUBTRACT_FLOAT) {
BINARY_OP_FLOAT_FAST(SUBTRACT, -);
BINARY_OP_FAST_FLOAT(-);
}

TARGET(BINARY_OP_ADD_UNICODE) {
Expand Down Expand Up @@ -2172,11 +2183,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
}

TARGET(BINARY_OP_ADD_FLOAT) {
BINARY_OP_FLOAT_FAST(ADD, +);
BINARY_OP_FAST_FLOAT(+);
}

TARGET(BINARY_OP_ADD_INT) {
BINARY_OP_INT_FAST(ADD, +);
BINARY_OP_FAST_INT(+);
}

TARGET(BINARY_SUBSCR) {
Expand Down