[net.lang.c] fast string copy

colonel@gloria.UUCP (Col. G. L. Sicherman) (03/28/85)

["In the service of Virman Vundabar we learn perfection!"]

> I think this is cute, how VAX/VMS beats Unix at its own game.  The VMS C
> compiler generates code as good as or better than anything I have seen posted
> so far!
> 
> 	char *s,*t;
> 	while (*s++ = *t++);
> 
> generates (on a VAX 780):
> 
> 		movb	(r2)+,(r1)+
> 		beql	sym.2
> 	sym.1:
> 		movb	(r2)+,(r1)+
> 		bneq	sym.1
> 	sym.2:
> 
> This is as fast as you can get.

Careful!  You can probably speed it up by unrolling the loop....
-- 
Col. G. L. Sicherman
...{rocksvax|decvax}!sunybcs!colonel

ark@alice.UUCP (Andrew Koenig) (03/29/85)

> I think this is cute, how VAX/VMS beats Unix at its own game.  The VMS C
> compiler generates code as good as or better than anything I have seen posted
> so far!
> 
> 	char *s,*t;
> 	while (*s++ = *t++);
> 
> generates (on a VAX 780):
> 
> 		movb	(r2)+,(r1)+
> 		beql	sym.2
> 	sym.1:
> 		movb	(r2)+,(r1)+
> 		bneq	sym.1
> 	sym.2:
> 
> This is as fast as you can get.

Ummm... I hate to be a spoilsport, but I do not understand why
the code above is any faster than

	sym.1:
		movb	(r2)+,(r1)+
		bneq	sym.1

which is essentially what the Unix compilers generate.
Apparently, the VMS compiler is trying to be clever by
rewriting

	while (cond) stmt;

into

	if (cond) { do stmt; while (cond);}

This rewriting generally buys speed at the expense of space,
but in this particular case the speed gain is 0 and the space
cost is a factor of two.

By the way, Colonel, this loop is not improved by unrolling.