1 /*
2 * Copyright (C) 2012 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 /* Implementation of Jenkins one-at-a-time hash function. These choices are
18 * optimized for code size and portability, rather than raw speed. But speed
19 * should still be quite good.
20 **/
21
22 #ifndef ANDROID_JENKINS_HASH_H
23 #define ANDROID_JENKINS_HASH_H
24
25 #include <utils/TypeHelpers.h>
26
27 namespace android {
28
29 /* The Jenkins hash of a sequence of 32 bit words A, B, C is:
30 * Whiten(Mix(Mix(Mix(0, A), B), C)) */
31
JenkinsHashMix(uint32_t hash,uint32_t data)32 inline uint32_t JenkinsHashMix(uint32_t hash, uint32_t data) {
33 hash += data;
34 hash += (hash << 10);
35 hash ^= (hash >> 6);
36 return hash;
37 }
38
39 hash_t JenkinsHashWhiten(uint32_t hash);
40
41 /* Helpful utility functions for hashing data in 32 bit chunks */
42 uint32_t JenkinsHashMixBytes(uint32_t hash, const uint8_t* bytes, size_t size);
43
44 uint32_t JenkinsHashMixShorts(uint32_t hash, const uint16_t* shorts, size_t size);
45
46 }
47
48 #endif // ANDROID_JENKINS_HASH_H
49