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:

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


When compiled with optimization, I get:

[EMAIL PROTECTED] tmp $ /home/source5/rootdir-gcc402/bin/g++ -Wall -O2 -o swap
swap.cc 
[EMAIL PROTECTED] 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

Reply via email to