public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "james dot me at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c/29705] New: gcc optimizes necessary checks for isnan out of existence Date: Fri, 03 Nov 2006 22:27:00 -0000 [thread overview] Message-ID: <bug-29705-13513@http.gcc.gnu.org/bugzilla/> (raw) This bug applies at least to gcc version 4.0.3 and the current CVS 4.2.0 20061103. The assembly extracts included below were produced by gcc 4.2.0; the gcc 4.0.3 extracts are analogous with respect to this problem but are different in some respects. My particular host is an intel pentium4 xenon. With certain math optimizations enabled, gcc assumes that a test for isnan will always fail. This assumption is not correct, and results in the undesired propagation of NANs into other computations and altered control flow. This bug is exemplified by the following code which can be compiled without including any header files. For brevity in the test case, instead of include files I have used only the prototypes needed to silence gcc's warnings. The code compiles the same, when <math.h> and <stdio.h> are included instead of the prototypes, or when the prototypes are simply left out all together. float logf(float); int printf(const char *fmt, ...); int isnan(); double test(int i0, int n, double a) { double sum = 0.0; int i; for(i=i0; i<n; ++i) { float x = logf((float)i); sum += isnan(x) ? 0 : x; } return sum; } int main(void) { printf("test(4, 6, 0) = %f\n", test(4,6,0)); printf("test(0, 2, 0) = %f\n", test(0,2,0)); printf("test(-2, 3, 0) = %f\n", test(-2,3,0)); return 0; } james@recoil:~/project/cf/util$ /home/james/local/gcc/bin/gcc -O2 -march=i686 -funsafe-math-optimizations -fno-math-errno test.c -o test james@recoil:~/project/cf/util$ ./test test(4, 6, 0) = 2.995732 test(0, 2, 0) = -inf test(-2, 3, 0) = nan <- incorrect, as written, test should preclude nan as a result james@recoil:~/project/cf/util$ /home/james/local/gcc/bin/gcc -O2 -march=i686 test.c -o testb -lm james@recoil:~/project/cf/util$ ./testb test(4, 6, 0) = 2.995732 test(0, 2, 0) = -inf test(-2, 3, 0) = -inf <- correct james@recoil:~/project/cf/util$ /home/james/local/gcc/bin/gcc -v Using built-in specs. Target: i686-pc-linux-gnu Configured with: ../gcc-4-2/configure --prefix=/home/james/local/gcc Thread model: posix gcc version 4.2.0 20061103 (prerelease) The assembly produced for the test case that runs correctly has this as the main loop: .L5: pushl %ebx fildl (%esp) addl $4, %esp fstps (%esp) fstpl -24(%ebp) call logf fucomi %st(0), %st ;; check for NAN fldz fcmovnu %st(1), %st ;; conditional based on check fstp %st(1) addl $1, %ebx cmpl %esi, %ebx fldl -24(%ebp) faddp %st, %st(1) jne .L5 The (over) optimized assembly has this for the loop: .L5: pushl %eax addl $1, %eax fildl (%esp) addl $4, %esp cmpl %edx, %eax fldln2 fxch %st(1) fyl2x ;; log is computed inline due to -funsafe-math-optimizations faddp %st, %st(1) ;; result is added unconditionally this does ;; not belong in -funsafe-math-optimizations ;; (perhaps -funusable-math-optimizations) jne .L5 Uros Bizjak reported (on the gcc mailing list) that (1) gcc 4.3 does not exhibit this incorrect behavior (2) this is a bug. -- Summary: gcc optimizes necessary checks for isnan out of existence Product: gcc Version: 4.2.0 Status: UNCONFIRMED Severity: major Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: james dot me at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29705
next reply other threads:[~2006-11-03 22:27 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2006-11-03 22:27 james dot me at gmail dot com [this message] 2006-11-03 22:35 ` [Bug target/29705] " pinskia at gcc dot gnu dot org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-29705-13513@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).