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

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

P: n/a
On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.comwrote:
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

Nov 10 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Nick Keighley <ni******************@hotmail.comwrites:
On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.comwrote:
>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.
Nov 10 '08 #2

P: n/a
Ben Bacarisse wrote:
Nick Keighley <ni******************@hotmail.comwrites:
>On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.comwrote:
>>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.
Nov 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.