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

how does duff's device work?

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
9 2795
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
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
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
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
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

"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
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
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
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 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...
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;
5
by: Ark | last post by:
Hi everyone, Does anyone know if Direct3D overloads System.Math functions? Also is it possible to access the base functions of the overloaded function (in other words restore original of the...
11
by: Nick | last post by:
This is really starting to piss me off. Someone please prove me wrong. Here is a link to the function in DX 9 sdk....
5
by: Ruben Campos | last post by:
Greetings. I was recently reading the article "Typed buffers (II)", by Andrei Alexandrescu (C/C++ Users Journal, October 2001), and I found the next function in it: template <class T> inline...
18
by: sunny | last post by:
Hi Why does C allows declaration of variable inside switch block. ex: foll prg does not gives "undeclared "b" error msg. but also does not initialize b to 20 int a=1; switch(a) { int b=20;...
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
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.