https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87646

            Bug ID: 87646
           Summary: Wrong code at -O3 and above (but not at -O2 and below)
           Product: gcc
           Version: 4.8.5
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sukhovvl at gmail dot com
  Target Milestone: ---

gcc 4.8.5 20150623 miscompiles the following code with -O3 optimization flag on
x86-64
gcc 4.9 and above doesn't seem to have this bug

Correct output with -O2 flag:
[root@localhost home]# g++ -std=c++11 -O2 test.cpp -otest && ./test
 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff 0

Incorrect output with -O3 flag:
[root@localhost home]# g++ -std=c++11 -O3 test.cpp -otest && ./test
 0 0 0 0 0 0 0 0

[root@localhost home]# hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: <blanked out>
           Boot ID: <blanked out>
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.9.1.el7.x86_64
      Architecture: x86-64

------------------------

#include <iostream>
#include <cstdint>
#include <cstring>

void f(std::size_t N, std::int32_t* dst)
{
    if (N % 8)
    {
        std::int32_t l[8] = {0, 0, 0, 0, 0, 0, 0, 0};
        for (unsigned i = 0; i < N % 8; ++i)
            l[i] = -1;

        std::memcpy((void*)dst, (const void*)l, sizeof(int32_t)*8);
    }
}

int main()
{
    std::int32_t values[8];
    std::size_t N = 7;

    f(N, values);

    for (int i = 0; i < 8; ++i)
        std::cout << std::hex << " " << values[i];
    std::cout << std::endl;

    return 0;
}

Reply via email to