Compiler bug in pgcpp


I found a compiler bug in pgcpp 13.2-0 64-bit target on x86-64 Windows. The bug is caused due to erroneous optimization. Here is a test program that reproduces the bug:

/// Usage:
/// $ pgcpp bug.cpp -o bug
/// $ ./bug 10
/// $ pgcpp -fast bug.cpp -o bug
/// $ ./bug 10

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

int main(int, char* argv[])
  if (argv[1]) {
    uint64_t MAX_UINT32 = std::numeric_limits<uint32_t>::max();
    uint64_t MAX_UINT64 = std::numeric_limits<uint64_t>::max();

    int factor = atoi(argv[1]);
    uint64_t result = MAX_UINT64 - MAX_UINT32 * factor;

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

The non optimized version prints the correct result ‘18446744030759878665’ while the optimized version (-fast or -O2) prints ‘9’ which is an error.

Thanks Delph, I have sent a report to our engineers (TPR#19133) who will investigate this further.

The two quick work arounds that I see are to add the “volatile” attribute to MAX_UINT32 or disable auto-inlining (-Mnoautoinline).

  • Mat

TPR 19133 - Wrong answers when using optimization and integer limits.

Fixed in 13.6. Available now.