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