473,395 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Parentheses and compiler optimzation

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?
Apr 10 '08 #1
7 1488
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
Apr 10 '08 #2
In article <sl********************@snail.stack.nl>,
Willem <wi****@stack.nlwrote:
>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
Apr 10 '08 #3
On Apr 10, 8:48*pm, spasmous <spasm...@gmail.comwrote:
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.

Apr 10 '08 #4
Keith Thompson <ks...@mib.orgwrote:
spasmous <spasm...@gmail.comwrites:
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
Apr 11 '08 #5
On Apr 11, 3:37*pm, Kenneth Brody <kenbr...@spamcop.netwrote:
I see that my compiler will optimize it away for int, but not for
float. *Where would one find this prohibition in the standard?

Mathematically, "(a+b)-b" is the same as "a". *Of course, in this
case, there may be a loss of precision, but does the standard say
that you must preserve loss of precision? *Why doesn't the "as if"
rule apply to floating point?
The C Standard says that (a + b) - b means: Add a and b, subtract b
from the result. Same for integer and floating point. The C Standard
also says that the compiler is allowed to do absolutely anything it
likes, but only as long as the result is exactly what the C Standard
says. So the C Standard doesn't have to say "it's allowed for integer
and not allowed for floating-point". Instead, it is up to the compiler
writer to _prove_ that they produce exactly the same result.

And: "Mathematically" doesn't count. Actually, when you say
"mathematically" you mean "real numbers". Since the C Standard uses
floating-point numbers and not real numbers, it is irrelevant how real
numbers work.

Now an example: How could the compiler optimise this?

double f (int x) { double a = (double) x; double b = 1e300; return
(a + b) - b; }

(And no, it can't replace the return statement with "return a;" but
with something very different!)

Apr 11 '08 #6
On Fri, 11 Apr 2008 14:35:06 -0700, christian.bau wrote:
Now an example: How could the compiler optimise this?

double f (int x) { double a = (double) x; double b = 1e300; return
(a + b) - b; }

(And no, it can't replace the return statement with "return a;" but with
something very different!)
Yes, it can replace the return statement with "return a;", unless you
specifically tell it it can't.

6.5p8
"A floating expression may be /contracted/, that is, evaluated as though
it were an atomic operation, thereby omitting rounding errors implied by
the source code and the expression evaluation method. The FP_CONTRACT
pragma in <math.hprovides a way to disallow contracted expressions.
Otherwise, whether and how expressions are contracted is implementation-
defined."
7.12.2p2
"[...] The default state ("on" or "off") for the pragma is
implementation-defined."
Jun 27 '08 #7
On Thu, 10 Apr 2008 13:18:42 -0700, Keith Thompson wrote:
spasmous <sp******@gmail.comwrites:
>If a compiler see's the following statement

c = (a+b)-b

will it optimize it to c=a?
<snip>
One obvious way is:

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

but the compiler is still free to optimize it down to "c = a;".
Chances are, as soon as the compiler finishes parsing the code and starts
the first stage of compiling, this is already the form it's been reduced
to. As far as optimizations go, this change is most likely a no-op unless
tmp is volatile.
Judicious use of "volatile" might be a solution -- but again, without
knowing the underlying problem, I hesitate to make suggestions.
Correct.
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Norm Dotti | last post by:
In VS 2002 I could specify Dim x As System.Windows.Forms.Form() or Dim x As System.Windows.Forms.Form and everything was ok. Since upgrading to VS 2003 it won't let me put in the...
18
by: chellappa | last post by:
hi suppose like this function ,,, i want optimize to exceute more fast.... please give some optimization techiques for this routine and also give some ideas for c programming optimzation for...
13
by: rick | last post by:
the following line appears in the K&R book in section 5.7: leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; my compiler suggests parentheses "around && within ||" given that the AND...
44
by: Don Kim | last post by:
Ok, so I posted a rant earlier about the lack of marketing for C++/CLI, and it forked over into another rant about which was the faster compiler. Some said C# was just as fast as C++/CLI, whereas...
5
by: Chris McDonald | last post by:
I've been trying to wean myself off using parentheses after the sizeof operator (and after the return keyword, too), but my understanding is challenged by the 4th use of sizeof in the following...
3
by: Frederick Gotham | last post by:
If you wish to override the natural operator precedence and associativity in C, you can use parentheses: 5 * (4 - (2 + 7)) However, I was surprised to discover (by testing) yesterday that some...
6
by: natkw1 | last post by:
Hi all, I'm new to C and kind of confused on this. I've had a look around this group for suggestions but still not sure why the warning is occurring. What I've done is this <code snip> void...
3
by: Lyn | last post by:
Can anyone explain this for me? A sub procedure can be called with or without parentheses around the arguments. By personal convention, I normally use parentheses. I am in the middle of a...
11
by: rtalbot | last post by:
Can somebody clarify why: Given a simple template: template <class T> struct foo { }; This: foo<float> bar;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.