public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/25235]  New: byte swapping unreliable in optimized builds
@ 2005-12-02 23:30 cdfrey at netdirect dot ca
  2005-12-02 23:33 ` [Bug c++/25235] " pinskia at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: cdfrey at netdirect dot ca @ 2005-12-02 23:30 UTC (permalink / raw)
  To: gcc-bugs

GCC version:  g++ -v
        Using built-in specs.
        Target: i686-pc-linux-gnu
        Configured with: ../gcc-4.0.2/configure
--prefix=/home/source5/rootdir-gcc402 --enable-languages=c,c++
--enable-concept-checks
        Thread model: posix
        gcc version 4.0.2

Sample code that reproduces the bug:

#include <iostream>
#include <stdint.h>

// From glibc
#define pa_bswap_64(x) \
     ((((x) & 0xff00000000000000ull) >> 56)                                   \
      | (((x) & 0x00ff000000000000ull) >> 40)                                 \
      | (((x) & 0x0000ff0000000000ull) >> 24)                                 \
      | (((x) & 0x000000ff00000000ull) >> 8)                                  \
      | (((x) & 0x00000000ff000000ull) << 8)                                  \
      | (((x) & 0x0000000000ff0000ull) << 24)                                 \
      | (((x) & 0x000000000000ff00ull) << 40)                                 \
      | (((x) & 0x00000000000000ffull) << 56))

int main()
{
        double d = 42.42;
        uint64_t conv = *((uint64_t*) &d);
        conv = pa_bswap_64(conv);
        uint64_t back = pa_bswap_64(conv);
        double d2 = *((double*)&back);

        std::cout
                << "d = " << d << " d2 = " << d2 << std::endl
                << "conv = " << conv << " back = " << back << std::endl;
}


When compiled without optimization, I get:

source5@cube tmp $ /home/source5/rootdir-gcc402/bin/g++ -Wall -o swap swap.cc
source5@cube tmp $ ./swap
d = 42.42 d2 = 42.42
conv = 17737528904907048256 back = 4631166901565532406


When compiled with optimization, I get:

source5@cube tmp $ /home/source5/rootdir-gcc402/bin/g++ -Wall -O2 -o swap
swap.cc 
source5@cube tmp $ ./swap
d = 42.42 d2 = 10.3501
conv = 17626847325199074312 back = 577737629976797172


-- 
           Summary: byte swapping unreliable in optimized builds
           Product: gcc
           Version: 4.0.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: cdfrey at netdirect dot ca


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25235


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/25235] byte swapping unreliable in optimized builds
  2005-12-02 23:30 [Bug c++/25235] New: byte swapping unreliable in optimized builds cdfrey at netdirect dot ca
@ 2005-12-02 23:33 ` pinskia at gcc dot gnu dot org
  2005-12-03  0:32 ` cdfrey at netdirect dot ca
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-12-02 23:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2005-12-02 23:32 -------
You are violating C++ aliasing rules:
        uint64_t back = pa_bswap_64(conv);
        double d2 = *((double*)&back);


*** This bug has been marked as a duplicate of 21920 ***


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25235


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/25235] byte swapping unreliable in optimized builds
  2005-12-02 23:30 [Bug c++/25235] New: byte swapping unreliable in optimized builds cdfrey at netdirect dot ca
  2005-12-02 23:33 ` [Bug c++/25235] " pinskia at gcc dot gnu dot org
@ 2005-12-03  0:32 ` cdfrey at netdirect dot ca
  2005-12-03  0:41 ` pinskia at gcc dot gnu dot org
  2005-12-05  4:00 ` cdfrey at netdirect dot ca
  3 siblings, 0 replies; 5+ messages in thread
From: cdfrey at netdirect dot ca @ 2005-12-03  0:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from cdfrey at netdirect dot ca  2005-12-03 00:32 -------
Thanks for the info!

I can understand the examples in the article at
http://mail-index.netbsd.org/tech-kern/2003/08/11/0001.html but with my example
source code in this bug report, I can't seem to follow the optimization path
that results in what I'm seeing.

I understand that the optimizer can ignore duplicated code, or unnecessary
code, but conv and d2 need to be initialized somehow, and if the compiler
doesn't use the code that's there, what does it use?

A warning would be really nice.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25235


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/25235] byte swapping unreliable in optimized builds
  2005-12-02 23:30 [Bug c++/25235] New: byte swapping unreliable in optimized builds cdfrey at netdirect dot ca
  2005-12-02 23:33 ` [Bug c++/25235] " pinskia at gcc dot gnu dot org
  2005-12-03  0:32 ` cdfrey at netdirect dot ca
@ 2005-12-03  0:41 ` pinskia at gcc dot gnu dot org
  2005-12-05  4:00 ` cdfrey at netdirect dot ca
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-12-03  0:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2005-12-03 00:40 -------
(In reply to comment #2)
> A warning would be really nice.

The warning was filed as PR 14024 and fixed for 4.2.0.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25235


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/25235] byte swapping unreliable in optimized builds
  2005-12-02 23:30 [Bug c++/25235] New: byte swapping unreliable in optimized builds cdfrey at netdirect dot ca
                   ` (2 preceding siblings ...)
  2005-12-03  0:41 ` pinskia at gcc dot gnu dot org
@ 2005-12-05  4:00 ` cdfrey at netdirect dot ca
  3 siblings, 0 replies; 5+ messages in thread
From: cdfrey at netdirect dot ca @ 2005-12-05  4:00 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from cdfrey at netdirect dot ca  2005-12-05 04:00 -------
Just adding a link to a comp.lang.c++.moderated discussion on this, for future
reference, when other folks run into this again.

Subject line: alias rules and optimization
http://groups.google.ca/group/comp.lang.c++.moderated/browse_thread/thread/e7bf096832526f8e/5714701b02a2a3cc?hl=en#5714701b02a2a3cc


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25235


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-12-05  4:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-02 23:30 [Bug c++/25235] New: byte swapping unreliable in optimized builds cdfrey at netdirect dot ca
2005-12-02 23:33 ` [Bug c++/25235] " pinskia at gcc dot gnu dot org
2005-12-03  0:32 ` cdfrey at netdirect dot ca
2005-12-03  0:41 ` pinskia at gcc dot gnu dot org
2005-12-05  4:00 ` cdfrey at netdirect dot ca

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).