A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hash-fnv.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 Lawrence Livermore National Laboratory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19  *
20  * This copyright notice applies strictly to the wrapper material.
21  *
22  * The FNV source code itself is in the public domain. The FNV source
23  * code sections are marked by
24  * // Begin <fnv-file> ---->
25  * and
26  * // End <fnv-file> ---->
27  * comments.
28  *
29  * Changes from the FNV distribution are marked with `//PDB'
30  */
31 
32 #include <sys/types.h>
33 #include <stdlib.h>
34 
35 #include "log.h"
36 #include "hash-fnv.h"
37 
38 
39 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("Hash-Fnv");
42 
43 namespace Hash {
44 
45 namespace Function {
46 
47 namespace Fnv1aImplementation {
48 
49 /*************************************************
50  ** class FnvHashImplementation
51  ************************************************/
52 
53 extern "C" {
54 
55 // Changes from FNV distribution are marked with `//PDB'
56 //
57 
58 // Begin fnv.h ----------------------------------->
59 
60 /*
61  * fnv - Fowler/Noll/Vo- hash code
62  *
63  * @(#) $Revision: 5.4 $
64  * @(#) $Id: fnv.h,v 5.4 2009/07/30 22:49:13 chongo Exp $
65  * @(#) $Source: /usr/local/src/cmd/fnv/RCS/fnv.h,v $
66  *
67  ***
68  *
69  * Fowler/Noll/Vo- hash
70  *
71  * The basis of this hash algorithm was taken from an idea sent
72  * as reviewer comments to the IEEE POSIX P1003.2 committee by:
73  *
74  * Phong Vo (http://www.research.att.com/info/kpv/)
75  * Glenn Fowler (http://www.research.att.com/~gsf/)
76  *
77  * In a subsequent ballot round:
78  *
79  * Landon Curt Noll (http://www.isthe.com/chongo/)
80  *
81  * improved on their algorithm. Some people tried this hash
82  * and found that it worked rather well. In an EMail message
83  * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
84  *
85  * FNV hashes are designed to be fast while maintaining a low
86  * collision rate. The FNV speed allows one to quickly hash lots
87  * of data while maintaining a reasonable collision rate. See:
88  *
89  * http://www.isthe.com/chongo/tech/comp/fnv/index.html
90  *
91  * for more details as well as other forms of the FNV hash.
92  *
93  ***
94  *
95  * NOTE: The FNV-0 historic hash is not recommended. One should use
96  * the FNV-1 hash instead.
97  *
98  * To use the 32 bit FNV-0 historic hash, pass FNV0_32_INIT as the
99  * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
100  *
101  * To use the 64 bit FNV-0 historic hash, pass FNV0_64_INIT as the
102  * Fnv64_t hashval argument to fnv_64_buf() or fnv_64_str().
103  *
104  * To use the recommended 32 bit FNV-1 hash, pass FNV1_32_INIT as the
105  * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
106  *
107  * To use the recommended 64 bit FNV-1 hash, pass FNV1_64_INIT as the
108  * Fnv64_t hashval argument to fnv_64_buf() or fnv_64_str().
109  *
110  * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
111  * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
112  *
113  * To use the recommended 64 bit FNV-1a hash, pass FNV1A_64_INIT as the
114  * Fnv64_t hashval argument to fnv_64a_buf() or fnv_64a_str().
115  *
116  ***
117  *
118  * Please do not copyright this code. This code is in the public domain.
119  *
120  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
121  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
122  * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
123  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
124  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
125  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
126  * PERFORMANCE OF THIS SOFTWARE.
127  *
128  * By:
129  * chongo <Landon Curt Noll> /\oo/\
130  * http://www.isthe.com/chongo/
131  *
132  * Share and Enjoy! :-)
133  */
134 
135 #if !defined(__FNV_H__)
136 #define __FNV_H__
137 
138 
139 //#include <sys/types.h> //PDB
140 
141 #define FNV_VERSION "5.0.2" /* @(#) FNV Version */
142 
143 
144 /*
145  * 32 bit FNV-0 hash type
146  */
147 typedef u_int32_t Fnv32_t;
148 
149 
150 /*
151  * 32 bit FNV-0 zero initial basis
152  *
153  * This historic hash is not recommended. One should use
154  * the FNV-1 hash and initial basis instead.
155  *
156  * Use fully qualified type so this define works outside this scope //PDB
157  */
158 #define FNV0_32_INIT ((Fnv1aImplementation::Fnv32_t)0)
159 
160 
161 /*
162  * 32 bit FNV-1 and FNV-1a non-zero initial basis
163  *
164  * The FNV-1 initial basis is the FNV-0 hash of the following 32 octets:
165  *
166  * chongo <Landon Curt Noll> /\../\
167  *
168  * NOTE: The \'s above are not back-slashing escape characters.
169  * They are literal ASCII backslash 0x5c characters.
170  *
171  * NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
172  *
173  * Use fully qualified type so this define works outside this scope //PDB
174  */
175 #define FNV1_32_INIT ((Fnv1aImplementation::Fnv32_t)0x811c9dc5)
176 #define FNV1_32A_INIT FNV1_32_INIT
177 
178 
179 /*
180  * determine how 64 bit unsigned values are represented
181  */
182 //#include "longlong.h" //PDB - assume `unsigned long long' is 64 bit
183 #define HAVE_64BIT_LONG_LONG
184 
185 
186 
187 /*
188  * 64 bit FNV-0 hash
189  */
190 #if defined(HAVE_64BIT_LONG_LONG)
191 typedef u_int64_t Fnv64_t;
192 #else /* HAVE_64BIT_LONG_LONG */
193 typedef struct {
194  u_int32_t w32[2]; /* w32[0] is low order, w32[1] is high order word */
195 } Fnv64_t;
196 #endif /* HAVE_64BIT_LONG_LONG */
197 
198 
199 /*
200  * 64 bit FNV-0 zero initial basis
201  *
202  * This historic hash is not recommended. One should use
203  * the FNV-1 hash and initial basis instead.
204  *
205  * Use fully qualified type so this define works outside this scope //PDB
206  */
207 #if defined(HAVE_64BIT_LONG_LONG)
208 #define FNV0_64_INIT ((Fnv1aImplementation::Fnv64_t)0)
209 #else /* HAVE_64BIT_LONG_LONG */
210 extern const Fnv64_t fnv0_64_init;
211 #define FNV0_64_INIT (Fnv1aImplementation::fnv0_64_init)
212 #endif /* HAVE_64BIT_LONG_LONG */
213 
214 
215 /*
216  * 64 bit FNV-1 non-zero initial basis
217  *
218  * The FNV-1 initial basis is the FNV-0 hash of the following 32 octets:
219  *
220  * chongo <Landon Curt Noll> /\../\
221  *
222  * NOTE: The \'s above are not back-slashing escape characters.
223  * They are literal ASCII backslash 0x5c characters.
224  *
225  * NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
226  */
227 #if defined(HAVE_64BIT_LONG_LONG)
228 #define FNV1_64_INIT ((Fnv1aImplementation::Fnv64_t)0xcbf29ce484222325ULL)
229 #define FNV1A_64_INIT FNV1_64_INIT
230 #else /* HAVE_64BIT_LONG_LONG */
231 extern const fnv1_64_init;
232 extern const Fnv64_t fnv1a_64_init;
233 #define FNV1_64_INIT (fnv1_64_init)
234 #define FNV1A_64_INIT (fnv1a_64_init)
235 #endif /* HAVE_64BIT_LONG_LONG */
236 
237 
238 /*
239  * hash types
240  */
241 enum fnv_type {
242  FNV_NONE = 0, /* invalid FNV hash type */
243  FNV0_32 = 1, /* FNV-0 32 bit hash */
244  FNV1_32 = 2, /* FNV-1 32 bit hash */
245  FNV1a_32 = 3, /* FNV-1a 32 bit hash */
246  FNV0_64 = 4, /* FNV-0 64 bit hash */
247  FNV1_64 = 5, /* FNV-1 64 bit hash */
248  FNV1a_64 = 6, /* FNV-1a 64 bit hash */
249 };
250 
251 //PDB test vector declarations deleted
252 
253 /*
254  * external functions //PDB converted to forward declarations
255  */
256 /* hash_32.c */
257 /* extern */ Fnv32_t fnv_32_buf(void *buf, size_t len, Fnv32_t hashval);
258 /* extern */ Fnv32_t fnv_32_str(char *buf, Fnv32_t hashval);
259 
260 /* hash_32a.c */
261 /* extern */ Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hashval);
262 /* extern */ Fnv32_t fnv_32a_str(char *buf, Fnv32_t hashval);
263 
264 /* hash_64.c */
265 /* extern */ Fnv64_t fnv_64_buf(void *buf, size_t len, Fnv64_t hashval);
266 /* extern */ Fnv64_t fnv_64_str(char *buf, Fnv64_t hashval);
267 
268 /* hash_64a.c */
269 /* extern */ Fnv64_t fnv_64a_buf(void *buf, size_t len, Fnv64_t hashval);
270 /* extern */ Fnv64_t fnv_64a_str(char *buf, Fnv64_t hashval);
271 
272 //PDB test vector declarations deleted
273 
274 
275 #endif /* __FNV_H__ */
276 
277 // End fnv.h ------------------------------->
278 
279 // Begin hash_32a.c ------------------------------>
280 
281 /*
282  * hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code
283  *
284  * @(#) $Revision: 5.1 $
285  * @(#) $Id: hash_32a.c,v 5.1 2009/06/30 09:13:32 chongo Exp $
286  * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $
287  *
288  ***
289  *
290  * Fowler/Noll/Vo hash
291  *
292  * The basis of this hash algorithm was taken from an idea sent
293  * as reviewer comments to the IEEE POSIX P1003.2 committee by:
294  *
295  * Phong Vo (http://www.research.att.com/info/kpv/)
296  * Glenn Fowler (http://www.research.att.com/~gsf/)
297  *
298  * In a subsequent ballot round:
299  *
300  * Landon Curt Noll (http://www.isthe.com/chongo/)
301  *
302  * improved on their algorithm. Some people tried this hash
303  * and found that it worked rather well. In an EMail message
304  * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
305  *
306  * FNV hashes are designed to be fast while maintaining a low
307  * collision rate. The FNV speed allows one to quickly hash lots
308  * of data while maintaining a reasonable collision rate. See:
309  *
310  * http://www.isthe.com/chongo/tech/comp/fnv/index.html
311  *
312  * for more details as well as other forms of the FNV hash.
313  ***
314  *
315  * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
316  * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
317  *
318  ***
319  *
320  * Please do not copyright this code. This code is in the public domain.
321  *
322  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
323  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
324  * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
325  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
326  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
327  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
328  * PERFORMANCE OF THIS SOFTWARE.
329  *
330  * By:
331  * chongo <Landon Curt Noll> /\oo/\
332  * http://www.isthe.com/chongo/
333  *
334  * Share and Enjoy! :-)
335  */
336 
337 //#include <stdlib.h> //PDB
338 //#include "fnv.h" //PDB
339 
340 
341 /*
342  * 32 bit magic FNV-1a prime
343  */
344 #define FNV_32_PRIME ((Fnv1aImplementation::Fnv32_t)0x01000193)
345 
346 
347 /*
348  * fnv_32a_buf - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a buffer
349  *
350  * input:
351  * buf - start of buffer to hash
352  * len - length of buffer in octets
353  * hval - previous hash value or 0 if first call
354  *
355  * returns:
356  * 32 bit hash as a static hash type
357  *
358  * NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the
359  * hval arg on the first call to either fnv_32a_buf() or fnv_32a_str().
360  */
361 Fnv32_t
362 fnv_32a_buf(void *buf, size_t len, Fnv32_t hval)
363 {
364  unsigned char *bp = (unsigned char *)buf; /* start of buffer */
365  unsigned char *be = bp + len; /* beyond end of buffer */
366 
367  /*
368  * FNV-1a hash each octet in the buffer
369  */
370  while (bp < be) {
371 
372  /* xor the bottom with the current octet */
373  hval ^= (Fnv32_t)*bp++;
374 
375  /* multiply by the 32 bit FNV magic prime mod 2^32 */
376 #if defined(NO_FNV_GCC_OPTIMIZATION)
377  hval *= FNV_32_PRIME;
378 #else
379  hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
380 #endif
381  }
382 
383  /* return our new hash value */
384  return hval;
385 }
386 
387 
388 /*
389  * fnv_32a_str - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a string
390  *
391  * input:
392  * str - string to hash
393  * hval - previous hash value or 0 if first call
394  *
395  * returns:
396  * 32 bit hash as a static hash type
397  *
398  * NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the
399  * hval arg on the first call to either fnv_32a_buf() or fnv_32a_str().
400  */
401 Fnv32_t
402 fnv_32a_str(char *str, Fnv32_t hval)
403 {
404  unsigned char *s = (unsigned char *)str; /* unsigned string */
405 
406  /*
407  * FNV-1a hash each octet in the buffer
408  */
409  while (*s) {
410 
411  /* xor the bottom with the current octet */
412  hval ^= (Fnv32_t)*s++;
413 
414  /* multiply by the 32 bit FNV magic prime mod 2^32 */
415 #if defined(NO_FNV_GCC_OPTIMIZATION)
416  hval *= FNV_32_PRIME;
417 #else
418  hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
419 #endif
420  }
421 
422  /* return our new hash value */
423  return hval;
424 }
425 
426 // End hash_32a.c -------------------------->
427 
428 // Begin hash_64a.c------------------------------->
429 
430 /*
431  * hash_64 - 64 bit Fowler/Noll/Vo-0 FNV-1a hash code
432  *
433  * @(#) $Revision: 5.1 $
434  * @(#) $Id: hash_64a.c,v 5.1 2009/06/30 09:01:38 chongo Exp $
435  * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_64a.c,v $
436  *
437  ***
438  *
439  * Fowler/Noll/Vo hash
440  *
441  * The basis of this hash algorithm was taken from an idea sent
442  * as reviewer comments to the IEEE POSIX P1003.2 committee by:
443  *
444  * Phong Vo (http://www.research.att.com/info/kpv/)
445  * Glenn Fowler (http://www.research.att.com/~gsf/)
446  *
447  * In a subsequent ballot round:
448  *
449  * Landon Curt Noll (http://www.isthe.com/chongo/)
450  *
451  * improved on their algorithm. Some people tried this hash
452  * and found that it worked rather well. In an EMail message
453  * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
454  *
455  * FNV hashes are designed to be fast while maintaining a low
456  * collision rate. The FNV speed allows one to quickly hash lots
457  * of data while maintaining a reasonable collision rate. See:
458  *
459  * http://www.isthe.com/chongo/tech/comp/fnv/index.html
460  *
461  * for more details as well as other forms of the FNV hash.
462  *
463  ***
464  *
465  * To use the recommended 64 bit FNV-1a hash, pass FNV1A_64_INIT as the
466  * Fnv64_t hashval argument to fnv_64a_buf() or fnv_64a_str().
467  *
468  ***
469  *
470  * Please do not copyright this code. This code is in the public domain.
471  *
472  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
473  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
474  * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
475  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
476  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
477  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
478  * PERFORMANCE OF THIS SOFTWARE.
479  *
480  * By:
481  * chongo <Landon Curt Noll> /\oo/\
482  * http://www.isthe.com/chongo/
483  *
484  * Share and Enjoy! :-)
485  */
486 
487 //#include <stdlib.h> //PDB
488 //#include "fnv.h" //PDB
489 
490 
491 /*
492  * FNV-1a defines the initial basis to be non-zero
493  */
494 #if !defined(HAVE_64BIT_LONG_LONG)
495 const Fnv64_t fnv1a_64_init = { 0x84222325, 0xcbf29ce4 };
496 #endif /* ! HAVE_64BIT_LONG_LONG */
497 
498 
499 /*
500  * 64 bit magic FNV-1a prime
501  */
502 #if defined(HAVE_64BIT_LONG_LONG)
503 #define FNV_64_PRIME ((Fnv1aImplementation::Fnv64_t)0x100000001b3ULL)
504 #else /* HAVE_64BIT_LONG_LONG */
505 #define FNV_64_PRIME_LOW ((unsigned long)0x1b3) /* lower bits of FNV prime */
506 #define FNV_64_PRIME_SHIFT (8) /* top FNV prime shift above 2^32 */
507 #endif /* HAVE_64BIT_LONG_LONG */
508 
509 
510 /*
511  * fnv_64a_buf - perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
512  *
513  * input:
514  * buf - start of buffer to hash
515  * len - length of buffer in octets
516  * hval - previous hash value or 0 if first call
517  *
518  * returns:
519  * 64 bit hash as a static hash type
520  *
521  * NOTE: To use the recommended 64 bit FNV-1a hash, use FNV1A_64_INIT as the
522  * hval arg on the first call to either fnv_64a_buf() or fnv_64a_str().
523  */
524 Fnv64_t
525 fnv_64a_buf(void *buf, size_t len, Fnv64_t hval)
526 {
527  unsigned char *bp = (unsigned char *)buf; /* start of buffer */
528  unsigned char *be = bp + len; /* beyond end of buffer */
529 
530 #if defined(HAVE_64BIT_LONG_LONG)
531  /*
532  * FNV-1a hash each octet of the buffer
533  */
534  while (bp < be) {
535 
536  /* xor the bottom with the current octet */
537  hval ^= (Fnv64_t)*bp++;
538 
539  /* multiply by the 64 bit FNV magic prime mod 2^64 */
540 #if defined(NO_FNV_GCC_OPTIMIZATION)
541  hval *= FNV_64_PRIME;
542 #else /* NO_FNV_GCC_OPTIMIZATION */
543  hval += (hval << 1) + (hval << 4) + (hval << 5) +
544  (hval << 7) + (hval << 8) + (hval << 40);
545 #endif /* NO_FNV_GCC_OPTIMIZATION */
546  }
547 
548 #else /* HAVE_64BIT_LONG_LONG */
549 
550  unsigned long val[4]; /* hash value in base 2^16 */
551  unsigned long tmp[4]; /* tmp 64 bit value */
552 
553  /*
554  * Convert Fnv64_t hval into a base 2^16 array
555  */
556  val[0] = hval.w32[0];
557  val[1] = (val[0] >> 16);
558  val[0] &= 0xffff;
559  val[2] = hval.w32[1];
560  val[3] = (val[2] >> 16);
561  val[2] &= 0xffff;
562 
563  /*
564  * FNV-1a hash each octet of the buffer
565  */
566  while (bp < be) {
567 
568  /* xor the bottom with the current octet */
569  val[0] ^= (unsigned long)*bp++;
570 
571  /*
572  * multiply by the 64 bit FNV magic prime mod 2^64
573  *
574  * Using 0x100000001b3 we have the following digits base 2^16:
575  *
576  * 0x0 0x100 0x0 0x1b3
577  *
578  * which is the same as:
579  *
580  * 0x0 1<<FNV_64_PRIME_SHIFT 0x0 FNV_64_PRIME_LOW
581  */
582  /* multiply by the lowest order digit base 2^16 */
583  tmp[0] = val[0] * FNV_64_PRIME_LOW;
584  tmp[1] = val[1] * FNV_64_PRIME_LOW;
585  tmp[2] = val[2] * FNV_64_PRIME_LOW;
586  tmp[3] = val[3] * FNV_64_PRIME_LOW;
587  /* multiply by the other non-zero digit */
588  tmp[2] += val[0] << FNV_64_PRIME_SHIFT; /* tmp[2] += val[0] * 0x100 */
589  tmp[3] += val[1] << FNV_64_PRIME_SHIFT; /* tmp[3] += val[1] * 0x100 */
590  /* propagate carries */
591  tmp[1] += (tmp[0] >> 16);
592  val[0] = tmp[0] & 0xffff;
593  tmp[2] += (tmp[1] >> 16);
594  val[1] = tmp[1] & 0xffff;
595  val[3] = tmp[3] + (tmp[2] >> 16);
596  val[2] = tmp[2] & 0xffff;
597  /*
598  * Doing a val[3] &= 0xffff; is not really needed since it simply
599  * removes multiples of 2^64. We can discard these excess bits
600  * outside of the loop when we convert to Fnv64_t.
601  */
602  }
603 
604  /*
605  * Convert base 2^16 array back into an Fnv64_t
606  */
607  hval.w32[1] = ((val[3]<<16) | val[2]);
608  hval.w32[0] = ((val[1]<<16) | val[0]);
609 
610 #endif /* HAVE_64BIT_LONG_LONG */
611 
612  /* return our new hash value */
613  return hval;
614 }
615 
616 
617 /*
618  * fnv_64a_str - perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
619  *
620  * input:
621  * buf - start of buffer to hash
622  * hval - previous hash value or 0 if first call
623  *
624  * returns:
625  * 64 bit hash as a static hash type
626  *
627  * NOTE: To use the recommended 64 bit FNV-1a hash, use FNV1A_64_INIT as the
628  * hval arg on the first call to either fnv_64a_buf() or fnv_64a_str().
629  */
630 Fnv64_t
631 fnv_64a_str(char *str, Fnv64_t hval)
632 {
633  unsigned char *s = (unsigned char *)str; /* unsigned string */
634 
635 #if defined(HAVE_64BIT_LONG_LONG)
636 
637  /*
638  * FNV-1a hash each octet of the string
639  */
640  while (*s) {
641 
642  /* xor the bottom with the current octet */
643  hval ^= (Fnv64_t)*s++;
644 
645  /* multiply by the 64 bit FNV magic prime mod 2^64 */
646 #if defined(NO_FNV_GCC_OPTIMIZATION)
647  hval *= FNV_64_PRIME;
648 #else /* NO_FNV_GCC_OPTIMIZATION */
649  hval += (hval << 1) + (hval << 4) + (hval << 5) +
650  (hval << 7) + (hval << 8) + (hval << 40);
651 #endif /* NO_FNV_GCC_OPTIMIZATION */
652  }
653 
654 #else /* !HAVE_64BIT_LONG_LONG */
655 
656  unsigned long val[4]; /* hash value in base 2^16 */
657  unsigned long tmp[4]; /* tmp 64 bit value */
658 
659  /*
660  * Convert Fnv64_t hval into a base 2^16 array
661  */
662  val[0] = hval.w32[0];
663  val[1] = (val[0] >> 16);
664  val[0] &= 0xffff;
665  val[2] = hval.w32[1];
666  val[3] = (val[2] >> 16);
667  val[2] &= 0xffff;
668 
669  /*
670  * FNV-1a hash each octet of the string
671  */
672  while (*s) {
673 
674  /* xor the bottom with the current octet */
675 
676  /*
677  * multiply by the 64 bit FNV magic prime mod 2^64
678  *
679  * Using 1099511628211, we have the following digits base 2^16:
680  *
681  * 0x0 0x100 0x0 0x1b3
682  *
683  * which is the same as:
684  *
685  * 0x0 1<<FNV_64_PRIME_SHIFT 0x0 FNV_64_PRIME_LOW
686  */
687  /* multiply by the lowest order digit base 2^16 */
688  tmp[0] = val[0] * FNV_64_PRIME_LOW;
689  tmp[1] = val[1] * FNV_64_PRIME_LOW;
690  tmp[2] = val[2] * FNV_64_PRIME_LOW;
691  tmp[3] = val[3] * FNV_64_PRIME_LOW;
692  /* multiply by the other non-zero digit */
693  tmp[2] += val[0] << FNV_64_PRIME_SHIFT; /* tmp[2] += val[0] * 0x100 */
694  tmp[3] += val[1] << FNV_64_PRIME_SHIFT; /* tmp[3] += val[1] * 0x100 */
695  /* propagate carries */
696  tmp[1] += (tmp[0] >> 16);
697  val[0] = tmp[0] & 0xffff;
698  tmp[2] += (tmp[1] >> 16);
699  val[1] = tmp[1] & 0xffff;
700  val[3] = tmp[3] + (tmp[2] >> 16);
701  val[2] = tmp[2] & 0xffff;
702  /*
703  * Doing a val[3] &= 0xffff; is not really needed since it simply
704  * removes multiples of 2^64. We can discard these excess bits
705  * outside of the loop when we convert to Fnv64_t.
706  */
707  val[0] ^= (unsigned long)(*s++);
708  }
709 
710  /*
711  * Convert base 2^16 array back into an Fnv64_t
712  */
713  hval.w32[1] = ((val[3]<<16) | val[2]);
714  hval.w32[0] = ((val[1]<<16) | val[0]);
715 
716 #endif /* !HAVE_64BIT_LONG_LONG */
717 
718  /* return our new hash value */
719  return hval;
720 }
721 
722 // End hash_64a.c--------------------------->
723 
724 } /* extern "C" */
725 
726 //-----------------------------------------------------------------------------
727 
728 
729 } // namespace Fnv1aImplementation
730 
731 
733 {
734  clear ();
735 }
736 
737 uint32_t
738 Fnv1a::GetHash32 (const char * buffer, const size_t size)
739 {
740  m_hash32 =
741  Fnv1aImplementation::fnv_32a_buf ((void *)buffer, size, m_hash32);
742  return m_hash32;
743 }
744 
745 uint64_t
746 Fnv1a::GetHash64 (const char * buffer, const size_t size)
747 {
748  m_hash64 =
749  Fnv1aImplementation::fnv_64a_buf ((void *)buffer, size, m_hash64);
750  return m_hash64;
751 }
752 
753 void
755 {
758 }
759 
760 } // namespace Function
761 
762 } // namespace Hash
763 
764 } // namespace ns3
Fnv64_t fnv_64_str(char *buf, Fnv64_t hashval)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
Fnv32_t fnv_32a_str(char *buf, Fnv32_t hashval)
Definition: hash-fnv.cc:402
uint32_t m_hash32
Cache last hash value, for incremental hashing.
Definition: hash-fnv.h:101
Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hashval)
Definition: hash-fnv.cc:362
uint64_t GetHash64(const char *buffer, const size_t size)
Compute 64-bit hash of a byte buffer.
Definition: hash-fnv.cc:746
Ptr< SampleEmitter > s
Fnv32_t fnv_32_str(char *buf, Fnv32_t hashval)
Fnv32_t fnv_32_buf(void *buf, size_t len, Fnv32_t hashval)
#define FNV1A_64_INIT
Definition: hash-fnv.cc:229
#define FNV_32_PRIME
Definition: hash-fnv.cc:344
Fnv64_t fnv_64_buf(void *buf, size_t len, Fnv64_t hashval)
Fnv64_t fnv_64a_str(char *buf, Fnv64_t hashval)
Definition: hash-fnv.cc:631
uint32_t GetHash32(const char *buffer, const size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash-fnv.cc:738
virtual void clear(void)
Restore initial state.
Definition: hash-fnv.cc:754
#define FNV_64_PRIME
Definition: hash-fnv.cc:503
#define FNV1_32A_INIT
Definition: hash-fnv.cc:176
uint64_t m_hash64
Cache last hash value, for incremental hashing.
Definition: hash-fnv.h:102
Fnv64_t fnv_64a_buf(void *buf, size_t len, Fnv64_t hashval)
Definition: hash-fnv.cc:525
Fnv1a()
Constructor.
Definition: hash-fnv.cc:732