Remove FOLLY_SKIP_LIBCPP_4000_THROW_BACKPORTS
[folly.git] / folly / Fingerprint.h
1 /*
2  * Copyright 2017 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 template <int BITS>
64 const uint64_t FingerprintTable<BITS>::table[8][256][1 + (BITS - 1) / 64] = {};
65
66 #ifndef _MSC_VER
67 // MSVC 2015 can't handle these extern specialization declarations,
68 // but they aren't needed for things to work right, so we just don't
69 // declare them in the header for MSVC.
70
71 #define FOLLY_DECLARE_FINGERPRINT_TABLES(BITS)                      \
72   template <>                                                       \
73   const uint64_t FingerprintTable<BITS>::poly[1 + (BITS - 1) / 64]; \
74   template <>                                                       \
75   const uint64_t FingerprintTable<BITS>::table[8][256][1 + (BITS - 1) / 64]
76
77 FOLLY_DECLARE_FINGERPRINT_TABLES(64);
78 FOLLY_DECLARE_FINGERPRINT_TABLES(96);
79 FOLLY_DECLARE_FINGERPRINT_TABLES(128);
80
81 #undef FOLLY_DECLARE_FINGERPRINT_TABLES
82 #endif
83
84 } // namespace detail
85
86 /**
87  * Compute the Rabin fingerprint.
88  *
89  * TODO(tudorb): Extend this to allow removing values from the computed
90  * fingerprint (so we can fingerprint a sliding window, as in the Rabin-Karp
91  * string matching algorithm)
92  *
93  * update* methods return *this, so you can chain them together:
94  * Fingerprint<96>().update8(x).update(str).update64(val).write(output);
95  */
96 template <int BITS>
97 class Fingerprint {
98  public:
99   Fingerprint() {
100     // Use a non-zero starting value. We'll use (1 << (BITS-1))
101     fp_[0] = 1ULL << 63;
102     for (int i = 1; i < size(); i++)
103       fp_[i] = 0;
104   }
105
106   Fingerprint& update8(uint8_t v) {
107     uint8_t out = shlor8(v);
108     xortab(detail::FingerprintTable<BITS>::table[0][out]);
109     return *this;
110   }
111
112   // update32 and update64 are convenience functions to update the fingerprint
113   // with 4 and 8 bytes at a time.  They are faster than calling update8
114   // in a loop.  They process the bytes in big-endian order.
115   Fingerprint& update32(uint32_t v) {
116     uint32_t out = shlor32(v);
117     for (int i = 0; i < 4; i++) {
118       xortab(detail::FingerprintTable<BITS>::table[i][out&0xff]);
119       out >>= 8;
120     }
121     return *this;
122   }
123
124   Fingerprint& update64(uint64_t v) {
125     uint64_t out = shlor64(v);
126     for (int i = 0; i < 8; i++) {
127       xortab(detail::FingerprintTable<BITS>::table[i][out&0xff]);
128       out >>= 8;
129     }
130     return *this;
131   }
132
133   Fingerprint& update(StringPiece str) {
134     // TODO(tudorb): We could be smart and do update64 or update32 if aligned
135     for (auto c : str) {
136       update8(uint8_t(c));
137     }
138     return *this;
139   }
140
141   /**
142    * Return the number of uint64s needed to hold the fingerprint value.
143    */
144   static int size() {
145     return 1 + (BITS-1)/64;
146   }
147
148   /**
149    * Write the computed fingeprint to an array of size() uint64_t's.
150    * For Fingerprint<64>,  size()==1; we write 64 bits in out[0]
151    * For Fingerprint<96>,  size()==2; we write 64 bits in out[0] and
152    *                                  the most significant 32 bits of out[1]
153    * For Fingerprint<128>, size()==2; we write 64 bits in out[0] and
154    *                                  64 bits in out[1].
155    */
156   void write(uint64_t* out) const {
157     for (int i = 0; i < size(); i++) {
158       out[i] = fp_[i];
159     }
160   }
161
162  private:
163   // XOR the fingerprint with a value from one of the tables.
164   void xortab(const uint64_t* tab) {
165     for (int i = 0; i < size(); i++) {
166       fp_[i] ^= tab[i];
167     }
168   }
169
170   // Helper functions: shift the fingerprint value left by 8/32/64 bits,
171   // return the "out" value (the bits that were shifted out), and add "v"
172   // in the bits on the right.
173   uint8_t  shlor8(uint8_t v);
174   uint32_t shlor32(uint32_t v);
175   uint64_t shlor64(uint64_t v);
176
177   uint64_t fp_[1 + (BITS-1)/64];
178 };
179
180 // Convenience functions
181
182 /**
183  * Return the 64-bit Rabin fingerprint of a string.
184  */
185 inline uint64_t fingerprint64(StringPiece str) {
186   uint64_t fp;
187   Fingerprint<64>().update(str).write(&fp);
188   return fp;
189 }
190
191 /**
192  * Compute the 96-bit Rabin fingerprint of a string.
193  * Return the 64 most significant bits in *msb, and the 32 least significant
194  * bits in *lsb.
195  */
196 inline void fingerprint96(StringPiece str,
197                           uint64_t* msb, uint32_t* lsb) {
198   uint64_t fp[2];
199   Fingerprint<96>().update(str).write(fp);
200   *msb = fp[0];
201   *lsb = (uint32_t)(fp[1] >> 32);
202 }
203
204 /**
205  * Compute the 128-bit Rabin fingerprint of a string.
206  * Return the 64 most significant bits in *msb, and the 64 least significant
207  * bits in *lsb.
208  */
209 inline void fingerprint128(StringPiece str,
210                            uint64_t* msb, uint64_t* lsb) {
211   uint64_t fp[2];
212   Fingerprint<128>().update(str).write(fp);
213   *msb = fp[0];
214   *lsb = fp[1];
215 }
216
217
218 template <>
219 inline uint8_t Fingerprint<64>::shlor8(uint8_t v) {
220   uint8_t out = (uint8_t)(fp_[0] >> 56);
221   fp_[0] = (fp_[0] << 8) | ((uint64_t)v);
222   return out;
223 }
224
225 template <>
226 inline uint32_t Fingerprint<64>::shlor32(uint32_t v) {
227   uint32_t out = (uint32_t)(fp_[0] >> 32);
228   fp_[0] = (fp_[0] << 32) | ((uint64_t)v);
229   return out;
230 }
231
232 template <>
233 inline uint64_t Fingerprint<64>::shlor64(uint64_t v) {
234   uint64_t out = fp_[0];
235   fp_[0] = v;
236   return out;
237 }
238
239 template <>
240 inline uint8_t Fingerprint<96>::shlor8(uint8_t v) {
241   uint8_t out = (uint8_t)(fp_[0] >> 56);
242   fp_[0] = (fp_[0] << 8) | (fp_[1] >> 56);
243   fp_[1] = (fp_[1] << 8) | ((uint64_t)v << 32);
244   return out;
245 }
246
247 template <>
248 inline uint32_t Fingerprint<96>::shlor32(uint32_t v) {
249   uint32_t out = (uint32_t)(fp_[0] >> 32);
250   fp_[0] = (fp_[0] << 32) | (fp_[1] >> 32);
251   fp_[1] = ((uint64_t)v << 32);
252   return out;
253 }
254
255 template <>
256 inline uint64_t Fingerprint<96>::shlor64(uint64_t v) {
257   uint64_t out = fp_[0];
258   fp_[0] = fp_[1] | (v >> 32);
259   fp_[1] = v << 32;
260   return out;
261 }
262
263 template <>
264 inline uint8_t Fingerprint<128>::shlor8(uint8_t v) {
265   uint8_t out = (uint8_t)(fp_[0] >> 56);
266   fp_[0] = (fp_[0] << 8) | (fp_[1] >> 56);
267   fp_[1] = (fp_[1] << 8) | ((uint64_t)v);
268   return out;
269 }
270
271 template <>
272 inline uint32_t Fingerprint<128>::shlor32(uint32_t v) {
273   uint32_t out = (uint32_t)(fp_[0] >> 32);
274   fp_[0] = (fp_[0] << 32) | (fp_[1] >> 32);
275   fp_[1] = (fp_[1] << 32) | ((uint64_t)v);
276   return out;
277 }
278
279 template <>
280 inline uint64_t Fingerprint<128>::shlor64(uint64_t v) {
281   uint64_t out = fp_[0];
282   fp_[0] = fp_[1];
283   fp_[1] = v;
284   return out;
285 }
286
287 }  // namespace folly