Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Nick Keighley
Guest
 
Posts: n/a
#1: Nov 10 '08
On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.comwrote:
Quote:
yawnmoth said:
Quote:
how does duff's device work?>
>
Quote:
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);
* *}
}
>
Quote:
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
Guest
 
Posts: n/a
#2: Nov 10 '08

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


Nick Keighley <nick_keighley_nospam@hotmail.comwrites:
Quote:
On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.comwrote:
Quote:
>yawnmoth said:
>
Quote:
>how does duff's device work?>
>>
Quote:
>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>
Quote:
Quote:
>>
>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.
James Kuyper
Guest
 
Posts: n/a
#3: Nov 10 '08

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


Ben Bacarisse wrote:
Quote:
Nick Keighley <nick_keighley_nospam@hotmail.comwrites:
>
Quote:
>On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.comwrote:
Quote:
>>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>
Quote:
Quote:
>>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".
Quote:
Quote:
>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.
Closed Thread


Similar C / C++ bytes