My version of gcc $ ~/gcc/bin/gcc --version gcc (GCC) 3.5.0 20040517 (experimental) produces code that aborts when it encounters the expression "0 * (-1)" when compiled with -ftrapv. The failing programme is int test (int a) { return a * (a-1); } int main (int argc, char ** argv) { test (0); return 0; } which I compile with $ ~/gcc/bin/gcc -o mul-trapv mul-trapv.c -ftrapv
Can't reproduce on alphaev68-linux 3.5.0 20040513. Probably platform specific.
Confirmed this caused by the following patch: 2003-06-30 Bruno Haible <bruno@clisp.org> PR middle-end/6578 * libgcc2.c (__subvsi3): Remove simplification that would not work when subtracting -0x80000000. (__subvdi3): Remove simplification that would return a wrong result. (__mulvsi3): Fix overflow check. (__absvdi2): Fix simplification that would return a wrong result. (__mulvdi3): Fix overflow check.
This should fix it (but I don't have the proper platform to test it): diff -u -p -r1.170 libgcc2.c --- libgcc2.c 14 Nov 2003 02:23:13 -0000 1.170 +++ libgcc2.c 18 May 2004 22:21:45 -0000 @@ -130,9 +130,7 @@ __mulvsi3 (Wtype a, Wtype b) { const DWtype w = (DWtype) a * (DWtype) b; - if (((a >= 0) == (b >= 0)) - ? (UDWtype) w > (UDWtype) (((DWtype) 1 << (WORD_SIZE - 1)) - 1) - : (UDWtype) w < (UDWtype) ((DWtype) -1 << (WORD_SIZE - 1))) + if ((Wtype) (w >> WORD_SIZE) != (Wtype) w >> (WORD_SIZE - 1)) abort (); return w;
Yes, Falk Hueffner's patch will fix it. Sorry for the bug: during my testing of the patch I tried various arguments but not multiplying with 0 :-(
Subject: Bug 15526 CVSROOT: /cvs/gcc Module name: gcc Changes by: falk@gcc.gnu.org 2004-05-19 23:43:20 Modified files: gcc : ChangeLog libgcc2.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/gcc.dg: ftrapv-1.c Log message: PR other/15526 * libgcc2.c (__mulvsi3): Fix overflow test. * gcc.dg/ftrapv-1.c: New test case. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.3701&r2=2.3702 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/libgcc2.c.diff?cvsroot=gcc&r1=1.170&r2=1.171 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.3763&r2=1.3764 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/ftrapv-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
Fixed.
Reopening because this is not fixed for 3.4.x, only in 4.0.0.
Subject: Bug 15526 CVSROOT: /cvs/gcc Module name: gcc Branch: gcc-3_4-branch Changes by: sayle@gcc.gnu.org 2004-09-26 20:47:23 Modified files: gcc : ChangeLog libgcc2.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/gcc.dg: ftrapv-1.c Log message: PR other/15526 Backport from mainline 2004-05-20 Falk Hueffner <falk@debian.org> * libgcc2.c (__mulvsi3): Fix overflow test. * gcc.dg/ftrapv-1.c: New test case. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=2.2326.2.635&r2=2.2326.2.636 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/libgcc2.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.170.6.1&r2=1.170.6.2 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3389.2.273&r2=1.3389.2.274 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/ftrapv-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.22.1