On Sat, 2003-07-26 at 22:21, address@hidden wrote:
(And memcmp can be implemented more efficiently than strncmp, I hope,
because you don't have to test for \0 and (on bigendian machines only?, with
appropriate string alignment) one can compare more than a byte inside the
comparison loop of memcmp. BTW: string comparison is an area where Scheme can
outperform C :-) )
In fact it's possible and practical to copy or compare C strings more
than one byte at a time, though it will never be *quite* as fast as if
you didn't have to deal with null termination.
The C expression...
~n & (n - 0x01010101) & 0x80808080
... gives zero if and only if none of the bytes in the n are nonzero.
If the result is nonzero then the position of the first 1 tells you which
byte was nonzero.
This expression is three instructions on many machines, which doesn't add
too much overhead to the several instructions already needed to laod and
store the data, especially on modern machines with spare integer units
sitting around.