• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COMMON_COMPONENTS_PLATFORM_STRING_HASH_COMMON_H
17 #define COMMON_COMPONENTS_PLATFORM_STRING_HASH_COMMON_H
18 
19 #include <cstdint>
20 
21 #include "common_components/base/config.h"
22 #include "common_components/platform/string_hash.h"
23 
24 namespace common {
25 class StringHashInternal {
26 friend class StringHashHelper;
27 private:
28     template <typename T>
ComputeHashForDataOfLongString(const T * data,size_t size,uint32_t hashSeed)29     static uint32_t ComputeHashForDataOfLongString(const T *data, size_t size,
30                                                    uint32_t hashSeed)
31     {
32         constexpr uint32_t blockSize = StringHash::BLOCK_SIZE;
33         constexpr uint32_t scale = StringHash::BLOCK_MULTIPLY;
34         uint32_t hash[blockSize] = {};
35         uint32_t index = 0;
36         uint32_t remainder = size & (blockSize - 1);
37         switch (remainder) {
38 #define CASE(N) case (N): \
39     hash[blockSize - (N)] = data[index++] * StringHash::MULTIPLIER[blockSize - (N)]; [[fallthrough]]
40             CASE(StringHash::SIZE_3);
41             CASE(StringHash::SIZE_2);
42             CASE(StringHash::SIZE_1);
43 #undef CASE
44             default:
45                 break;
46         }
47         hash[0] += hashSeed * StringHash::MULTIPLIER[blockSize - 1 - remainder];
48 
49         uint32_t dataMul[blockSize] = {};
50         for (; index < size; index += blockSize) {
51             for (size_t i = 0; i < blockSize; i++) {
52                 dataMul[i] = data[index + i] * StringHash::MULTIPLIER[i];
53                 hash[i] = hash[i] * scale + dataMul[i];
54             }
55         }
56         uint32_t hashTotal = 0;
57         for (size_t i = 0; i < blockSize; i++) {
58             hashTotal += hash[i];
59         }
60         return hashTotal;
61     }
62 };
63 }  // namespace common
64 #endif  // COMMON_COMPONENTS_PLATFORM_STRING_HASH_COMMON_H