[Info-vax] decrementing & for loops in C

Johnny Billquist bqt at update.uu.se
Mon Jan 12 17:33:53 EST 2009


Bill Gunshannon skrev:
> In article <RBJ9l.9487$cu.6865 at news-server.bigpond.net.au>,
> 	"Tim E. Sneddon" <tesneddon at bigpond.com> writes:
>> Bill Gunshannon wrote:
>>> In article <49667A6E.3050507 at nowhere.com>,
>>> 	nobody <nobody at nowhere.com> writes:
>>>> Turning off optimization should normally cause the program to execute 
>>>> your program exactly as written.
>>> A program should always execute exactly as written.  No machine is smart
>>> enough to second guess the intent of a human.
>>>
>> That is arguable. After your code has been optimised
>> it doesn't quite execute 'exactly as written'. The
>> end result might be 'exactly as written'. However, the
>> path to get there has been completely shuffled around
>> by the compiler.
> 
> Well, let's be clear on this.  By "exactly as written" I mean just what it
> says.  Not "as the writer intended", but "exactly as written".  That allows
> for optimizing out code that would never be executed because I wrote the 
> code wrong, like the original example.  It may not be what I wanted, but
> it is what I wrote.  But, the system should not be changing the order of
> operations for any code I wrote because it has absolutely no way of
> determining that there was not a particular reason for doing it in that
> order.  No machine (or compiler) is that smart.  Someone mentioned Ada
> doing this in an earlier post.  Knowing what the primary purposes of Ada
> are, that would really scare me.
> "Let's see.  The programmer wants me to check for weight on the wheel
>  struts before raising the landing gear.  but I think it would be much
>  more efficient to raise the gear first."   :-)

The poster was partially wrong. The optimizer won't do anything like that. It 
can only reorder things that are independent of each other, and for which there 
is no sequential "connection" between.

For instance (in C):

The following two lines could be executed in any order, and therefore an 
optimizer could shuffle them around with no ill effect.

     a = b + 42;
     c = d + 11;

> What, you say that's just silly.  How about this?
> 
> {
>    int thrust_reversers, power_level;
> 
> { thrust_reversers = 10; power_level = 20; }
> }
> 
> Does the order of these two (apparently) equal operations make a difference?
> Can a machine determine that?

That is an example of things that an optimizer could do in any order, yes.

If the order makes a difference, then the programmer should say so. In C, you do 
that by saying:

{
   volatile int thrust_reversers, power_level;

   { thrust_reversers = 10; power_level = 20; }
}

If you write code as above, Bill, you will get into trouble if those variables 
actually were reflected to anything outside in the real world. In fact, a proper 
optmimzer would throw your whole code away, since it's not actually using those 
values for anything, so in effect it's just meaningless code.

That's what volatile are for.

	Johnny

-- 
Johnny Billquist                  || "I'm on a bus
                                   ||  on a psychedelic trip
email: bqt at softjar.se             ||  Reading murder books
pdp is alive!                     ||  tryin' to stay hip" - B. Idol



More information about the Info-vax mailing list