Bug 32099 - not constant folding sin
Summary: not constant folding sin
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2007-05-26 18:15 UTC by Andrei
Modified: 2007-06-10 03:23 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrei 2007-05-26 18:15:23 UTC
when I'm trying to compile the program
============================
#include <math.h>

static double s = sin(0.0);

int main(){
        return 0;
}
============================
with gcc t.c, the compiler produces the warnings

t.c:3: warning: initializer element is not constant
t.c:3: warning: (near initialization for ‘s’)
t.c:3: warning: ‘s’ defined but not used

I'm worried about the first warning, since sin(0) is constant

gcc -v generates:
=======================================
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
Comment 1 Andrew Pinski 2007-05-26 18:26:59 UTC
No, sin(0) is not constant in C.  C has specific rules about constant expression.  You can use __builtin_sin(0) to always get the constant folding.
Comment 2 Andrei 2007-05-26 18:47:13 UTC
Ok, but __builtin_sin(M_PI) is not constant again
Comment 3 Manuel López-Ibáñez 2007-05-28 12:54:29 UTC
Worse, with __builtin_sin(M_PI), I get an error:

test.c:2: error: initializer element is not constant

Maybe this is fixed now in GCC 4.3 by using MPFR.
Comment 4 Andrew Pinski 2007-06-10 03:23:25 UTC
This is an extension that GCC allows some constant folding with some functions.

So this is just a missed optimization of not constant folding sin.

And yes this is fixed in 4.3.0 by the use of MPFR.