C Programming Tutorials and advice
CProgrammingTrends News Archives About Us Feedback


Click to Play

Wolfram Alpha on Search, Social...
The computational knowledge engine, Wolfram Alpha, is continuing to fascinate many. WebProNews spoke with Barak Berkowitz and Schoeller Porter...

Recent Articles

To Switch, Or Not To Switch
When it comes to making selections in C programming, the obvious first choice of most programmers is the if,else combination. This can become a hassle when there are multiple choices with different or similar...

New Apple Rules Support Objective-C, C, C++
As anyone who's gone near an Apple store on the day a new product's released must know, there's a sizable segment of the population that more or less worships the company's products. It's interesting - and...

New Apple Rules Support Objective-C, C, C++
As anyone who's gone near an Apple store on the day a new product's released must know, there's a sizable segment of the population that more or less worships the company's products. It's interesting - and possibly...

C Returns To Top Of Tiobe Index
C fans, your time has come (again). After four arguably long years, the C programming language has made it back to the top of the Tiobe Programming...

Palm Nods To C, C++ With Plug-in Development Kit
By all accounts, mobile is the future. Companies of all sorts are making iPhone apps, Google's investing millions in its Android operating system...

Using C++ To Clean Up Your CSS Files
A few weeks back i found out that the method I use to minify CSS was about 5% more efficient than the YUI Compressor. I tweeted about it and was encouraged...



05.20.10



Dissecting Duff's Device

By Bryan Young

One of the most confusing pieces of code I have ever had a chance to look at is Duff's Device. At first glance, it seems that your compiler would laugh at you for attempting such a feat. For those of you who haven't yet been introduced to this programming marvel, I suggest you sit down before reading on.

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

Okay, say it with me..."huh?" Believe it or not, this is actual, compile-able C code. But what does it do? Let us first look at the purpose for which it was written. According to Tom Duff, while working for Lucasfilm there was a need for a more efficient way to copy an array of data into registers. This is the code that was being used.

send(to, from, count)
register short *to, *from;
register count;
{
   do {
      *to = *from++;
   } while (--count >0);
}

This creates a bottleneck by forcing a comparison call for every register that is written to. In order to speed up the process, conventional coding calls for unwinding the loop a few times, meaning that for each iteration of the loop, there are eight copies for each comparison made.


send(to, from, count)
register short *to, *from;
register count;
{
   do {
      *to = *from++;
      *to = *from++;
      *to = *from++;
      *to = *from++;
      *to = *from++;
      *to = *from++;
      *to = *from++;
      *to = *from++;
   } while (--count >0);
}

Of course, the obvious problem with this is the case where the number of copies is not evenly divisible by eight. In order to fix this, you need a separate piece of code to handle the remainder of the copies, typically using a switch in this fashion.

switch (count % 8) {
   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++;
}

This is of course follows the do-while loop described above. Duff simply took the loop and the switch and combined them, creating the single piece of code shown at the top of this article. The device, utilizing the fall-through property of the switch statement and the ability for C code to jump directly into the middle of a loop, boggles the mind while compiling and running flawlessly. For these reasons, Duff's Device goes down in the books as a staple for students who want to learn more about code optimization.

About the Author:
Bryan Young is a staff writer for WebProNews.
About CProgrammingTrends
A collection of articles and tutorials designed to help C and C variant programmers in their programming work. C Programming Tutorials and advice





CProgrammingTrends is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagmentNews.com





-- CProgrammingTrends is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2010 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal


archives | advertising info | news headlines | free newsletters | comments/feedback | submit article


CProgrammingTrends Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact