public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/53792] New: [C++11][constexpr] improving compiler-time constexpr evaluation
@ 2012-06-28  6:59 vincenzo.innocente at cern dot ch
  2012-06-28  9:34 ` [Bug c++/53792] " rguenth at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: vincenzo.innocente at cern dot ch @ 2012-06-28  6:59 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53792

             Bug #: 53792
           Summary: [C++11][constexpr] improving compiler-time constexpr
                    evaluation
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: vincenzo.innocente@cern.ch


Filed under c++ even if it is most probably an optimization issue.

At the moment it looks like that constexpr are evaluated at compile time only
if explicitly assigned to a constexpr constant. There are cases though where
the compiler can infer that the expression can still be evaluated at compile
time even if used in a run-time context.

Take the following quite realistic example of a consexpr "indexing table" used
to access a non-const array using string literals though an inline function.
In principle foo and bar are equivalent.
At the moment gcc evaluates "getIndex" at compile time for bar (where the marco
expansion explicitly instantiates a constexpr int, while it generates runtime
code for foo that uses the inlined function getV.

Would the compiler be able to transform getV in something like the code in the
macro?



constexpr entry theMap[] = {
 {"a", 0},
 {"b", 1},
 {nullptr,2}
};

// filled at run time 
double v[3];


constexpr bool  same(char const *x, char const *y)   {
 return !*x && !*y ? true : (*x == *y && same(x+1, y+1));
}

constexpr int getIndex(char const *label, entry const *entries)   {
 return !entries->label ? entries->index  : same(entries->label, label) ?
entries->index : getIndex(label, entries+1);
}


inline  double __attribute__((always_inline)) getV(const char * name )  {
 return  v[getIndex(name,theMap)];
}



#define SetV(X,NAME) \
constexpr int i_##X = getIndex(NAME, theMap);\
const double X = v[i_##X]


int foo() {
 const double a = getV("a");
 const double b = getV("b");

 if (a==b) return 1;
 return 0;

}

int bar() {
 SetV(a,"a");
 SetV(b,"b");

 if (a==b) return 1;
 return 0;

}


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-05-06 15:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-28  6:59 [Bug c++/53792] New: [C++11][constexpr] improving compiler-time constexpr evaluation vincenzo.innocente at cern dot ch
2012-06-28  9:34 ` [Bug c++/53792] " rguenth at gcc dot gnu.org
2012-06-28 11:08 ` vincenzo.innocente at cern dot ch
2012-07-19 14:28 ` jason at gcc dot gnu.org
2012-07-27 10:48 ` vincenzo.innocente at cern dot ch
2012-07-27 15:06 ` jason at redhat dot com
2012-08-08 11:18 ` giulio.eulisse at cern dot ch
2012-08-08 18:19 ` paolo.carlini at oracle dot com
2015-05-06 15:03 ` [Bug c++/53792] [C++11] " balakrishnan.erode at gmail dot com

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).