String copy idiom.

David W. Donald dwd at ccice6.UUCP
Thu Mar 21 09:50:30 AEST 1985


>
> The idiom "while (*s++ = *t++);" generates the fastest possible code
> if s and t are declared to be registers ...
>

Beware of saying things like "the fastest possible".
I found a better way for for any string longer than two characters.
The following is from 4.2BSD on a VAX.

main()
{
	register char *s, *d;
	while( *d++ = *s++ );			/* ok */
	if (*d) do {} while ( *d++ = *s++ );	/* <-- FAST -- */
}

_main:
L16:	/* ok: a 3 instruction loop */
	movb	(r11)+,(r10)+
	jeql	L17
	jbr 	L16
L17:
	/* FAST: a two instruction loop */
	tstb	(r10)
	jeql	L18
L21:
L20:
	movb	(r11)+,(r10)+
	jneq	L21
L19:
L18:
	ret



More information about the Comp.lang.c mailing list