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: felix
Subject: Re: [Chicken-users] Re: (sorry, ist verlorengegangen)
Date: Mon, 28 Jul 2003 23:48:05 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021130

Bruce Hoult wrote:
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.


BTW, for more pearls like this, see Hank Warren's "Hackers Delight".


cheers,
felix






reply via email to

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