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

            Bug ID: 87458
           Summary: fma and -O3 wrong result
           Product: gcc
           Version: 7.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mattwi at fysik dot dtu.dk
  Target Milestone: ---

Created attachment 44760
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44760&action=edit
preprocessed source

Dear developers, 

I have an issue when I combine std::fma with -O3. It leads to wrong output.
I apologize if this has already been addressed somewhere. 

Best regards
Matthias

COMMAND LIND
g++-7 -O3 -Wall -Wextra -mfma  test.cpp -o test

OUTPUT
Need to output x to reproduce error 100
In dot u is 200 (Correct value is 93)
80000 (Correct value is 17298)

OUTPUT COMPILED WITH -O2 INSTEAD OF -O3
Need to output x to reproduce error 100
In dot u is 93 (Correct value is 93)
17298 (Correct value is 17298)

GCC INFO:

Using built-in specs.
COLLECT_GCC=g++-7
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
7.3.0-21ubuntu1~16.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-7
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib
--with-target-system-zlib --enable-objc-gc=auto --enable-multiarch
--disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 7.3.0 (Ubuntu 7.3.0-21ubuntu1~16.04) 

PROGRAM: test.cpp

#include <iostream>
#include <array>
#include <cmath>


std::array<double, 2> create(double x)
{
    std::cout << "Need to output x to reproduce error ";
    std::cout << x << std::endl;
    return {x,x};
}

void axpby( double alpha, const std::array<double,2>& x, double beta,
std::array<double,2>& y)
{
    double tmp = beta*y[0];
    y[0] = std::fma( alpha,x[0],tmp);
    tmp = beta*y[1];
    y[1] = std::fma( alpha,x[1],tmp);
}
double dot( const std::array<double,2>& u)
{
    //we also need this output to reproduce error
    std::cout << "In dot u is "<<u[0] << " (Correct value is 93)\n";
    return u[0]*u[0]+u[1]*u[1];
}   

int main()
{
    std::array<double,2> u = {{7,7}};
    axpby( 1., create(100), -1., u); //100 - 7 , 100 - 7
    std::cout << dot( u)<<" (Correct value is 17298)" << std::endl;

    return 0;
}

Reply via email to