Explanation, please!
Nathaniel Stitt
nat at bales.UUCP
Wed Aug 31 07:23:24 AEST 1988
In article <dpmuY#2EBC4R=eric at snark.UUCP> eric at snark.UUCP (Eric S. Raymond) writes:
>(Code below reproduced so that comp.arch people seeing this followup only
>won't get terminally frustrated. This is *really neat*, gang...)
>
>In article <638 at paris.ics.uci.edu> Douglas C. Schmidt writes:
>>
>> void send(int *to,int *from, int count) {
>> int 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);
>> }
>>
>> }
>>
>> Finally, Stroustrup asks the rhetorical question ``why would anyone
>> want to write something like this.'' Any guesses?!
>
>Yeah. That's the most hackish way of trying to write a portable optimized
>copy routine I've ever seen. I gather the whole point of the shenanigans
>is to get all the *from++ -> *to++ instructions in the generated code to be
>adjacent.
>
Here is my own personal version of the "Portable Optimized Copy" routine.
It certainly seems more clear than the above example, and I would expect
it to be at least as fast on virtually any machine. (Note the use of division
and mod in the above routine.)
Code does NOT have to be ugly to be fast.
/* Copy 'count' ints from *from to *to. */
void nats_send(to, from, count)
register int *to;
register int *from;
register int count;
{
/* Copy the bulk of the data */
while(count >= 8)
{
*to++ = *from++;
*to++ = *from++;
*to++ = *from++;
*to++ = *from++;
*to++ = *from++;
*to++ = *from++;
*to++ = *from++;
*to++ = *from++;
count -= 8;
}
/* Copy the rest. */
if(count >= 4)
{
*to++ = *from++;
*to++ = *from++;
*to++ = *from++;
*to++ = *from++;
count -= 4;
}
if(count >= 2)
{
*to++ = *from++;
*to++ = *from++;
count -= 2;
}
if(count)
*to++ = *from++;
}
Hope this helps.
--
Nathaniel Stitt | This life is a test. It is only a test. Had
Guidelines Software, Inc. | this been an actual life, you would have received
ucbvax!cogsci!z!nat | further instructions as to what to do and where
(415) 376-1395 | to go.
More information about the Comp.lang.c
mailing list