Re: Order of function evaluation
From: Glen Herrmannsfeldt (gah_at_ugcs.caltech.edu)
Date: 10/25/03
- Next message: Richard: "Re: FORTRAN or COBOL first ?"
- Previous message: Robert Corbett: "Re: optimization??"
- Next in thread: Chris Torek: "Re: Order of function evaluation"
- Reply: Chris Torek: "Re: Order of function evaluation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 25 Oct 2003 01:19:22 GMT
"Sheldon Simms" <sheldonsimms@yahoo.com> wrote in message
news:pan.2003.10.25.00.22.19.527576@yahoo.com...
> On Fri, 24 Oct 2003 23:41:34 +0000, Jens.Toerring wrote:
>
> > I have a possibly rather stupid question about the order of evaluation
> > in a statement like this:
> >
> > result = foo( x ) - bar( y );
> >
> > How can I make 100% sure that foo(x) is evaluated before bar(y), where
> > foo() and bar() can be either functions or macros?
(snip)
> In the first case, there is only one sequence point at the end of the
> single line and since the compiler doesn't actually have to complete
> any side effects until then, it is free to rearrange the evaluation
> of foo() and bar().
>
> Some refs:
>
> 5.1.2.3 Program execution
> 2 Accessing a volatile object, modifying an object, modifying a
> file, or calling a function that does any of those operations
> are all side effects, which are changes in the state of the
> execution environment. Evaluation of an expression may produce
> side effects. At certain specified points in the execution
> sequence called sequence points, all side effects of previous
> evaluations shall be complete and no side effects of subsequent
> evaluations shall have taken place.
I am not at all sure what optimizers are allowed to do. At least in Fortran
there once was a traditional optimization of moving invariant expressions
out of loops. Though doing that for function calls can sometimes cause
problems.
One favorite example from Fortran, but translated to C goes something like:
for(i=0;i<n;i++) {
if(a>0) b[i] *= sqrt(a);
c[i] *= a;
}
There were compilers that would evaluate sqrt(a) outside the loop, keep the
result in a register and use it in the multiply.
Function calls don't tend to allow that optimization in general, though. I
believe that C compilers can still do common subexpression elimination,
except in the case of volatile variables.
-- glen
- Next message: Richard: "Re: FORTRAN or COBOL first ?"
- Previous message: Robert Corbett: "Re: optimization??"
- Next in thread: Chris Torek: "Re: Order of function evaluation"
- Reply: Chris Torek: "Re: Order of function evaluation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|