Briefly: of all the ldexp variants only straight ldexp consistently get folded. The overloaded c++ version, std::ldexp, is the easiest to derail, as this testcase demonstrates (note: you can't get much more than 1 such call folded); but it has also happened to me with ldexpf, it's just harder to trigger. $ cat pr-ldexp.cc #include <cmath> #define L1(x) ldexp((float)(x | (1<<23)), -23) #define L2(x) std::ldexp((float)(x | (1<<23)), -23) int main(int argc, char *argv[]) { const float tbl[] = { L(0), L(1), L(2), L(3) }; return int(tbl[argc]); } $ /usr/local/gcc-4.3-20080104/bin/g++ -O2 -march=k8 -DL=L1 pr-ldexp.cc -o pr1 $ /usr/local/gcc-4.3-20080104/bin/g++ -O2 -march=k8 -DL=L2 pr-ldexp.cc -o pr2 pr1: 401091: f3 0f 10 04 9d 00 20 movss 0x402000(,%ebx,4),%xmm0 40109e: f3 0f 2c c0 cvttss2si %xmm0,%eax 4010a2: c9 leave pr2: 401090: c7 45 e8 00 00 80 3f movl $0x3f800000,0xffffffe8(%ebp) 401097: c7 45 ec 01 00 80 3f movl $0x3f800001,0xffffffec(%ebp) 40109e: c7 45 f0 02 00 80 3f movl $0x3f800002,0xfffffff0(%ebp) 4010a5: c7 45 f4 03 00 80 3f movl $0x3f800003,0xfffffff4(%ebp) 4010ac: f3 0f 10 44 9d e8 movss 0xffffffe8(%ebp,%ebx,4),%xmm0 4010b2: 8b 5d fc mov 0xfffffffc(%ebp),%ebx 4010b5: f3 0f 2c c0 cvttss2si %xmm0,%eax 4010b9: c9 leave (k8/sse codegen selected for clarity)
It is constant folded but not until after inlining. A simplier example is: static inline int f(int a) { return a+2; } int g(int a) { const int b[] = {f(1), f(2), f(3) }; return b[a]; } int g1(int a) { const int b[] = {(1)+2, (2)+2, (3)+2 }; return b[a]; } ---- CUT ---- Where g and g1 could produce the same code if wanted to.
Gah. Seems that i've managed to hit another issue than what i was after with my simplistic testcase then, because in the original code there's no array anywhere - but static definitions - and i get calls to ldexpf (at runtime init) if use std::ldexp or ldexpf instead of straight ldexp.
It's probably still the same issue - in initializers we only fold builtin functions, where the std:: variants are not recognized as such (they are wrapper functions around the builtin). So this gets folded only after inlining those, but then the initializer is already no longer marked as constant, but initialized at runtime (with constants).
Hmm. If i correctly understand what you're saying, *cough*, ldexp should be immune to this missed-folding-because-a-builtin-isn't-recognized-as-such; but in the original code that lead to PR34774 there's nothing but ldexp, yet *some* of them occur at runtime under some weird conditions. Of course i'd prefer to use a properly typed ldexp better than hope the compiler realize it's about mere floats and not doubles, but that would be the icing on the cake :)
Subject: Re: array constants after inlining and "staticification" On Thu, 14 Feb 2008, tbptbp at gmail dot com wrote: > ------- Comment #4 from tbptbp at gmail dot com 2008-02-14 10:02 ------- > Hmm. If i correctly understand what you're saying, *cough*, ldexp should be > immune to this missed-folding-because-a-builtin-isn't-recognized-as-such; but yes > in the original code that lead to PR34774 there's nothing but ldexp, yet *some* > of them occur at runtime under some weird conditions. I'd like to see a testcase for this as this would be a bug ;) Richard.
Well, this was my best attempt so far at cornering that issue. I'm gonna wave a dead chicken and try another attack vector. Sigh.