A Discrete-Event Network Simulator
API
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  * Code changes from the FNV distribution are marked with `//PDB'
30  * In addition comment blocks have been converted to Doxygen format.
31  */
32 
33 #include <sys/types.h>
34 #include <stdlib.h>
35 
36 #include "log.h"
37 #include "hash-fnv.h"
38 
46 namespace ns3 {
47 
48 NS_LOG_COMPONENT_DEFINE ("Hash-Fnv");
49 
50 namespace Hash {
51 
52 namespace Function {
53 
55 namespace Fnv1aImplementation {
56 
57 /*************************************************
58  ** class FnvHashImplementation
59  ************************************************/
60 
67 extern "C" {
68 
69 // Changes from FNV distribution are marked with `//PDB'
70 //
71 
72 /* Begin fnv.h -------------- *NS_CHECK_STYLE_OFF* ----> */
73 
74 /*
75  * fnv - Fowler/Noll/Vo- hash code
76  *
77  * @(#) $Revision: 5.4 $
78  * @(#) $Id: fnv.h,v 5.4 2009/07/30 22:49:13 chongo Exp $
79  * @(#) $Source: /usr/local/src/cmd/fnv/RCS/fnv.h,v $
80  *
81  ***
82  *
83  * Fowler/Noll/Vo- hash
84  *
85  * The basis of this hash algorithm was taken from an idea sent
86  * as reviewer comments to the IEEE POSIX P1003.2 committee by:
87  *
88  * Phong Vo (http://www.research.att.com/info/kpv/)
89  * Glenn Fowler (http://www.research.att.com/~gsf/)
90  *
91  * In a subsequent ballot round:
92  *
93  * Landon Curt Noll (http://www.isthe.com/chongo/)
94  *
95  * improved on their algorithm. Some people tried this hash
96  * and found that it worked rather well. In an EMail message
97  * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
98  *
99  * FNV hashes are designed to be fast while maintaining a low
100  * collision rate. The FNV speed allows one to quickly hash lots
101  * of data while maintaining a reasonable collision rate. See:
102  *
103  * http://www.isthe.com/chongo/tech/comp/fnv/index.html
104  *
105  * for more details as well as other forms of the FNV hash.
106  *
107  ***
108  *
109  * NOTE: The FNV-0 historic hash is not recommended. One should use
110  * the FNV-1 hash instead.
111  *
112  * To use the 32 bit FNV-0 historic hash, pass FNV0_32_INIT as the
113  * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
114  *
115  * To use the 64 bit FNV-0 historic hash, pass FNV0_64_INIT as the
116  * Fnv64_t hashval argument to fnv_64_buf() or fnv_64_str().
117  *
118  * To use the recommended 32 bit FNV-1 hash, pass FNV1_32_INIT as the
119  * Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
120  *
121  * To use the recommended 64 bit FNV-1 hash, pass FNV1_64_INIT as the
122  * Fnv64_t hashval argument to fnv_64_buf() or fnv_64_str().
123  *
124  * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
125  * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
126  *
127  * To use the recommended 64 bit FNV-1a hash, pass FNV1A_64_INIT as the
128  * Fnv64_t hashval argument to fnv_64a_buf() or fnv_64a_str().
129  *
130  ***
131  *
132  * Please do not copyright this code. This code is in the public domain.
133  *
134  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
135  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
136  * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
137  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
138  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
139  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
140  * PERFORMANCE OF THIS SOFTWARE.
141  *
142  * By:
143  * chongo <Landon Curt Noll> /\oo/\
144  * http://www.isthe.com/chongo/
145  *
146  * Share and Enjoy! :-)
147  */
148 
149 #if !defined(__FNV_H__)
151 #define __FNV_H__
152 
153 
154 //#include <sys/types.h> //PDB
155 
156 #define FNV_VERSION "5.0.2"
162 typedef uint32_t Fnv32_t; //PDB
163 
164 
171 // Use fully qualified type so this define works outside this scope //PDB
172 #define FNV0_32_INIT ((Fnv1aImplementation::Fnv32_t)0)
173 
174 
187 // Use fully qualified type so this define works outside this scope //PDB
188 #define FNV1_32_INIT ((Fnv1aImplementation::Fnv32_t)0x811c9dc5)
190 #define FNV1_32A_INIT FNV1_32_INIT
191 
192 
196 //#include "longlong.h" //PDB - assume `unsigned long long' is 64 bit
197 #define HAVE_64BIT_LONG_LONG
198 
199 
200 
204 #if defined(HAVE_64BIT_LONG_LONG)
205 typedef uint64_t Fnv64_t; //PDB
206 #else /* HAVE_64BIT_LONG_LONG */
207 typedef struct {
208  uint32_t w32[2]; /* w32[0] is low order, w32[1] is high order word */ //PDB
209 } Fnv64_t;
210 #endif /* HAVE_64BIT_LONG_LONG */
211 
212 
219 // Use fully qualified type so this define works outside this scope //PDB
220 #if defined(HAVE_64BIT_LONG_LONG)
221 #define FNV0_64_INIT ((Fnv1aImplementation::Fnv64_t)0)
222 #else /* HAVE_64BIT_LONG_LONG */
223 extern const Fnv64_t fnv0_64_init;
224 #define FNV0_64_INIT (Fnv1aImplementation::fnv0_64_init)
225 #endif /* HAVE_64BIT_LONG_LONG */
226 
227 
240 #if defined(HAVE_64BIT_LONG_LONG)
241 #define FNV1_64_INIT ((Fnv1aImplementation::Fnv64_t)0xcbf29ce484222325ULL)
243 #define FNV1A_64_INIT FNV1_64_INIT
244 #else /* HAVE_64BIT_LONG_LONG */
245 extern const fnv1_64_init;
246 extern const Fnv64_t fnv1a_64_init;
247 #define FNV1_64_INIT (fnv1_64_init)
249 #define FNV1A_64_INIT (fnv1a_64_init)
250 #endif /* HAVE_64BIT_LONG_LONG */
251 
252 
256 enum fnv_type {
257  FNV_NONE = 0,
258  FNV0_32 = 1,
259  FNV1_32 = 2,
260  FNV1a_32 = 3,
261  FNV0_64 = 4,
262  FNV1_64 = 5,
263  FNV1a_64 = 6,
264 };
265 
266 //PDB test vector declarations deleted
267 
268 /*
269  * external functions //PDB converted to forward declarations
270  */
272 /* extern */ Fnv32_t fnv_32_buf(void *buf, size_t len, Fnv32_t hval);
274 /* extern */ Fnv32_t fnv_32_str(char *str, Fnv32_t hval);
275 
276 /* hash_32a.c */
277 /* extern */ Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hashval);
278 /* extern */ Fnv32_t fnv_32a_str(char *buf, Fnv32_t hashval);
279 
280 /* hash_64.c */
282 /* extern */ Fnv64_t fnv_64_buf(void *buf, size_t len, Fnv64_t hval);
284 /* extern */ Fnv64_t fnv_64_str(char *str, Fnv64_t hval);
285 
286 /* hash_64a.c */
287 /* extern */ Fnv64_t fnv_64a_buf(void *buf, size_t len, Fnv64_t hashval);
288 /* extern */ Fnv64_t fnv_64a_str(char *buf, Fnv64_t hashval);
289 
290 //PDB test vector declarations deleted
291 
292 
293 #endif /* __FNV_H__ */
294 
295 /* End fnv.h ---------------- *NS_CHECK_STYLE_ON* -----> */
296 
297 /* Begin hash_32a.c --------- *NS_CHECK_STYLE_OFF* ----> */
298 
299 /*
300  * hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code
301  *
302  * @(#) $Revision: 5.1 $
303  * @(#) $Id: hash_32a.c,v 5.1 2009/06/30 09:13:32 chongo Exp $
304  * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $
305  *
306  ***
307  *
308  * Fowler/Noll/Vo hash
309  *
310  * The basis of this hash algorithm was taken from an idea sent
311  * as reviewer comments to the IEEE POSIX P1003.2 committee by:
312  *
313  * Phong Vo (http://www.research.att.com/info/kpv/)
314  * Glenn Fowler (http://www.research.att.com/~gsf/)
315  *
316  * In a subsequent ballot round:
317  *
318  * Landon Curt Noll (http://www.isthe.com/chongo/)
319  *
320  * improved on their algorithm. Some people tried this hash
321  * and found that it worked rather well. In an EMail message
322  * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
323  *
324  * FNV hashes are designed to be fast while maintaining a low
325  * collision rate. The FNV speed allows one to quickly hash lots
326  * of data while maintaining a reasonable collision rate. See:
327  *
328  * http://www.isthe.com/chongo/tech/comp/fnv/index.html
329  *
330  * for more details as well as other forms of the FNV hash.
331  ***
332  *
333  * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
334  * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
335  *
336  ***
337  *
338  * Please do not copyright this code. This code is in the public domain.
339  *
340  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
341  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
342  * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
343  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
344  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
345  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
346  * PERFORMANCE OF THIS SOFTWARE.
347  *
348  * By:
349  * chongo <Landon Curt Noll> /\oo/\
350  * http://www.isthe.com/chongo/
351  *
352  * Share and Enjoy! :-)
353  */
354 
355 //#include <stdlib.h> //PDB
356 //#include "fnv.h" //PDB
357 
358 
362 #define FNV_32_PRIME ((Fnv1aImplementation::Fnv32_t)0x01000193)
363 
364 
378 Fnv32_t
379 fnv_32a_buf(void *buf, size_t len, Fnv32_t hval)
380 {
381  unsigned char *bp = (unsigned char *)buf; /* start of buffer */
382  unsigned char *be = bp + len; /* beyond end of buffer */
383 
384  /*
385  * FNV-1a hash each octet in the buffer
386  */
387  while (bp < be) {
388 
389  /* xor the bottom with the current octet */
390  hval ^= (Fnv32_t)*bp++;
391 
392  /* multiply by the 32 bit FNV magic prime mod 2^32 */
393 #if defined(NO_FNV_GCC_OPTIMIZATION)
394  hval *= FNV_32_PRIME;
395 #else
396  hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
397 #endif
398  }
399 
400  /* return our new hash value */
401  return hval;
402 }
403 
404 
417 Fnv32_t
418 fnv_32a_str(char *str, Fnv32_t hval)
419 {
420  unsigned char *s = (unsigned char *)str; /* unsigned string */
421 
422  /*
423  * FNV-1a hash each octet in the buffer
424  */
425  while (*s) {
426 
427  /* xor the bottom with the current octet */
428  hval ^= (Fnv32_t)*s++;
429 
430  /* multiply by the 32 bit FNV magic prime mod 2^32 */
431 #if defined(NO_FNV_GCC_OPTIMIZATION)
432  hval *= FNV_32_PRIME;
433 #else
434  hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
435 #endif
436  }
437 
438  /* return our new hash value */
439  return hval;
440 }
441 
442 /* End hash_32a.c ----------- *NS_CHECK_STYLE_ON* -----> */
443 
444 /* Begin hash_64a.c --------- *NS_CHECK_STYLE_OFF* ----> */
445 
446 /*
447  * hash_64 - 64 bit Fowler/Noll/Vo-0 FNV-1a hash code
448  *
449  * @(#) $Revision: 5.1 $
450  * @(#) $Id: hash_64a.c,v 5.1 2009/06/30 09:01:38 chongo Exp $
451  * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_64a.c,v $
452  *
453  ***
454  *
455  * Fowler/Noll/Vo hash
456  *
457  * The basis of this hash algorithm was taken from an idea sent
458  * as reviewer comments to the IEEE POSIX P1003.2 committee by:
459  *
460  * Phong Vo (http://www.research.att.com/info/kpv/)
461  * Glenn Fowler (http://www.research.att.com/~gsf/)
462  *
463  * In a subsequent ballot round:
464  *
465  * Landon Curt Noll (http://www.isthe.com/chongo/)
466  *
467  * improved on their algorithm. Some people tried this hash
468  * and found that it worked rather well. In an EMail message
469  * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
470  *
471  * FNV hashes are designed to be fast while maintaining a low
472  * collision rate. The FNV speed allows one to quickly hash lots
473  * of data while maintaining a reasonable collision rate. See:
474  *
475  * http://www.isthe.com/chongo/tech/comp/fnv/index.html
476  *
477  * for more details as well as other forms of the FNV hash.
478  *
479  ***
480  *
481  * To use the recommended 64 bit FNV-1a hash, pass FNV1A_64_INIT as the
482  * Fnv64_t hashval argument to fnv_64a_buf() or fnv_64a_str().
483  *
484  ***
485  *
486  * Please do not copyright this code. This code is in the public domain.
487  *
488  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
489  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
490  * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
491  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
492  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
493  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
494  * PERFORMANCE OF THIS SOFTWARE.
495  *
496  * By:
497  * chongo <Landon Curt Noll> /\oo/\
498  * http://www.isthe.com/chongo/
499  *
500  * Share and Enjoy! :-)
501  */
502 
503 //#include <stdlib.h> //PDB
504 //#include "fnv.h" //PDB
505 
506 
510 #if !defined(HAVE_64BIT_LONG_LONG)
511 const Fnv64_t fnv1a_64_init = { 0x84222325, 0xcbf29ce4 };
512 #endif /* ! HAVE_64BIT_LONG_LONG */
513 
514 
519 #if defined(HAVE_64BIT_LONG_LONG)
520 #define FNV_64_PRIME ((Fnv1aImplementation::Fnv64_t)0x100000001b3ULL)
521 #else /* HAVE_64BIT_LONG_LONG */
522 #define FNV_64_PRIME_LOW ((unsigned long)0x1b3) /* lower bits of FNV prime */
523 #define FNV_64_PRIME_SHIFT (8) /* top FNV prime shift above 2^32 */
524 #endif /* HAVE_64BIT_LONG_LONG */
541 Fnv64_t
542 fnv_64a_buf(void *buf, size_t len, Fnv64_t hval)
543 {
544  unsigned char *bp = (unsigned char *)buf; /* start of buffer */
545  unsigned char *be = bp + len; /* beyond end of buffer */
546 
547 #if defined(HAVE_64BIT_LONG_LONG)
548  /*
549  * FNV-1a hash each octet of the buffer
550  */
551  while (bp < be) {
552 
553  /* xor the bottom with the current octet */
554  hval ^= (Fnv64_t)*bp++;
555 
556  /* multiply by the 64 bit FNV magic prime mod 2^64 */
557 #if defined(NO_FNV_GCC_OPTIMIZATION)
558  hval *= FNV_64_PRIME;
559 #else /* NO_FNV_GCC_OPTIMIZATION */
560  hval += (hval << 1) + (hval << 4) + (hval << 5) +
561  (hval << 7) + (hval << 8) + (hval << 40);
562 #endif /* NO_FNV_GCC_OPTIMIZATION */
563  }
564 
565 #else /* HAVE_64BIT_LONG_LONG */
566 
567  unsigned long val[4]; /* hash value in base 2^16 */
568  unsigned long tmp[4]; /* tmp 64 bit value */
569 
570  /*
571  * Convert Fnv64_t hval into a base 2^16 array
572  */
573  val[0] = hval.w32[0];
574  val[1] = (val[0] >> 16);
575  val[0] &= 0xffff;
576  val[2] = hval.w32[1];
577  val[3] = (val[2] >> 16);
578  val[2] &= 0xffff;
579 
580  /*
581  * FNV-1a hash each octet of the buffer
582  */
583  while (bp < be) {
584 
585  /* xor the bottom with the current octet */
586  val[0] ^= (unsigned long)*bp++;
587 
588  /*
589  * multiply by the 64 bit FNV magic prime mod 2^64
590  *
591  * Using 0x100000001b3 we have the following digits base 2^16:
592  *
593  * 0x0 0x100 0x0 0x1b3
594  *
595  * which is the same as:
596  *
597  * 0x0 1<<FNV_64_PRIME_SHIFT 0x0 FNV_64_PRIME_LOW
598  */
599  /* multiply by the lowest order digit base 2^16 */
600  tmp[0] = val[0] * FNV_64_PRIME_LOW;
601  tmp[1] = val[1] * FNV_64_PRIME_LOW;
602  tmp[2] = val[2] * FNV_64_PRIME_LOW;
603  tmp[3] = val[3] * FNV_64_PRIME_LOW;
604  /* multiply by the other non-zero digit */
605  tmp[2] += val[0] << FNV_64_PRIME_SHIFT; /* tmp[2] += val[0] * 0x100 */
606  tmp[3] += val[1] << FNV_64_PRIME_SHIFT; /* tmp[3] += val[1] * 0x100 */
607  /* propagate carries */
608  tmp[1] += (tmp[0] >> 16);
609  val[0] = tmp[0] & 0xffff;
610  tmp[2] += (tmp[1] >> 16);
611  val[1] = tmp[1] & 0xffff;
612  val[3] = tmp[3] + (tmp[2] >> 16);
613  val[2] = tmp[2] & 0xffff;
614  /*
615  * Doing a val[3] &= 0xffff; is not really needed since it simply
616  * removes multiples of 2^64. We can discard these excess bits
617  * outside of the loop when we convert to Fnv64_t.
618  */
619  }
620 
621  /*
622  * Convert base 2^16 array back into an Fnv64_t
623  */
624  hval.w32[1] = ((val[3]<<16) | val[2]);
625  hval.w32[0] = ((val[1]<<16) | val[0]);
626 
627 #endif /* HAVE_64BIT_LONG_LONG */
628 
629  /* return our new hash value */
630  return hval;
631 }
632 
633 
646 Fnv64_t
647 fnv_64a_str(char *str, Fnv64_t hval)
648 {
649  unsigned char *s = (unsigned char *)str; /* unsigned string */
650 
651 #if defined(HAVE_64BIT_LONG_LONG)
652 
653  /*
654  * FNV-1a hash each octet of the string
655  */
656  while (*s) {
657 
658  /* xor the bottom with the current octet */
659  hval ^= (Fnv64_t)*s++;
660 
661  /* multiply by the 64 bit FNV magic prime mod 2^64 */
662 #if defined(NO_FNV_GCC_OPTIMIZATION)
663  hval *= FNV_64_PRIME;
664 #else /* NO_FNV_GCC_OPTIMIZATION */
665  hval += (hval << 1) + (hval << 4) + (hval << 5) +
666  (hval << 7) + (hval << 8) + (hval << 40);
667 #endif /* NO_FNV_GCC_OPTIMIZATION */
668  }
669 
670 #else /* !HAVE_64BIT_LONG_LONG */
671 
672  unsigned long val[4]; /* hash value in base 2^16 */
673  unsigned long tmp[4]; /* tmp 64 bit value */
674 
675  /*
676  * Convert Fnv64_t hval into a base 2^16 array
677  */
678  val[0] = hval.w32[0];
679  val[1] = (val[0] >> 16);
680  val[0] &= 0xffff;
681  val[2] = hval.w32[1];
682  val[3] = (val[2] >> 16);
683  val[2] &= 0xffff;
684 
685  /*
686  * FNV-1a hash each octet of the string
687  */
688  while (*s) {
689 
690  /* xor the bottom with the current octet */
691 
692  /*
693  * multiply by the 64 bit FNV magic prime mod 2^64
694  *
695  * Using 1099511628211, we have the following digits base 2^16:
696  *
697  * 0x0 0x100 0x0 0x1b3
698  *
699  * which is the same as:
700  *
701  * 0x0 1<<FNV_64_PRIME_SHIFT 0x0 FNV_64_PRIME_LOW
702  */
703  /* multiply by the lowest order digit base 2^16 */
704  tmp[0] = val[0] * FNV_64_PRIME_LOW;
705  tmp[1] = val[1] * FNV_64_PRIME_LOW;
706  tmp[2] = val[2] * FNV_64_PRIME_LOW;
707  tmp[3] = val[3] * FNV_64_PRIME_LOW;
708  /* multiply by the other non-zero digit */
709  tmp[2] += val[0] << FNV_64_PRIME_SHIFT; /* tmp[2] += val[0] * 0x100 */
710  tmp[3] += val[1] << FNV_64_PRIME_SHIFT; /* tmp[3] += val[1] * 0x100 */
711  /* propagate carries */
712  tmp[1] += (tmp[0] >> 16);
713  val[0] = tmp[0] & 0xffff;
714  tmp[2] += (tmp[1] >> 16);
715  val[1] = tmp[1] & 0xffff;
716  val[3] = tmp[3] + (tmp[2] >> 16);
717  val[2] = tmp[2] & 0xffff;
718  /*
719  * Doing a val[3] &= 0xffff; is not really needed since it simply
720  * removes multiples of 2^64. We can discard these excess bits
721  * outside of the loop when we convert to Fnv64_t.
722  */
723  val[0] ^= (unsigned long)(*s++);
724  }
725 
726  /*
727  * Convert base 2^16 array back into an Fnv64_t
728  */
729  hval.w32[1] = ((val[3]<<16) | val[2]);
730  hval.w32[0] = ((val[1]<<16) | val[0]);
731 
732 #endif /* !HAVE_64BIT_LONG_LONG */
733 
734  /* return our new hash value */
735  return hval;
736 }
737 
738 /* End hash_64a.c ----------- *NS_CHECK_STYLE_ON* -----> */
739 
740 } /* extern "C" */
741 
742 //-----------------------------------------------------------------------------
743  // \defgroup hash_fnv
745 
746 } // namespace Fnv1aImplementation
747 
748 
750 {
751  clear ();
752 }
753 
754 uint32_t
755 Fnv1a::GetHash32 (const char * buffer, const std::size_t size)
756 {
757  m_hash32 =
758  Fnv1aImplementation::fnv_32a_buf ((void *)buffer, size, m_hash32);
759  return m_hash32;
760 }
761 
762 uint64_t
763 Fnv1a::GetHash64 (const char * buffer, const std::size_t size)
764 {
765  m_hash64 =
766  Fnv1aImplementation::fnv_64a_buf ((void *)buffer, size, m_hash64);
767  return m_hash64;
768 }
769 
770 void
772 {
775 }
776 
777 } // namespace Function
778 
779 } // namespace Hash
780 
781 } // namespace ns3
uint64_t GetHash64(const char *buffer, const size_t size)
Compute 64-bit hash of a byte buffer.
Definition: hash-fnv.cc:763
uint32_t m_hash32
Cache last hash value, for incremental hashing.
Definition: hash-fnv.h:105
virtual void clear(void)
Restore initial state.
Definition: hash-fnv.cc:771
uint64_t m_hash64
Cache last hash value, for incremental hashing.
Definition: hash-fnv.h:106
uint32_t GetHash32(const char *buffer, const size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash-fnv.cc:755
uint64_t Fnv64_t
64 bit FNV-0 hash
Definition: hash-fnv.cc:205
#define FNV1A_64_INIT
64 bit FNV-1 non-zero initial basis
Definition: hash-fnv.cc:243
#define FNV1_32A_INIT
32 bit FNV-1 and FNV-1a non-zero initial basis
Definition: hash-fnv.cc:190
Fnv32_t fnv_32_str(char *str, Fnv32_t hval)
fnv_32a_str - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a string
Fnv64_t fnv_64_str(char *str, Fnv64_t hval)
fnv_64a_str - perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
Fnv32_t fnv_32a_str(char *buf, Fnv32_t hashval)
fnv_32a_str - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a string
Definition: hash-fnv.cc:418
#define FNV_64_PRIME
FNV-1a defines the initial basis to be non-zero.
Definition: hash-fnv.cc:520
uint32_t Fnv32_t
32 bit FNV-0 hash type
Definition: hash-fnv.cc:162
Fnv64_t fnv_64_buf(void *buf, size_t len, Fnv64_t hval)
fnv_64a_buf - perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
Fnv64_t fnv_64a_str(char *buf, Fnv64_t hashval)
fnv_64a_str - perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
Definition: hash-fnv.cc:647
Fnv32_t fnv_32_buf(void *buf, size_t len, Fnv32_t hval)
fnv_32a_buf - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a buffer
#define FNV_32_PRIME
32 bit magic FNV-1a prime
Definition: hash-fnv.cc:362
Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hashval)
fnv_32a_buf - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a buffer
Definition: hash-fnv.cc:379
Fnv64_t fnv_64a_buf(void *buf, size_t len, Fnv64_t hashval)
fnv_64a_buf - perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
Definition: hash-fnv.cc:542
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::Hash::Function::Fnv1a declaration.
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.