Comparing strings...

Paul John Falstad pfalstad at dry.Princeton.EDU
Tue Oct 16 01:01:47 AEST 1990


In article <1990Oct15.042851.18595 at zoo.toronto.edu> henry at zoo.toronto.edu (Henry Spencer) writes:
>In article <3343 at idunno.Princeton.EDU> pfalstad at drops.Princeton.EDU (Paul John Falstad) writes:
>>	while((c = (unsigned char) *s++) == (d == (unsigned char) *t++) && c);
>Uh-uh, you don't want to do the conversions inside the loop; they can add
>significant expense, and are irrelevant to an equality comparison.  You
>do the conversions -- and conversions *back* to int so the difference
>is signed -- in the post-loop wrapup.

You're right, I forgot to convert back to int; but no, it doesn't
matter where you do the `conversions,' as you call them, though that
word implies that the compiler is generating instructions to actually
convert the data from signed to unsigned.  What it is really doing is
generating different instructions to handle c and d as unsigned chars.
In fact, I tried both ways, compiling with gcc -O -S; doing the
conversions outside the loop actually generated more instructions on my
SPARCstation.  Here's a diff:

12c12,13
< 	and %l0,0xff,%o5
---
> 	sll %l0,0x18,%o5
> 	sra %o5,0x18,%o5
15,16c16,18
< 	and %o4,0xff,%o1
< 	ldub [%o2],%o0
---
> 	sll %o4,0x18,%o1
> 	sra %o1,0x18,%o1
> 	ldsb [%o2],%o0

When I did the conversion inside the loop, gcc just generated an and
instruction to mask out the upper 24 bits, but when I did the conversion
outside the loop, gcc had to sign-extend the byte with a
shift-left-logical and a shift-right-arithmetic.

What I should have done was to declare s and t as pointers to
unsigned chars (since I hate signed chars), but then the compiler would
have complained, since the string functions are usually prototyped with
pointers to signed chars.

>By the way, tacking the ";" on the end of the while is a great way to
>trip up your readers, which is thought amusing by amateurs and is avoided
>by professionals.

I'm sorry if it confused you; I'm quite used to it.  I think the
indentation makes my intent clear in most cases.  We amateurs have to
get our amusement somehow.

--
Paul Falstad, pfalstad at phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
"And she's always on about men following her.  I don't know what she
thinks they're going to do to her.  Vomit on her, Basil, says."-Flowery Twats



More information about the Comp.lang.c mailing list