Re: (part 13) Han from China answers your C questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick Keighley

    Re: (part 13) Han from China answers your C questions

    On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.c omwrote:
    yawnmoth said:
    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?
    >
    The short answer is that the above code, as it stands, may
    not work if it's optimized away by the compiler into something
    entirely different. There would need to be a 'volatile' qualification
    somewhere along the line
    this is untrue. A conforming C compiler is not permitted to
    "optimize away" this code. It must translate it to something
    that has the same semantics.

    <snip>

    --
    Nick Keighley



  • Ben Bacarisse

    #2
    Re: (part 13) Han from China answers your C questions

    Nick Keighley <nick_keighley_ nospam@hotmail. comwrites:
    On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.c omwrote:
    >yawnmoth said:
    >
    >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);
       }
    >}
    <snip>
    >>
    >The short answer is that the above code, as it stands, may
    >not work if it's optimized away by the compiler into something
    >entirely different. There would need to be a 'volatile' qualification
    >somewhere along the line
    >
    this is untrue. A conforming C compiler is not permitted to
    "optimize away" this code. It must translate it to something
    that has the same semantics.
    But that is surely just a single assignment of one char, no? To that
    extent the phrase "optimise away ... into something entirely
    different" is not unreasonable (though I think it can be put more
    clearly). Of course, I doubt any compiler has the special
    optimisation needed to work out exactly which char in 'from' gets put
    in '*to' but, in theory, it could work it out.

    --
    Ben.

    Comment

    • James Kuyper

      #3
      Re: (part 13) Han from China answers your C questions

      Ben Bacarisse wrote:
      Nick Keighley <nick_keighley_ nospam@hotmail. comwrites:
      >
      >On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.c omwrote:
      >>yawnmoth said:
      >>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);
      >>> }
      >>>}
      <snip>
      >>The short answer is that the above code, as it stands, may
      >>not work if it's optimized away by the compiler into something
      >>entirely different. There would need to be a 'volatile' qualification
      >>somewhere along the line
      'volatile' did not exist at the time Duff invented his device. It is
      true that for this code to do what it was originally intended to do
      would require use of "volatile char*to" in a modern version of C. That's
      why Stroustrup's C++ version used "*to++" instead of "*to".
      >this is untrue. A conforming C compiler is not permitted to
      >"optimize away" this code. It must translate it to something
      >that has the same semantics.
      But that is surely just a single assignment of one char, no?
      To an extent, you're right; without the 'volatile' keyword, this code
      has the same effective semantics as

      if(count)
      *to = from[count-1];

      and could therefore be optimized into the equivalent of that code.
      However, with 'volatile' keyword, such optimization would not be permitted.

      Comment

      Working...