tried the following code on both gcc 4.1 and gcc  3.2,:
----------------------------------------------------
        int result, i;

        i = 0;
        result = -2*abs(i-2);
        printf("result = %d\n", result);
----------------------------------------------------

the result is 4, while the correct result should be '-4;'. 

if use '-fno-builtin' flag to use the glibc's abs() implementation, then the
result is correct (-4). also, if the expression is changed something like
'0-2*abs(i-2)', then the result is also correct.

so, it seems there is some problem with the builtin abs() function. simply look
at the assembly result, it seems gcc thinks A*abs(B) is the same as abs(A*B).

'gcc -v' result

Using built-in specs.
Target: i686-pc-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-libgcj-multifile --disable-libmudflap
--enable-languages=c,c++,java,fortran --disable-libgcj --with-cpu=generic
--host=i686-pc-linux
Thread model: posix
gcc version 4.2.1


-- 
           Summary: the builtin abs() gives wrong result when used in some
                    expression
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: pengchengzou at gmail dot com
  GCC host triplet: i686-pc-linux


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

Reply via email to