Skip to content

Improved halide_popcount#7225

Merged
vksnk merged 3 commits intohalide:mainfrom
Aelphy:feature/popcount
Jan 25, 2023
Merged

Improved halide_popcount#7225
vksnk merged 3 commits intohalide:mainfrom
Aelphy:feature/popcount

Conversation

@Aelphy
Copy link
Copy Markdown
Contributor

@Aelphy Aelphy commented Dec 9, 2022

This is a slightly more efficient version of a popcount, which has as many iterations as many ones are in the binary representation.

Comment thread src/CodeGen_C.cpp
@@ -155,8 +155,9 @@ template<typename T>
inline int halide_popcount(T a) {
int bits_set = 0;
while (a != 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, ~all modern compilers should have popcount as an intrinsic -- see popcount64 in Util.cpp, maybe we should just adapt that here

@steven-johnson
Copy link
Copy Markdown
Contributor

What's the story on this, ready to land?

Comment thread src/CodeGen_C.cpp

template<typename T>
inline int halide_popcount(T a) {
inline int halide_popcount_fallback(T a) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave a link to the source for this algorithm for the reference (found this one https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan, for example).

Comment thread src/CodeGen_C.cpp Outdated
bits_set += a & 1;
a >>= 1;
bits_set += 1;
// NOTE(aelphy): remove least significant zeros and the first met one
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just make it a regular comment (i.e. remove NOTE(aelphy) part).

@steven-johnson
Copy link
Copy Markdown
Contributor

Where does this PR stand?

@vksnk vksnk merged commit dd973f4 into halide:main Jan 25, 2023
ardier pushed a commit to ardier/Halide-mutation that referenced this pull request Mar 3, 2024
* Improved halide_popcount

* reused popcount64 from Utils.cpp in CodeGen_C

* Fixed comment for popcount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants