String copy idiom.
Regis Crinon
regisc at tekgvs.UUCP
Thu Mar 14 03:56:34 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
*** REPLACE THIS LINE WITH YOUR MESSAGE ***
Ok. Following are the 68000 compiler results:
i) Using while(*s++ = *t++);
07530 12D8 MOVE.B (A0)+,(A1)+
07532 6600FFFC BNE.L $007530
07536
ii) Using while((*s = *t) != '\0'){s++;t++}
07530 1290 MOVE.B (A0),(A1)
07532 6706 BEQ.S $00753A
07534 5286 ADDQ.L #1,A1
07536 5288 ADDQ.L #1,A0
07538 60F6 BRA.S $007530
0753A
It seems to me that version i) is faster and requires less memory.
--
crinon
More information about the Comp.lang.c
mailing list