Bug 47473 - [4.5 Regression] Incorrect computation with complex numbers when using -std=c99
[4.5 Regression] Incorrect computation with complex numbers when using -std=c99
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: c
4.5.0
: P2 normal
: 4.5.3
Assigned To: Jakub Jelinek
: wrong-code
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-01-26 11:10 UTC by Abel K
Modified: 2011-04-07 18:40 UTC (History)
1 user (show)

See Also:
Host:
Target: i?86-*-*
Build:
Known to work: 4.4.4, 4.6.0
Known to fail: 4.5.0, 4.5.2
Last reconfirmed: 2011-01-26 11:26:29


Attachments
complexbug3.i (20.63 KB, text/plain)
2011-01-26 11:10 UTC, Abel K
Details
gcc46-pr47473.patch (1.43 KB, patch)
2011-01-26 15:19 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Abel K 2011-01-26 11:10:54 UTC
Created attachment 23131 [details]
complexbug3.i

This is a report about an issue encountered in MinGW GCC. The maintainers of the MinGW project suggested to report it here.

The command line that triggers the bug is:

gcc -v -save-temps -Wall -std=c99 complexbug3.c -o complexbug3.exe

The option -std=c99 triggers the bug.

The resultant output is:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/program files/mingw/bin/../libexec/gcc/mingw32/4.5.0/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.5.0/configure --enable-languages=c,c++,ada,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --disable-werror --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.5.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c99' '-o' 'complexbug3.exe' '-mtune=i386' '-march=i386'
 c:/program files/mingw/bin/../libexec/gcc/mingw32/4.5.0/cc1.exe -E -quiet -v -iprefix c:\program files\mingw\bin\../lib/gcc/mingw32/4.5.0/ complexbug3.c -mtune=i386 -march=i386 -std=c99 -Wall -fpch-preprocess -o complexbug3.i
ignoring nonexistent directory "c:\program files\mingw\bin\../lib/gcc/mingw32/4.5.0/../../../../mingw32/include"
ignoring nonexistent directory "c:/mingw/include"
ignoring nonexistent directory "/mingw/include"
ignoring duplicate directory "c:/program files/mingw/lib/gcc/../../lib/gcc/mingw32/4.5.0/include"
ignoring duplicate directory "c:/program files/mingw/lib/gcc/../../lib/gcc/mingw32/4.5.0/include-fixed"
ignoring nonexistent directory "c:/program files/mingw/lib/gcc/../../lib/gcc/mingw32/4.5.0/../../../../mingw32/include"
ignoring nonexistent directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
 c:\program files\mingw\bin\../lib/gcc/mingw32/4.5.0/../../../../include
 c:\program files\mingw\bin\../lib/gcc/mingw32/4.5.0/include
 c:\program files\mingw\bin\../lib/gcc/mingw32/4.5.0/include-fixed
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c99' '-o' 'complexbug3.exe' '-mtune=i386' '-march=i386'
 c:/program files/mingw/bin/../libexec/gcc/mingw32/4.5.0/cc1.exe -fpreprocessed complexbug3.i -quiet -dumpbase complexbug3.c -mtune=i386 -march=i386 -auxbase complexbug3 -Wall -std=c99 -version -o complexbug3.s
GNU C (GCC) version 4.5.0 (mingw32)
	compiled by GNU C version 4.5.0, GMP version 5.0.1, MPFR version 2.4.1, MPC version 0.8.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C (GCC) version 4.5.0 (mingw32)
	compiled by GNU C version 4.5.0, GMP version 5.0.1, MPFR version 2.4.1, MPC version 0.8.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 6773400e8bc9df4b012089071ac4c86b
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c99' '-o' 'complexbug3.exe' '-mtune=i386' '-march=i386'
 c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/as.exe -o complexbug3.o complexbug3.s
COMPILER_PATH=c:/program files/mingw/bin/../libexec/gcc/mingw32/4.5.0/;c:/program files/mingw/bin/../libexec/gcc/;c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/
LIBRARY_PATH=c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/;c:/program files/mingw/bin/../lib/gcc/;c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/lib/;c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wall' '-std=c99' '-o' 'complexbug3.exe' '-mtune=i386' '-march=i386'
 c:/program files/mingw/bin/../libexec/gcc/mingw32/4.5.0/collect2.exe -Bdynamic -o complexbug3.exe c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../crt2.o c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/crtbegin.o -Lc:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0 -Lc:/program files/mingw/bin/../lib/gcc -Lc:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/lib -Lc:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/../../.. complexbug3.o -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.0/crtend.o


The result of executing complexbug3.exe is mathematically incorrect:
(0.2 - I*0.3)*(0.3 - I*0.9) = (0.06, -0.09)
Comment 1 Richard Biener 2011-01-26 11:26:29 UTC
Confirmed on i?86-linux.  Seems to be a problem with libgcc __mulxc3, linking
with the libgcc from 4.4 makes it work for the failing releases.
Comment 2 Jakub Jelinek 2011-01-26 13:42:50 UTC
Unincluded testcase:
int
main (void)
{
 long double _Complex w = 0.2L - 0.3iL;
  w = w * (0.3L - (0.0F + 1.0iF) * 0.9L);
  __builtin_printf("(0.2 - I*0.3)*(0.3 - I*0.9) = (%g, %g)\n", (double) __real__ w, (double) __imag__ w);
  return 0;
}

On x86_64-linux the results look correct.
Using
w = w * (0.3L - 0.9iL);
instead works too.  And the results I get don't depend on libgcc version used (4.4 behaves like 4.6).
Comment 3 Jakub Jelinek 2011-01-26 15:10:07 UTC
Mine.
Comment 4 H.J. Lu 2011-01-26 15:14:10 UTC
It is caused by revision 147281:

http://gcc.gnu.org/ml/gcc-cvs/2009-05/msg00255.html
Comment 5 Jakub Jelinek 2011-01-26 15:19:56 UTC
Created attachment 23132 [details]
gcc46-pr47473.patch

Untested fix.
Comment 6 Jakub Jelinek 2011-01-26 20:07:02 UTC
Author: jakub
Date: Wed Jan 26 20:06:57 2011
New Revision: 169299

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=169299
Log:
	PR c/47473
	* c-lex.c (interpret_float): If CPP_N_IMAGINARY, ensure
	EXCESS_PRECISION_EXPR is created with COMPLEX_TYPE instead of
	REAL_TYPE.

	* gcc.dg/torture/pr47473.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/pr47473.c
Modified:
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-lex.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Jakub Jelinek 2011-01-26 20:08:40 UTC
Fixed on the trunk so far.
Comment 8 Diego Novillo 2011-02-02 17:53:56 UTC
Author: dnovillo
Date: Wed Feb  2 17:53:51 2011
New Revision: 169624

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=169624
Log:
	PR c/47473
	* c-lex.c (interpret_float): If CPP_N_IMAGINARY, ensure
	EXCESS_PRECISION_EXPR is created with COMPLEX_TYPE instead of
	REAL_TYPE.

	* gcc.dg/torture/pr47473.c: New test.

Added:
    branches/google/integration/gcc/testsuite/gcc.dg/torture/pr47473.c
Modified:
    branches/google/integration/gcc/c-family/ChangeLog
    branches/google/integration/gcc/c-family/c-lex.c
    branches/google/integration/gcc/testsuite/ChangeLog
Comment 9 Jakub Jelinek 2011-04-07 18:24:45 UTC
Author: jakub
Date: Thu Apr  7 18:24:43 2011
New Revision: 172113

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172113
Log:
	Backported from mainline
	2011-01-26  Jakub Jelinek  <jakub@redhat.com>

	PR c/47473
	* c-lex.c (interpret_float): If CPP_N_IMAGINARY, ensure
	EXCESS_PRECISION_EXPR is created with COMPLEX_TYPE instead of
	REAL_TYPE.

	* gcc.dg/torture/pr47473.c: New test.

Added:
    branches/gcc-4_5-branch/gcc/testsuite/gcc.dg/torture/pr47473.c
Modified:
    branches/gcc-4_5-branch/gcc/ChangeLog
    branches/gcc-4_5-branch/gcc/c-lex.c
    branches/gcc-4_5-branch/gcc/testsuite/ChangeLog
Comment 10 Jakub Jelinek 2011-04-07 18:40:04 UTC
Fixed.