Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site gitpyr.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!ihnp4!houxm!whuxl!whuxlm!akgua!gatech!gitpyr!robert From: robert@gitpyr.UUCP (Robert Viduya) Newsgroups: net.lang.c Subject: Re: String copy idiom. Message-ID: <209@gitpyr.UUCP> Date: Mon, 11-Mar-85 03:56:54 EST Article-I.D.: gitpyr.209 Posted: Mon Mar 11 03:56:54 1985 Date-Received: Wed, 13-Mar-85 00:39:12 EST References: <7044@watdaisy.UUCP> <3448@alice.UUCP> Organization: Georgia Tech, Atlanta Lines: 64 >< 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