Use the popcount64c bit-counting sequence in Dar_WordCountOnes - #533
Open
TrevorHansen wants to merge 1 commit into
Open
Use the popcount64c bit-counting sequence in Dar_WordCountOnes#533TrevorHansen wants to merge 1 commit into
TrevorHansen wants to merge 1 commit into
Conversation
The sequence currently used is popcount64a, which no GCC from 7 to 16 and no clang from 9 to 22 recognises as a population count, so it always emits 23 instructions. popcount64c is recognised by GCC 10 and later and clang 10 and later, which emit a single popcnt where the target has the instruction, and is 8 instructions shorter where it does not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The popcount (hamming weight) instruction has been common on x86 processors since about 2008. It returns the number of 1-bits in a register with 3-cycle latency and 1-cycle throughput.
ABC uses hand rolled SIMD within a register (SWAR) to achieve the same.
When passed the -mpopcnt flag, clang and GCC will pattern match and replace some matching SWAR with a popcount instruction.
Unfortunately, the SWAR above is not recognised by any GCC I tested (7 to 16), or any clang (9 to 22), so it emits 23 instructions instead of 1.
Popcounts are in other places in ABC (e.g. #436), so other users would benefit if the other uses were sped up. Only this function is in our hot-path - some CNF generation problems have 7% of their runtime in
Dar_WordCountOnes, so I've focused on it.The above function is the naive implementation (popcount64a) from:
https://en.wikipedia.org/wiki/Hamming_weight
This PR changes to the best implementation (popcount64c) from the same page:
Which is converted by GCC 10+ and clang 10+ to a single instruction, and is 8 fewer instructions if not compiled with -mpopcnt.