Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 (Tek) 9/28/84 based on 9/17/84; site tekgvs.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!bellcore!decvax!tektronix!tekcrl!tekgvs!regisc From: regisc@tekgvs.UUCP (Regis Crinon) Newsgroups: net.lang.c Subject: Re: String copy idiom. Message-ID: <1071@tekgvs.UUCP> Date: Wed, 13-Mar-85 12:56:34 EST Article-I.D.: tekgvs.1071 Posted: Wed Mar 13 12:56:34 1985 Date-Received: Sat, 16-Mar-85 02:24:06 EST References: <7044@watdaisy.UUCP> <3448@alice.UUCP> <209@gitpyr.UUCP> Organization: Tektronix, Beaverton OR Lines: 88 > >< > Posted from ark@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