What does this C code do [Duff’s device]? [duplicate]

This is Duff’s Device. It’s a method of unrolling loops that avoids having to add a secondary fix-up loop to deal with times when the number of loop iteration isn’t know to be an exact multiple of the unrolling factor.

Since most answers here seem to be generally positive about it I’m going to speak out against it.

With this code a compiler is going to struggle to apply any optimization to the loop body. If you just wrote the code as a simple loop a modern compiler should be able to handle the unrolling for you. This way you maintain readability and performance and have some hope of other optimizations being applied to the loop body.

The Wikipedia article referenced by others even says when this ‘pattern’ was removed from the Xfree86 source code performance actually improved.

This outcome is typical of blindly hand optimizing any code you happen to think might need it. It prevents the compiler from doing its job properly, makes your code less readable and more prone to bugs and typically slows it down. If you were doing things the right way in the first place, i.e. writing simple code, then profiling for bottlenecks, then optimizing, you’d never even think to use something like this. Not with a modern CPU and compiler anyway.

It’s fine to understand it, but I’d be surprised if you ever actually use it.

Leave a Comment