By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,949 Members | 1,345 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,949 IT Pros & Developers. It's quick & easy.

how does duff's device work?

P: n/a
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?
Nov 9 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
yawnmoth <te*******@yahoo.comwrites:
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
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
Nov 9 '08 #2

P: n/a
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
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.
No. Syntax errors are a concept that is only relevant at the time of
translation. By the time the program has been translated and (if
necessary) linked, there is no way to get a syntax error.

The translation will result in some way of expressing the above in a
different language. For example, making up some (not terribly efficient)
assembly language syntax, and starting at the switch:

MOV R7, TO ; save the destination pointer in a register
MOV R1, COUNT ; save the count in a register
MOV R2, N ; save n in a register
MOV R3, R1 ; copy count to a temporary place
DIV R3, 8, R4 ; divide R3 by 8, storing the remainder in R4
C0:
CMP R4, 0 ; is count % 8 == 0?
JNZ C7 ; no
MOV R5, FROM ; yes, so save the source pointer in a register
MOV R6, *R5 ; deref source ptr
MOV *R7, R6 ; copy fetched value to destination
C7:
CMP R4, 7 ; is count % 8 == 7?
JNZ C6 ; no
MOV R5, FROM ; yes, so save the source pointer in a register
MOV R6, *R5 ; deref source ptr
MOV *R7, R6 ; copy fetched value to destination
C6:

; etc (cases 6, 5, 4, 3, 2...)

C1:
MOV R5, FROM ; save source ptr in register
MOV R6, *R5 ; deref source ptr
MOV *R7, R6 ; copy fetched value to destination
DEC R2 ; --n
CMP R2, 0 ; now decide whether to jump back to case 0
JNZ C0

So the mystery lies only in the C source, where the above is probably only
legal because nobody got around to forbidding it, but it is nevertheless
legal. As you can see, it isn't quite so mysterious, once you've worked
through how it would be translated into something a little more - um -
linear.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 9 '08 #3

P: n/a
Phil Carmody said:
yawnmoth <te*******@yahoo.comwrites:
<snip>
>If count % 8 is 7, the switch statement would skip the 'do' keyword

Are you mistaking C for a dumb interpreted language?
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 9 '08 #4

P: n/a
In article <TZ******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>Are you mistaking C for a dumb interpreted language?
>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
--
Please remember to mention me / in tapes you leave behind.
Nov 9 '08 #5

P: n/a
On 9 Nov, 00:38, yawnmoth <terra1...@yahoo.comwrote:
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
--
James
Nov 9 '08 #6

P: n/a

"James Harris" <ja************@googlemail.comwrote in message
news:6b**********************************@g17g2000 prg.googlegroups.com...
On 9 Nov, 00:38, yawnmoth <terra1...@yahoo.comwrote:
>dsend(to, from, count)
char *to, *from;
int count;
{
int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
>I'm a little confused how the code works. Maybe there's some quirk in
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?

--
Bartc

Nov 9 '08 #7

P: n/a
In article <9Y*******************@text.news.virginmedia.com >,
Bartc <bc@freeuk.comwrote:
>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

--
Please remember to mention me / in tapes you leave behind.
Nov 9 '08 #8

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
Phil Carmody said:
>yawnmoth <te*******@yahoo.comwrites:

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

Are you mistaking C for a dumb interpreted language?

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
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
Nov 9 '08 #9

P: n/a
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <TZ******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>Are you mistaking C for a dumb interpreted language?
>>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.
Yup, exactly. I posted at nearly 3am, he responded at half 4, I
guess this kind of misunderstanding was probable.

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
Nov 9 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.