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
Confirmed. int foo(int i) { return -2*__builtin_abs(i-2); } get's folded to return ABS_EXPR <(2 - i) * 2>; doh.
Mine.
Subject: Bug 34130 Author: rguenth Date: Sat Nov 17 14:22:42 2007 New Revision: 130258 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130258 Log: 2007-11-17 Richard Guenther <rguenther@suse.de> PR middle-end/34130 * fold-const.c (extract_muldiv_1): Do not move negative constants inside ABS_EXPR. * gcc.c-torture/execute/pr34130.c: New testcase. Added: trunk/gcc/testsuite/gcc.c-torture/execute/pr34130.c Modified: trunk/gcc/ChangeLog trunk/gcc/fold-const.c trunk/gcc/testsuite/ChangeLog
Fixed on the trunk.
Subject: Bug 34130 Author: doko Date: Thu Nov 22 15:34:03 2007 New Revision: 130352 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130352 Log: 2007-11-22 Matthias Klose <doko@ubuntu.com> Backport from mainline: 2007-11-17 Richard Guenther <rguenther@suse.de> PR middle-end/34130 * fold-const.c (extract_muldiv_1): Do not move negative constants inside ABS_EXPR. PR middle-end/34130 * gcc.c-torture/execute/pr34130.c: New testcase. Added: branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34130.c - copied unchanged from r130258, trunk/gcc/testsuite/gcc.c-torture/execute/pr34130.c Modified: branches/gcc-4_2-branch/gcc/ChangeLog branches/gcc-4_2-branch/gcc/fold-const.c branches/gcc-4_2-branch/gcc/testsuite/ChangeLog
Subject: Bug 34130 Author: doko Date: Thu Nov 29 00:32:58 2007 New Revision: 130507 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130507 Log: gcc/ 2007-11-29 Matthias Klose <doko@ubuntu.com> Backport from mainline: 2007-11-17 Richard Guenther <rguenther@suse.de> PR middle-end/34130 * fold-const.c (extract_muldiv_1): Do not move negative constants inside ABS_EXPR. gcc/testsuite/ 2007-11-29 Matthias Klose <doko@ubuntu.com> Backport from mainline: 2007-11-17 Richard Guenther <rguenther@suse.de> PR middle-end/34130 * gcc.c-torture/execute/pr34130.c: New testcase. Added: branches/gcc-4_1-branch/gcc/testsuite/gcc.c-torture/execute/pr34130.c - copied unchanged from r130352, branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34130.c Modified: branches/gcc-4_1-branch/gcc/ChangeLog branches/gcc-4_1-branch/gcc/fold-const.c branches/gcc-4_1-branch/gcc/testsuite/ChangeLog
Fixed on active branches.