I figured it out. Can't believe it's that simple, I spent the last half an hour on it.
It's OK that the check
if (((longword - lomagic) & himagic) != 0)
lets values like 0x81818181
pass, because if it passes, the following test on every byte would not return since there are no all-zero byte. So the loop can continue to test the next longword
.
The algorithm behind the check is based on Determine if a word has a zero byte
unsigned int v;
bool hasZeroByte = ~((((v & 0x7F7F7F7F) + 0x7F7F7F7F) | v) | 0x7F7F7F7F);
In 2's complement, - 0x01010101
has the same effect with + 0xFEFEFEFF
. The difference is because glibc doesn't have v & 0x7F7F7F7F
, which makes sure the bytes in the word has the most significant bit of 0
. This prevents examples like 0x81818181
, but glibc omits it because it doesn't have to let it pass as stated earlier, The check is correct as long as it won't miss any word that does have all-zero bytes.