Quoting Wikipedia, "Duff's Device is an optimized implementation of a serial copy that uses a technique widely applied in assembly language for loop unwinding". Basically, a standard for-loop copy, e.g.
can be unrolled to minimize the amount of branches by using a while loop nested into a switch-statement:do
*to++ = *from++;
while(--count>0);
Many sources, including Wikipedia, claim that Duff's Device is a waste of optimization time and that sticking to library routines is always better. Others claim it has its uses though, albeit not as a general replacement to memcpy or std::copy. To quote Tom Duff himself, "If your code is too slow, you must make it faster. If no better algorithm is available, you must trim cycles."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);
}
No comments:
Post a Comment