how does duff's device work?

Discussion in 'C Programming' started by yawnmoth, Nov 9, 2008.

  1. yawnmoth

    yawnmoth Guest

    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?
    yawnmoth, Nov 9, 2008
    #1
    1. Advertising

  2. yawnmoth

    Phil Carmody Guest

    yawnmoth <> writes:
    > 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 /.
    Phil Carmody, Nov 9, 2008
    #2
    1. Advertising

  3. In article <>,
    Richard Heathfield <> wrote:

    >> 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.
    Richard Tobin, Nov 9, 2008
    #3
  4. yawnmoth

    James Harris Guest

    On 9 Nov, 00:38, yawnmoth <> wrote:
    > 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
    James Harris, Nov 9, 2008
    #4
  5. yawnmoth

    Bartc Guest

    "James Harris" <> wrote in message
    news:...
    > On 9 Nov, 00:38, yawnmoth <> wrote:
    >> 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
    Bartc, Nov 9, 2008
    #5
  6. In article <9YzRk.84942$>,
    Bartc <> wrote:
    >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.
    Richard Tobin, Nov 9, 2008
    #6
  7. yawnmoth

    Phil Carmody Guest

    Richard Heathfield <> writes:
    > Phil Carmody said:
    >
    >> yawnmoth <> writes:

    >
    > <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 /.
    Phil Carmody, Nov 9, 2008
    #7
  8. yawnmoth

    Phil Carmody Guest

    (Richard Tobin) writes:
    > In article <>,
    > Richard Heathfield <> wrote:
    >
    >>> 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 /.
    Phil Carmody, Nov 9, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jamie Risk

    Duff Device reference ...

    Jamie Risk, Oct 16, 2004, in forum: C Programming
    Replies:
    1
    Views:
    410
    Michael Wojcik
    Oct 20, 2004
  2. Christopher Benson-Manica

    A small question about Duff's Device

    Christopher Benson-Manica, Oct 21, 2004, in forum: C Programming
    Replies:
    2
    Views:
    304
    Michael Wojcik
    Oct 21, 2004
  3. Jan Richter

    duff's device / loop unriolling

    Jan Richter, Aug 19, 2005, in forum: C Programming
    Replies:
    19
    Views:
    674
    Tim Rentsch
    Aug 29, 2005
  4. Hallvard B Furuseth

    Duff's Device

    Hallvard B Furuseth, Sep 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    638
    Hallvard B Furuseth
    Oct 3, 2006
  5. Replies:
    10
    Views:
    701
Loading...

Share This Page