ENH: Expose `bit_count` ufunc equivalent to the scalar methods
See original GitHub issueModern CPUs have a builtin popcount instruction that counts the number of 1 bit in an unsigned integer. It would be great if numpy has a routine that exposes this operation.
Both GCC and MSVC have builtin functions for this instruction.
Possible usage:
>>> np.popcount(4)
1
>>> np.popcount(np.arange(5, dtype=np.uint))
array([0, 1, 1, 2, 1])
Issue Analytics
- State:
- Created 3 years ago
- Reactions:23
- Comments:33 (22 by maintainers)
Top Results From Across the Web
Universal functions (ufunc) — NumPy v1.24 Manual
A universal function (or ufunc for short) is a function that operates on ndarrays in an element-by-element fashion, supporting array broadcasting, ...
Read more >Computation on NumPy Arrays: Universal Functions
This section motivates the need for NumPy's ufuncs, which can be used to make repeated calculations on array elements much more efficient. It...
Read more >Chapter 4. NumPy Basics: Arrays and Vectorized Computation
Arrays enable you to perform mathematical operations on whole blocks of data using similar syntax to the equivalent operations between scalar elements:
Read more >Universal functions (ufunc) — NumPy v1.9 Manual
That is, a ufunc is a “vectorized” wrapper for a function that takes a fixed number of scalar inputs and produces a fixed...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Table-driven is probably the best choice for this situation, and this is a nice implementation. Thanks!
That said, still want a
popcount
primitive. 😃Frankly I think we should add both; a new
bit_count
ufunc, and anp.integer.bit_count()
wrapper just to make numpy ints work slightly better whereint
is expected