• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  #include <utils/JenkinsHash.h>
23  
24  namespace android {
25  
JenkinsHashWhiten(uint32_t hash)26  hash_t JenkinsHashWhiten(uint32_t hash) {
27      hash += (hash << 3);
28      hash ^= (hash >> 11);
29      hash += (hash << 15);
30      return hash;
31  }
32  
JenkinsHashMixBytes(uint32_t hash,const uint8_t * bytes,size_t size)33  uint32_t JenkinsHashMixBytes(uint32_t hash, const uint8_t* bytes, size_t size) {
34      hash = JenkinsHashMix(hash, (uint32_t)size);
35      size_t i;
36      for (i = 0; i < (size & -4); i += 4) {
37          uint32_t data = bytes[i] | (bytes[i+1] << 8) | (bytes[i+2] << 16) | (bytes[i+3] << 24);
38          hash = JenkinsHashMix(hash, data);
39      }
40      if (size & 3) {
41          uint32_t data = bytes[i];
42          data |= ((size & 3) > 1) ? (bytes[i+1] << 8) : 0;
43          data |= ((size & 3) > 2) ? (bytes[i+2] << 16) : 0;
44          hash = JenkinsHashMix(hash, data);
45      }
46      return hash;
47  }
48  
JenkinsHashMixShorts(uint32_t hash,const uint16_t * shorts,size_t size)49  uint32_t JenkinsHashMixShorts(uint32_t hash, const uint16_t* shorts, size_t size) {
50      hash = JenkinsHashMix(hash, (uint32_t)size);
51      size_t i;
52      for (i = 0; i < (size & -2); i += 2) {
53          uint32_t data = shorts[i] | (shorts[i+1] << 16);
54          hash = JenkinsHashMix(hash, data);
55      }
56      if (size & 1) {
57          uint32_t data = shorts[i];
58          hash = JenkinsHashMix(hash, data);
59      }
60      return hash;
61  }
62  
63  }
64  
65