folly: fix clang's -Wundefined-var-template
[folly.git] / folly / Fingerprint.h
1 /*
2  * Copyright 2016 Facebook, Inc.
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 /**
18  * Compute 64-, 96-, and 128-bit Rabin fingerprints, as described in
19  * Michael O. Rabin (1981)
20  *   Fingerprinting by Random Polynomials
21  *   Center for Research in Computing Technology, Harvard University
22  *   Tech Report TR-CSE-03-01
23  *
24  * The implementation follows the optimization described in
25  * Andrei Z. Broder (1993)
26  *   Some applications of Rabin's fingerprinting method
27  *
28  * extended for fingerprints larger than 64 bits, and modified to use
29  * 64-bit instead of 32-bit integers for computation.
30  *
31  * The precomputed tables are in FingerprintTable.cpp, which is automatically
32  * generated by ComputeFingerprintTable.cpp.
33  *
34  * Benchmarked on 10/13/2009 on a 2.5GHz quad-core Xeon L5420,
35  * - Fingerprint<64>::update64() takes about 12ns
36  * - Fingerprint<96>::update64() takes about 30ns
37  * - Fingerprint<128>::update128() takes about 30ns
38  * (unsurprisingly, Fingerprint<96> and Fingerprint<128> take the
39  * same amount of time, as they both use 128-bit operations; the least
40  * significant 32 bits of Fingerprint<96> will always be 0)
41  *
42  * @author Tudor Bosman (tudorb@facebook.com)
43  */
44
45 #pragma once
46
47 #include <cstdint>
48
49 #include <folly/Range.h>
50
51 namespace folly {
52
53 namespace detail {
54
55 template <int BITS>
56 struct FingerprintTable {
57   static const uint64_t poly[1 + (BITS - 1) / 64];
58   static const uint64_t table[8][256][1 + (BITS - 1) / 64];
59 };
60
61 template <int BITS>
62 const uint64_t FingerprintTable<BITS>::poly[1 + (BITS - 1) / 64] = {};
63
64 template <int BITS>
65 const uint64_t FingerprintTable<BITS>::table[8][256][1 + (BITS - 1) / 64] = {};
66
67 } // namespace detail
68
69 /**
70  * Compute the Rabin fingerprint.
71  *
72  * TODO(tudorb): Extend this to allow removing values from the computed
73  * fingerprint (so we can fingerprint a sliding window, as in the Rabin-Karp
74  * string matching algorithm)
75  *
76  * update* methods return *this, so you can chain them together:
77  * Fingerprint<96>().update8(x).update(str).update64(val).write(output);
78  */
79 template <int BITS>
80 class Fingerprint {
81  public:
82   Fingerprint() {
83     // Use a non-zero starting value. We'll use (1 << (BITS-1))
84     fp_[0] = 1ULL << 63;
85     for (int i = 1; i < size(); i++)
86       fp_[i] = 0;
87   }
88
89   Fingerprint& update8(uint8_t v) {
90     uint8_t out = shlor8(v);
91     xortab(detail::FingerprintTable<BITS>::table[0][out]);
92     return *this;
93   }
94
95   // update32 and update64 are convenience functions to update the fingerprint
96   // with 4 and 8 bytes at a time.  They are faster than calling update8
97   // in a loop.  They process the bytes in big-endian order.
98   Fingerprint& update32(uint32_t v) {
99     uint32_t out = shlor32(v);
100     for (int i = 0; i < 4; i++) {
101       xortab(detail::FingerprintTable<BITS>::table[i][out&0xff]);
102       out >>= 8;
103     }
104     return *this;
105   }
106
107   Fingerprint& update64(uint64_t v) {
108     uint64_t out = shlor64(v);
109     for (int i = 0; i < 8; i++) {
110       xortab(detail::FingerprintTable<BITS>::table[i][out&0xff]);
111       out >>= 8;
112     }
113     return *this;
114   }
115
116   Fingerprint& update(StringPiece str) {
117     // TODO(tudorb): We could be smart and do update64 or update32 if aligned
118     for (auto c : str) {
119       update8(uint8_t(c));
120     }
121     return *this;
122   }
123
124   /**
125    * Return the number of uint64s needed to hold the fingerprint value.
126    */
127   static int size() {
128     return 1 + (BITS-1)/64;
129   }
130
131   /**
132    * Write the computed fingeprint to an array of size() uint64_t's.
133    * For Fingerprint<64>,  size()==1; we write 64 bits in out[0]
134    * For Fingerprint<96>,  size()==2; we write 64 bits in out[0] and
135    *                                  the most significant 32 bits of out[1]
136    * For Fingerprint<128>, size()==2; we write 64 bits in out[0] and
137    *                                  64 bits in out[1].
138    */
139   void write(uint64_t* out) const {
140     for (int i = 0; i < size(); i++) {
141       out[i] = fp_[i];
142     }
143   }
144
145  private:
146   // XOR the fingerprint with a value from one of the tables.
147   void xortab(const uint64_t* tab) {
148     for (int i = 0; i < size(); i++) {
149       fp_[i] ^= tab[i];
150     }
151   }
152
153   // Helper functions: shift the fingerprint value left by 8/32/64 bits,
154   // return the "out" value (the bits that were shifted out), and add "v"
155   // in the bits on the right.
156   uint8_t  shlor8(uint8_t v);
157   uint32_t shlor32(uint32_t v);
158   uint64_t shlor64(uint64_t v);
159
160   uint64_t fp_[1 + (BITS-1)/64];
161 };
162
163 // Convenience functions
164
165 /**
166  * Return the 64-bit Rabin fingerprint of a string.
167  */
168 inline uint64_t fingerprint64(StringPiece str) {
169   uint64_t fp;
170   Fingerprint<64>().update(str).write(&fp);
171   return fp;
172 }
173
174 /**
175  * Compute the 96-bit Rabin fingerprint of a string.
176  * Return the 64 most significant bits in *msb, and the 32 least significant
177  * bits in *lsb.
178  */
179 inline void fingerprint96(StringPiece str,
180                           uint64_t* msb, uint32_t* lsb) {
181   uint64_t fp[2];
182   Fingerprint<96>().update(str).write(fp);
183   *msb = fp[0];
184   *lsb = (uint32_t)(fp[1] >> 32);
185 }
186
187 /**
188  * Compute the 128-bit Rabin fingerprint of a string.
189  * Return the 64 most significant bits in *msb, and the 64 least significant
190  * bits in *lsb.
191  */
192 inline void fingerprint128(StringPiece str,
193                            uint64_t* msb, uint64_t* lsb) {
194   uint64_t fp[2];
195   Fingerprint<128>().update(str).write(fp);
196   *msb = fp[0];
197   *lsb = fp[1];
198 }
199
200
201 template <>
202 inline uint8_t Fingerprint<64>::shlor8(uint8_t v) {
203   uint8_t out = (uint8_t)(fp_[0] >> 56);
204   fp_[0] = (fp_[0] << 8) | ((uint64_t)v);
205   return out;
206 }
207
208 template <>
209 inline uint32_t Fingerprint<64>::shlor32(uint32_t v) {
210   uint32_t out = (uint32_t)(fp_[0] >> 32);
211   fp_[0] = (fp_[0] << 32) | ((uint64_t)v);
212   return out;
213 }
214
215 template <>
216 inline uint64_t Fingerprint<64>::shlor64(uint64_t v) {
217   uint64_t out = fp_[0];
218   fp_[0] = v;
219   return out;
220 }
221
222 template <>
223 inline uint8_t Fingerprint<96>::shlor8(uint8_t v) {
224   uint8_t out = (uint8_t)(fp_[0] >> 56);
225   fp_[0] = (fp_[0] << 8) | (fp_[1] >> 56);
226   fp_[1] = (fp_[1] << 8) | ((uint64_t)v << 32);
227   return out;
228 }
229
230 template <>
231 inline uint32_t Fingerprint<96>::shlor32(uint32_t v) {
232   uint32_t out = (uint32_t)(fp_[0] >> 32);
233   fp_[0] = (fp_[0] << 32) | (fp_[1] >> 32);
234   fp_[1] = ((uint64_t)v << 32);
235   return out;
236 }
237
238 template <>
239 inline uint64_t Fingerprint<96>::shlor64(uint64_t v) {
240   uint64_t out = fp_[0];
241   fp_[0] = fp_[1] | (v >> 32);
242   fp_[1] = v << 32;
243   return out;
244 }
245
246 template <>
247 inline uint8_t Fingerprint<128>::shlor8(uint8_t v) {
248   uint8_t out = (uint8_t)(fp_[0] >> 56);
249   fp_[0] = (fp_[0] << 8) | (fp_[1] >> 56);
250   fp_[1] = (fp_[1] << 8) | ((uint64_t)v);
251   return out;
252 }
253
254 template <>
255 inline uint32_t Fingerprint<128>::shlor32(uint32_t v) {
256   uint32_t out = (uint32_t)(fp_[0] >> 32);
257   fp_[0] = (fp_[0] << 32) | (fp_[1] >> 32);
258   fp_[1] = (fp_[1] << 32) | ((uint64_t)v);
259   return out;
260 }
261
262 template <>
263 inline uint64_t Fingerprint<128>::shlor64(uint64_t v) {
264   uint64_t out = fp_[0];
265   fp_[0] = fp_[1];
266   fp_[1] = v;
267   return out;
268 }
269
270 }  // namespace folly