# 言之凿凿

Try harder.

## 一种循环展开的优化方法：达夫设备（Duff device）

2019.09.01

### 1. What it for?

In the C programming language, Duff’s device is a way of manually implementing loop unrolling by interleaving two syntactic constructs of C: the do-while loop and a switch statement. Its discovery is credited to Tom Duff in November 1983, when Duff was working for Lucasfilm and used it to speed up a real-time animation program.

### 2. How it does?

send(to, from, count)
register short *to, *from;
register count;
{
do {                          /* count > 0 assumed */
*to = *from++;
} while(--count > 0);
}


send(to, from, count)
register short *to, *from;
register count;
{
register n = count / 8;
register m = count % 8;
do {
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
} while (--n > 0);
do {
*to = *from++;
} while (--m > 0);
}


send(to, from, count)
register short *to, *from;
register count;
{
register 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);
}
}


### 3. 总结

When numerous instances of Duff’s device were removed from the XFree86 Server in version 4.0, there was an improvement in performance and a noticeable reduction in size of the executable.[7] Therefore, when considering this code as a program optimization, it may be worth running a few benchmarks to verify that it actually is the fastest code on the target architecture, at the target optimization level, with the target compiler, as well as weighing the risk that the optimized code will later be used on different platforms where it is not the fastest code.