/root/bitcoin/src/leveldb/util/hash.cc
| Line | Count | Source | 
| 1 |  | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. | 
| 2 |  | // Use of this source code is governed by a BSD-style license that can be | 
| 3 |  | // found in the LICENSE file. See the AUTHORS file for names of contributors. | 
| 4 |  |  | 
| 5 |  | #include "util/hash.h" | 
| 6 |  |  | 
| 7 |  | #include <string.h> | 
| 8 |  |  | 
| 9 |  | #include "util/coding.h" | 
| 10 |  |  | 
| 11 |  | // The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through | 
| 12 |  | // between switch labels. The real definition should be provided externally. | 
| 13 |  | // This one is a fallback version for unsupported compilers. | 
| 14 |  | #ifndef FALLTHROUGH_INTENDED | 
| 15 |  | #define FALLTHROUGH_INTENDED \ | 
| 16 |  |   do {                       \ | 
| 17 |  |   } while (0) | 
| 18 |  | #endif | 
| 19 |  |  | 
| 20 |  | namespace leveldb { | 
| 21 |  |  | 
| 22 | 0 | uint32_t Hash(const char* data, size_t n, uint32_t seed) { | 
| 23 |  |   // Similar to murmur hash | 
| 24 | 0 |   const uint32_t m = 0xc6a4a793; | 
| 25 | 0 |   const uint32_t r = 24; | 
| 26 | 0 |   const char* limit = data + n; | 
| 27 | 0 |   uint32_t h = seed ^ (n * m); | 
| 28 |  |  | 
| 29 |  |   // Pick up four bytes at a time | 
| 30 | 0 |   while (limit - data >= 4) { | 
| 31 | 0 |     uint32_t w = DecodeFixed32(data); | 
| 32 | 0 |     data += 4; | 
| 33 | 0 |     h += w; | 
| 34 | 0 |     h *= m; | 
| 35 | 0 |     h ^= (h >> 16); | 
| 36 | 0 |   } | 
| 37 |  |  | 
| 38 |  |   // Pick up remaining bytes | 
| 39 | 0 |   switch (limit - data) { | 
| 40 | 0 |     case 3: | 
| 41 | 0 |       h += static_cast<uint8_t>(data[2]) << 16; | 
| 42 | 0 |       FALLTHROUGH_INTENDED; | 
| 43 | 0 |     case 2: | 
| 44 | 0 |       h += static_cast<uint8_t>(data[1]) << 8; | 
| 45 | 0 |       FALLTHROUGH_INTENDED; | 
| 46 | 0 |     case 1: | 
| 47 | 0 |       h += static_cast<uint8_t>(data[0]); | 
| 48 | 0 |       h *= m; | 
| 49 | 0 |       h ^= (h >> r); | 
| 50 | 0 |       break; | 
| 51 | 0 |   } | 
| 52 | 0 |   return h; | 
| 53 | 0 | } | 
| 54 |  |  | 
| 55 |  | }  // namespace leveldb |