From ec74f58b9446cfbcc1634e76d1ff53e5eece202d Mon Sep 17 00:00:00 2001 From: Christian Helmich Date: Sat, 3 Mar 2018 02:13:55 +0900 Subject: [PATCH] made HashFnv functions constexpr (#4640) * added FLATBUFFERS_CONSTEXPR_CPP14 define for C++14 and above constexpr * made HashFnv functions constexpr (depending on FLATBUFFERS_CONSTEXPR) --- include/flatbuffers/base.h | 7 +++++++ include/flatbuffers/hash.h | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/flatbuffers/base.h b/include/flatbuffers/base.h index 784a7d078..6806739bf 100644 --- a/include/flatbuffers/base.h +++ b/include/flatbuffers/base.h @@ -121,6 +121,13 @@ #define FLATBUFFERS_CONSTEXPR #endif +#if (defined(__cplusplus) && __cplusplus >= 201402L) || \ + (defined(__cpp_constexpr) && __cpp_constexpr >= 201304) + #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR +#else + #define FLATBUFFERS_CONSTEXPR_CPP14 +#endif + #if defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC__ * 10 + __GNUC_MINOR__ >= 46 || \ defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023026 #define FLATBUFFERS_NOEXCEPT noexcept diff --git a/include/flatbuffers/hash.h b/include/flatbuffers/hash.h index 16536cb49..a97b23152 100644 --- a/include/flatbuffers/hash.h +++ b/include/flatbuffers/hash.h @@ -39,7 +39,7 @@ template<> struct FnvTraits { static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL; }; -template T HashFnv1(const char *input) { +template FLATBUFFERS_CONSTEXPR_CPP14 T HashFnv1(const char *input) { T hash = FnvTraits::kOffsetBasis; for (const char *c = input; *c; ++c) { hash *= FnvTraits::kFnvPrime; @@ -48,7 +48,7 @@ template T HashFnv1(const char *input) { return hash; } -template T HashFnv1a(const char *input) { +template FLATBUFFERS_CONSTEXPR_CPP14 T HashFnv1a(const char *input) { T hash = FnvTraits::kOffsetBasis; for (const char *c = input; *c; ++c) { hash ^= static_cast(*c); @@ -57,12 +57,12 @@ template T HashFnv1a(const char *input) { return hash; } -template <> inline uint16_t HashFnv1(const char *input) { +template <> FLATBUFFERS_CONSTEXPR_CPP14 inline uint16_t HashFnv1(const char *input) { uint32_t hash = HashFnv1(input); return (hash >> 16) ^ (hash & 0xffff); } -template <> inline uint16_t HashFnv1a(const char *input) { +template <> FLATBUFFERS_CONSTEXPR_CPP14 inline uint16_t HashFnv1a(const char *input) { uint32_t hash = HashFnv1a(input); return (hash >> 16) ^ (hash & 0xffff); }