10000 bpo-46055: Speed up binary shifting operators by xuxinhang · Pull Request #30044 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46055: Speed up binary shifting operators #30044

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

Merged
merged 18 commits into from
Dec 27, 2021
Merged
Changes from 1 commit
Commits
File filter
8000

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
Refactor: Clean code.
  • Loading branch information
xuxinhang committed Dec 23, 2021
commit c00a963661a9a48a9b29119b5f7eded570a37d77
17 changes: 8 additions & 9 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4496,11 +4496,11 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
digit lomask, himask, omitmark;

if (IS_MEDIUM_VALUE(a)) {
stwodigits x;
if (wordshift > 0) {
return get_small_int(-(Py_SIZE(a) < 0));
}
x = Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, medium_value(a), remshift);
stwodigits m, x;
digit shift;
m = medium_value(a);
shift = wordshift == 0 ? remshift : PyLong_SHIFT;
x = m < 0 ? ~(~m >> shift) : m >> shift;
return _PyLong_FromSTwoDigits(x);
}

Expand Down Expand Up @@ -4590,14 +4590,13 @@ long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
Py_ssize_t oldsize, newsize, i, j;
twodigits accum;

if (wordshift == 0 && IS_MEDIUM_VALUE(a) &&
(a->ob_digit[0] & ~(PyLong_MASK >> remshift)) == 0) {
if (wordshift == 0 && IS_MEDIUM_VALUE(a)) {
stwodigits m = medium_value(a);
// bypass undefined shift operator behavior
stwodigits x = (stwodigits)((twodigits)medium_value(a) << remshift);
stwodigits x = m < 0 ? -(-m << remshift) : m << remshift;
return _PyLong_FromSTwoDigits(x);
}

/* This version due to Tim Peters */
oldsize = Py_ABS(Py_SIZE(a));
newsize = oldsize + wordshift;
if (remshift)
Expand Down
0