xref: /aosp_15_r20/system/chre/util/hash.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
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 
17 #include <cstddef>
18 #include <cstdint>
19 
20 #include "chre/util/hash.h"
21 
22 namespace chre {
23 
fnv1a32Hash(const uint8_t * data,size_t size)24 uint32_t fnv1a32Hash(const uint8_t* data, size_t size) {
25   if (data == nullptr || size == 0) {
26     return UINT32_MAX;
27   }
28 
29   constexpr uint32_t kFnvPrime = 0x01000193;
30   constexpr uint32_t kFnvOffset = 0x811c9dc5;
31 
32   uint32_t hash = kFnvOffset;
33   for (size_t i = 0; i < size; ++i) {
34     hash ^= data[i];
35     hash *= kFnvPrime;
36   }
37   return hash;
38 }
39 
40 }  // namespace chre
41