/Users/deen/code/yugabyte-db/src/yb/gutil/hash/jenkins_lookup2.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2011 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // The following only applies to changes made to this file as part of YugaByte development. |
4 | | // |
5 | | // Portions Copyright (c) YugaByte, Inc. |
6 | | // |
7 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
8 | | // in compliance with the License. You may obtain a copy of the License at |
9 | | // |
10 | | // http://www.apache.org/licenses/LICENSE-2.0 |
11 | | // |
12 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
13 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
14 | | // or implied. See the License for the specific language governing permissions and limitations |
15 | | // under the License. |
16 | | // |
17 | | // Legacy implementation of the core Jenkins lookup2 algorithm. This is used in |
18 | | // many older hash functions which we are unable to remove or change due to the |
19 | | // values being recorded. New code should not use any of these routines and |
20 | | // should not include this header file. It pollutes the global namespace with |
21 | | // the 'mix' function. |
22 | | // |
23 | | // This file contains the basic hash "mix" code which is widely referenced. |
24 | | // |
25 | | // This file also contains routines used to load an unaligned little-endian |
26 | | // word from memory. This relatively generic functionality probably |
27 | | // shouldn't live in this file. |
28 | | |
29 | | #ifndef UTIL_HASH_JENKINS_LOOKUP2_H_ |
30 | | #define UTIL_HASH_JENKINS_LOOKUP2_H_ |
31 | | |
32 | | #include "yb/gutil/integral_types.h" |
33 | | #include "yb/gutil/port.h" |
34 | | |
35 | | // ---------------------------------------------------------------------- |
36 | | // mix() |
37 | | // The hash function I use is due to Bob Jenkins (see |
38 | | // http://burtleburtle.net/bob/hash/index.html). |
39 | | // Each mix takes 36 instructions, in 18 cycles if you're lucky. |
40 | | // |
41 | | // On x86 architectures, this requires 45 instructions in 27 cycles, |
42 | | // if you're lucky. |
43 | | // ---------------------------------------------------------------------- |
44 | | |
45 | 89.7M | static inline void mix(uint32& a, uint32& b, uint32& c) { // 32bit version |
46 | 89.7M | a -= b; a -= c; a ^= (c>>13); |
47 | 89.7M | b -= c; b -= a; b ^= (a<<8); |
48 | 89.7M | c -= a; c -= b; c ^= (b>>13); |
49 | 89.7M | a -= b; a -= c; a ^= (c>>12); |
50 | 89.7M | b -= c; b -= a; b ^= (a<<16); |
51 | 89.7M | c -= a; c -= b; c ^= (b>>5); |
52 | 89.7M | a -= b; a -= c; a ^= (c>>3); |
53 | 89.7M | b -= c; b -= a; b ^= (a<<10); |
54 | 89.7M | c -= a; c -= b; c ^= (b>>15); |
55 | 89.7M | } Unexecuted instantiation: hash_util-test.cc:_ZL3mixRjS_S_ Unexecuted instantiation: hash.cc:_ZL3mixRjS_S_ Line | Count | Source | 45 | 89.7M | static inline void mix(uint32& a, uint32& b, uint32& c) { // 32bit version | 46 | 89.7M | a -= b; a -= c; a ^= (c>>13); | 47 | 89.7M | b -= c; b -= a; b ^= (a<<8); | 48 | 89.7M | c -= a; c -= b; c ^= (b>>13); | 49 | 89.7M | a -= b; a -= c; a ^= (c>>12); | 50 | 89.7M | b -= c; b -= a; b ^= (a<<16); | 51 | 89.7M | c -= a; c -= b; c ^= (b>>5); | 52 | 89.7M | a -= b; a -= c; a ^= (c>>3); | 53 | 89.7M | b -= c; b -= a; b ^= (a<<10); | 54 | 89.7M | c -= a; c -= b; c ^= (b>>15); | 55 | 89.7M | } |
Unexecuted instantiation: split.cc:_ZL3mixRjS_S_ Unexecuted instantiation: stringpiece.cc:_ZL3mixRjS_S_ Unexecuted instantiation: partition.cc:_ZL3mixRjS_S_ Unexecuted instantiation: yb_partition.cc:_ZL3mixRjS_S_ |
56 | | |
57 | 27.3M | static inline void mix(uint64& a, uint64& b, uint64& c) { // 64bit version |
58 | 27.3M | a -= b; a -= c; a ^= (c>>43); |
59 | 27.3M | b -= c; b -= a; b ^= (a<<9); |
60 | 27.3M | c -= a; c -= b; c ^= (b>>8); |
61 | 27.3M | a -= b; a -= c; a ^= (c>>38); |
62 | 27.3M | b -= c; b -= a; b ^= (a<<23); |
63 | 27.3M | c -= a; c -= b; c ^= (b>>5); |
64 | 27.3M | a -= b; a -= c; a ^= (c>>35); |
65 | 27.3M | b -= c; b -= a; b ^= (a<<49); |
66 | 27.3M | c -= a; c -= b; c ^= (b>>11); |
67 | 27.3M | a -= b; a -= c; a ^= (c>>12); |
68 | 27.3M | b -= c; b -= a; b ^= (a<<18); |
69 | 27.3M | c -= a; c -= b; c ^= (b>>22); |
70 | 27.3M | } Unexecuted instantiation: hash_util-test.cc:_ZL3mixRyS_S_ Unexecuted instantiation: hash.cc:_ZL3mixRyS_S_ Line | Count | Source | 57 | 27.3M | static inline void mix(uint64& a, uint64& b, uint64& c) { // 64bit version | 58 | 27.3M | a -= b; a -= c; a ^= (c>>43); | 59 | 27.3M | b -= c; b -= a; b ^= (a<<9); | 60 | 27.3M | c -= a; c -= b; c ^= (b>>8); | 61 | 27.3M | a -= b; a -= c; a ^= (c>>38); | 62 | 27.3M | b -= c; b -= a; b ^= (a<<23); | 63 | 27.3M | c -= a; c -= b; c ^= (b>>5); | 64 | 27.3M | a -= b; a -= c; a ^= (c>>35); | 65 | 27.3M | b -= c; b -= a; b ^= (a<<49); | 66 | 27.3M | c -= a; c -= b; c ^= (b>>11); | 67 | 27.3M | a -= b; a -= c; a ^= (c>>12); | 68 | 27.3M | b -= c; b -= a; b ^= (a<<18); | 69 | 27.3M | c -= a; c -= b; c ^= (b>>22); | 70 | 27.3M | } |
Unexecuted instantiation: split.cc:_ZL3mixRyS_S_ Unexecuted instantiation: stringpiece.cc:_ZL3mixRyS_S_ Unexecuted instantiation: partition.cc:_ZL3mixRyS_S_ Unexecuted instantiation: yb_partition.cc:_ZL3mixRyS_S_ |
71 | | |
72 | | |
73 | | // Load an unaligned little endian word from memory. |
74 | | // |
75 | | // These routines are named Word32At(), Word64At() and Google1At(). |
76 | | // Long ago, the 32-bit version of this operation was implemented using |
77 | | // signed characters. The hash function that used this variant creates |
78 | | // persistent hash values. The hash routine needs to remain backwards |
79 | | // compatible, so we renamed the word loading function 'Google1At' to |
80 | | // make it clear this implements special functionality. |
81 | | // |
82 | | // If a machine has alignment constraints or is big endian, we must |
83 | | // load the word a byte at a time. Otherwise we can load the whole word |
84 | | // from memory. |
85 | | // |
86 | | // [Plausibly, Word32At() and Word64At() should really be called |
87 | | // UNALIGNED_LITTLE_ENDIAN_LOAD32() and UNALIGNED_LITTLE_ENDIAN_LOAD64() |
88 | | // but that seems overly verbose.] |
89 | | |
90 | | #if !defined(NEED_ALIGNED_LOADS) && defined(IS_LITTLE_ENDIAN) |
91 | | static inline uint64 Word64At(const char *ptr) { |
92 | | return UNALIGNED_LOAD64(ptr); |
93 | | } |
94 | | |
95 | | static inline uint32 Word32At(const char *ptr) { |
96 | | return UNALIGNED_LOAD32(ptr); |
97 | | } |
98 | | |
99 | | // This produces the same results as the byte-by-byte version below. |
100 | | // Here, we mask off the sign bits and subtract off two copies. To |
101 | | // see why this is the same as adding together the sign extensions, |
102 | | // start by considering the low-order byte. If we loaded an unsigned |
103 | | // word and wanted to sign extend it, we isolate the sign bit and subtract |
104 | | // that from zero which gives us a sequence of bits matching the sign bit |
105 | | // at and above the sign bit. If we remove (subtract) the sign bit and |
106 | | // add in the low order byte, we now have a sign-extended byte as desired. |
107 | | // We can then operate on all four bytes in parallel because addition |
108 | | // is associative and commutative. |
109 | | // |
110 | | // For example, consider sign extending the bytes 0x01 and 0x81. For 0x01, |
111 | | // the sign bit is zero, and 0x01 - 0 -0 = 1. For 0x81, the sign bit is 1 |
112 | | // and we are computing 0x81 - 0x80 + (-0x80) == 0x01 + 0xFFFFFF80. |
113 | | // |
114 | | // Similarily, if we start with 0x8200 and want to sign extend that, |
115 | | // we end up calculating 0x8200 - 0x8000 + (-0x8000) == 0xFFFF8000 + 0x0200 |
116 | | // |
117 | | // Suppose we have two bytes at the same time. Doesn't the adding of all |
118 | | // those F's generate something wierd? Ignore the F's and reassociate |
119 | | // the addition. For 0x8281, processing the bytes one at a time (like |
120 | | // we used to do) calculates |
121 | | // [0x8200 - 0x8000 + (-0x8000)] + [0x0081 - 0x80 + (-0x80)] |
122 | | // == 0x8281 - 0x8080 - 0x8000 - 0x80 |
123 | | // == 0x8281 - 0x8080 - 0x8080 |
124 | | |
125 | | static inline uint32 Google1At(const char *ptr) { |
126 | | uint32 t = UNALIGNED_LOAD32(ptr); |
127 | | uint32 masked = t & 0x80808080; |
128 | | return t - masked - masked; |
129 | | } |
130 | | |
131 | | #else |
132 | | |
133 | | // NOTE: This code is not normally used or tested. |
134 | | |
135 | 50.4M | static inline uint64 Word64At(const char *ptr) { |
136 | 50.4M | return (static_cast<uint64>(ptr[0]) + |
137 | 50.4M | (static_cast<uint64>(ptr[1]) << 8) + |
138 | 50.4M | (static_cast<uint64>(ptr[2]) << 16) + |
139 | 50.4M | (static_cast<uint64>(ptr[3]) << 24) + |
140 | 50.4M | (static_cast<uint64>(ptr[4]) << 32) + |
141 | 50.4M | (static_cast<uint64>(ptr[5]) << 40) + |
142 | 50.4M | (static_cast<uint64>(ptr[6]) << 48) + |
143 | 50.4M | (static_cast<uint64>(ptr[7]) << 56)); |
144 | 50.4M | } Unexecuted instantiation: hash_util-test.cc:_ZL8Word64AtPKc Unexecuted instantiation: hash.cc:_ZL8Word64AtPKc jenkins.cc:_ZL8Word64AtPKc Line | Count | Source | 135 | 50.4M | static inline uint64 Word64At(const char *ptr) { | 136 | 50.4M | return (static_cast<uint64>(ptr[0]) + | 137 | 50.4M | (static_cast<uint64>(ptr[1]) << 8) + | 138 | 50.4M | (static_cast<uint64>(ptr[2]) << 16) + | 139 | 50.4M | (static_cast<uint64>(ptr[3]) << 24) + | 140 | 50.4M | (static_cast<uint64>(ptr[4]) << 32) + | 141 | 50.4M | (static_cast<uint64>(ptr[5]) << 40) + | 142 | 50.4M | (static_cast<uint64>(ptr[6]) << 48) + | 143 | 50.4M | (static_cast<uint64>(ptr[7]) << 56)); | 144 | 50.4M | } |
Unexecuted instantiation: split.cc:_ZL8Word64AtPKc Unexecuted instantiation: stringpiece.cc:_ZL8Word64AtPKc Unexecuted instantiation: partition.cc:_ZL8Word64AtPKc Unexecuted instantiation: yb_partition.cc:_ZL8Word64AtPKc |
145 | | |
146 | 0 | static inline uint32 Word32At(const char *ptr) { |
147 | 0 | return (static_cast<uint32>(ptr[0]) + |
148 | 0 | (static_cast<uint32>(ptr[1]) << 8) + |
149 | 0 | (static_cast<uint32>(ptr[2]) << 16) + |
150 | 0 | (static_cast<uint32>(ptr[3]) << 24)); |
151 | 0 | } Unexecuted instantiation: hash_util-test.cc:_ZL8Word32AtPKc Unexecuted instantiation: hash.cc:_ZL8Word32AtPKc Unexecuted instantiation: jenkins.cc:_ZL8Word32AtPKc Unexecuted instantiation: split.cc:_ZL8Word32AtPKc Unexecuted instantiation: stringpiece.cc:_ZL8Word32AtPKc Unexecuted instantiation: partition.cc:_ZL8Word32AtPKc Unexecuted instantiation: yb_partition.cc:_ZL8Word32AtPKc |
152 | | |
153 | 131M | static inline uint32 Google1At(const char *ptr2) { |
154 | 131M | const schar * ptr = reinterpret_cast<const schar *>(ptr2); |
155 | 131M | return (static_cast<schar>(ptr[0]) + |
156 | 131M | (static_cast<uint32>(ptr[1]) << 8) + |
157 | 131M | (static_cast<uint32>(ptr[2]) << 16) + |
158 | 131M | (static_cast<uint32>(ptr[3]) << 24)); |
159 | 131M | } Unexecuted instantiation: hash_util-test.cc:_ZL9Google1AtPKc Unexecuted instantiation: hash.cc:_ZL9Google1AtPKc jenkins.cc:_ZL9Google1AtPKc Line | Count | Source | 153 | 131M | static inline uint32 Google1At(const char *ptr2) { | 154 | 131M | const schar * ptr = reinterpret_cast<const schar *>(ptr2); | 155 | 131M | return (static_cast<schar>(ptr[0]) + | 156 | 131M | (static_cast<uint32>(ptr[1]) << 8) + | 157 | 131M | (static_cast<uint32>(ptr[2]) << 16) + | 158 | 131M | (static_cast<uint32>(ptr[3]) << 24)); | 159 | 131M | } |
Unexecuted instantiation: split.cc:_ZL9Google1AtPKc Unexecuted instantiation: stringpiece.cc:_ZL9Google1AtPKc Unexecuted instantiation: partition.cc:_ZL9Google1AtPKc Unexecuted instantiation: yb_partition.cc:_ZL9Google1AtPKc |
160 | | |
161 | | #endif /* !NEED_ALIGNED_LOADS && IS_LITTLE_ENDIAN */ |
162 | | |
163 | | // Historically, WORD_HASH has always been defined as we always run on |
164 | | // machines that don't NEED_ALIGNED_LOADS and which IS_LITTLE_ENDIAN. |
165 | | // |
166 | | // TODO(user): find occurences of WORD_HASH and adjust the code to |
167 | | // use more meaningful concepts. |
168 | | # define WORD_HASH |
169 | | |
170 | | #endif // UTIL_HASH_JENKINS_LOOKUP2_H_ |