Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Parentheses and compiler optimzation

Reply
Thread Tools

Parentheses and compiler optimzation

 
 
spasmous
Guest
Posts: n/a
 
      04-10-2008
If a compiler see's the following statement

c = (a+b)-b

will it optimize it to c=a? I want to force the compiler to evaluate (a
+b) first. Do the parentheses ensure an ANSI conforming compiler HAS
to do that? If not, is there a way?
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      04-10-2008
spasmous wrote:
) If a compiler see's the following statement
)
) c = (a+b)-b
)
) will it optimize it to c=a?

It could, yes.

) I want to force the compiler to evaluate (a+b) first.

Why would you want to do that ?

) Do the parentheses ensure an ANSI conforming compiler HAS to do that?

No, I don't think so. The as-if rule and such, you know.

) If not, is there a way?

That depends. What *exactly* do you want to do ?
The result will be the same no matter if the (a+b) is evaluated or not.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      04-10-2008
In article <(E-Mail Removed)>,
Willem <(E-Mail Removed)> wrote:
>spasmous wrote:
>) If a compiler see's the following statement


>) c = (a+b)-b


>) will it optimize it to c=a?


>That depends. What *exactly* do you want to do ?
>The result will be the same no matter if the (a+b) is evaluated or not.


Think floating point. If a has a much smaller magnitude than b,
then a+b would evaluate to b; b - b would then evaluate to 0, not a.
Thus for floating point, optimizing (a+b)-b to simply a would give
the "wrong" answer relative to floating point characteristics.

--
"I want to be remembered as the guy who gave his all whenever
he was on the field." -- Walter Payton
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-10-2008
spasmous <(E-Mail Removed)> writes:
> If a compiler see's the following statement
>
> c = (a+b)-b
>
> will it optimize it to c=a?


It's likely to to so, though there's no requirement to perform such an
optimization.

What types are a, b, and c? It might make a difference.


> I want to force the compiler to evaluate (a
> +b) first.


Why? What problem are you really trying to solve? Ordinarily,
replacing (a+b)-b by a will give you the same result.

> Do the parentheses ensure an ANSI conforming compiler HAS
> to do that?


No, parentheses merely specify which operands are associated with
which operators; they don't force any order of evaluation.

> If not, is there a way?


One obvious way is:

tmp = a + b;
c = tmp - b;

but the compiler is still free to optimize it down to "c = a;".

Judicious use of "volatile" might be a solution -- but again, without
knowing the underlying problem, I hesitate to make suggestions.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      04-10-2008
Morris Dovey wrote:
>
> spasmous wrote:
> >
> > If a compiler see's the following statement
> >
> > c = (a+b)-b
> >
> > will it optimize it to c=a? I want to force the compiler to evaluate (a
> > +b) first. Do the parentheses ensure an ANSI conforming compiler HAS
> > to do that? If not, is there a way?

>
> AFAICT, section 6.5[3] and footnote 71 assure that the
> parenthesized expression (a+b) is evaluated first, then b is
> subtracted from the result.


AFAIK, unless b is volatile, no such requirement exists, just as
there is no such requirement in

c = ( printf("one") + printf("two") ) - printf("three");

that the output be "onetwothree".

And, AFAIK, even if b is volatile, there is no requirement that the
one inside the parentheses is evaluated before the one outside.

You could do something like

c = a + b;
c = c - b;

but even then, a compiler could still optimize out the b's (unless
it's volatile).

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      04-10-2008
In article <(E-Mail Removed)>,
spasmous <(E-Mail Removed)> wrote:

>If a compiler see's the following statement
>
>c = (a+b)-b
>
>will it optimize it to c=a? I want to force the compiler to evaluate (a
>+b) first. Do the parentheses ensure an ANSI conforming compiler HAS
>to do that?


No.

I seem to recall there was some dispute about this during the original
C standardisation - you might be able to find it with Google.

>If not, is there a way?


Assigning the intermediate result to a volatile variable should
ensure it.

But you need to tell us what you are really trying to achieve.

-- Richard
--
:wq
 
Reply With Quote
 
christian.bau
Guest
Posts: n/a
 
      04-10-2008
On Apr 10, 8:48*pm, spasmous <(E-Mail Removed)> wrote:
> If a compiler see's the following statement
>
> c = (a+b)-b
>
> will it optimize it to c=a? I want to force the compiler to evaluate (a
> +b) first. Do the parentheses ensure an ANSI conforming compiler HAS
> to do that? If not, is there a way?


The compiler will add a and b, then subtract b. Whether you use
parentheses or not doesn't make any difference at all.

However, there is the "as if" rule: The compiler is allowed to do
_anything_ that is guaranteed to give exactly the same result. For
example, if a and b are both of type unsigned int then it is
guaranteed that (a + b) - b and a give the same result. On the other
hand, if there _is_ a difference between (a + b) - b and a, then the
compiler will _not_ produce a.

 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      04-10-2008
Morris Dovey <(E-Mail Removed)> wrote:
>
> AFAICT, section 6.5[3] and footnote 71 assure that the
> parenthesized expression (a+b) is evaluated first, then b is
> subtracted from the result.


In the abstract machine, but the as-if rule allows the compiler to
optimize as long as it gets the same answer.

-Larry Jones

See if we can sell Mom and Dad into slavery for a star cruiser. -- Calvin
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      04-11-2008
Keith Thompson <(E-Mail Removed)> wrote:
> spasmous <(E-Mail Removed)> writes:
> > If a compiler see's the following statement
> >
> > c = (a+b)-b
> >
> > will it optimize it to c=a?

>
> It's likely to to so,


Maybe, maybe not.

> though there's no requirement to
> perform such an optimization.


There _is_ a requirement that no optimisation
should change the behaviour from the virtual
machine [if there is no unspecified or
undefined behaviour.]

Consider...

int a = -1;
unsigned b = 0;

The expression (a + b) - b has neither the
type nor value of a.

Even if a and b have the same type there can
be problems. See Walter's floating point
example.

--
Peter
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      04-11-2008
Walter wrote:
) In article <(E-Mail Removed)>,
) Willem <(E-Mail Removed)> wrote:
)>spasmous wrote:
)>) If a compiler see's the following statement
)
)>) c = (a+b)-b
)
)>) will it optimize it to c=a?
)
)>That depends. What *exactly* do you want to do ?
)>The result will be the same no matter if the (a+b) is evaluated or not.
)
) Think floating point. If a has a much smaller magnitude than b,
) then a+b would evaluate to b; b - b would then evaluate to 0, not a.
) Thus for floating point, optimizing (a+b)-b to simply a would give
) the "wrong" answer relative to floating point characteristics.

Oh, sorry. I assumed integer.

If it's floating point, then no, the compiler is not allowed
to optimize it away.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
parentheses and newlines jsnark Ruby 2 02-18-2012 07:56 PM
Eliminating Parentheses and Words Between Them In Word Doc Martin Computer Support 9 07-13-2007 07:06 AM
C parentheses, brackets and brace case.learning@gmail.com C Programming 8 06-16-2007 12:41 AM
AND and OR and parentheses rick C Programming 13 08-20-2005 01:51 PM
Give some ideas for c Optimzation chellappa C Programming 18 07-08-2005 05:10 AM



Advertisments