473,378 Members | 1,360 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,378 software developers and data experts.

duff's device / loop unriolling

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;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
}
I understand the code so far, but I have a question about the loops.
To me, it seems he used loop unrolling (aka duff's device) to optimize
the code while it is executed. But why did he use four loops? When
the function is invoked, he didn't know how big "s" is. Or am I wrong here?
I always thought, to unroll a loop I need to know how often the loop is
used.

Any help would be appreciated,
JR
Nov 15 '05 #1
22 2183
Jan Richter wrote:
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;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
}
I understand the code so far, but I have a question about the loops.
To me, it seems he used loop unrolling (aka duff's device) to optimize
the code while it is executed. But why did he use four loops? When
the function is invoked, he didn't know how big "s" is. Or am I wrong here?
I always thought, to unroll a loop I need to know how often the loop is
used.

Any help would be appreciated,
JR


In my linbox, strlen() gives by far better results...

--
one's freedom stops where others' begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #2

"Giannis Papadopoulos" wrote:

[...]

In my linbox, strlen() gives by far better results...


Yes, same for me, so where is the benefit?

Cheers,
JR
Nov 15 '05 #3
Jan Richter wrote:
"Giannis Papadopoulos" wrote:

[...]

In my linbox, strlen() gives by far better results...

Yes, same for me, so where is the benefit?

Cheers,
JR


No benefit... Maybe it is written for a compiler that does not know how
to unroll loops...

And yes, loop unrolling works perfectly when you have a hint how many
times this loop will be executed.

Doesn't the author of this peculiar str_len() say anything?

--
one's freedom stops where others' begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #4
Jan Richter wrote:
But why did he use four loops? When
the function is invoked, he didn't know how big "s" is.


may be based on the consideration of pipeline.
I heard Intel PII CPU has four levels pipelines of integer operation.
is that right?

Nov 15 '05 #5
"Jan Richter" <vi**********@yahoo.de> wrote:
"Giannis Papadopoulos" wrote:
In my linbox, strlen() gives by far better results...


Yes, same for me, so where is the benefit?


The original Duff's Device did a bit more than merely count. It's in the
FAQ, btw, which you should've read.
<http://www.eskimo.com/~scs/C-faq/top.html>.

Richard
Nov 15 '05 #6
On Fri, 19 Aug 2005 11:53:21 +0200, Jan Richter wrote:
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;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
}
I understand the code so far, but I have a question about the loops.
To me, it seems he used loop unrolling (aka duff's device) to optimize
the code while it is executed.
As someone else has said most optimising compilers will do this for you.
It's a far more appropriate technique for assembly. But perhaps the
author had a specific implementation in mind for which this happened to
work best. Also as pointed out elsewhere Duff's Device is a more specific
term than loop unrolling.
But why did he use four loops? When the function is invoked, he didn't
know how big "s" is. Or am I wrong here? I always thought, to unroll a
loop I need to know how often the loop is used.


There are two main considerations: speed vs code size.

More unrolling == less jump opcodes == more loop body code.

Which is equivalent to a speed increase at the cost of a code size
increase.

If you know how often the loop body will be iterated on average then you
can avoid unrolling the loop by more than that number. Hence you avoid
the extra code without affecting the speed increase.

If you don't know, then you make a decision as to how much extra code
you're willing to tolerate. There are other issues like keeping all the
loop body code in the instruction cache and possibly others that I'm not
too familiar with not being an assembly coder.

For the strlen function as you point out you can't know what the average
number of iterations will be so the only consideration is code size.
Providing you can keep it all in the instruction cache the loop could be
unrolled indefinitely and continue to provide speed increases for calls
with a sufficiently long string.

Why did the author choose four? Who knows - perhaps he wanted the
function to be reasonably small whilst still providing a modest speed
increase. Which as you and others point out, it doesn't. Going to show
once again that usually optimisations are best left to the compiler or
assembly.

--
http://members.dodo.com.au/~netocrat

Nov 15 '05 #7
Jan Richter wrote:

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;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
}


Why only four if statements in the loop?

I guess that's not supposed to be an example of portable code.
The (t-s) expressions are of type ptrdiff_t,
which is unsuitable for use that way, in portable code.

That's not Duff's Device.
http://www.lysator.liu.se/c/duffs-device.html

--
pete
Nov 15 '05 #8
Giannis Papadopoulos <ip******@inf.uth.gr> wrote:
No benefit... Maybe it is written for a compiler that does not know how
to unroll loops...


Probably; an implementation simple-minded enough to trust the
programmer when he uses the "register" storage class specifier
probably could use some help unrolling a loop. The VAX compiler for
which Duff originally wrote his device was (presumably) such an
implementation.

--
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 15 '05 #9
"Jan Richter" <vi**********@yahoo.de> writes:
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;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
}
I understand the code so far, but I have a question about the loops.
To me, it seems he used loop unrolling (aka duff's device) to optimize
the code while it is executed. But why did he use four loops? When
the function is invoked, he didn't know how big "s" is. Or am I wrong here?
I always thought, to unroll a loop I need to know how often the loop is
used.


Duff's device is a specific technique, not a synonym for loop
unrolling. See question 20.35 of the C FAQ.

The use of unsigned int is questionable. The return type should be
size_t. It's possible that this implementation predates the existence
of size_t (though it's been modernized with a prototype). I would
guess that it might be more efficient than strlen() only on a very old
implementation.

A matter of terminology: it doesn't have four loops. It has a single
loop containing four if statements.

The statement that's executed N times is

if (!*t) return t - s; ++t;

N needn't be a multiple of 4 because it can break out of the loop
partway through the body of the for loop.

In the following, I'll use the term "iteration" for an execution of
the entire body of the loop, and "sub-iteration" for an execution of a
single line within the loop.

The idea of loop unrolling is that it avoids the overhead of a test on
each iteration, falling through from one statement to the next and
performing the test and branch only once every 4 elements. The number
of times a loop is to be unrolled is a tradeoff between speed and code
size -- and if it's unrolled too much (say, 1024 times), the code size
itself can make it run slower due to cache issues. This is all
*extremely* system-specific, which is why the whole thing is best left
to the compiler.

Furthermore, the "beauty" of Duff's Device is that it jumps into the
middle of the loop, so the loop never has to exit from the middle, and
it really does avoid the overhead on each sub-iteration. The code
above doesn't do this; instead, it performs a test and branch on each
sub-iteration.

I'd be surprised to see the above code performing better than strlen()
on any modern implementation, particularly since an implementation is
free to implement strlen() using whatever non-portable tricks it likes
to squeeze out the last clock cycle.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10
In article <3m*************@individual.net>,
"Jan Richter" <vi**********@yahoo.de> wrote:
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;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
} I understand the code so far, but I have a question about the loops.
To me, it seems he used loop unrolling (aka duff's device) to optimize
the code while it is executed.
Loop unrolling != Duff's device.

Duff's device is a combination of loop unrolling + completely perverted
usage of a switch statement, which is likely to prevent the compiler
from optimising the code
But why did he use four loops? When
the function is invoked, he didn't know how big "s" is. Or am I wrong here?
I always thought, to unroll a loop I need to know how often the loop is
used.


As you can see, knowing how often a loop will be executed is not
necessary. However, before you unroll a loop by hand, you should check
whether the compiler can do loop unrolling itself, which is likely to
produce more efficient code.
Nov 15 '05 #11
Keith Thompson wrote:

"Jan Richter" <vi**********@yahoo.de> writes:
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;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
}
The idea of loop unrolling is that it avoids the overhead of a test on
each iteration,


That's supposed to be the idea,
but the above code has one test per increment
just like a naive portable implementation.

size_t str_len(const char *s)
{
size_t n = 0;

while (s[n] != '\0') {
++n;
}
return n;
}
--
pete
Nov 15 '05 #12
pete <pf*****@mindspring.com> wrote:
Keith Thompson wrote:
"Jan Richter" <vi**********@yahoo.de> writes:
> 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;
> if (!*t) return t - s; ++t;
> if (!*t) return t - s; ++t;
> if (!*t) return t - s; ++t;
> }
> }

The idea of loop unrolling is that it avoids the overhead of a test on
each iteration,


That's supposed to be the idea,
but the above code has one test per increment
just like a naive portable implementation.

size_t str_len(const char *s)
{
size_t n = 0;

while (s[n] != '\0') {
++n;
}
return n;
}


IANAAP (I am not an assembly programmer) so I don't know how the code
typically translates, but it seems to me that it does provide some benefit
- it reduces by 75%[1] the number of jump statements executed (to return
from the end to the start of the loop).

[1] When the number of iterations is not a multiple of 4 this is only
approximate.

--
http://members.dodo.com.au/~netocrat

Nov 15 '05 #13
Christian Bau wrote
(in article
<ch*********************************@slb-newsm1.svr.pol.co.uk>):
In article <3m*************@individual.net>,
"Jan Richter" <vi**********@yahoo.de> wrote:
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;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
}

I understand the code so far, but I have a question about the loops.
To me, it seems he used loop unrolling (aka duff's device) to optimize
the code while it is executed.


Loop unrolling != Duff's device.

Duff's device is a combination of loop unrolling + completely perverted
usage of a switch statement, which is likely to prevent the compiler
from optimising the code


It's suddenly being discussed again, probably as a result of an
article in DDJ (Doctor Dobb's Journal) about it in the August
issue. Ralf Holly proposed using it in macro form for generic
loop unrolling, which probably makes people misunderstand its
original purpose.

He proposed something like

#define DUFF_DEVICE_8(macroCount, macroAction) \
do { \
size_t duffcount = (macroCount); \
size_t dufftimes = (duffcount + 7) >>3u; \
switch(duffcount & 7) { \
case 0: do { macroAction; \
case 7: macroAction; \
case 6: macroAction; \
case 5: macroAction; \
case 4: macroAction; \
case 3: macroAction; \
case 2: macroAction; \
case 1: macroAction; \
} while (--dufftimes > 0); \
} \
} while (0)

Of course, the caller has to know not to call with a 0
countvalue or it will execute 8 times instead.

Probably not all that practical in general use, but you might
find somewhere, (after profiling) where it makes some sense.

--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #14
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:

[...]
The idea of loop unrolling is that it avoids the overhead of a test on
each iteration,


That's supposed to be the idea,
but the above code has one test per increment
just like a naive portable implementation.


Yes, as I said in the portion of the article that you snipped.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #15
In article <de**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Giannis Papadopoulos <ip******@inf.uth.gr> wrote:
No benefit... Maybe it is written for a compiler that does not know how
to unroll loops...


Probably; an implementation simple-minded enough to trust the
programmer when he uses the "register" storage class specifier
probably could use some help unrolling a loop. The VAX compiler for
which Duff originally wrote his device was (presumably) such an
implementation.


Make that probably and presumably to "certainly". Loop unrolling was pretty
unheard of before the 80s. It came only in general use in the 80s when more
pipe-lined processors appeared (and it made most sense on those). General
compilers doing loop unrolling only appeared in the early 90s (if I remember
right). Even CDC Fortran compilers of the 70s and early 80s did not do
loop unrolling, while it made perfect sense on the CDC Cybers.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #16
In article <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
....
The idea of loop unrolling is that it avoids the overhead of a test on
each iteration, falling through from one statement to the next and
performing the test and branch only once every 4 elements.
No. It all has to do with pipelining processors, and the *instruction*
cache. It also has effect on systems where a backward branch is much
more expensive than a forward branch.
The number
of times a loop is to be unrolled is a tradeoff between speed and code
size -- and if it's unrolled too much (say, 1024 times), the code size
itself can make it run slower due to cache issues. This is all
*extremely* system-specific, which is why the whole thing is best left
to the compiler.
Indeed. But I think the code dates from a time when compilers did not do
loop unrolling.
I'd be surprised to see the above code performing better than strlen()
on any modern implementation, particularly since an implementation is
free to implement strlen() using whatever non-portable tricks it likes
to squeeze out the last clock cycle.


Again, indeed. There are extremely easy, and efficient, techniques to
determine (on a 64 bit system) whether any of 8 consecutive 8-bit entities
is zero.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #17

In article <00*****************************@news.verizon.net> , Randy Howard <ra*********@FOOverizonBAR.net> writes:

It's suddenly being discussed again, probably as a result of an
article in DDJ (Doctor Dobb's Journal) about it in the August
issue. Ralf Holly proposed using it in macro form for generic
loop unrolling, which probably makes people misunderstand its
original purpose.

He proposed something like
[Tabs converted to spaces to avoid wrapping.]
#define DUFF_DEVICE_8(macroCount, macroAction) \
do { \
size_t duffcount = (macroCount); \
size_t dufftimes = (duffcount + 7) >>3u; \
switch(duffcount & 7) { \
case 0: do { macroAction; \
case 7: macroAction; \
case 6: macroAction; \
case 5: macroAction; \
case 4: macroAction; \
case 3: macroAction; \
case 2: macroAction; \
case 1: macroAction; \
} while (--dufftimes > 0); \
} \
} while (0)

Of course, the caller has to know not to call with a 0
countvalue or it will execute 8 times instead.
Unless I'm missing something, that's easily fixed with a

if (!duffcount) break;

before the switch. (Testing duffcount avoids using macroCount,
which might have side effects, twice, of course.)

A worse problem would be using it with a negative count; hopefully
the compiler would provide a useful diagnostic for the conversion
between a signed value and a size_t when duffcount is initialized,
but that's a QoI issue. (Also, I know far too many C programmers
who routinely ignore such diagnostics, partly because their code is
full of them. I suppose that's a QoP issue.)
Probably not all that practical in general use, but you might
find somewhere, (after profiling) where it makes some sense.


Agreed. I have found cases where relatively recent commercial
implementations don't unroll even simple loops where unrolling has a
significant benefit. Of course, if the loop isn't in a performance-
critical path, it doesn't matter anyway.

--
Michael Wojcik mi************@microfocus.com

We are subdued to what we work in. (E M Forster)
Nov 15 '05 #18
Michael Wojcik wrote
(in article <de********@news1.newsguy.com>):

In article <00*****************************@news.verizon.net> , Randy Howard
<ra*********@FOOverizonBAR.net> writes:

It's suddenly being discussed again, probably as a result of an
article in DDJ (Doctor Dobb's Journal) about it in the August
issue. Ralf Holly proposed using it in macro form for generic
loop unrolling, which probably makes people misunderstand its
original purpose.

He proposed something like


[Tabs converted to spaces to avoid wrapping.]


Ack, sorry about that. I recently changed newsreaders, and
didn't pick up on it not doing that magically in the new
version. I'll be more careful in the future.

--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #19
mw*****@newsguy.com (Michael Wojcik) writes:
In article <00*****************************@news.verizon.net> , Randy Howard <ra*********@FOOverizonBAR.net> writes:

It's suddenly being discussed again, probably as a result of an
article in DDJ (Doctor Dobb's Journal) about it in the August
issue. Ralf Holly proposed using it in macro form for generic
loop unrolling, which probably makes people misunderstand its
original purpose.

He proposed something like


[Tabs converted to spaces to avoid wrapping.]
#define DUFF_DEVICE_8(macroCount, macroAction) \
do { \
size_t duffcount = (macroCount); \
size_t dufftimes = (duffcount + 7) >>3u; \
switch(duffcount & 7) { \
case 0: do { macroAction; \
case 7: macroAction; \
case 6: macroAction; \
case 5: macroAction; \
case 4: macroAction; \
case 3: macroAction; \
case 2: macroAction; \
case 1: macroAction; \
} while (--dufftimes > 0); \
} \
} while (0)

Of course, the caller has to know not to call with a 0
countvalue or it will execute 8 times instead.


Unless I'm missing something, that's easily fixed with a

if (!duffcount) break;

before the switch. (Testing duffcount avoids using macroCount,
which might have side effects, twice, of course.)

Alternatively:

#define DUFF_DEVICE_8(macroCount, macroAction) \
do { \
size_t duffcount = (macroCount); \
size_t dufftimes = duffcount >>3u; \
switch(duffcount & 7) { \
do { macroAction; \
case 7: macroAction; \
case 6: macroAction; \
case 5: macroAction; \
case 4: macroAction; \
case 3: macroAction; \
case 2: macroAction; \
case 1: macroAction; \
case 0: ; \
} while (dufftimes-- > 0); \
} \
} while (0)

handles the case with zero iterations without needing an
extra 'if' before the switch, and simplifies the calculation
of 'dufftimes'.
Nov 15 '05 #20
# #define DUFF_DEVICE_8(macroCount, macroAction) \

With a modern, optimising compiler, it's bad idea. Compilers
can do unrolling for you. Duff's Device is going to make the
code so complicated that it will prevent the compiler from
doing a number of additional optimisations. If you insist
on unrolling your loops, do something like

for (i=0; i+8<n; i+=8) {
body(i+0);
body(i+1);
body(i+2);
body(i+3);
body(i+4);
body(i+5);
body(i+6);
body(i+7);
}
switch (n-i) {
case 7: body(i); i++;
case 6: body(i); i++;
case 5: body(i); i++;
case 4: body(i); i++;
case 3: body(i); i++;
case 2: body(i); i++;
case 1: body(i); i++;
}

This leaves the aggregate loop body in an optimisable form.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
No pleasure, no rapture, no exquisite sin greater than central air.
Nov 15 '05 #21
SM Ryan wrote
(in article <11*************@corp.supernews.com>):
# #define DUFF_DEVICE_8(macroCount, macroAction) \

With a modern, optimising compiler, it's bad idea. Compilers
can do unrolling for you.
If you read the article in DDJ, you will see that your
assumption, although likely broadly true is not /always/ true.
As usual, it depends.
Duff's Device is going to make the
code so complicated that it will prevent the compiler from
doing a number of additional optimisations.


That is why profilers are useful. To find out, for each and
every case, which happens to apply.
--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #22
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
# #define DUFF_DEVICE_8(macroCount, macroAction) \

With a modern, optimising compiler, it's bad idea.
As with most widely known programming techniques, whether
it makes sense to employ Duff's Device in some particular
circumstances depends on the circumstances.

Compilers can do unrolling for you.
The question, however, usually is not whether some
hypothetical compiler *can* but whether some actual compiler
*does*. These questions often have different answers.

Duff's Device is going to make the
code so complicated that it will prevent the compiler from
doing a number of additional optimisations.
Use of Duff's Device may complicate the code to the point
where it *might* prevent the compiler from doing additional
useful optimizations, but there's no guarantee of that. At
this point Duff's Device is well known enough so advanced
compilers using structural analysis to do their flow
analysis may very well recognize the particular form of
multiple-entry loop that Duff's Device uses and deal with it
appropriately.

More significantly, the loop body that is being unrolled is
usually very simple (otherwise why are we bothering to
unroll it?); often times it won't be improved beyond what
is already done in the multiple-entry loop form.

If you insist
on unrolling your loops, do something like

for (i=0; i+8<n; i+=8) {
body(i+0);
body(i+1);
body(i+2);
body(i+3);
body(i+4);
body(i+5);
body(i+6);
body(i+7);
}
switch (n-i) {
case 7: body(i); i++;
case 6: body(i); i++;
case 5: body(i); i++;
case 4: body(i); i++;
case 3: body(i); i++;
case 2: body(i); i++;
case 1: body(i); i++;
}

This leaves the aggregate loop body in an optimisable form.


Minor correction - the code shown is wrong: if n is a
multiple of eight (and greater than zero), n-8 cases are
done rather than n cases. (There are at least two easy
fixes, left as exercises for the reader.)

As noted before, often times the compiler won't be able
to optimize the code in the 'for' beyond what it would
do in the corresponding expression in Duff's Device.

The technique of incrementing i by 8, and using 'i+0', etc,
in the unrolled loop body, is a good one to know; however,
this technique can also be used with Duff's Device:

switch( i = n % 8 ) do {
i += 8;
body(i-8);
case 7: body(i-7);
case 6: body(i-6);
case 5: body(i-5);
case 4: body(i-4);
case 3: body(i-3);
case 2: body(i-2);
case 1: body(i-1);
case 0: ;
} while( i < n );

If you compare the generated code for the two approaches, I
expect you'll find cases where the generated code for the
unrolled-using-switch approach is better than the generated
code for the for-then-switch approach, along every important
axis. (That is what I found with some simple loop 'body's.)

Certainly it doesn't make sense to use Duff's Device in all
cases. Most commonly, loops shouldn't be unrolled at all,
because the benefit that might come from unrolling just
isn't significant (and may very well be negative rather than
positive). But when it is important to unroll a loop,
Duff's Device is one possible approach to do that; its
use should be considered, compared and evaluated along with
any other alternatives. Other things being equal, code that
runs faster and has fewer lines of code seems like a better
choice. In cases where Duff's Device produces such code,
there is a pretty strong argument that it's the right
approach to use in those circumstances.

In short, I don't think Duff's Device is either always good
or always bad. It's just a technique to know and compare
against other possible approaches; whether it should be
used or not depends on how it stacks up against the other
possibilities, in the particular circumstances being
investigated.
Nov 15 '05 #23

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...
2
by: Christopher Benson-Manica | last post by:
(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...
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{...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.