Hello,

I am working on a larger project processing audio signals in double format. As
I needed to estimate the max. Amplitude of a buffer, it returned a faulty
value.

I debugged the assembler code and realized, that the optimizer created a faulty
computation of the maximum. By only changing one instruction, the computation
is correct.

I've reduced the code to a minimum of code supplementing function calls that
fill the buffer with a simple memcpy function.

The code is standalone. I'll include the factor.ii which is also a command line
program demonstrating the error. I've compiled it with the command line found
below. I also tried out leaving the compiler flag -march=i686 away. It just
changed my assembler code but didn't get rid of the error. Further down I'll
include my analysis of the assembler code.


g++ -o factor -Wall -v -O2 -march=i686 factor.cpp:
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2
--program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug
--enable-objc-gc --enable-mpfr --disable-libmudflap --enable-targets=all
--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu
--target=i486-linux-gnu
Thread model: posix
gcc version 4.2.3 (Debian 4.2.3-3)
 /usr/lib/gcc/i486-linux-gnu/4.2.3/cc1plus -E -quiet -v -D_GNU_SOURCE
factor.cpp -march=i686 -Wall -O2 -fpch-preprocess -o factor.ii
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2
 /usr/include/c++/4.2/i486-linux-gnu
 /usr/include/c++/4.2/backward
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.2.3/include
 /usr/include
End of search list.
 /usr/lib/gcc/i486-linux-gnu/4.2.3/cc1plus -fpreprocessed factor.ii -quiet
-dumpbase factor.cpp -march=i686 -auxbase factor -O2 -Wall -version -o factor.s
GNU C++ version 4.2.3 (Debian 4.2.3-3) (i486-linux-gnu)
        compiled by GNU C version 4.2.3 (Debian 4.2.3-3).
GGC heuristics: --param ggc-min-expand=45 --param ggc-min-heapsize=29241
Compiler executable checksum: f63294e1c8ecc1bf2473a5bae1642fbe
 as -V -Qy -o factor.o factor.s
GNU assembler version 2.18.0 (i486-linux-gnu) using BFD version (GNU Binutils
for Debian) 2.18.0.20080103
 /usr/lib/gcc/i486-linux-gnu/4.2.3/collect2 --eh-frame-hdr -m elf_i386
--hash-style=both -dynamic-linker /lib/ld-linux.so.2 -o factor
/usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../lib/crt1.o
/usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../lib/crti.o
/usr/lib/gcc/i486-linux-gnu/4.2.3/crtbegin.o
-L/usr/lib/gcc/i486-linux-gnu/4.2.3 -L/usr/lib/gcc/i486-linux-gnu/4.2.3
-L/usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../lib -L/lib/../lib
-L/usr/lib/../lib -L/usr/lib/gcc/i486-linux-gnu/4.2.3/../../.. factor.o
-lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/i486-linux-gnu/4.2.3/crtend.o
/usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../lib/crtn.o


The optimizer generates the error with the following line of code:

for (unsigned long i = 0; i < insize; ++i) _factor = std::max(_factor,
std::max(-data[i], data[i]));

with
double * data; (Audio Data)
unsigned long insize; (number of doubles in the buffer)
double _factor; (currently estimated, global factor)


Generated Assembler code from factor.s:

// Load data Buffer pointer to eax
        movl    -36(%ebp), %eax
// Clear edx (that's our i)
        xorl    %edx, %edx
// Load _factor on FP-Stack
        fldl    (%esi)
// Have _factor twice on the FP-Stack (Stack: (original)_factor,
(current)_factor)
        fld     %st(0)
// Start the for-loop
        jmp     .L45
        .p2align 4,,7


.L64:
// This at the beginning of every iteration i >= 1
// This is the actual consistency error
// At this pointer we have the following on the FP-Stack: st(0):
_factor(current), st(1): _factor(original)
// After executing this, we'll have st(0): _factor(original), st(1):
_factor(current), which creates a problem further down
// To show what is actually going wrong, I'll label _factor

        fxch    %st(1)
.L45:
        fldl    (%eax)
        fchs
        fstl    -16(%ebp)
        fldl    (%eax)
        fucomi  %st(1), %st
        fcmovbe %st(1), %st
        fstp    %st(1)
        fucomi  %st(2), %st
        fstp    %st(2)
        fxch    %st(1)
        fcmovbe %st(1), %st
        addl    $1, %edx
        addl    $8, %eax
        cmpl    %ebx, %edx
        jne     .L64
        fstp    %st(1)
        fstpl   (%esi)
.L43:
        cmpb    $0, 8(%esi)
        je      .L53


-- 
           Summary: Optimizer generates faulty assembler code when
                    estimating max. floating point value in a for loop
           Product: gcc
           Version: 4.2.3
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tcm dot home at gmx dot de


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

Reply via email to