how does duff's device work?

Y

yawnmoth

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: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
}

If count % 8 is 7, the switch statement would skip the 'do' keyword
and the open bracket used in case 0, would go down the rest of the
cases, and, upon getting to case 1, would find a close bracket without
any corresponding open bracket, yielding a syntax error. As such, I'm
a little confused how the code works. Maybe there's some quirk in
some C compiler which interprets that differently? Maybe the ISO
standards for the C language dictate that it do that? If the latter,
it seems like Java or Python, or whatever, are liable not to support
duff's device?
 
P

Phil Carmody

yawnmoth said:
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: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
}

If count % 8 is 7, the switch statement would skip the 'do' keyword

Are you mistaking C for a dumb interpreted language?
and the open bracket used in case 0, would go down the rest of the
cases, and, upon getting to case 1, would find a close bracket without
any corresponding open bracket, yielding a syntax error. As such, I'm
a little confused how the code works.

Start with something simpler. Try this:

int signum(int i)
{
if(!i) { goto foo; }
if(i>0)
{
return 1;
foo:
return 0;
}
return -1;
}


Phil
 
R

Richard Tobin

Are you mistaking C for a dumb interpreted language?
[/QUOTE]
A number of C interpreters exist. Whether a language is interpreted or
compiled (or a mixture) is a function of the implementation, not the
language.

True of course, but I think the implication of "dumb interpretated
language" was clear: one which can be interpreted from something very
close to the text, rather than from a representation of the structure
of the program.

-- Richard
 
J

James Harris

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: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}

}

If count % 8 is 7, the switch statement would skip the 'do' keyword
and the open bracket used in case 0, would go down the rest of the
cases, and, upon getting to case 1, would find a close bracket without
any corresponding open bracket, yielding a syntax error. As such, I'm
a little confused how the code works. Maybe there's some quirk in
some C compiler which interprets that differently? Maybe the ISO
standards for the C language dictate that it do that? If the latter,
it seems like Java or Python, or whatever, are liable not to support
duff's device?

Understandably you are looking at the code structure from the point of
view of the switch statement. Yet switch is only partially structured.
Its case labels can appear at strange places within the following
code.

Instead, mentally remove the switch and case construct and the
trailing brace. You should be left with code that exhibits more
recognisable structure. In particular the do ... while loop will look
like a loop. It will also function as one.

Then adding the switch and case construct allows you (as shown in
Richard Heathfield's assembler translation post) to jump to arbitrary
places within that loop. Using switch this way does have its uses but,
IMHO, is generally to be avoided.

If you've not already seen it take a look at

http://en.wikipedia.org/wiki/Duffs_device
 
B

Bartc

Understandably you are looking at the code structure from the point of
view of the switch statement. Yet switch is only partially structured.
Its case labels can appear at strange places within the following
code.

This is crazy. What happens when there is a nested switch statement, how
does it know whether a particular 'case' belongs to the inner or outer
switch?
 
R

Richard Tobin

This is crazy. What happens when there is a nested switch statement, how
does it know whether a particular 'case' belongs to the inner or outer
switch?

Cases in enclosed switch statements are not visible to the outer switch.
Or to put it another way, cases are scoped by switch statements.

Other compound statements are "transparent" and do not introduce a
new scope for cases.

-- Richard
 
P

Phil Carmody

Richard Heathfield said:
Phil Carmody said:


A number of C interpreters exist. Whether a language is interpreted or
compiled (or a mixture) is a function of the implementation, not the
language.

Yes, I have and even use a C interpreter. However, it realises that
between the switch and the case it's interested in, there's a do {.
That makes it not dumb. I guess I should have used an adverb rather
than an adjective.

Phil
 
P

Phil Carmody

A number of C interpreters exist. Whether a language is interpreted or
compiled (or a mixture) is a function of the implementation, not the
language.

True of course, but I think the implication of "dumb interpretated
language" was clear: one which can be interpreted from something very
close to the text, rather than from a representation of the structure
of the program.[/QUOTE]

Yup, exactly. I posted at nearly 3am, he responded at half 4, I
guess this kind of misunderstanding was probable.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top