String copy idiom.

Robert Viduya robert at gitpyr.UUCP
Mon Mar 11 18:56:54 AEST 1985


><
Posted from  ark at alice.UUCP (Andrew Koenig)
> If s and t are char pointers in registers,
> 
> 	while (*s++ = *t++) ;
> 
> generates the best code I could possibly imagine.
> 
> 	while ((*s = *t) != '\0') {s++; t++;}
> 
> is considerably worse.  Try it with register variables on your compiler.

Ok, here's the results.  This was done on a Pyramid 90x ("-O" on the cc command;
disassembled the results).

---First Method---
Code:
    while (*s++ = *t++)
	;

Assembly:
 00000058: 01000870                movw     lr1,tr0
 0000005c: 14000061                addw     $1,lr1
 00000060: 01000831                movw     lr0,tr1
 00000064: 14000060                addw     $1,lr0
 00000068: 81100c31                movb     (tr0),(tr1)
 0000006c: 32200c70                cvtbw    (tr1),tr0
 00000070: f024fffa                bfc      z,0x58

---Second Method---
Code:
    while ((*s = *t) != '\0') {
	s++;
	t++;
    }

Assembly:
 00000054: f0200003                br       0x60
 00000058: 14000060                addw     $1,lr0
 0000005c: 14000061                addw     $1,lr1
 00000060: 81100860                movb     (lr1),(lr0)
 00000064: 32200830                cvtbw    (lr0),tr0
 00000068: f024fffc                bfc      z,0x58

------
 Seems that the second method (the longer C version) actually takes one less
 instruction and also uses one less register (the first one used tr0 & tr1; the
 second only needed tr0). [For the unfamilier, at any given time, the Pyramid
 has available 16 global registers (gr0-gr15), 16 parameter registers
 (pr0-pr15), 16 local registers (lr0-lr15) and 16 temporary registers
 (tr0-tr15).]

 I think what matters here is whether your machine has an auto-increment/
 decrement type of instruction.  I'm not sure if the Pyramid does or not,
 but obviously, the C compiler doesn't use it, so I think I can safely assume
 that it does not.  Anyone want to try this on a 68000?

				robert
-- 
Robert Viduya
Georgia Institute of Technology

...!{akgua,allegra,amd,hplabs,ihnp4,masscomp,ut-ngp}!gatech!gitpyr!robert
...!{rlgvax,sb1,uf-cgrl,unmvax,ut-sally}!gatech!gitpyr!robert



More information about the Comp.lang.c mailing list