Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site tellab2.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!ihnp4!tellab1!tellab2!thoth From: thoth@tellab2.UUCP (Marcus Hall) Newsgroups: net.lang.c Subject: Re: Tricky way to unroll string copy loops Message-ID: <235@tellab2.UUCP> Date: Wed, 10-Apr-85 17:38:26 EST Article-I.D.: tellab2.235 Posted: Wed Apr 10 17:38:26 1985 Date-Received: Thu, 11-Apr-85 02:04:43 EST References: <799@u1100a.UUCP> <582@lsuc.UUCP> Reply-To: thoth@tellab2.UUCP (Marcus Hall) Organization: Tellabs, Inc., Lisle, IL Lines: 50 In article <582@lsuc.UUCP> msb@lsuc.UUCP (Mark Brader) codes: (inside a routine to string copying in an unrolled loop) > if (count > 0) { > n = (count+7) / 8; > switch (count % 8) { > > case 0: do { *to++ = *from++; > case 7: *to++ = *from++; > case 6: *to++ = *from++; > case 5: *to++ = *from++; > case 4: *to++ = *from++; > case 3: *to++ = *from++; > case 2: *to++ = *from++; > case 1: *to++ = *from++; > } while (--n > 0); > } > } Which is very clever, but it seems that still better code should be generated by the following: n = count / 8; switch (count % 8) { case 7: *to++ = *from++; case 6: *to++ = *from++; case 5: *to++ = *from++; case 4: *to++ = *from++; case 3: *to++ = *from++; case 2: *to++ = *from++; case 1: *to++ = *from++; } while (n-- > 0) { *to++ = *from++; *to++ = *from++; *to++ = *from++; *to++ = *from++; *to++ = *from++; *to++ = *from++; *to++ = *from++; *to++ = *from++; } Which reads much more clearly. Note that if the compiler is at all smart in combining code segments, it will generate code very similar to the first routine. The first version, however, has to do an extra test (if count is considered signed, my routine would have to make this test as well, but copying a negative number of bytes never made much sense to me) and an extra add when generating n to make the loop work out right. I guess that the moral is that cleverness may be an advantage some times, but sometimes the direct approach can be better. marcus hall ..!ihnp4!tellab1!tellab2!thoth