lkml.org 
[lkml]   [1998]   [Aug]   [31]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: 2.1.120pre2: Bug in strnicmp() noted in 2.1.119pre1 is still there!
Linus Torvalds wrote:

> Ok, shoot holes in this one instead.
>
> Linus
>
> -----
> 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; c2 = *s2;
> s1++; s2++;
> if (!c1)
> goto end_of_string1;
> if (!c2)
> goto end_of_string2;
> if (c1 != c2) {
> c1 = tolower(c1);
> c2 = tolower(c2);
> if (c1 != c2)
> goto different;
> }
> len--;
> }
> return 0;
>
> end_of_string1:
> return c2 ? -1 : 0;
>
> end_of_string2:
> return 1;
>
> different:
> return c1 < c2 ? -1 : 1;
> }

Cool, creative use of gotos keeps nested ifs to a minimum. Since this seems to
be hand optimized code you might as well move s1++;s2++ after the if
statements to save some cycles. That is if the compiler does'nt catch it.

Well, lets do a fast test... gcc -O1... gcc -O2... gcc-O3... gcc -O4.
Yup gcc -O4 catches this and generates the same code regardless of where I put
the s1++;s2++. egcs cathes this at -O3. I must be tired why do I bother
checking all this...

Hans Eric (hes@xinit.se)



-
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

\
 
 \ /
  Last update: 2005-03-22 13:44    [W:1.195 / U:0.080 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site