String copy idiom.

Chris Torek chris at umcp-cs.UUCP
Sat Mar 16 15:02:37 AEST 1985


I didn't really want to make a fuss over it, but robert at gitpyr is
correct; the reason "*s++ = *t++" is "harder" on Pyramids and other
such machines is that they have no auto-increment addressing modes.  If
you write

    while (*s = *t) s++, t++;

then both s and t point *to* the final '\0'; if you write

    while (*s++ = *t++) /* null */;

then both s and t point *past* the final '\0'.  The obvious way to
generate the sequence "*s++" on a machine without autoincrement, when
the value of the assignment might also be required, is

    copy s to temp
    incr s
    indirect using temp

This is, of course, not always the best way.  The increment can often
be deferred, saving the temporary register (or top of stack) and the copy.

In the particular code samples for strcpy() used earlier, a truly
optimizing compiler would "realize" that both codings have the same
semantics, and generate the most optimal instruction sequence for
both.  (They are the same since the values of s and t are not used
except for indirections---at which time they have the same
values---and, in theory, are inaccessible outside the function itself.
In actuality they are usually in some registers or stack frame and thus
accessible, but such possibilities are normally ignored during
optimization [which of course is the reason for introducing the
"volatile" keyword].)

I am surprised that the VMS C compiler doesn't realize that all it
needs to do is generate a loop of inline "locc" and "movc3"
instructions :-).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at maryland



More information about the Comp.lang.c mailing list