chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Re: (sorry, ist verlorengegangen)


From: Bruce Hoult
Subject: Re: [Chicken-users] Re: (sorry, ist verlorengegangen)
Date: 27 Jul 2003 19:05:40 +1200

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.

Of course the obvious extension to 64 or 128 bits works fine too if you
have an Athlon-64 or PPC G5.  Or a vector unit such as on the PPC (and I
assume others), which lets you do the test even more directly and 128 bits
at a time.

-- 
Bruce Hoult <address@hidden>





reply via email to

[Prev in Thread] Current Thread [Next in Thread]