[cfe-dev] C++11: new builtin to allow constexpr to be applied to performance-critical functions

Richard Smith richard at metafoo.co.uk
Fri Oct 19 22:04:04 PDT 2012


[Crossposted to both GCC and Clang dev lists]

Hi,

One issue facing library authors wanting to use C++11's constexpr feature
is that the same implementation must be provided for both the case of
function invocation substitution and for execution at runtime. Due to the
constraints on constexpr function definitions, this can force an
implementation of a library function to be inefficient. To counteract this,
I'd like to propose the addition of a builtin:

  bool __builtin_constexpr_p()

This builtin would only be supported within constexpr function definitions.
If the containing function is undergoing function invocation substitution,
it returns true. Otherwise, it returns false. Hence we can implement
library functions with a pattern like:

  constexpr int constexpr_strncmp(const char *p, const char *q, size_t n) {
    return !n ? 0 : *p != *q ? *p - *q : !*p ? 0 : constexpr_strncmp(p+1,
q+1, n-1);
  }
  __attribute__((always_inline)) constexpr int my_strncmp(const char *p,
const char *q, size_t n) {
    return __builtin_constexpr_p() ? constexpr_strncmp(p, q, n) :
strncmp(p, q, n);
  }

Does this seem reasonable?

Thanks!
Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121019/4335f266/attachment.html>


More information about the cfe-dev mailing list