On 10 Nov, 11:30, Nomen Nescio <nob...@dizum.c omwrote:
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
yawnmoth said:
how does duff's device work?>
>
>
>
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
>
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);
}
}
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?
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
"optimize away" this code. It must translate it to something
that has the same semantics.
<snip>
--
Nick Keighley
Comment