Re:Counting set bits in an interesting way
by robalni on Saturday April 30, 2022 @01:38PM
(#31217665)
Speaking of the while head, after posting this I realized that there is an optimization you can do by moving the bitshift into the while head, like this:
while (x >>= 1)
This makes gcc compile the code to one instruction less per iteration because it can use the status flags generated by the bitshift to determine whether to jump. Now the loop will only be 3 instructions long and the entire function 8 instructions. https://godbolt.org/z/fna8de367
while (x >>= 1)
This makes gcc compile the code to one instruction less per iteration because it can use the status flags generated by the bitshift to determine whether to jump. Now the loop will only be 3 instructions long and the entire function 8 instructions. https://godbolt.org/z/fna8de367