I have found some code that produces different results
depending upon whether optimisation is turned on.
Optimisation O3 inlines, optimises, and doesn't generate
an overflow, thus both red then green are turned on.
Without optimisation, the function is called, there is an
overflow in the second case, and only the red is turned on.
Not sure if this is already documented or not, but it seemed
worth pointing out just in case.
void TestFunction(s32 TestVar)
{
if ((TestVar + 100) < 0)
{
Port(TurnOn, RedLed); // Turn Red Led On
}
else
{
Port(TurnOn, GreenLed); // Turn Green Led On
}
}
int main(void)
{
TestFunction(-200);
TestFunction(0x7ffffff0);
while(1);
}
(disclamer: yes I have posted to gnu.gcc without any comment, and
yes, I know port is not a valid c statement, its just illustrating the
point)
|
|
0
|
|
|
|
Reply
|
Paul
|
12/8/2008 9:45:17 AM |
|
On Dec 8, 11:45 am, "Paul" <-> wrote:
> I have found some code that produces different results
> depending upon whether optimisation is turned on.
> Optimisation O3 inlines, optimises, and doesn't generate
> an overflow, thus both red then green are turned on.
> Without optimisation, the function is called, there is an
> overflow in the second case, and only the red is turned on.
> Not sure if this is already documented or not, but it seemed
> worth pointing out just in case.
>
> void TestFunction(s32 TestVar)
> {
> if ((TestVar + 100) < 0)
> {
> Port(TurnOn, RedLed); // Turn Red Led On
> }
> else
> {
> Port(TurnOn, GreenLed); // Turn Green Led On
> }
>
> }
>
> int main(void)
> {
> TestFunction(-200);
>
> TestFunction(0x7ffffff0);
>
> while(1);
>
> }
>
> (disclamer: yes I have posted to gnu.gcc without any comment, and
> yes, I know port is not a valid c statement, its just illustrating the
> point)
Signed integer conversion out of range is implementation-defined. Read
the docs of your implementation about it, and you'll find out why this
occurs. (assuming s32 means 31 value bits 1 signed bit)
|
|
0
|
|
|
|
Reply
|
vippstar (1211)
|
12/8/2008 9:54:48 AM
|
|
On Dec 8, 10:45=A0pm, "Paul" <-> wrote:
> I have found some code that produces different results
> depending upon =A0whether optimisation is turned on.
>
> void TestFunction(s32 TestVar)
> {
> =A0if ((TestVar + 100) < 0)
> =A0 Port(TurnOn, RedLed); =A0// Turn Red Led On
> =A0else
> =A0 Port(TurnOn, GreenLed); =A0// Turn Green Led On
> }
>
> int main(void)
> {
> =A0TestFunction(-200);
> =A0TestFunction(0x7ffffff0);
> }
I'm assuming "s32" is a 32-bit signed int.
The C standard does not define what happens
when the result of an addition is too big for
the signed type in question. When you add 100
to 0x7fffffff0, the behaviour is undefined.
Practically speaking, what this means is that
the compiler may optimise its code, based on
the assumption that your code does not do anything
undefined. In this particular instance, the
compiler may have done something like rearranging
the test to be:
if ( TestVar < -100 )
when optimisation was on. You could check the
intermediate assembly to see for sure.
|
|
0
|
|
|
|
Reply
|
oldwolf (2278)
|
12/8/2008 1:24:48 PM
|
|
On Dec 8, 9:45=A0am, "Paul" <-> wrote:
> I have found some code that produces different results
> depending upon =A0whether optimisation is turned on.
> Optimisation O3 inlines, optimises, and doesn't generate
> an overflow, thus =A0both red then green are turned on.
> Without optimisation, the function is called, there is an
> overflow in the =A0second case, and only the red is turned on.
> Not sure if this is already documented or not, but it seemed
> worth pointing out just in case.
>
> void TestFunction(s32 TestVar)
> {
> =A0if ((TestVar + 100) < 0)
> =A0{
> =A0 Port(TurnOn, RedLed); =A0// Turn Red Led On
> =A0}
> =A0else
> =A0{
> =A0 Port(TurnOn, GreenLed); =A0// Turn Green Led On
> =A0}
>
> }
>
> int main(void)
> {
> =A0TestFunction(-200);
>
> =A0TestFunction(0x7ffffff0);
>
> =A0while(1);
>
> }
>
> (disclamer: yes I have posted to gnu.gcc without any comment, and
> yes, I know port is not a valid c statement, its just illustrating the
> point)
The second function call leads to an arithmetic overflow, which
invokes undefined behaviour, which means anything can happen. In this
case, apparently you get different behaviour of the program with and
without compiler optimisation. This is just one of the many things
that could happen with undefined behaviour. Not the compilers fault,
but the programmer's fault.
|
|
0
|
|
|
|
Reply
|
christian.bau1 (403)
|
12/8/2008 10:32:58 PM
|
|
Paul wrote:
>
> I have found some code that produces different results depending
> upon whether optimisation is turned on. Optimisation O3 inlines,
> optimises, and doesn't generate an overflow, thus both red then
> green are turned on. Without optimisation, the function is called,
> there is an overflow in the second case, and only the red is
> turned on. Not sure if this is already documented or not, but it
> seemed worth pointing out just in case.
This sort of behaviour normally indicates that you have violated
some applicable C rule.
>
> void TestFunction(s32 TestVar) {
> if ((TestVar + 100) < 0) {
> Port(TurnOn, RedLed); // Turn Red Led On
> }
> else {
> Port(TurnOn, GreenLed); // Turn Green Led On
> }
> }
>
> int main(void) {
> TestFunction(-200);
> TestFunction(0x7ffffff0);
> while(1);
> }
>
> (disclamer: yes I have posted to gnu.gcc without any comment, and
> yes, I know port is not a valid c statement, its just illustrating
> the point)
In this case we have no idea what the type s32 is, but my guess is
that you have exceeded its maximum value. Your code is not
compilable - it is missing various includes, definitions etc. I
have also modified it slightly to take less room in the message.
In addition // comments are not advisable in Internet messages -
use /* ... */, which wrap.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
12/8/2008 11:16:53 PM
|
|
On Mon, 8 Dec 2008 01:54:48 -0800 (PST), vippstar@gmail.com wrote in
comp.lang.c:
> On Dec 8, 11:45 am, "Paul" <-> wrote:
> > I have found some code that produces different results
> > depending upon whether optimisation is turned on.
> > Optimisation O3 inlines, optimises, and doesn't generate
> > an overflow, thus both red then green are turned on.
> > Without optimisation, the function is called, there is an
> > overflow in the second case, and only the red is turned on.
> > Not sure if this is already documented or not, but it seemed
> > worth pointing out just in case.
> >
> > void TestFunction(s32 TestVar)
> > {
> > if ((TestVar + 100) < 0)
> > {
> > Port(TurnOn, RedLed); // Turn Red Led On
> > }
> > else
> > {
> > Port(TurnOn, GreenLed); // Turn Green Led On
> > }
> >
> > }
> >
> > int main(void)
> > {
> > TestFunction(-200);
> >
> > TestFunction(0x7ffffff0);
> >
> > while(1);
> >
> > }
> >
> > (disclamer: yes I have posted to gnu.gcc without any comment, and
> > yes, I know port is not a valid c statement, its just illustrating the
> > point)
>
> Signed integer conversion out of range is implementation-defined. Read
> the docs of your implementation about it, and you'll find out why this
> occurs. (assuming s32 means 31 value bits 1 signed bit)
Signed integer conversion if not happening here, at all. Signed
integer overflow is, and the behavior is undefined.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
|
|
0
|
|
|
|
Reply
|
jackklein (3932)
|
12/9/2008 3:21:02 AM
|
|
>I have found some code that produces different results
>depending upon whether optimisation is turned on.
Part of the point of undefined behavior is to allow the compiler
to do what it wants without worrying about certain problems.
Different optimization levels may do different things.
>Optimisation O3 inlines, optimises, and doesn't generate
>an overflow, thus both red then green are turned on.
Signed overflow causes undefined behavior.
I'm assuming that s32 is a signed 32-bit integer.
With the numbers in your program, this will cause
signed overflow.
|
|
0
|
|
|
|
Reply
|
gordonb.92b98 (1)
|
12/9/2008 3:38:49 AM
|
|
On Dec 9, 5:21 am, Jack Klein <jackkl...@spamcop.net> wrote:
> On Mon, 8 Dec 2008 01:54:48 -0800 (PST), vipps...@gmail.com wrote in
> comp.lang.c:
>
>
>
> > On Dec 8, 11:45 am, "Paul" <-> wrote:
> > > I have found some code that produces different results
> > > depending upon whether optimisation is turned on.
> > > Optimisation O3 inlines, optimises, and doesn't generate
> > > an overflow, thus both red then green are turned on.
> > > Without optimisation, the function is called, there is an
> > > overflow in the second case, and only the red is turned on.
> > > Not sure if this is already documented or not, but it seemed
> > > worth pointing out just in case.
>
> > > void TestFunction(s32 TestVar)
> > > {
> > > if ((TestVar + 100) < 0)
> > > {
> > > Port(TurnOn, RedLed); // Turn Red Led On
> > > }
> > > else
> > > {
> > > Port(TurnOn, GreenLed); // Turn Green Led On
> > > }
>
> > > }
>
> > > int main(void)
> > > {
> > > TestFunction(-200);
>
> > > TestFunction(0x7ffffff0);
>
> > > while(1);
>
> > > }
>
> > > (disclamer: yes I have posted to gnu.gcc without any comment, and
> > > yes, I know port is not a valid c statement, its just illustrating the
> > > point)
>
> > Signed integer conversion out of range is implementation-defined. Read
> > the docs of your implementation about it, and you'll find out why this
> > occurs. (assuming s32 means 31 value bits 1 signed bit)
>
> Signed integer conversion if not happening here, at all. Signed
> integer overflow is, and the behavior is undefined.
I'm sorry, you're right. I thought something happened in TestFunction
(0x7ffffff0) but then I realized what number that is. I see the
overflow now too.
|
|
0
|
|
|
|
Reply
|
vippstar (1211)
|
12/10/2008 12:43:09 AM
|
|
|
7 Replies
19 Views
(page loaded in 0.13 seconds)
Similiar Articles: Why gcc translate a c program into assemble as follow - comp.lang ...... alignment purposes i dont recommend understanding this issue. gcc will produce two different ... */ ix86_preferred_stack_boundary = (optimize_size ... VisualC++ vs GCC: read in a number in exponential notation - comp ...... are being done on that data, and the desired results. ... library correctly reads both notations and produces the ... Identical parsing of different FP formats by the stream ... improve strlen - comp.lang.asm.x86... routine faster than gcc compiler, simply routine only about strlen the result ... ... if you're trying to produce generic results ... ll probably get quite different results ... nasm gcc basic problem for - comp.lang.asm.x86... errors (that the tools mention), but not produce ... Both are 32-bit, but otherwise very different. If it's djgpp gcc (and ... the data in a code section (and will result in ... NASM on WINXP x64 creating EXE - comp.lang.asm.x86... quick response, yep a I mentioned I did try this already with gcc but produces the same result. ... to be first class and see no reason why the ld should > be any different ... problem with mixed c and fortran code - comp.lang.fortran ...I expected the two different pointers ... ... cp2=0xbfdc8b67('B') Expected result: c1='F' c2='B' $ gcc ... underscore) x64 compile/build/run produces result ... fpu code optimisation request - comp.lang.asm.x86... if b = 0 then test_b = 0 else test_b = 1 result ... The standard optimization trick is to load the data into ... unrelated note> Actually, when I see lots of different ... Explanation needed for const int "error: variably modified ... at ...Hi, The following code results in the following error with GCC (v.4.3.2 ... either a constraint violation or will produce ... you risk confusing others who have a different ... how to optimize c code of Cordic algorithm - comp.dspCan something tell me how can i optimize the ... the internals can be significantly different ... left by 1, and adds it to src. > Produces a saturated 32-bit result. C decompiler/disassembler? - comp.lang.c... is %f\n", pi); > > return 0; > } > > // gcc ... bytes, or it's an > =A0 =A0 =A0 =A0 =A0 optimization of ... But manual disassembly will always produce better results. optimization - Why does GCC generate such radically different ...... knowing exactly how the relevant GCC optimization ... have to work as hard and as a result produces more ... did you look at what different versions of gcc produce? 3.x and 4.x ... An Introduction to GCC - Compiling with optimization... with optimization. GCC is ... final result ... for different instructions, depending on how they are ordered. GCC takes all these factors into account and tries to produce the ... 7/17/2012 5:52:58 AM
|