6893554 (original) (raw)

Author comments:

The problem occurs with negative numbers, as the 32-bit input values
are sign extended into the 64-bit registers. When doing:

// x |= (x >> 1); // x |= (x >> 2); // x |= (x >> 4); // x |= (x >> 8); // x |= (x >> 16); // return (WORDBITS - popc(x));

__ srl(Rsrc, 1, Rtmp);
__ or3(Rsrc, Rtmp, Rdst);
<snip>

the 32-bit logical right shift clears the upper 32 bits but the OR
with the source value adds in the sign extended upper 32 bits again.
This leads to a POPC value for e.g. -1 of 64 instead of 32.

The fix is to zero-extend the 32-bit source value before applying the
algorithm on it.

Additionally two typos are fixed.