seen at least on x86_64. bug submitter writes:

g++ appears to be (incorrectly) using a signed int comparison when -O3
optimization is enabled. Compiling with -O2 or lower produces the correct
output.

#include <numeric>
#include <iostream>

template <typename T>
struct maximum
{
    T operator()(T& a, T& b)
    { 
        return (a < b) ? b : a;
    }
};

int main(void)
{
    typedef unsigned int T;

    T data[13] = {2411691434,  187758716, 2874577865, 1532192406,
                   850395381, 3670100461, 1052104929,  352534891,
                  1000719294, 2219234747, 4264598888, 4166615811,
                  1898580612};

    T init = 0;

    T result = std::accumulate(data, data + 13, init, maximum<T>());

    std::cout << result << std::endl;

    return 0;
}

$ g++ -O2 -Wall bug.cpp && ./a.out 
4264598888
g++ -O3 -Wall bug.cpp && ./a.out 
1898580612


-- 
           Summary:  g++ -O3 produces incorrect results for less-than
                    operator
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: debian-gcc at lists dot debian dot org


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

Reply via email to