473,407 Members | 2,359 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,407 software developers and data experts.

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

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
2 1260
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nomen Nescio | last post by:
A new learner's question Flash Gordon wrote: To the OP, please be aware that Flash Gordon has a history of either deliberately misleading and "miss-informing" posters to this newsgroup or...
0
by: Nomen Nescio | last post by:
what is the type for this call? Neo said: Neo, buddy, I've decided to give you a little summary for reference so that you don't have to sift through excess verbiage, cryptic meanderings,...
0
by: Nomen Nescio | last post by:
No format string passed to variable argument list function Adam said: Then Adam laters says: Then Adam later says:
2
by: Chris Dollin | last post by:
Joachim Schmitz wrote: I think that remark is an ill-considered and unhelpful generalisation. -- "We dance, and the worlds melt away." - Curved Air, /Metamorphosis/ ...
2
by: Borked Pseudo Mailed | last post by:
float and integers c.lang.myself@gmail.com said I will be making assumptions here about your system that will infuriate the pedants, but I don't care. Given your X-HTTP-UserAgent string, it...
1
by: George Orwell | last post by:
its urgent.pls help me. mona said: mona said: Hey again, mona. Please check the thread "(part 21) Han from China answers your C questions", in which I gave you a program with
0
by: George Orwell | last post by:
fread breaks file descriptors opened in "w" mode. ... Yes, once you break out of ANSI/ISO C, all things are possible. You have posted an on-topic question to comp.lang.c. See version 2.0 of...
16
by: Borked Pseudo Mailed | last post by:
Error handling in C Nick Keighley said: ... ... To the OP, please be aware that Nick Keighley is a known troll and is deliberately providing you with unsound advice. You asked for error...
2
by: Nomen Nescio | last post by:
What is this? Eric said: That looks like either someone's poor coding or someone's poor pasting for an implementation of offsetof() from stddef.h. The (char *) leads me to think that there's...
1
by: George Orwell | last post by:
File Seeking / Overwriting bytes Martien Verbruggen said: And this is, of course, also not right. fseek() does support SEEK_END on text streams. If you want to know why, please start a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.