1 /*
2  * Copyright (c) 2024 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 API_BASE_UTIL_COMPILE_TIME_HASHES_H
17 #define API_BASE_UTIL_COMPILE_TIME_HASHES_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/type_traits.h>
22 #include <base/namespace.h>
23 
BASE_BEGIN_NAMESPACE()24 BASE_BEGIN_NAMESPACE()
25 namespace CompileTime {
26 constexpr uint64_t FNV_OFFSET_BASIS = 0xcbf29ce484222325ull;
27 constexpr uint64_t FNV_PRIME = 1099511628211ull;
28 
29 constexpr uint64_t lo(uint64_t x)
30 {
31     return x & 0xFFFFFFFF;
32 }
33 
34 constexpr uint64_t hi(uint64_t x)
35 {
36     return x >> 32ull;
37 }
38 
39 constexpr uint64_t mulu64(uint64_t a, uint64_t b)
40 {
41     return 0 + (lo(a) * lo(b) & uint32_t(-1)) +
42            (((((hi(lo(a) * lo(b)) + lo(a) * hi(b)) & uint32_t(-1)) + hi(a) * lo(b)) & uint32_t(-1)) << 32ull);
43 }
44 
45 inline constexpr uint64_t FNV1aHash(const char* const first, uint64_t hash = FNV_OFFSET_BASIS)
46 {
47     if (*first == 0) {
48         return hash;
49     }
50     hash ^= (uint64_t)*first;
51     // Using custom 64 bit multiply to silence "constant integer overflow" warning.
52     hash = mulu64(hash, FNV_PRIME);
53     return FNV1aHash(first + 1, hash);
54 }
55 } // namespace CompileTime
56 BASE_END_NAMESPACE()
57 #endif // API_BASE_UTIL_COMPILE_TIME_HASHES_H