Duff's Device - Original Version

Original Version

Straightforward code to copy items from an array to a memory-mapped output register might look like this:

do { /* count > 0 assumed */ *to = *from++; /* Note that the 'to' pointer is NOT incremented */ } while(--count > 0);

Note that this is not a memory-to-memory copy, in which you would see *to++.

While optimizing this, Duff realized that an unrolled version of his loop could be implemented by interlacing the structures of a switch and a loop.

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); } }

Notice that Duff's device can just as easily be applied with any other size for the unrolled loop, not just 8.

Read more about this topic:  Duff's Device

Famous quotes containing the words original and/or version:

    Thir dread commander: he above the rest
    In shape and gesture proudly eminent
    Stood like a Towr; his form had yet not lost
    All her Original brightness, nor appear’d
    Less than Arch Angel ruind, and th’ excess
    Of Glory obscur’d: As when the Sun new ris’n
    Looks through the Horizontal misty Air
    Shorn of his Beams, or from behind the Moon
    In dim Eclips disastrous twilight sheds
    On half the Nations, and with fear of change
    Perplexes Monarchs.
    John Milton (1608–1674)

    Exercise is the yuppie version of bulimia.
    Barbara Ehrenreich (b. 1941)