-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Add an efficient popcount method for integers #74068
Comments
An efficient popcount (something equivalent to bin(a).count("1")) would DESIGN DECISIONS
SURVEY gmpy calls the operation popcount and returns -1/None for negative values: >>> import gmpy2
>>> gmpy2.popcount(-10)
-1
From the documentation [1]:
(I am not a fan of the arbitrary return value). The bitarray module has a count(value=True) method: >>> from bitarray import bitarray
>>> bitarray(bin(123456789).strip("0b")).count()
16 Bitsets [2] exposes __len__. There is an SSE4 POPCNT instruction. C compilers call the corresponding Rust calls the operation count_ones [3]. Ones are counted in the binary Introducing popcount was previously considered here but closed for lack Sensible names could be bit_count along the lines of the existing PERFORMANCE $ ./python -m timeit "bin(123456789).count('1')" # equivalent
1000000 loops, best of 5: 286 nsec per loop
$ ./python -m timeit "(123456789).bit_count()" # fallback
5000000 loops, best of 5: 46.3 nsec per loop [1] https://gmpy2.readthedocs.io/en/latest/mpz.html#mpz-functions |
Can you give some examples of concrete use-cases? I've spent the last six years or so writing scientific applications and parsing all sorts of odd binary formats, and haven't needed or wanted a popcount yet.
Agreed: if this were implemented, I think raising ValueError would be the most appropriate thing to do for negative inputs. |
Searching popcount in Python files on GitHub yields https://github.com/search?utf8=%E2%9C%93&q=popcount+extension%3Apy&type=Code Perhaps intresting:
Probably most important:
--- Btw. not a concrete application. I just stumbled upon this. |
Many of those applications are really for bitstrings (chess bitboards, hamming distance), which aren't really the same thing as integers. Nice find for the mathmodule.c case. I'd forgotten about that one (though according to git blame, apparently I'm responsible for checking it in). It's a fairly obscure corner case, though. Overall, I'm -1 on adding this: I don't think it meets the bar of being useful enough to justify the extra method. I'd suggest that people needing this kind of efficient bitstring operation use a 3rd-party bitstring library instead. |
I think that adding bitarray or bitset (or both) in the stdlib would better satisfy the needs. There are open issues for adding ability to read or set selected bits or range of bits in int or for bitwise operations on bytes. I think that bitarray and bitset would provide better interface for these operations. |
See also:
As that says, there are a number of languages and processors with first class support for a popcount function. I've frequently implemented it in Python when using integers as integer bitsets ( |
I like the name bit_count and I'll gladly add it to gmpy2 with the appropriate changes to exceptions, etc. |
Is everyone comfortable with how negative numbers are handled by this patch? It might be better to limit the domain and raise a ValueError rather than make a presumption about what the user intends. |
I am going to add the imath module. If we decide to add popcount(), it may be better to add it in this module instead of int class. |
Not entirely, but it's not terribly wrong and it's consistent with how |
I prefer that a negative int raise ValueError, but am OK with it using the absolute value instead (i.e., what it does now). |
Aimed at 3.8, or 3.9? This seems somewhat rushed for 3.8. |
I'm re-reviewing this discussion three years on. I'd be happy for this to go into 3.10. Are there other strong opinions? It would be good to either update and merge the PR, or close as rejected. |
I see I never explicitly said +1, so I will now: +1 on merging this :-) |
Adding a function to a new hypothetical imath module sounds reasonable. I'm less comfortable with adding a new method to int type: it would mean that any int subtype "should" implement it. For example, should numpy.int64 get this method as well? What is the effect on https://docs.python.org/3.9/library/numbers.html? Does it make sense to call (True).popcount()? |
Python/hamt.c contains an optimized function: static inline uint32_t
} Python/pymath.c provides a "unsigned int _Py_bit_length(unsigned long d)" function used by math.factorial, _PyLong_NumBits(), int.__format__(), long / long, _PyLong_Frexp() and PyLong_AsDouble(), etc. Maybe we could add a _Py_bit_count(). See also bpo-29782: "Use __builtin_clzl for bits_in_digit if available" which proposes to micro-optimize _Py_bit_length(). -- In the meanwhile, I also added pycore_byteswap.h *internal* header which provides static inline function which *do* use builtin functions like __builtin_bswap32(). |
That's for the NumPy folks to decide (and I've added Nathaniel Smith to the nosy in case he wants to comment), but I don't see any particularly strong reason that NumPy would need to add it. It looks as though the NumPy integer types have survived happily without a bit_length method, for example - I don't even see any issues in the NumPy tracker suggesting that anyone missed it. (Though perhaps that's because in the case of a NumPy int one always has at least an upper bound on the bit_length available.)
No effect, just as int.bit_length has no effect.
It would be spelled |
PR is updated and mergeable. |
Why are calling a population count method "bit_count()"? I might reasonable expect that 0b1000.bit_count() be 4, not 1. It does have 4 bits. I have no objection to adding this method, just the choice of name. |
Naming things is hard, but I don't think this is an unreasonable name, and it's not without precedent. Java similarly has Integer.bitCount and BigInteger.bitCount. MySQL has BIT_COUNT. |
A couple of other data points:
@mark Shannon: what name would you suggest, and why? The term "population count" feels too non-obvious and specialist to me, and anything involving "Hamming" likewise. "count_ones" isn't obviously a bit operation. "count_set_bits"? |
Well, bit_sum is what it really is. But I agree it's a terrible name. :-) |
Explicit is better than implicit. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: