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