Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!decwrl!pa.dec.com!bacchus!mwm From: mwm@pa.dec.com (Mike (My Watch Has Windows) Meyer) Newsgroups: comp.sys.amiga.programmer Subject: Re: Lemmings - a tutorial Part V (last) Message-ID: Date: 4 Apr 91 11:02:33 GMT References: <1991Mar31.003933.1483@mintaka.lcs.mit.edu> <1991Apr1.020748.26863@mintaka.lcs.mit.edu> Sender: news@pa.dec.com (News) Organization: Missionaria Phonibalonica Lines: 101 In-Reply-To: mykes@amiga0.SF-Bay.ORG's message of 1 Apr 91 10:14:03 GMT In article mykes@amiga0.SF-Bay.ORG (Mike Schwartz) writes: >I don't have SAS C on the Amiga, but I'm sure it produces simular results. > >/* test.c */ > >char buf[20]; >main() >{ > char *d=(char *)&buf; > const char *s="This is a test\n"; > while(*s) { *d++=*s++; } >} > /* Test.s produced by gcc */ #NO_APP gcc_compiled.: .text LC0: .ascii "This is a test\12\0" .even .globl _main _main: ; (cycles) lea _buf,a1 ; 8 lea LC0,a0 ; 8 tstb a0@ ; 8 jeq L5 ; 8 L4: moveb a0@+,a1@+ ; 14*12 tstb a0@ ; 14*8 jne L4 ; 13*10+1*8 L5: rts ; 16 .comm _buf,20 ;/* test.s produced by me */ ; (cycles) lea text(pc),a0 ; 8 lea buf(pc),a1 ; 8 .loop move.b (a0)+,(a1)+ ; 14*12 bne.s .loop ; 13*10+1*8 rts ; 16 text dc.b 'This is a test',10,0 buf ds.b 20 NO COMPARISON DUDE! GCC makes 3 totally wasted instructions, and one of them is inside your loop. Well, since you declared open season on tweaking the algorithm, I rewrote that short program with the _obvious_ C code (it's not what I'd call wonderful to start with). The algorithm used will work with any valid C pointer to char, even those that point to null strings. It may even correclty ignore null pointers (but I wouldn't bet on it). I got this out of SAS C: LEA 0012(PC),A0 LEA 01.00000000,A1 MOVE.L (A0)+,(A1)+ MOVE.L (A0)+,(A1)+ MOVE.L (A0)+,(A1)+ MOVE.L (A0)+,(A1)+ RTS In your own words "NO COMPARISON DUDE!" It got rid of the loop completely. It turned 18 memory references into four, a major win on stock 680[01]0s. Since loop unrolling - especially from small objects to machine words - is one of the oldest tricks in the book, I'm surprised you didn't know about it. Just think, the OS ROM routines are written in 'C' and compiled with a lesser compiler than gcc (5 years ago). From the above, it looks like it could have been worse - it could have been written by a lesser assembler programmer (I'm assuming you're a greater one) 5 years ago. For the curious, here's the modified source... #include char buf[20] void main(void) { char *d=buf; char *s="This is a test\n"; strcpy(d, s); } Note that the const is now gone; strcpy's second argument is not declared const, so passing it a pointer to const is questionable.