-
From the page: Duff realized that an unrolled version of his loop could be implemented by interlacing the structures of a switch and a loop. int n = (count 7) / 8; /* count > 0 assumed */ switch (count % 8) { case 0: do { *to = *from++; case 7:... more
Reviewed by Inertial-Mass Oct 07 2005, 07:13am ( 1 review ) • wikipedia.org
-
Stumbleman83
Stumble...
4,536 Favs
-
sweemeng
sweemeng
3,206 Favs
-
blogstumbler
blogstu...
32K Favs
-
ragas123
ragas123
141 Favs
-
infynyteloop
infynyt...
847 Favs
-
msdos464
msdos464
33K Favs
-
nasimuto
nasimuto
552 Favs
-
Fliegendreck
Fliegen...
4,157 Favs
-
don-gecko
don-gecko
1,973 Favs
-
Hck-Dev
Hck-Dev
3,238 Favs
- 1 review
- Reviews of the site
-
Join StumbleUpon or login to add a review!
-
Rated by Inertial-Mass on Oct 07 2005, 7:13am
From the page: Duff realized that an unrolled version of his loop could be implemented by interlacing the structures of a switch and a loop. int n = (count 7) / 8; /* count > 0 assumed */ 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); } Based on an algorithm used widely by assemblers for minimizing the number of tests and branches during a copy, Duff's Device appears out of place when implemented in C. The device is indeed perfectly valid, legal C, and many compilers will optimize the switch into a jump table just as would be done in an assembler implementation. C's default fall-through in case statements has long been its most controversial single feature; Duff observed that "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against." Additional note: The original Device was made for copying to a register. To actually copy memory from one location to another, you must add an auto-increment to every reference to to, like so: *to++ = *from++; This modified form of the Device appears as a "what does this code do?" exercise in Bjarne Stroustrup's book The C++ Programming Language, presumably because novice programmers cannot be expected to know about memory-mapped output registers. However, it is not particularly useful in this form since the standard C library already supplies a memory-copying function which is likely to be even better optimized.
