Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / externals / flatbuffers / hash.h
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2015 Google Inc. All rights reserved.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #ifndef FLATBUFFERS_HASH_H_
19 #define FLATBUFFERS_HASH_H_
20
21 #include <cstdint>
22 #include <cstring>
23
24 #include "flatbuffers/flatbuffers.h"
25
26 namespace flatbuffers
27 {
28
29 template <typename T> struct FnvTraits
30 {
31   static const T kFnvPrime;
32   static const T kOffsetBasis;
33 };
34
35 template <> struct FnvTraits<uint32_t>
36 {
37   static const uint32_t kFnvPrime = 0x01000193;
38   static const uint32_t kOffsetBasis = 0x811C9DC5;
39 };
40
41 template <> struct FnvTraits<uint64_t>
42 {
43   static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
44   static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
45 };
46
47 template <typename T> T HashFnv1(const char *input)
48 {
49   T hash = FnvTraits<T>::kOffsetBasis;
50   for (const char *c = input; *c; ++c)
51   {
52     hash *= FnvTraits<T>::kFnvPrime;
53     hash ^= static_cast<unsigned char>(*c);
54   }
55   return hash;
56 }
57
58 template <typename T> T HashFnv1a(const char *input)
59 {
60   T hash = FnvTraits<T>::kOffsetBasis;
61   for (const char *c = input; *c; ++c)
62   {
63     hash ^= static_cast<unsigned char>(*c);
64     hash *= FnvTraits<T>::kFnvPrime;
65   }
66   return hash;
67 }
68
69 template <> inline uint16_t HashFnv1<uint16_t>(const char *input)
70 {
71   uint32_t hash = HashFnv1<uint32_t>(input);
72   return (hash >> 16) ^ (hash & 0xffff);
73 }
74
75 template <> inline uint16_t HashFnv1a<uint16_t>(const char *input)
76 {
77   uint32_t hash = HashFnv1a<uint32_t>(input);
78   return (hash >> 16) ^ (hash & 0xffff);
79 }
80
81 template <typename T> struct NamedHashFunction
82 {
83   const char *name;
84
85   typedef T (*HashFunction)(const char *);
86   HashFunction function;
87 };
88
89 const NamedHashFunction<uint16_t> kHashFunctions16[] = {
90   {"fnv1_16", HashFnv1<uint16_t>},
91   {"fnv1a_16", HashFnv1a<uint16_t>},
92 };
93
94 const NamedHashFunction<uint32_t> kHashFunctions32[] = {
95   {"fnv1_32", HashFnv1<uint32_t>},
96   {"fnv1a_32", HashFnv1a<uint32_t>},
97 };
98
99 const NamedHashFunction<uint64_t> kHashFunctions64[] = {
100   {"fnv1_64", HashFnv1<uint64_t>},
101   {"fnv1a_64", HashFnv1a<uint64_t>},
102 };
103
104 inline NamedHashFunction<uint16_t>::HashFunction FindHashFunction16(const char *name)
105 {
106   std::size_t size = sizeof(kHashFunctions16) / sizeof(kHashFunctions16[0]);
107   for (std::size_t i = 0; i < size; ++i)
108   {
109     if (std::strcmp(name, kHashFunctions16[i].name) == 0)
110     {
111       return kHashFunctions16[i].function;
112     }
113   }
114   return nullptr;
115 }
116
117 inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(const char *name)
118 {
119   std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);
120   for (std::size_t i = 0; i < size; ++i)
121   {
122     if (std::strcmp(name, kHashFunctions32[i].name) == 0)
123     {
124       return kHashFunctions32[i].function;
125     }
126   }
127   return nullptr;
128 }
129
130 inline NamedHashFunction<uint64_t>::HashFunction FindHashFunction64(const char *name)
131 {
132   std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]);
133   for (std::size_t i = 0; i < size; ++i)
134   {
135     if (std::strcmp(name, kHashFunctions64[i].name) == 0)
136     {
137       return kHashFunctions64[i].function;
138     }
139   }
140   return nullptr;
141 }
142
143 } // namespace flatbuffers
144
145 #endif // FLATBUFFERS_HASH_H_