Tom Duff on Duff's Device (1988)
lysator.liu.se
lysator.liu.se
He told me he hoped that in the end he'd be remembered for all the other work he did and not his "device".
I guess this means like http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html ?
register n=count/8;
do{
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
*to = *from++;
}while(--n>0);
This is almost the same as Duff's code without the switch statement. It's a simple unrolled loop that copies 8 words per iteration.Next, consider how to handle a count that is not a multiple of 8. You could simply add a loop that copies a single word at a time, but to avoid the overhead of a loop, you could instead use a switch statement, making use of C's fallthrough behavior when omitting the break after each case:
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++;
}
Now take a look at the actual Duff's device implementation line by line: register n=(count+7)/8;
n is the number of iterations of the copy-8-words unrolled loop.
The +7 causes the division by 8 to round up in order to count the extra partial loop iteration required when count is not a multiple of 8. switch(count%8){
count % 8 is the number of left-over words that need to be handled with a partial copy loop (rather than full 8-byte copy loops). The switch will jump to one of the following case labels in order to skip to a part of the unrolled copy loop such that exactly the number of left-over bytes will be copied. case 0: do{ *to = *from++;
This is the first case of the switch, but also the beginning of the unrolled copy loop. If count happened to be a multiple of 8, then count % 8 would be 0, and the unrolled copy loop would begin executing as normal, identical to the simplified code above that only handles multiples of 8. Note that C's switch statement cases are essentially just labels; execution will fall through to the next case label unless a break is encountered, so after first entering the do{}while loop, the case statements are essentially ignored. case 7: *to = *from++;
This line is where the real magic begins. If count % 8 was not 0, part of the first iteration of the unrolled loop will be skipped by jumping into the middle of the loop using the case label matching the number of extra words required. Then further iterations of the loop will continue as normal, copying the full 8 words per iteration.The following case statements all serve the same purpose: to set up a label for a partial copy loop of the given number of words during the first iteration.
}while(--n>0);
This is the end of the unrolled copy loop. n, as calculated above, is the number of times to run the unrolled loop; remember that it included an extra iteration (due to rounding up) to account for the partial first iteration if count % 8 was not 0. When execution reaches this while, it will jump back up to the do (which happens to be in the middle of a switch, but that doesn't matter anymore - the case labels are ignored and the unrolled loop will now execute as normal, copying the full 8 words each iteration.The magic is in reusing the same unrolled copy loop code to also do the initial partial copy.
Edit: aaaand that makes sense. I see now why he thought it was a bit of a hack, but it's still very nifty. Thanks again!
On the other hand portability is a non-issue and in that circumstance there's no such thing as source-level debugging (not that I would want to step through Duff's with a source-level debugger) so everything is usually just written in assembly. I'm also not convinced I'd trust the compiler to get parallelization right, especially since as another poster mentions Duff's gives you a really unusual CFG.
Unless you actually do something in the loop body, all modern superscalar HW would be bottlenecked on the writes, making multiple iterations impossible. For example, SNB has 5 main execution ports -- two agus, 3 alus, and it can do either of 2 reads or 1 read + 1 write in a single cycle. This means that if you are storing something every cycle, for each cycle you can do 3 instructions without slowing down the loop. Also, if the jump instruction is immediately preceded by the alu instruction that generates the flags for it, they get fusioned into a single instruction, meaning you don't need to count the jmp as an instruction.
> The rollback machinery in branch speculation hardware can only handle a limited number of branches
This used to be more of a problem before modern PRF cpus -- with PRF, recovery from branch miss is much cheaper (it's essentially the copy of 17 8-bit pointers), so Sandy Bridge and later can typically handle more in-flight branches than their pipeline length, so confirmations of branch predictions shouldn't ever stall you unless you use division to get the flags or something.
Then again, we were using an ancient version of GCC which didn't have great optimisation for ARM.
Sorry, I've just realised this doesn't really answer your question.
If you are running a lot of iterations of a very tight loop, it might still be worth trying. Just remember to profile.
Last I heard, 'register' is pretty much obsolete these days, because there is no way you can do better at guessing which variables are candidates to be stored in registers than the compiler can.
*to = *from++
Note that there isn't a '++' on 'to'. Make people think that Duff's device is for making a fast memcpy (which you would get with to++ = from++), but in fact it was for copying data to a MMR.Nowadays, most of the loop would be optimised away by the compiler, unless you marked to as volatile. Assuming you mark to as volatile, this kind of code is still used to write the MMR, although they are much less used than they used to be. The last time I wrote code anything like this was on the Nintendo Gameboy Advance. I don't know if the DS / 3DS still use similar code.
Edit: That's answering the question I think you meant, which was "Is memory mapped I/O in use anywhere ..."
And yes, they're often programmed in C.