News for hackers. Stuff that matters.
Re:How Duff's Device Works by anamax on Friday November 12, 2010 @01:12PM (#1898535)
> When such thing should be done? 1) If you measured your loop and it was too slow with a one assignment and one loop pass for every n. 2) you know that your compiler can't do the loop unwinding automatically -- modern compilers can do such things even if you write a normal loop if the compiler estimates that this can really help and when you give to it a specific direction to produce such code (e.g. aggressively optimize for speed).

Note that many compilers generate crappy code for duff's device. The switch statement, specifically the intermediate destinations, keeps them from generating good code for the unrolled loop body.

The compiled code can be so bad that a "not unrolled" loop to get to alignment followed by an unrolled aligned loop is significantly faster.

And, because this stuff is never simple, the fastest version may be range dependent, as in one version is faster for up to 100 elements while another is faster for more than 100.

There are no comments on this comment yet.