Monday, December 8, 2008

Processing, meet Tom Duff

My girlfriend has turned me on to Taste Spotting, so I gave it whirl with chicken parmesan. The pictures are so enticing that I immediately started rifeling through the fridge, pushing leftover containers aside desperately searching for the ingredients. I knew they were in there somewhere, but it occurred to me that if there was a way to get rid of all those leftover containers faster, then I could get to my tasty meal that much sooner.

This reminded me of the pin register trick I wrote about in a previous post, but this trick starts to become really interesting when used with the concept of loop unrolling for hard-core code optimization.



Thanks to Tom Duff and his cleverly named "Duff Device", we can show this optimization technique in C and not assembler.

/* A Duff Device
antipastohw.blogspot.com */
#define DUFF_DEVICE_8(aCount, aAction) \
do { \ int count_ = (aCount); \
int times_ = (count_ + 7) >> 3; \
switch (count_ & 7){\
case 0: do { aAction; \
case 7: aAction; \
case 6: aAction; \
case 5: aAction; \
case 4: aAction; \
case 3: aAction; \
case 2: aAction; \
case 1: aAction; \
} while (--times_ > 0); \ } \ } while (0)
This code listing takes a variable length loop action and unrolls it into batches of 8 sequential actions. What a clever exploit of the switch statement!

Thanks Tom, and now it's possible to enhance many of the low-level drawing routines of Matt's Sub-Processing library on the TouchShield to support screen painting at 60 frames per second. Not too shabby for an 8-bit device.

Chris

1 comment:

Matt said...

wow - all that, just to avoid some extra conditional jumps in assembly language... crazy! i can't wait to test out the faster core to see what it looks like :)