Traditional stuff

This page contains examples of unusual or interesting code that are longer known to mankind.

What you will see here:

  1. Duff's Device


Duff's Device

This is an example for how good C is to write really nasty code. "Duff's Device" was first used by Tom Duff, when he tried to unroll a loop to optimize code (todays compilers should do that without help of the programmer). The code below interleaves a loop and a switch statement and is completely legal C code:

    int i = (count + 7) / 8;
    switch (count % 8) {
        case 0: do {    action();
        case 7:         action();
        case 6:         action();
        case 5:         action();
        case 4:         action();
        case 3:         action();
        case 2:         action();
        case 1:         action();
                } while (--i >= 0);
    }
Go back.
sntrad.html; last change: 01-Aug-98
uz@musoftware.de