-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-29882: Add an efficient popcount method for integers #771
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 11 commits
74e3ea6
16cc8e5
19da190
070ac45
b87f162
48084e7
4c6819a
729737c
79f4d16
e3228cf
a0fed82
46d8bc9
3e8422b
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,2 @@ | ||
Add :meth:`int.bit_count()`, counting the number of ones in the binary | ||
representation of an integer. Patch by Niklas Fiekas. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5304,6 +5304,75 @@ int_bit_length_impl(PyObject *self) | |
return NULL; | ||
} | ||
|
||
static int | ||
popcount_digit(digit d) | ||
{ | ||
/* 32bit SWAR popcount. */ | ||
uint32_t u = d; | ||
u -= (u >> 1) & 0x55555555; | ||
u = (u & 0x33333333) + ((u >> 2) & 0x33333333); | ||
u = (u + (u >> 4)) & 0x0f0f0f0f; | ||
return (u * 0x01010101) >> 24; | ||
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 just realised that this line isn't quite portable. The code is relying on the multiplication being a multiplication of 32-bit integers, so that the result is implicitly reduced modulo However, if we're on a platform with a 64-bit 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. Fix implemented, in the shape of an extra Note that the But with the The other 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. Python/hamt.c has basically a copy of this function, it's just written a little bit differently, but its return type is uint32_t. It exists since Python 3.8 and we didn't get any bug report :-) I added pycore_byteswap.h. Maybe we could have a pycore_bitutils.h for such function and declare it as static inline, to reuse it here and in hamt.c. Or it can be done later ;-) @pitrou also asked if "pycore_byteswap.h" filename was not too specific, maybe we could pick a more generic name to put more functions there. Like "bits and bytes utilities" :-) Note: _testinternalcapi has tests for the byte swap functions ;-) We could have tests on this popcount function as well to detect if a compiler "miscompiles" this function. 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'm volunteer to move this function as static inline function and add the new test ;-) As I wrote, it can be done later. 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.
That's probably because it was never run on a machine with a 64-bit C The version of the code in Realistically, the undefined behaviour is unlikely to ever be an issue in practice. But the wrong result with a 64-bit 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'll merge this, then we can look at combining the implementations or fixing the |
||
} | ||
|
||
/*[clinic input] | ||
int.bit_count | ||
|
||
Number of ones in the binary representation of the absolute value of self. | ||
|
||
Also known as the population count. | ||
|
||
>>> bin(13) | ||
'0b1101' | ||
>>> (13).bit_count() | ||
3 | ||
[clinic start generated code]*/ | ||
|
||
static PyObject * | ||
int_bit_count_impl(PyObject *self) | ||
/*[clinic end generated code: output=2e571970daf1e5c3 input=7e0adef8e8ccdf2e]*/ | ||
{ | ||
mdickinson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert(self != NULL); | ||
assert(PyLong_Check(self)); | ||
|
||
PyLongObject *z = (PyLongObject *)self; | ||
Py_ssize_t ndigits = Py_ABS(Py_SIZE(z)); | ||
Py_ssize_t bit_count = 0; | ||
|
||
/* Each digit has up to PyLong_SHIFT ones, so the accumulated bit count | ||
from the first PY_SSIZE_T_MAX/PyLong_SHIFT digits can't overflow a | ||
Py_ssize_t. */ | ||
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. Nice comment, thanks! Before the comment, I was surprised by the two loops. |
||
Py_ssize_t ndigits_fast = Py_MIN(ndigits, PY_SSIZE_T_MAX/PyLong_SHIFT); | ||
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 proposed to work use size_t for bit_count, but then I realzied that i is a Py_ssize_t, so it's better to leave the code as it is: work on Py_ssize_t for i and bit_count. |
||
for (Py_ssize_t i = 0; i < ndigits_fast; i++) { | ||
bit_count += popcount_digit(z->ob_digit[i]); | ||
} | ||
|
||
PyObject *result = PyLong_FromSsize_t(bit_count); | ||
if (result == NULL) { | ||
return NULL; | ||
niklasf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/* Use Python integers if bit_count would overflow. */ | ||
for (Py_ssize_t i = ndigits_fast; i < ndigits; i++) { | ||
PyObject *x = PyLong_FromLong(popcount_digit(z->ob_digit[i])); | ||
if (x == NULL) { | ||
goto error; | ||
} | ||
PyObject *y = long_add((PyLongObject *)result, (PyLongObject *)x); | ||
Py_DECREF(x); | ||
if (y == NULL) { | ||
goto error; | ||
} | ||
Py_DECREF(result); | ||
result = y; | ||
} | ||
|
||
return result; | ||
|
||
error: | ||
Py_DECREF(result); | ||
return NULL; | ||
} | ||
|
||
/*[clinic input] | ||
int.as_integer_ratio | ||
|
@@ -5460,6 +5529,7 @@ static PyMethodDef long_methods[] = { | |
{"conjugate", long_long_meth, METH_NOARGS, | ||
"Returns self, the complex conjugate of any int."}, | ||
INT_BIT_LENGTH_METHODDEF | ||
INT_BIT_COUNT_METHODDEF | ||
INT_TO_BYTES_METHODDEF | ||
INT_FROM_BYTES_METHODDEF | ||
INT_AS_INTEGER_RATIO_METHODDEF | ||
|
Uh oh!
There was an error while loading. Please reload this page.