Re: 2.1.120pre2: Bug in strnicmp() noted in 2.1.119pre1 is still

Etienne Lorrain (lorrain@fb.sony.de)
Wed, 2 Sep 1998 13:11:18 +0001


thospel@mail.dma.be (Ton Hospel) wrote:
> This one is somewhat faster (one less test in the main loop) and
> (I think) cleaner. (It does assume tolower(n)==0 iff n==0)
>
> static int strnicmp(const char *s1, const char *s2, int len)
> {
> /* Yes, Virginia, it had better be unsigned */
> unsigned char c1, c2;
>
> while (len) {
> c1 = *s1;
> s1++;
> c2 = *s2;
> s2++;
> if (c1 != c2) {
> c1 = tolower(c1);
> c2 = tolower(c2);
> if (c1 != c2)
> return c1 < c2 ? -1 : 1;
> }
> if (!c1) return 0;
> len--;
> }
> return 0;
> }

If you want to help compiler, try to decrement and test
in the same 'C' line (i.e. without an assembly jump in
between).

For instance,
while (len) {
....
len --;
}
is usually not completely optimised, the flags set up by the
decrement are not reused in the while().

This will usually execute one assembly "cmp" less per loop,
but takes more code space:
if (!len)
return 0;
do {
....
} while (--len);

I did not check it for GCC/EGCS, just on some other commercial
compilers.

Anyway with current processor, so two or three level cache,
dynamic register allocation, speculative execution, branch
prediction, mesures will look like /dev/random, IMHO.

just my $0.002,
Etienne.

--
-- The world belong to its organizer.

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html