-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
Changes from 1 commit
7b11d7d
403652a
dcd2796
25bbf4d
b2ef93d
7d2203f
932ad87
596b8cf
6545e9a
7f80cd9
b24c7dc
c00a963
b7ad599
c359521
241da4c
9ed09aa
c24ad0a
59672cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4502,47 +4502,53 @@ long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) | |
|
||
if (IS_MEDIUM_VALUE(a)) { | ||
stwodigits sval; | ||
if (wordshift > 0) | ||
if (wordshift > 0) { | ||
return get_small_int(-(Py_SIZE(a) < 0)); | ||
|
||
} | ||
sval = Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, medium_value(a), remshift); | ||
if (IS_SMALL_INT(sval)) | ||
if (IS_SMALL_INT(sval)) { | ||
return get_small_int((sdigit)sval); | ||
|
||
} | ||
z = _PyLong_New(Py_SIZE(a)); | ||
z->ob_digit[0] = (digit)(a->ob_digit[0] >> remshift); | ||
if (Py_SIZE(a) < 0 && (a->ob_digit[0] & ~(PyLong_MASK << remshift))) | ||
if (Py_SIZE(a) < 0 && (a->ob_digit[0] & ~(PyLong_MASK << remshift))) { | ||
z->ob_digit[0] += 1; | ||
} | ||
return (PyObject *)z; | ||
} | ||
|
||
newsize = Py_ABS(Py_SIZE(a)) - wordshift; | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (newsize <= 0) | ||
if (newsize <= 0) { | ||
return PyLong_FromLong(0); | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
z = _PyLong_New(newsize); | ||
if (z == NULL) | ||
if (z == NULL) { | ||
return NULL; | ||
} | ||
|
||
hishift = PyLong_SHIFT - remshift; | ||
lomask = ((digit)1 << hishift) - 1; | ||
himask = PyLong_MASK ^ lomask; | ||
for (i = 0, j = wordshift; i < newsize; i++, j++) { | ||
z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask; | ||
if (i + 1 < newsize) | ||
if (i + 1 < newsize) { | ||
z->ob_digit[i] |= (a->ob_digit[j + 1] << hishift) & himask; | ||
} | ||
} | ||
|
||
if (Py_SIZE(a) < 0) { | ||
Py_SET_SIZE(z, -newsize); | ||
omitmark = a->ob_digit[wordshift] & ((1 << remshift) - 1); | ||
for (j = 0; omitmark == 0 && j < wordshift; j++) | ||
for (j = 0; omitmark == 0 && j < wordshift; j++) { | ||
omitmark |= a->ob_digit[j]; | ||
} | ||
if (omitmark) { | ||
PyLongObject *z0 = z; | ||
z = (PyLongObject *) long_sub(z0, (PyLongObject *)_PyLong_GetOne()); | ||
Py_DECREF(z0); | ||
if (z == NULL) | ||
if (z == NULL) { | ||
return NULL; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -4596,8 +4602,9 @@ long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) | |
if (wordshift == 0 && IS_MEDIUM_VALUE(a) && | ||
(a->ob_digit[0] & ~(PyLong_MASK >> remshift)) == 0) { | ||
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 is the 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. I think it's necessary because 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. Oh I guess it's not necessary if we use 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's necessary to ensure the shifting result fits 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.
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. Ah, Okay. Thanks. |
||
stwodigits sval = medium_value(a) << remshift; | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (IS_SMALL_INT(sval)) | ||
if (IS_SMALL_INT(sval)) { | ||
return get_small_int((sdigit)sval); | ||
} | ||
z = _PyLong_New(Py_SIZE(a)); | ||
z->ob_digit[0] = (digit)(a->ob_digit[0] << remshift); | ||
return (PyObject *)z; | ||
|
Uh oh!
There was an error while loading. Please reload this page.