This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/80265] __builtin_{memcmp,memchr,strlen} are not usable in constexpr functions


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80265

--- Comment #12 from Pedro Alves <palves at redhat dot com> ---
This seems to work equally well (or better):

// true if the whole string is known at compile time.
static inline constexpr bool constant_string(const char *s)
{
  while (__builtin_constant_p (*s) && *s)
    s++;
  if (!__builtin_constant_p (*s))
    return false;
  return true;
}

constexpr size_t constexpr_strlen(const char* s)
{
  const char *p = s;
  while (*p)
    p++;
  return p - s;
}

and then use it in ce_char_traits like:

  static constexpr std::size_t length(const char_type* s) noexcept
  {
    if (constant_string(s))
      return constexpr_strlen (s);

    return __builtin_strlen (s);
  }

I.e., decouple the "is the whole string constant" from the strlen algorithm.
This should make it easier to reuse the "is compile-time string" in other
compile-time algorithms, though the previous version in comment #11 potentially
optimized the computing the length of the constant prefix part of the string. 
(which is probably not a common use case to aim for anyway.)

Sorry for the constant spam...

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]