ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
hash.h
Go to the documentation of this file.
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
25
26namespace flatbuffers
27{
28
29template <typename T> struct FnvTraits
30{
31 static const T kFnvPrime;
32 static const T kOffsetBasis;
33};
34
35template <> struct FnvTraits<uint32_t>
36{
37 static const uint32_t kFnvPrime = 0x01000193;
38 static const uint32_t kOffsetBasis = 0x811C9DC5;
39};
40
41template <> struct FnvTraits<uint64_t>
42{
43 static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
44 static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
45};
46
47template <typename T> T HashFnv1(const char *input)
48{
50 for (const char *c = input; *c; ++c)
51 {
53 hash ^= static_cast<unsigned char>(*c);
54 }
55 return hash;
56}
57
58template <typename T> T HashFnv1a(const char *input)
59{
61 for (const char *c = input; *c; ++c)
62 {
63 hash ^= static_cast<unsigned char>(*c);
65 }
66 return hash;
67}
68
69template <> 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
75template <> 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
81template <typename T> struct NamedHashFunction
82{
83 const char *name;
84
85 typedef T (*HashFunction)(const char *);
87};
88
90 {"fnv1_16", HashFnv1<uint16_t>},
91 {"fnv1a_16", HashFnv1a<uint16_t>},
92};
93
95 {"fnv1_32", HashFnv1<uint32_t>},
96 {"fnv1a_32", HashFnv1a<uint32_t>},
97};
98
100 {"fnv1_64", HashFnv1<uint64_t>},
101 {"fnv1a_64", HashFnv1a<uint64_t>},
102};
103
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
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
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_
NamedHashFunction< uint64_t >::HashFunction FindHashFunction64(const char *name)
Definition hash.h:130
T HashFnv1a(const char *input)
Definition hash.h:58
const NamedHashFunction< uint32_t > kHashFunctions32[]
Definition hash.h:94
const NamedHashFunction< uint16_t > kHashFunctions16[]
Definition hash.h:89
NamedHashFunction< uint32_t >::HashFunction FindHashFunction32(const char *name)
Definition hash.h:117
uint16_t HashFnv1< uint16_t >(const char *input)
Definition hash.h:69
T HashFnv1(const char *input)
Definition hash.h:47
NamedHashFunction< uint16_t >::HashFunction FindHashFunction16(const char *name)
Definition hash.h:104
uint16_t HashFnv1a< uint16_t >(const char *input)
Definition hash.h:75
const NamedHashFunction< uint64_t > kHashFunctions64[]
Definition hash.h:99
int32_t size[5]
Definition Slice.cpp:35
static const T kFnvPrime
Definition hash.h:31
static const T kOffsetBasis
Definition hash.h:32
T(* HashFunction)(const char *)
Definition hash.h:85