String copy idiom.

Griff Smith ggs at ulysses.UUCP
Sat Mar 9 12:56:09 AEST 1985


1) I don't think the second idiom is any clearer.

2) On my VAX compiler, the code for the "easier to read" idiom is worse
   than that for the compact one.  The only advantage of the long-winded
   idiom is that it doesn't change much if you neglect to declare register
   variables; it's a bit slow either way.  The following test cases show
   the difference:

-----

test(osp, isp)
char *osp, *isp;
{
register char *ra, *rb;
char *ma, *mb;

ra = osp;
rb = isp;

/* case 1, register pointers */

while (*ra++ = *rb++);

/* assembly code

L16:
	movb	(r10)+,(r11)+
	jeql	L17
	jbr 	L16
L17:

*/

ma = osp;
mb = isp;

/* case 2, memory pointers */

while (*ma++ = *mb++);

/* assembly code

L18:
	movl	-8(fp),r0
	incl	-8(fp)
	movl	-4(fp),r1
	incl	-4(fp)
	movb	(r0),(r1)
	jeql	L19
	jbr 	L18
L19:

*/

ra = osp;
rb = isp;

/* case 3, register pointers, "easy to read" loop */

while ((*ra = *rb) != '\0')
{
    ra++;
    rb++;
}

/* assembly code

L20:
	movb	(r10),(r11)
	jeql	L21
	incl	r11
	incl	r10
	jbr 	L20
L21:

*/

ma = osp;
mb = isp;

/* case 4, memory pointers, "easy to read" loop */

while ((*ma = *mb) != '\0')
{
    ma++;
    mb++;
}

/* assembly code

L22:
	movb	*-8(fp),*-4(fp)
	jeql	L23
	incl	-4(fp)
	incl	-8(fp)
	jbr 	L22
L23:

*/

}
-- 

Griff Smith	AT&T Bell Laboratories, Murray Hill
Phone:		(201) 582-7736
Internet:	ggs at ulysses.uucp
UUCP:		ulysses!ggs  ( {allegra|ihnp4}!ulysses!ggs )



More information about the Comp.lang.c mailing list