question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: Expose `bit_count` ufunc equivalent to the scalar methods

See original GitHub issue

Modern 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:open
  • Created 3 years ago
  • Reactions:23
  • Comments:33 (22 by maintainers)

github_iconTop GitHub Comments

5reactions
BartMasseycommented, Jan 7, 2021

Table-driven is probably the best choice for this situation, and this is a nice implementation. Thanks!

That said, still want a popcount primitive. 😃

2reactions
eric-wiesercommented, Jun 27, 2021

Frankly I think we should add both; a new bit_count ufunc, and a np.integer.bit_count() wrapper just to make numpy ints work slightly better where int is expected

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found