-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-100239: specialize bitwise logical binary ops on ints #128927
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
Changes from 2 commits
c6f2deb
c57fe46
862bc67
69e66f5
0173413
61d8bb3
1f48300
e43ac58
6916df4
6476205
8448b4b
634b388
6041025
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Specialize ``BINARY_OP`` for bitwise logical ops on non-negative ints. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2414,6 +2414,35 @@ | |
|
||
/** Binary Op Specialization Extensions */ | ||
|
||
/* long-long */ | ||
|
||
static inline int | ||
is_nonnegative_compactlong(PyObject *v) | ||
{ | ||
return PyLong_CheckExact(v) && | ||
(!_PyLong_IsNegative((PyLongObject *)v)) && | ||
_PyLong_IsCompact((PyLongObject *)v); | ||
} | ||
|
||
static int | ||
nonnegative_compactlongs_guard(PyObject *lhs, PyObject *rhs) | ||
{ | ||
return (is_nonnegative_compactlong(lhs) && is_nonnegative_compactlong(rhs)); | ||
} | ||
|
||
#define NONNEGATIVE_LONGS_ACTION(NAME, OP) \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why restrict to nonnegative longs here? If we do restrict, then we can replace the calls _PyLong_CompactValue with direct access to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because negative ints need more work at runtime and I don't think they're common with bitwise logical ops. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The extra work is already done by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, I think you're right. Good point.
For bitwise logical operators we should expect the results to have the same size as the inputs. |
||
static PyObject * \ | ||
(NAME)(PyObject *lhs, PyObject *rhs) \ | ||
{ \ | ||
Py_ssize_t rhs_val = _PyLong_CompactValue((PyLongObject *)rhs); \ | ||
Py_ssize_t lhs_val = _PyLong_CompactValue((PyLongObject *)lhs); \ | ||
return PyLong_FromLong(lhs_val OP rhs_val); \ | ||
iritkatriel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
NONNEGATIVE_LONGS_ACTION(nonnegative_compactlongs_or, |) | ||
Check warning on line 2441 in Python/specialize.c
|
||
NONNEGATIVE_LONGS_ACTION(nonnegative_compactlongs_and, &) | ||
Check warning on line 2442 in Python/specialize.c
|
||
NONNEGATIVE_LONGS_ACTION(nonnegative_compactlongs_xor, ^) | ||
Check warning on line 2443 in Python/specialize.c
|
||
#undef NONNEGATIVE_LONGS_ACTION | ||
|
||
/* float-long */ | ||
|
||
static int | ||
|
@@ -2466,6 +2495,12 @@ | |
LONG_FLOAT_ACTION(compactlong_float_true_div, /) | ||
#undef LONG_FLOAT_ACTION | ||
|
||
static _PyBinaryOpSpecializationDescr nonnegative_compactlongs_specs[NB_OPARG_LAST+1] = { | ||
[NB_OR] = {nonnegative_compactlongs_guard, nonnegative_compactlongs_or}, | ||
[NB_AND] = {nonnegative_compactlongs_guard, nonnegative_compactlongs_and}, | ||
[NB_XOR] = {nonnegative_compactlongs_guard, nonnegative_compactlongs_xor}, | ||
}; | ||
|
||
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}, | ||
|
@@ -2494,6 +2529,7 @@ | |
|
||
LOOKUP_SPEC(compactlong_float_specs, oparg); | ||
LOOKUP_SPEC(float_compactlong_specs, oparg); | ||
LOOKUP_SPEC(nonnegative_compactlongs_specs, oparg); | ||
#undef LOOKUP_SPEC | ||
return 0; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.