Unary plus

Blair P. Houghton bhoughto at hopi.intel.com
Wed Mar 27 16:48:12 AEST 1991


In article <370 at ptcburp.ptcbu.oz.au> michi at ptcburp.ptcbu.oz.au (Michael Henning) writes:
>If I want to enforce evaluation order by assigning to temporary variables, e.g.
>
>	tmp = x + y;
>	tmp -= x;
>	tmp -= y;
>	result = tmp;
>
>is it necessary to declare tmp as volatile to prevent the optimizer from
>optimizing tmp out of existence ?  Or are there some rules that say that the
>optimizer should not get rid of tmp in this case ?

Yes.  The result has to be the one expressed.  The optimizer
would be broken if it produced a result different from the
one obtained with `tmp' intact.

Optimization is moot, always, when the Standard is concerned.
The only time things are undefined is when the Standard says
they are undefined.  If your optimizer produces code that
operates otherwise, then your implementor is not telling you
the truth about conforming to the Standard.

				--Blair
				  "Moot."

P.S. However, if it can guarantee, through correct order of evaluation,
that this would give the same result:

	copy x to result;
	add y to result;
	subtract x from result;
	subtract y from result

then it's free never to allocate storage for tmp.

In fact, if it's capable of guaranteeing that the types
you've selected for x, y, result, _and_ tmp mean that the
result _is_ guaranteed to be zero (e.g., unsigned short
x,y; signed long tmp,result; enough bits in long to hold
short*short), then it may simply do

	result = 0;

or even nothing, if it can further determine that result==0
produces no effect that just 0 couldn't produce.

I.e., if you use the temporary variable, the answer
has to be the one the syntax implies, not the one that
possible optimizations might imply.

Orders of evaluation are often undefined; so optimization
is moot.  It's the syntax which says you're not being
explicit.

I know this P.S. is longer than the body, but I like muddling
around in the moot; you meet an interesting class of moot-monster
in here...



More information about the Comp.std.c mailing list