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