Repository navigation
gh-151289: Specialize non-compact int add/subtract via BINARY_OP_EXTEND #159688
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
KRRT7
wants to merge
1
commit into
python:main
Choose a base branch
from
KRRT7:wide-int-extend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core_and_Builtins/2026-10-18-12-00-00.gh-issue-151289.wIdEx7.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Specialize :class:`int` addition and subtraction where an operand is not a | ||
| compact integer, using ``BINARY_OP_EXTEND``. This skips the generic binary | ||
| operator dispatch for these cases. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2157,6 +2157,21 @@ SEQ_INT_MULTIPLY_ACTION(tuple_int_multiply, _PyTuple_Repeat, lhs, rhs) | |
| SEQ_INT_MULTIPLY_ACTION(int_tuple_multiply, _PyTuple_Repeat, rhs, lhs) | ||
| #undef SEQ_INT_MULTIPLY_ACTION | ||
|
|
||
| /* Exact ints that are not both compact (those take BINARY_OP_ADD_INT and | ||
| friends). Calling the long implementation directly skips the generic | ||
| binary_op1 slot dispatch. */ | ||
| static PyObject * | ||
| longs_add(PyObject *lhs, PyObject *rhs) | ||
|
Comment on lines
+2163
to
+2164
Member
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. It seems you actually want this type of API. So, why not make PyObject *
_PyLong_Add(PyObject *a, PyObject *b)
{
return (PyObject *)long_add((PyLongObject *)a, (PyLongObject *)b);
}like BTW, it seems that |
||
| { | ||
| return _PyLong_Add((PyLongObject *)lhs, (PyLongObject *)rhs); | ||
| } | ||
|
|
||
| static PyObject * | ||
| longs_subtract(PyObject *lhs, PyObject *rhs) | ||
| { | ||
| return _PyLong_Subtract((PyLongObject *)lhs, (PyLongObject *)rhs); | ||
| } | ||
|
|
||
| static int | ||
| compactlongs_guard(PyObject *lhs, PyObject *rhs) | ||
| { | ||
|
|
@@ -2256,6 +2271,14 @@ static _PyBinaryOpSpecializationDescr binaryop_extend_descrs[] = { | |
| {NB_INPLACE_AND, compactlongs_guard, compactlongs_and, &PyLong_Type, 1, NULL, NULL}, | ||
| {NB_INPLACE_XOR, compactlongs_guard, compactlongs_xor, &PyLong_Type, 1, NULL, NULL}, | ||
|
|
||
| /* long-long add/subtract for operands that are not both compact. | ||
| The guard is a pure type check: the result may be non-compact, so | ||
| it is typed as int without compactness and follow-up int guards stay. */ | ||
| {NB_ADD, NULL, longs_add, &PyLong_Type, 1, &PyLong_Type, &PyLong_Type}, | ||
| {NB_SUBTRACT, NULL, longs_subtract, &PyLong_Type, 1, &PyLong_Type, &PyLong_Type}, | ||
| {NB_INPLACE_ADD, NULL, longs_add, &PyLong_Type, 1, &PyLong_Type, &PyLong_Type}, | ||
| {NB_INPLACE_SUBTRACT, NULL, longs_subtract, &PyLong_Type, 1, &PyLong_Type, &PyLong_Type}, | ||
|
|
||
| /* float-long arithmetic: guards also check NaN and compactness. */ | ||
| {NB_ADD, float_compactlong_guard, float_compactlong_add, &PyFloat_Type, 1, NULL, NULL}, | ||
| {NB_SUBTRACT, float_compactlong_guard, float_compactlong_subtract, &PyFloat_Type, 1, NULL, NULL}, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add comment, explaining why this added.