2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2015 Google Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #ifndef FLATBUFFERS_HASH_H_
19 #define FLATBUFFERS_HASH_H_
24 #include "flatbuffers/flatbuffers.h"
29 template <typename T> struct FnvTraits
31 static const T kFnvPrime;
32 static const T kOffsetBasis;
35 template <> struct FnvTraits<uint32_t>
37 static const uint32_t kFnvPrime = 0x01000193;
38 static const uint32_t kOffsetBasis = 0x811C9DC5;
41 template <> struct FnvTraits<uint64_t>
43 static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
44 static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
47 template <typename T> T HashFnv1(const char *input)
49 T hash = FnvTraits<T>::kOffsetBasis;
50 for (const char *c = input; *c; ++c)
52 hash *= FnvTraits<T>::kFnvPrime;
53 hash ^= static_cast<unsigned char>(*c);
58 template <typename T> T HashFnv1a(const char *input)
60 T hash = FnvTraits<T>::kOffsetBasis;
61 for (const char *c = input; *c; ++c)
63 hash ^= static_cast<unsigned char>(*c);
64 hash *= FnvTraits<T>::kFnvPrime;
69 template <> inline uint16_t HashFnv1<uint16_t>(const char *input)
71 uint32_t hash = HashFnv1<uint32_t>(input);
72 return (hash >> 16) ^ (hash & 0xffff);
75 template <> inline uint16_t HashFnv1a<uint16_t>(const char *input)
77 uint32_t hash = HashFnv1a<uint32_t>(input);
78 return (hash >> 16) ^ (hash & 0xffff);
81 template <typename T> struct NamedHashFunction
85 typedef T (*HashFunction)(const char *);
86 HashFunction function;
89 const NamedHashFunction<uint16_t> kHashFunctions16[] = {
90 {"fnv1_16", HashFnv1<uint16_t>},
91 {"fnv1a_16", HashFnv1a<uint16_t>},
94 const NamedHashFunction<uint32_t> kHashFunctions32[] = {
95 {"fnv1_32", HashFnv1<uint32_t>},
96 {"fnv1a_32", HashFnv1a<uint32_t>},
99 const NamedHashFunction<uint64_t> kHashFunctions64[] = {
100 {"fnv1_64", HashFnv1<uint64_t>},
101 {"fnv1a_64", HashFnv1a<uint64_t>},
104 inline NamedHashFunction<uint16_t>::HashFunction FindHashFunction16(const char *name)
106 std::size_t size = sizeof(kHashFunctions16) / sizeof(kHashFunctions16[0]);
107 for (std::size_t i = 0; i < size; ++i)
109 if (std::strcmp(name, kHashFunctions16[i].name) == 0)
111 return kHashFunctions16[i].function;
117 inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(const char *name)
119 std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);
120 for (std::size_t i = 0; i < size; ++i)
122 if (std::strcmp(name, kHashFunctions32[i].name) == 0)
124 return kHashFunctions32[i].function;
130 inline NamedHashFunction<uint64_t>::HashFunction FindHashFunction64(const char *name)
132 std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]);
133 for (std::size_t i = 0; i < size; ++i)
135 if (std::strcmp(name, kHashFunctions64[i].name) == 0)
137 return kHashFunctions64[i].function;
143 } // namespace flatbuffers
145 #endif // FLATBUFFERS_HASH_H_