public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/51885] New: g++ compiler options -O2 and -O3 modifies program results
@ 2012-01-17 18:59 laurentlouis.maurin at wanadoo dot fr
2012-01-17 18:59 ` [Bug c++/51885] " paolo.carlini at oracle dot com
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: laurentlouis.maurin at wanadoo dot fr @ 2012-01-17 18:59 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51885
Bug #: 51885
Summary: g++ compiler options -O2 and -O3 modifies program
results
Classification: Unclassified
Product: gcc
Version: 4.6.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: laurentlouis.maurin@wanadoo.fr
Created attachment 26356
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26356
example program with comments
platform : Fedora 16 x86_64
compiler : gcc-c++-4.6.2-1.fc16.x86_64
see attaches source file ; it is an example of the quicksort algorithm written
with pointers and templates
one template is specialised and uses a reinterpret_cast (it is part of a bigger
file)
it sorts this array : {12, -1, 2}
a) first test (compilation without any optimisation)
compilation : g++ main.cpp && ./a.out
result OK : {-1, 2, 12}
b) second test (-O2 optimisation)
compilation : g++ -O2 main.cpp && ./a.out
wrong result : {2, 12, -1}
3) third test (-O3 optimisation)
compilation : g++ -O3 main.cpp && ./a.out
wrong result : {2, 12, -1}
Strange behaviour : if i include between line #65 and line #67 (see attached
file)
std::cout<<*ptr_start<<std::endl;
then, the results are good in any case
i believe this is a bug ... the optimisation option should not change the
program result
best regards
Laurent
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/51885] g++ compiler options -O2 and -O3 modifies program results
2012-01-17 18:59 [Bug c++/51885] New: g++ compiler options -O2 and -O3 modifies program results laurentlouis.maurin at wanadoo dot fr
@ 2012-01-17 18:59 ` paolo.carlini at oracle dot com
2012-01-17 20:00 ` redi at gcc dot gnu.org
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-01-17 18:59 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51885
--- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-01-17 18:38:35 UTC ---
What happens if you add to the command line -fno-strict-aliasing? If things
'improve' it means that with your reinterpret_cast & co you have aliasing
violations, thus undefined behavior, thus, literally, anything can happen, and
this is not a bug (and we have hundreds of closed duplicate PRs in Bugzilla)
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/51885] g++ compiler options -O2 and -O3 modifies program results
2012-01-17 18:59 [Bug c++/51885] New: g++ compiler options -O2 and -O3 modifies program results laurentlouis.maurin at wanadoo dot fr
2012-01-17 18:59 ` [Bug c++/51885] " paolo.carlini at oracle dot com
@ 2012-01-17 20:00 ` redi at gcc dot gnu.org
2012-01-18 10:09 ` manu at gcc dot gnu.org
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-17 20:00 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51885
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-17 18:44:32 UTC ---
The reinterpret_cast definitely results in undefined behaviour, because it
accesses the float objects through a different type, violating [basic.lval]/10
And indeed, -fno-strict-aliasing makes the problem go away.
Someone didn't read http://gcc.gnu.org/bugs/ carefully.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/51885] g++ compiler options -O2 and -O3 modifies program results
2012-01-17 18:59 [Bug c++/51885] New: g++ compiler options -O2 and -O3 modifies program results laurentlouis.maurin at wanadoo dot fr
2012-01-17 18:59 ` [Bug c++/51885] " paolo.carlini at oracle dot com
2012-01-17 20:00 ` redi at gcc dot gnu.org
@ 2012-01-18 10:09 ` manu at gcc dot gnu.org
2012-01-20 20:04 ` laurentlouis.maurin at wanadoo dot fr
2012-01-20 20:06 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: manu at gcc dot gnu.org @ 2012-01-18 10:09 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51885
Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |manu at gcc dot gnu.org
--- Comment #3 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2012-01-18 10:02:04 UTC ---
(In reply to comment #2)
> The reinterpret_cast definitely results in undefined behaviour, because it
> accesses the float objects through a different type, violating [basic.lval]/10
>
> And indeed, -fno-strict-aliasing makes the problem go away.
It definitely does not make the problem go away, it merely hides it. The
undefined behavior does not disappear just by using command-line options.
On the other hand, is it possible to cast a float pointer to a uint32 pointer
and use it for anything that does not invoke undefined behaviour?
GCC gives a warning with -Wstrict-aliasing=1 but no with other levels.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/51885] g++ compiler options -O2 and -O3 modifies program results
2012-01-17 18:59 [Bug c++/51885] New: g++ compiler options -O2 and -O3 modifies program results laurentlouis.maurin at wanadoo dot fr
` (2 preceding siblings ...)
2012-01-18 10:09 ` manu at gcc dot gnu.org
@ 2012-01-20 20:04 ` laurentlouis.maurin at wanadoo dot fr
2012-01-20 20:06 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: laurentlouis.maurin at wanadoo dot fr @ 2012-01-20 20:04 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51885
--- Comment #4 from Laurent MAURIN <laurentlouis.maurin at wanadoo dot fr> 2012-01-20 19:18:13 UTC ---
i am quite sure there is something strange : reinterpret_cast is used to cast a
float pointer to a uint32_t pointer
these two types have the same size (4 bytes)
therefore, the swap function (following the reinterpret_cast) should work
properly ...
maybe the -O2 and -O3 compiler optimisation flags have an influence on the
binary code generated for the swap function in this example
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c++/51885] g++ compiler options -O2 and -O3 modifies program results
2012-01-17 18:59 [Bug c++/51885] New: g++ compiler options -O2 and -O3 modifies program results laurentlouis.maurin at wanadoo dot fr
` (3 preceding siblings ...)
2012-01-20 20:04 ` laurentlouis.maurin at wanadoo dot fr
@ 2012-01-20 20:06 ` redi at gcc dot gnu.org
4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-20 20:06 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51885
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-20 19:31:48 UTC ---
(In reply to comment #4)
> i am quite sure there is something strange : reinterpret_cast is used to cast a
> float pointer to a uint32_t pointer
>
> these two types have the same size (4 bytes)
>
> therefore, the swap function (following the reinterpret_cast) should work
> properly ...
No, the C++ standard says your code has undefined behaviour.
Please read the section "Casting does not work as expected when optimization is
turned on" at http://gcc.gnu.org/bugs/#nonbugs_c and the article it links to,
http://mail-index.netbsd.org/tech-kern/2003/08/11/0001.html
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-01-20 19:32 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-17 18:59 [Bug c++/51885] New: g++ compiler options -O2 and -O3 modifies program results laurentlouis.maurin at wanadoo dot fr
2012-01-17 18:59 ` [Bug c++/51885] " paolo.carlini at oracle dot com
2012-01-17 20:00 ` redi at gcc dot gnu.org
2012-01-18 10:09 ` manu at gcc dot gnu.org
2012-01-20 20:04 ` laurentlouis.maurin at wanadoo dot fr
2012-01-20 20:06 ` redi at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).