Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Duff's Device work in other languages?

Tags:

c

Many years ago while working on a tight graphics I/O problem, Tom Duff unrolled a loop and created his Duff's Device as follows:

dsend(to, from, count)
char *to, *from;
int count;
{
    int 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);
    }
}

(Note this uses old style function parameters - that's not an error.)

This coding comes directly out of thinking in assembler and coding in C and is dependent on C's case statement fall-through. Can this kind of creativity in interlacing control structures work in any other languages?

like image 873
Shannon Nelson Avatar asked May 03 '09 17:05

Shannon Nelson


1 Answers

You can do it in any language that supports computed GOTO statements (Fortran, some BASICs, etc.)

like image 90
Dave Avatar answered Oct 18 '22 13:10

Dave