Tricky way to unroll string copy loops
Marcus Hall
thoth at tellab2.UUCP
Thu Apr 11 08:38:26 AEST 1985
In article <582 at lsuc.UUCP> msb at 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
More information about the Comp.lang.c
mailing list