From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23751 invoked by alias); 13 Dec 2010 17:11:46 -0000 Received: (qmail 23739 invoked by uid 22791); 13 Dec 2010 17:11:45 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 13 Dec 2010 17:11:40 +0000 From: "jameskuyper at verizon dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/46926] New: Paired sin() cos() calls optimized to sincos() call. X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jameskuyper at verizon dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Date: Mon, 13 Dec 2010 17:11:00 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2010-12/txt/msg01437.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46926 Summary: Paired sin() cos() calls optimized to sincos() call. Product: gcc Version: 4.4.4 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned@gcc.gnu.org ReportedBy: jameskuyper@verizon.net I discovered this problem when porting from a mandriva Linux system using gcc 4.2.2 to a centos linux system using gcc 4.4.4. After much simplification, I can now demonstrate the problem with an extremely short example program: #include #include void sincos(double val, double *sin_val, double *cos_val) { *sin_val = sin(val); *cos_val = cos(val); return; } int func( double * pd ){ double cosine, sine; /* must use value accessed through pointer; bug disappears * without the pointer access. */ cosine = cos(*pd); sine = sin(*pd); /* Need to use cosine[] and sine[], or optimizer drops them. */ return cosine < 0.0 && sine > 0.0 ? EXIT_FAILURE : EXIT_SUCCESS; } int main (void) { double d=0.0; return func(&d) ; } In the original program, the sincos() function defined above occurred in a third-party library designed for use with a fully conforming implementation of C90. Such implementations cannot provide a sincos() function in their standard C library in any way that would conflict with a user-defined function of the same name. I compiled and linked this program using the following command: gcc -ansi -pedantic -O1 -g sincos_prob.c -lm -o sincos_prob When I execute the program, it produces a bus error. gdb indicates that the bus error occurs inside a recursive (!?) call to sincos(). The problem does not come up if I lower the optimization level to -O0. The reason appears to be that, at optimization levels of 1 or higher, paired calls to sin() and cos(), like those that occur in two seperate locations above, are replaced with a single call to sincos() - in itself, a seemingly reasonable thing to do. The sincos() definition provided by the third party library is used in place of the one provided as an extension in the C standard library, which also seems reasonable. Where this all goes wrong is inside the body of the third-party library's definition of sincos(). The sin()/cos() optimization turns that function into an infinitely recursive call to itself. Because sincos() is not a C standard library routine, the code given above does not violate any constraint, and has well defined behavior - behavior which does not include generating a bus error. When invoked in a mode that's supposed to be fully conforming to either C90 or C99, the compiler should not generate spurious calls to functions whose names are not reserved to the implementation. Use of a reserved name, such as __sincos(), seems to me like it would be the simplest fix for this problem.