473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

A small question about Duff's Device

(Fear not, I have no intention of actually using it!)

Was there a particular reason Duff chose to unroll the loop 8 times,
as opposed to some other number of times (besides some reason having
to do with the original application of the device)?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
2 1339
In article <cl**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
(Fear not, I have no intention of actually using it!)

Was there a particular reason Duff chose to unroll the loop 8 times,
as opposed to some other number of times (besides some reason having
to do with the original application of the device)?


A power of two would make the math that's left for the compiler to do
simpler (it can be done entirely with bitwise operators rather than
having to do arithmetic).

Other than style or application-specific reasons, I don't see why 8
would be inherently a better or worse choice than any other power of two,
except possibly that it's a sweet spot wrt the law of diminishing returns
(less unrolling gives less improvement, more unrolling doesn't get enough
extra improvement to be worth the extra typing).
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Much as we might regret it sometimes, comp.lang.c is not actually
normative.
--Richard Heathfield in comp.lang.c
Nov 14 '05 #2

In article <cl**********@rumours.uwaterloo.ca>, dj******@csclub.uwaterloo.ca (Dave Vandervies) writes:
In article <cl**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
(Fear not, I have no intention of actually using it!)

Was there a particular reason Duff chose to unroll the loop 8 times,


Other than style or application-specific reasons, I don't see why 8
would be inherently a better or worse choice than any other power of two,
except possibly that it's a sweet spot wrt the law of diminishing returns
(less unrolling gives less improvement, more unrolling doesn't get enough
extra improvement to be worth the extra typing).


And more unrolling could result in making the loop too large to fit
in an instruction cache line, severely impairing performance.

This is just the sort of thing that makes optimization context-
dependent and tricky.
--
Michael Wojcik mi************@microfocus.com
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jamie Risk | last post by:
In Tom's original post (see http://www.lysator.liu.se/c/duffs-device.html) discussing "Duff's Device" he refers to "another revolting way to use swtiches to implement interrupt driven state...
22
by: Jan Richter | last post by:
Hi there, the Code below shows DJBs own implementation of strlen (str_len): unsigned int str_len(char *s) { register char *t; t = s; for (;;) { if (!*t) return t - s; ++t;
11
by: Sensei | last post by:
Hi again! I have still curiosity about the reason of some C constructs/keywords... The first is about static functions. What was the reason of restricting a function to be visible just in a...
11
by: Hallvard B Furuseth | last post by:
I've been wondering sometimes: Anyone know why Duff's device is usually written like this: void duff(const char *str, int len) { int n = (len + 7) / 8; switch (len % 8) { case 0: do{...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
10
by: anon.asdf | last post by:
Here's a reminder of duff's device: /*************************************/ #include <stdio.h> #define STEP 8 #define MAX_LEN STEP*4+1 #define SOURCE_LEN 28
9
by: yawnmoth | last post by:
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.