hash.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2015 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FLATBUFFERS_HASH_H_
  17. #define FLATBUFFERS_HASH_H_
  18. #include <cstdint>
  19. #include <cstring>
  20. #include "packages/TensorflowLiteMicro/flatbuffers/flatbuffers.h"
  21. namespace flatbuffers {
  22. template<typename T> struct FnvTraits {
  23. static const T kFnvPrime;
  24. static const T kOffsetBasis;
  25. };
  26. template<> struct FnvTraits<uint32_t> {
  27. static const uint32_t kFnvPrime = 0x01000193;
  28. static const uint32_t kOffsetBasis = 0x811C9DC5;
  29. };
  30. template<> struct FnvTraits<uint64_t> {
  31. static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
  32. static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
  33. };
  34. template<typename T> T HashFnv1(const char *input) {
  35. T hash = FnvTraits<T>::kOffsetBasis;
  36. for (const char *c = input; *c; ++c) {
  37. hash *= FnvTraits<T>::kFnvPrime;
  38. hash ^= static_cast<unsigned char>(*c);
  39. }
  40. return hash;
  41. }
  42. template<typename T> T HashFnv1a(const char *input) {
  43. T hash = FnvTraits<T>::kOffsetBasis;
  44. for (const char *c = input; *c; ++c) {
  45. hash ^= static_cast<unsigned char>(*c);
  46. hash *= FnvTraits<T>::kFnvPrime;
  47. }
  48. return hash;
  49. }
  50. template<> inline uint16_t HashFnv1<uint16_t>(const char *input) {
  51. uint32_t hash = HashFnv1<uint32_t>(input);
  52. return (hash >> 16) ^ (hash & 0xffff);
  53. }
  54. template<> inline uint16_t HashFnv1a<uint16_t>(const char *input) {
  55. uint32_t hash = HashFnv1a<uint32_t>(input);
  56. return (hash >> 16) ^ (hash & 0xffff);
  57. }
  58. template<typename T> struct NamedHashFunction {
  59. const char *name;
  60. typedef T (*HashFunction)(const char *);
  61. HashFunction function;
  62. };
  63. const NamedHashFunction<uint16_t> kHashFunctions16[] = {
  64. { "fnv1_16", HashFnv1<uint16_t> },
  65. { "fnv1a_16", HashFnv1a<uint16_t> },
  66. };
  67. const NamedHashFunction<uint32_t> kHashFunctions32[] = {
  68. { "fnv1_32", HashFnv1<uint32_t> },
  69. { "fnv1a_32", HashFnv1a<uint32_t> },
  70. };
  71. const NamedHashFunction<uint64_t> kHashFunctions64[] = {
  72. { "fnv1_64", HashFnv1<uint64_t> },
  73. { "fnv1a_64", HashFnv1a<uint64_t> },
  74. };
  75. inline NamedHashFunction<uint16_t>::HashFunction FindHashFunction16(
  76. const char *name) {
  77. std::size_t size = sizeof(kHashFunctions16) / sizeof(kHashFunctions16[0]);
  78. for (std::size_t i = 0; i < size; ++i) {
  79. if (std::strcmp(name, kHashFunctions16[i].name) == 0) {
  80. return kHashFunctions16[i].function;
  81. }
  82. }
  83. return nullptr;
  84. }
  85. inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(
  86. const char *name) {
  87. std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);
  88. for (std::size_t i = 0; i < size; ++i) {
  89. if (std::strcmp(name, kHashFunctions32[i].name) == 0) {
  90. return kHashFunctions32[i].function;
  91. }
  92. }
  93. return nullptr;
  94. }
  95. inline NamedHashFunction<uint64_t>::HashFunction FindHashFunction64(
  96. const char *name) {
  97. std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]);
  98. for (std::size_t i = 0; i < size; ++i) {
  99. if (std::strcmp(name, kHashFunctions64[i].name) == 0) {
  100. return kHashFunctions64[i].function;
  101. }
  102. }
  103. return nullptr;
  104. }
  105. } // namespace flatbuffers
  106. #endif // FLATBUFFERS_HASH_H_