A Discrete-Event Network Simulator
API
hash-murmur3.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 murmur3 source code itself is in the public domain. The murmur3 source
23  * code sections are marked by
24  * // Begin <murmur3-file> ---->
25  * and
26  * // End <murmur3-file> ---->
27  * comments.
28  *
29  * Changes from the murmur3 distribution are marked with `//PDB'
30  * In addition comment blocks have been converted to Doxygen format.
31  * Function arguments for buffer length which were originally
32  * "int len" or "int i" have been changed to "std::size_t".
33  * In the _x86 versions the main loop used negative indexes, as shown.
34  * Other conversions to std::size_t are marked.
35  */
36 
37 #include "log.h"
38 #include "hash-murmur3.h"
39 
40 #include <iomanip>
41 
48 namespace ns3 {
49 
50 NS_LOG_COMPONENT_DEFINE ("Hash-Murmur3");
51 
52 namespace Hash {
53 
54 namespace Function {
55 
57 namespace Murmur3Implementation {
58 
66 // Changes from Murmur3 distribution are marked with `//PDB'
67 //
68 
69 /*************************************************
70  ** class Murmur3HashImplementation
71  ************************************************/
72 
73 // Adapted from http://code.google.com/p/smhasher/
74 
75 // Begin Murmur3.cpp -------- *NS_CHECK_STYLE_OFF* ---->
76 
77 //
78 //-----------------------------------------------------------------------------
79 // MurmurHash3 was written by Austin Appleby, and is placed in the public
80 // domain. The author hereby disclaims copyright to this source code.
81 
82 // Note - The x86 and x64 versions do _not_ produce the same results, as the
83 // algorithms are optimized for their respective platforms. You can still
84 // compile and run any of them on any platform, but your performance with the
85 // non-native version will be less than optimal.
86 
87 
95 inline uint32_t rotl32 ( uint32_t x, int8_t r )
96 {
97  return (x << r) | (x >> (32 - r));
98 }
99 
107 inline uint64_t rotl64 ( uint64_t x, int8_t r )
108 {
109  return (x << r) | (x >> (64 - r));
110 }
111 
113 #define BIG_CONSTANT(x) (x##LLU)
114 
115 //-----------------------------------------------------------------------------
126 inline uint32_t getblock ( const uint32_t * p, std::size_t i )
127 {
128  return p[i];
129 }
131 inline uint64_t getblock ( const uint64_t * p, std::size_t i )
132 {
133  return p[i];
134 }
135 
136 //-----------------------------------------------------------------------------
143 inline uint32_t fmix ( uint32_t h )
144 {
145  h ^= h >> 16;
146  h *= 0x85ebca6b;
147  h ^= h >> 13;
148  h *= 0xc2b2ae35;
149  h ^= h >> 16;
150 
151  return h;
152 }
153 
154 //----------
156 inline uint64_t fmix ( uint64_t h )
157 {
158  h ^= h >> 33;
159  h *= BIG_CONSTANT(0xff51afd7ed558ccd);
160  h ^= h >> 33;
161  h *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
162  h ^= h >> 33;
163 
164  return h;
165 }
166 
167 //-----------------------------------------------------------------------------
168 
169 //PDB forward
178 void MurmurHash3_x86_32_incr ( const void * key, std::size_t len,
179  uint32_t seed, void * out );
187 void MurmurHash3_x86_32_fin ( std::size_t len,
188  uint32_t seed, void * out );
189 
190 //PDB - incremental hashing
192 void MurmurHash3_x86_32 ( const void * key, std::size_t len,
193  uint32_t seed, void * out )
194 {
195  uint32_t h1;
196  MurmurHash3_x86_32_incr (key, len, seed, &h1);
197  MurmurHash3_x86_32_fin (len, h1, out);
198 }
199 
200 void MurmurHash3_x86_32_incr ( const void * key, std::size_t len,
201  uint32_t seed, void * out )
202 {
203  const uint8_t * data = (const uint8_t*)key;
204  const std::size_t nblocks = len / 4; //PDB: was const int nblocks
205 
206  uint32_t h1 = seed;
207 
208  uint32_t c1 = 0xcc9e2d51;
209  uint32_t c2 = 0x1b873593;
210 
211  //----------
212  // body
213 
214  //PDB: const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
215  const uint32_t * blocks = (const uint32_t *)(data);
216 
217  //PDB: for(int i = -nblocks; i; i++)
218  for(std::size_t i = 0; i < nblocks; i++)
219  {
220  uint32_t k1 = getblock(blocks,i);
221 
222  k1 *= c1;
223  k1 = rotl32(k1,15);
224  k1 *= c2;
225 
226  h1 ^= k1;
227  h1 = rotl32(h1,13);
228  h1 = h1*5+0xe6546b64;
229  }
230 
231  //----------
232  // tail
233 
234  const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
235 
236  uint32_t k1 = 0;
237 
238  switch(len & 3)
239  {
240  case 3: k1 ^= tail[2] << 16;
241  case 2: k1 ^= tail[1] << 8;
242  case 1: k1 ^= tail[0];
243  k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
244  };
245 
246  *(uint32_t *)out = h1;
247 }
248 
249 //PDB - incremental hashing - finalization
250 void MurmurHash3_x86_32_fin ( std::size_t len,
251  uint32_t seed, void * out )
252 {
253  uint32_t h1 = seed;
254 
255  //----------
256  // finalization
257 
258  h1 ^= len;
259 
260  h1 = fmix(h1);
261 
262  *(uint32_t *)out = h1;
263 }
264 
265 //-----------------------------------------------------------------------------
266 
267 //PDB forward
276 void MurmurHash3_x86_128_incr ( const void * key, const std::size_t len,
277  uint32_t * seeds, void * out );
285 void MurmurHash3_x86_128_fin ( const std::size_t len,
286  uint32_t * seeds, void * out );
287 
288 //PDB - incremental hashing
297 void MurmurHash3_x86_128 ( const void * key, const std::size_t len,
298  uint32_t seed, void * out )
299 {
300  uint32_t seeds[4];
301  uint32_t h[4];
302  seeds[0] = seeds[1] = seeds[2] = seeds[3] = seed;
303  MurmurHash3_x86_128_incr (key, len, seeds, h);
304  MurmurHash3_x86_128_fin (len, h, out);
305 }
306 
307 void MurmurHash3_x86_128_incr ( const void * key, const std::size_t len,
308  uint32_t * seeds, void * out )
309 {
310  const uint8_t * data = (const uint8_t*)key;
311  const std::size_t nblocks = len / 16; //PDB: was const int nblocks
312 
313  uint32_t h1 = seeds[0];
314  uint32_t h2 = seeds[1];
315  uint32_t h3 = seeds[2];
316  uint32_t h4 = seeds[3];
317 
318  uint32_t c1 = 0x239b961b;
319  uint32_t c2 = 0xab0e9789;
320  uint32_t c3 = 0x38b34ae5;
321  uint32_t c4 = 0xa1e38b93;
322 
323  //----------
324  // body
325 
326  //PDB: const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
327  const uint32_t * blocks = (const uint32_t *)(data);
328 
329  //PDB: for(int i = -nblocks; i; i++)
330  for(std::size_t i = 0; i < nblocks; i++)
331  {
332  uint32_t k1 = getblock(blocks,i*4+0);
333  uint32_t k2 = getblock(blocks,i*4+1);
334  uint32_t k3 = getblock(blocks,i*4+2);
335  uint32_t k4 = getblock(blocks,i*4+3);
336 
337  k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
338 
339  h1 = rotl32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
340 
341  k2 *= c2; k2 = rotl32(k2,16); k2 *= c3; h2 ^= k2;
342 
343  h2 = rotl32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
344 
345  k3 *= c3; k3 = rotl32(k3,17); k3 *= c4; h3 ^= k3;
346 
347  h3 = rotl32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
348 
349  k4 *= c4; k4 = rotl32(k4,18); k4 *= c1; h4 ^= k4;
350 
351  h4 = rotl32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
352  }
353 
354  //----------
355  // tail
356 
357  const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
358 
359  uint32_t k1 = 0;
360  uint32_t k2 = 0;
361  uint32_t k3 = 0;
362  uint32_t k4 = 0;
363 
364  switch(len & 15)
365  {
366  case 15: k4 ^= tail[14] << 16;
367  case 14: k4 ^= tail[13] << 8;
368  case 13: k4 ^= tail[12] << 0;
369  k4 *= c4; k4 = rotl32(k4,18); k4 *= c1; h4 ^= k4;
370 
371  case 12: k3 ^= tail[11] << 24;
372  case 11: k3 ^= tail[10] << 16;
373  case 10: k3 ^= tail[ 9] << 8;
374  case 9: k3 ^= tail[ 8] << 0;
375  k3 *= c3; k3 = rotl32(k3,17); k3 *= c4; h3 ^= k3;
376 
377  case 8: k2 ^= tail[ 7] << 24;
378  case 7: k2 ^= tail[ 6] << 16;
379  case 6: k2 ^= tail[ 5] << 8;
380  case 5: k2 ^= tail[ 4] << 0;
381  k2 *= c2; k2 = rotl32(k2,16); k2 *= c3; h2 ^= k2;
382 
383  case 4: k1 ^= tail[ 3] << 24;
384  case 3: k1 ^= tail[ 2] << 16;
385  case 2: k1 ^= tail[ 1] << 8;
386  case 1: k1 ^= tail[ 0] << 0;
387  k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
388  };
389 
390  ((uint32_t *)out)[0] = h1;
391  ((uint32_t *)out)[1] = h2;
392  ((uint32_t *)out)[2] = h3;
393  ((uint32_t *)out)[3] = h4;
394 }
395 
396 //PDB - incremental hashing - finalization
397 void MurmurHash3_x86_128_fin ( const std::size_t len,
398  uint32_t * seeds, void * out )
399 {
400  //----------
401  // finalization
402 
403  uint32_t h1 = seeds[0];
404  uint32_t h2 = seeds[1];
405  uint32_t h3 = seeds[2];
406  uint32_t h4 = seeds[3];
407 
408  h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
409 
410  h1 += h2; h1 += h3; h1 += h4;
411  h2 += h1; h3 += h1; h4 += h1;
412 
413  h1 = fmix(h1);
414  h2 = fmix(h2);
415  h3 = fmix(h3);
416  h4 = fmix(h4);
417 
418  h1 += h2; h1 += h3; h1 += h4;
419  h2 += h1; h3 += h1; h4 += h1;
420 
421  ((uint32_t *)out)[0] = h1;
422  ((uint32_t *)out)[1] = h2;
423  ((uint32_t *)out)[2] = h3;
424  ((uint32_t *)out)[3] = h4;
425 }
426 
427 //-----------------------------------------------------------------------------
429 void MurmurHash3_x64_128 ( const void * key, const std::size_t len,
430  const uint32_t seed, void * out )
431 {
432  const uint8_t * data = (const uint8_t*)key;
433  const std::size_t nblocks = len / 16; //PDB: was const int nblocks
434 
435  uint64_t h1 = seed;
436  uint64_t h2 = seed;
437 
438  uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
439  uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
440 
441  //----------
442  // body
443 
444  const uint64_t * blocks = (const uint64_t *)(data);
445 
446  for(std::size_t i = 0; i < nblocks; i++) //PDB: was int i
447  {
448  uint64_t k1 = getblock(blocks,i*2+0);
449  uint64_t k2 = getblock(blocks,i*2+1);
450 
451  k1 *= c1; k1 = rotl64(k1,31); k1 *= c2; h1 ^= k1;
452 
453  h1 = rotl64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
454 
455  k2 *= c2; k2 = rotl64(k2,33); k2 *= c1; h2 ^= k2;
456 
457  h2 = rotl64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
458  }
459 
460  //----------
461  // tail
462 
463  const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
464 
465  uint64_t k1 = 0;
466  uint64_t k2 = 0;
467 
468  switch(len & 15)
469  {
470  case 15: k2 ^= uint64_t(tail[14]) << 48;
471  case 14: k2 ^= uint64_t(tail[13]) << 40;
472  case 13: k2 ^= uint64_t(tail[12]) << 32;
473  case 12: k2 ^= uint64_t(tail[11]) << 24;
474  case 11: k2 ^= uint64_t(tail[10]) << 16;
475  case 10: k2 ^= uint64_t(tail[ 9]) << 8;
476  case 9: k2 ^= uint64_t(tail[ 8]) << 0;
477  k2 *= c2; k2 = rotl64(k2,33); k2 *= c1; h2 ^= k2;
478 
479  case 8: k1 ^= uint64_t(tail[ 7]) << 56;
480  case 7: k1 ^= uint64_t(tail[ 6]) << 48;
481  case 6: k1 ^= uint64_t(tail[ 5]) << 40;
482  case 5: k1 ^= uint64_t(tail[ 4]) << 32;
483  case 4: k1 ^= uint64_t(tail[ 3]) << 24;
484  case 3: k1 ^= uint64_t(tail[ 2]) << 16;
485  case 2: k1 ^= uint64_t(tail[ 1]) << 8;
486  case 1: k1 ^= uint64_t(tail[ 0]) << 0;
487  k1 *= c1; k1 = rotl64(k1,31); k1 *= c2; h1 ^= k1;
488  };
489 
490  //----------
491  // finalization
492 
493  h1 ^= len; h2 ^= len;
494 
495  h1 += h2;
496  h2 += h1;
497 
498  h1 = fmix(h1);
499  h2 = fmix(h2);
500 
501  h1 += h2;
502  h2 += h1;
503 
504  ((uint32_t *)out)[0] = static_cast<uint32_t> (h1); //PDB cast
505  ((uint32_t *)out)[1] = static_cast<uint32_t> (h2); //PDB cast
506 }
507 
508 
509 // End Murmur3.cpp ---------- *NS_CHECK_STYLE_ON* ----->
510 
511 #undef BIG_CONSTANT
512 
513 
514 //-----------------------------------------------------------------------------
515 
516  // \defgroup hash_murmur3
518 
519 } // namespace Murmur3Implementation
520 
521 
523 {
524  clear ();
525 }
526 
527 uint32_t
528 Murmur3::GetHash32 (const char * buffer, const std::size_t size)
529 {
530  using namespace Murmur3Implementation;
531 
532  MurmurHash3_x86_32_incr (buffer, size,
533  m_hash32, (void *) &m_hash32);
534  m_size32 += static_cast<uint32_t> (size);
535  uint32_t hash;
537 
538  return hash;
539 }
540 
541 uint64_t
542 Murmur3::GetHash64 (const char * buffer, const std::size_t size)
543 {
544  using namespace Murmur3Implementation;
545 
546  MurmurHash3_x86_128_incr (buffer, static_cast<int> (size),
547  (uint32_t *)(void *)m_hash64, m_hash64);
548  m_size64 += size;
549 
550  // Simpler would be:
551  //
552  // uint64_t hash[2];
553  // MurmurHash3_x86_128_fin (m_size64, m_hash64, hash);
554  // return hash[0];
555  //
556  // but this triggers an aliasing bug in gcc-4.4 (perhaps related to
557  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39390).
558  // In ns-3, this bug produces incorrect results in static optimized
559  // builds only.
560  //
561  // Using uint32_t here avoids the bug, and continues to works with newer gcc.
562  uint32_t hash[4];
563 
564  MurmurHash3_x86_128_fin (static_cast<int> (m_size64),
565  (uint32_t *)(void *)m_hash64, hash);
566  uint64_t result = hash[1];
567  result = (result << 32) | hash[0];
568  return result;
569 }
570 
571 void
573 {
574  m_hash32 = (uint32_t)SEED;
575  m_size32 = 0;
576  m_hash64[0] = m_hash64[1] = ((uint64_t)SEED << 32) | (uint32_t)SEED;
577  m_size64 = 0;
578 }
579 
580 } // namespace Function
581 
582 } // namespace Hash
583 
584 } // namespace ns3
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::Hash::Function::Murmur3Implementation::MurmurHash3_x86_128_fin
void MurmurHash3_x86_128_fin(const std::size_t len, uint32_t *seeds, void *out)
Finalize a hash.
Definition: hash-murmur3.cc:397
ns3::Hash::Function::Murmur3::GetHash32
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash-murmur3.cc:528
ns3::Hash::Function::Murmur3::m_size64
std::size_t m_size64
murmur3 produces 128-bit hash and state; we use just the first 64-bits.
Definition: hash-murmur3.h:118
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Hash::Function::Murmur3Implementation::getblock
uint32_t getblock(const uint32_t *p, std::size_t i)
Block read.
Definition: hash-murmur3.cc:126
ns3::Hash::Function::Murmur3::Murmur3
Murmur3()
Constructor, clears internal state.
Definition: hash-murmur3.cc:522
hash-murmur3.h
ns3::Hash::Function::Murmur3 declaration.
ns3::Hash::Function::Murmur3::GetHash64
uint64_t GetHash64(const char *buffer, const std::size_t size)
Compute 64-bit hash of a byte buffer.
Definition: hash-murmur3.cc:542
BIG_CONSTANT
#define BIG_CONSTANT(x)
Unsigned long long constants.
Definition: hash-murmur3.cc:113
ns3::Hash::Function::Murmur3Implementation::MurmurHash3_x86_32_fin
void MurmurHash3_x86_32_fin(std::size_t len, uint32_t seed, void *out)
Finalize a hash.
Definition: hash-murmur3.cc:250
ns3::Hash::Function::Murmur3::SEED
@ SEED
Definition: hash-murmur3.h:104
ns3::Hash::Function::Murmur3::m_size32
std::size_t m_size32
Cache last hash value, and total bytes hashed (needed to finalize), for incremental hashing.
Definition: hash-murmur3.h:112
ns3::Hash::Function::Murmur3Implementation::MurmurHash3_x86_32
void MurmurHash3_x86_32(const void *key, std::size_t len, uint32_t seed, void *out)
Initial and incremental hash.
Definition: hash-murmur3.cc:192
data
uint8_t data[writeSize]
Definition: socket-bound-tcp-static-routing.cc:53
ns3::Hash::Function::Murmur3::clear
virtual void clear(void)
Restore initial state.
Definition: hash-murmur3.cc:572
ns3::Hash::Function::Murmur3Implementation::MurmurHash3_x64_128
void MurmurHash3_x64_128(const void *key, const std::size_t len, const uint32_t seed, void *out)
Initial and incremental hash.
Definition: hash-murmur3.cc:429
ns3::Hash::Function::Murmur3Implementation::MurmurHash3_x86_128
void MurmurHash3_x86_128(const void *key, const std::size_t len, uint32_t seed, void *out)
Initial and incremental hash.
Definition: hash-murmur3.cc:297
hash
int32_t hash
Definition: fq-codel-queue-disc-test-suite.cc:41
test-ns3.result
result
Definition: test-ns3.py:576
ns3::Hash::Function::Murmur3Implementation::MurmurHash3_x86_32_incr
void MurmurHash3_x86_32_incr(const void *key, std::size_t len, uint32_t seed, void *out)
Initial and incremental hash.
Definition: hash-murmur3.cc:200
log.h
Debug message logging.
ns3::Hash::Function::Murmur3::m_hash64
uint64_t m_hash64[2]
murmur3 produces 128-bit hash and state; we use just the first 64-bits.
Definition: hash-murmur3.h:117
ns3::Hash::Function::Murmur3Implementation::rotl64
uint64_t rotl64(uint64_t x, int8_t r)
Barrel shift (rotate) left on 64 bits.
Definition: hash-murmur3.cc:107
sample-rng-plot.x
list x
Definition: sample-rng-plot.py:34
ns3::Hash::Function::Murmur3Implementation::fmix
uint32_t fmix(uint32_t h)
Finalization mix - force all bits of a hash block to avalanche.
Definition: hash-murmur3.cc:143
ns3::Hash::Function::Murmur3::m_hash32
uint32_t m_hash32
Cache last hash value, and total bytes hashed (needed to finalize), for incremental hashing.
Definition: hash-murmur3.h:111
ns3::Hash::Function::Murmur3Implementation::MurmurHash3_x86_128_incr
void MurmurHash3_x86_128_incr(const void *key, const std::size_t len, uint32_t *seeds, void *out)
Initial and incremental hash.
Definition: hash-murmur3.cc:307
ns3::Hash::Function::Murmur3Implementation::rotl32
uint32_t rotl32(uint32_t x, int8_t r)
Barrel shift (rotate) left on 32 bits.
Definition: hash-murmur3.cc:95