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