blob: 7da5dbd6a91321e7053c62668d6a8d4e14192be2 [file] [log] [blame]
Raph Levienb6ea1752012-10-25 23:11:13 -07001/*
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
Romain Guybdce9ba2012-11-28 17:37:03 -080022#ifndef ANDROID_JENKINS_HASH_H
23#define ANDROID_JENKINS_HASH_H
24
Raph Levienb6ea1752012-10-25 23:11:13 -070025#include <utils/TypeHelpers.h>
26
27namespace 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
32inline 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
39hash_t JenkinsHashWhiten(uint32_t hash);
40
41/* Helpful utility functions for hashing data in 32 bit chunks */
42uint32_t JenkinsHashMixBytes(uint32_t hash, const uint8_t* bytes, size_t size);
43
44uint32_t JenkinsHashMixShorts(uint32_t hash, const uint16_t* shorts, size_t size);
45
46}
47
Romain Guybdce9ba2012-11-28 17:37:03 -080048#endif // ANDROID_JENKINS_HASH_H