8000 gh-100239: specialize left and right shift ops on ints by eendebakpt · Pull Request #129431 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-100239: specialize left and right shift ops on ints #129431

8000 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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
use stwodigits for lshift
  • Loading branch information
eendebakpt committed Jan 30, 2025
commit 0085021ba4f98241d35473048d2f9e9978d36cdc
13 changes: 11 additions & 2 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2475,9 +2475,18 @@ shift_guard(PyObject *lhs, PyObject *rhs)

// rshift with value larger the the number of bits is undefined in C
// for lshift we do not want to overflow, but we always have at least 16 bits available
return (is_compactlong(lhs) && is_compactnonnegativelong(rhs) && (_PyLong_CompactValue((PyLongObject *)rhs) <= 12) );
return (is_compactlong(lhs) && is_compactnonnegativelong(rhs) && (_PyLong_CompactValue((PyLongObject *)rhs) <= 16) );
}

#define BITWISE_LONGS_ACTION_STWODIGITS(NAME, OP) \
static PyObject * \
(NAME)(PyObject *lhs, PyObject *rhs) \
{ \
stwodigits rhs_val = (stwodigits)_PyLong_CompactValue((PyLongObject *)rhs); \
stwodigits lhs_val = (stwodigits) _PyLong_CompactValue((PyLongObject *)lhs); \
return PyLong_FromLongLong(lhs_val OP rhs_val); \
}

#define BITWISE_LONGS_ACTION(NAME, OP) \
static PyObject * \
(NAME)(PyObject *lhs, PyObject *rhs) \
Expand All @@ -2489,7 +2498,7 @@ shift_guard(PyObject *lhs, PyObject *rhs)
BITWISE_LONGS_ACTION(compactlongs_or, |)
BITWISE_LONGS_ACTION(compactlongs_and, &)
BITWISE_LONGS_ACTION(compactlongs_xor, ^)
BITWISE_LONGS_ACTION(compactlongs_lshift, <<)
BITWISE_LONGS_ACTION_STWODIGITS(compactlongs_lshift, <<)
BITWISE_LONGS_ACTION(compactlongs_rshift, >>)
#undef BITWISE_LONGS_ACTION

Expand Down
Loading
0