A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
hash-murmur3.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Lawrence Livermore National Laboratory
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18 *
19 * This copyright notice applies strictly to the wrapper material.
20 *
21 * The murmur3 source code itself is in the public domain. The murmur3 source
22 * code sections are marked by
23 * // Begin <murmur3-file> ---->
24 * and
25 * // End <murmur3-file> ---->
26 * comments.
27 *
28 * Changes from the murmur3 distribution are marked with `//PDB'
29 * In addition comment blocks have been converted to Doxygen format.
30 * Function arguments for buffer length which were originally
31 * "int len" or "int i" have been changed to "std::size_t".
32 * In the _x86 versions the main loop used negative indexes, as shown.
33 * Other conversions to std::size_t are marked.
34 */
35
36#include "hash-murmur3.h"
37
38#include "log.h"
39
40#include <iomanip>
41
48namespace ns3
49{
50
51NS_LOG_COMPONENT_DEFINE("Hash-Murmur3");
52
53namespace Hash
54{
55
56namespace Function
57{
58
60namespace Murmur3Implementation
61{
62
69// Changes from Murmur3 distribution are marked with `//PDB'
70//
71
72/*************************************************
73 ** class Murmur3HashImplementation
74 ************************************************/
75
76// Adapted from http://code.google.com/p/smhasher/
77
78// NOLINTBEGIN
79// clang-format off
80
81//
82//-----------------------------------------------------------------------------
83// MurmurHash3 was written by Austin Appleby, and is placed in the public
84// domain. The author hereby disclaims copyright to this source code.
85
86// Note - The x86 and x64 versions do _not_ produce the same results, as the
87// algorithms are optimized for their respective platforms. You can still
88// compile and run any of them on any platform, but your performance with the
89// non-native version will be less than optimal.
90
91
100{
101 return (x << r) | (x >> (32 - r));
102}
103
111inline uint64_t rotl64 ( uint64_t x, int8_t r )
112{
113 return (x << r) | (x >> (64 - r));
114}
115
117#define BIG_CONSTANT(x) (x##LLU)
118
119//-----------------------------------------------------------------------------
130inline uint32_t getblock ( const uint32_t * p, std::size_t i )
131{
132 return p[i];
133}
135inline uint64_t getblock ( const uint64_t * p, std::size_t i )
136{
137 return p[i];
138}
139
140//-----------------------------------------------------------------------------
148{
149 h ^= h >> 16;
150 h *= 0x85ebca6b;
151 h ^= h >> 13;
152 h *= 0xc2b2ae35;
153 h ^= h >> 16;
154
155 return h;
156}
157
158//----------
160inline uint64_t fmix ( uint64_t h )
161{
162 h ^= h >> 33;
163 h *= BIG_CONSTANT(0xff51afd7ed558ccd);
164 h ^= h >> 33;
165 h *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
166 h ^= h >> 33;
167
168 return h;
169}
170
171//-----------------------------------------------------------------------------
172
173//PDB forward
182void MurmurHash3_x86_32_incr ( const void * key, std::size_t len,
183 uint32_t seed, void * out );
191void MurmurHash3_x86_32_fin ( std::size_t len,
192 uint32_t seed, void * out );
193
194//PDB - incremental hashing
196void MurmurHash3_x86_32 ( const void * key, std::size_t len,
197 uint32_t seed, void * out )
198{
199 uint32_t h1;
200 MurmurHash3_x86_32_incr (key, len, seed, &h1);
201 MurmurHash3_x86_32_fin (len, h1, out);
202}
203
204void MurmurHash3_x86_32_incr ( const void * key, std::size_t len,
205 uint32_t seed, void * out )
206{
207 const uint8_t * data = (const uint8_t*)key;
208 const std::size_t nblocks = len / 4; //PDB: was const int nblocks
209
210 uint32_t h1 = seed;
211
212 uint32_t c1 = 0xcc9e2d51;
213 uint32_t c2 = 0x1b873593;
214
215 //----------
216 // body
217
218 //PDB: const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
219 const uint32_t * blocks = (const uint32_t *)(data);
220
221 //PDB: for(int i = -nblocks; i; i++)
222 for(std::size_t i = 0; i < nblocks; i++)
223 {
224 uint32_t k1 = getblock(blocks,i);
225
226 k1 *= c1;
227 k1 = rotl32(k1,15);
228 k1 *= c2;
229
230 h1 ^= k1;
231 h1 = rotl32(h1,13);
232 h1 = h1*5+0xe6546b64;
233 }
234
235 //----------
236 // tail
237
238 const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
239
240 uint32_t k1 = 0;
241
242 switch(len & 3)
243 {
244 case 3: k1 ^= tail[2] << 16;
245 case 2: k1 ^= tail[1] << 8;
246 case 1: k1 ^= tail[0];
247 k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
248 };
249
250 *(uint32_t *)out = h1;
251}
252
253//PDB - incremental hashing - finalization
254void MurmurHash3_x86_32_fin ( std::size_t len,
255 uint32_t seed, void * out )
256{
257 uint32_t h1 = seed;
258
259 //----------
260 // finalization
261
262 h1 ^= len;
263
264 h1 = fmix(h1);
265
266 *(uint32_t *)out = h1;
267}
268
269//-----------------------------------------------------------------------------
270
271//PDB forward
280void MurmurHash3_x86_128_incr ( const void * key, const std::size_t len,
281 uint32_t * seeds, void * out );
289void MurmurHash3_x86_128_fin ( const std::size_t len,
290 uint32_t * seeds, void * out );
291
292//PDB - incremental hashing
301void MurmurHash3_x86_128 ( const void * key, const std::size_t len,
302 uint32_t seed, void * out )
303{
304 uint32_t seeds[4];
305 uint32_t h[4];
306 seeds[0] = seeds[1] = seeds[2] = seeds[3] = seed;
307 MurmurHash3_x86_128_incr (key, len, seeds, h);
308 MurmurHash3_x86_128_fin (len, h, out);
309}
310
311void MurmurHash3_x86_128_incr ( const void * key, const std::size_t len,
312 uint32_t * seeds, void * out )
313{
314 const uint8_t * data = (const uint8_t*)key;
315 const std::size_t nblocks = len / 16; //PDB: was const int nblocks
316
317 uint32_t h1 = seeds[0];
318 uint32_t h2 = seeds[1];
319 uint32_t h3 = seeds[2];
320 uint32_t h4 = seeds[3];
321
322 uint32_t c1 = 0x239b961b;
323 uint32_t c2 = 0xab0e9789;
324 uint32_t c3 = 0x38b34ae5;
325 uint32_t c4 = 0xa1e38b93;
326
327 //----------
328 // body
329
330 //PDB: const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
331 const uint32_t * blocks = (const uint32_t *)(data);
332
333 //PDB: for(int i = -nblocks; i; i++)
334 for(std::size_t i = 0; i < nblocks; i++)
335 {
336 uint32_t k1 = getblock(blocks,i*4+0);
337 uint32_t k2 = getblock(blocks,i*4+1);
338 uint32_t k3 = getblock(blocks,i*4+2);
339 uint32_t k4 = getblock(blocks,i*4+3);
340
341 k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
342
343 h1 = rotl32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
344
345 k2 *= c2; k2 = rotl32(k2,16); k2 *= c3; h2 ^= k2;
346
347 h2 = rotl32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
348
349 k3 *= c3; k3 = rotl32(k3,17); k3 *= c4; h3 ^= k3;
350
351 h3 = rotl32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
352
353 k4 *= c4; k4 = rotl32(k4,18); k4 *= c1; h4 ^= k4;
354
355 h4 = rotl32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
356 }
357
358 //----------
359 // tail
360
361 const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
362
363 uint32_t k1 = 0;
364 uint32_t k2 = 0;
365 uint32_t k3 = 0;
366 uint32_t k4 = 0;
367
368 switch(len & 15)
369 {
370 case 15: k4 ^= tail[14] << 16;
371 case 14: k4 ^= tail[13] << 8;
372 case 13: k4 ^= tail[12] << 0;
373 k4 *= c4; k4 = rotl32(k4,18); k4 *= c1; h4 ^= k4;
374
375 case 12: k3 ^= tail[11] << 24;
376 case 11: k3 ^= tail[10] << 16;
377 case 10: k3 ^= tail[ 9] << 8;
378 case 9: k3 ^= tail[ 8] << 0;
379 k3 *= c3; k3 = rotl32(k3,17); k3 *= c4; h3 ^= k3;
380
381 case 8: k2 ^= tail[ 7] << 24;
382 case 7: k2 ^= tail[ 6] << 16;
383 case 6: k2 ^= tail[ 5] << 8;
384 case 5: k2 ^= tail[ 4] << 0;
385 k2 *= c2; k2 = rotl32(k2,16); k2 *= c3; h2 ^= k2;
386
387 case 4: k1 ^= tail[ 3] << 24;
388 case 3: k1 ^= tail[ 2] << 16;
389 case 2: k1 ^= tail[ 1] << 8;
390 case 1: k1 ^= tail[ 0] << 0;
391 k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
392 };
393
394 ((uint32_t *)out)[0] = h1;
395 ((uint32_t *)out)[1] = h2;
396 ((uint32_t *)out)[2] = h3;
397 ((uint32_t *)out)[3] = h4;
398}
399
400//PDB - incremental hashing - finalization
401void MurmurHash3_x86_128_fin ( const std::size_t len,
402 uint32_t * seeds, void * out )
403{
404 //----------
405 // finalization
406
407 uint32_t h1 = seeds[0];
408 uint32_t h2 = seeds[1];
409 uint32_t h3 = seeds[2];
410 uint32_t h4 = seeds[3];
411
412 h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
413
414 h1 += h2; h1 += h3; h1 += h4;
415 h2 += h1; h3 += h1; h4 += h1;
416
417 h1 = fmix(h1);
418 h2 = fmix(h2);
419 h3 = fmix(h3);
420 h4 = fmix(h4);
421
422 h1 += h2; h1 += h3; h1 += h4;
423 h2 += h1; h3 += h1; h4 += h1;
424
425 ((uint32_t *)out)[0] = h1;
426 ((uint32_t *)out)[1] = h2;
427 ((uint32_t *)out)[2] = h3;
428 ((uint32_t *)out)[3] = h4;
429}
430
431//-----------------------------------------------------------------------------
433void MurmurHash3_x64_128 ( const void * key, const std::size_t len,
434 const uint32_t seed, void * out )
435{
436 const uint8_t * data = (const uint8_t*)key;
437 const std::size_t nblocks = len / 16; //PDB: was const int nblocks
438
439 uint64_t h1 = seed;
440 uint64_t h2 = seed;
441
442 uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
443 uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
444
445 //----------
446 // body
447
448 const uint64_t * blocks = (const uint64_t *)(data);
449
450 for(std::size_t i = 0; i < nblocks; i++) //PDB: was int i
451 {
452 uint64_t k1 = getblock(blocks,i*2+0);
453 uint64_t k2 = getblock(blocks,i*2+1);
454
455 k1 *= c1; k1 = rotl64(k1,31); k1 *= c2; h1 ^= k1;
456
457 h1 = rotl64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
458
459 k2 *= c2; k2 = rotl64(k2,33); k2 *= c1; h2 ^= k2;
460
461 h2 = rotl64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
462 }
463
464 //----------
465 // tail
466
467 const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
468
469 uint64_t k1 = 0;
470 uint64_t k2 = 0;
471
472 switch(len & 15)
473 {
474 case 15: k2 ^= uint64_t(tail[14]) << 48;
475 case 14: k2 ^= uint64_t(tail[13]) << 40;
476 case 13: k2 ^= uint64_t(tail[12]) << 32;
477 case 12: k2 ^= uint64_t(tail[11]) << 24;
478 case 11: k2 ^= uint64_t(tail[10]) << 16;
479 case 10: k2 ^= uint64_t(tail[ 9]) << 8;
480 case 9: k2 ^= uint64_t(tail[ 8]) << 0;
481 k2 *= c2; k2 = rotl64(k2,33); k2 *= c1; h2 ^= k2;
482
483 case 8: k1 ^= uint64_t(tail[ 7]) << 56;
484 case 7: k1 ^= uint64_t(tail[ 6]) << 48;
485 case 6: k1 ^= uint64_t(tail[ 5]) << 40;
486 case 5: k1 ^= uint64_t(tail[ 4]) << 32;
487 case 4: k1 ^= uint64_t(tail[ 3]) << 24;
488 case 3: k1 ^= uint64_t(tail[ 2]) << 16;
489 case 2: k1 ^= uint64_t(tail[ 1]) << 8;
490 case 1: k1 ^= uint64_t(tail[ 0]) << 0;
491 k1 *= c1; k1 = rotl64(k1,31); k1 *= c2; h1 ^= k1;
492 };
493
494 //----------
495 // finalization
496
497 h1 ^= len; h2 ^= len;
498
499 h1 += h2;
500 h2 += h1;
501
502 h1 = fmix(h1);
503 h2 = fmix(h2);
504
505 h1 += h2;
506 h2 += h1;
507
508 ((uint32_t *)out)[0] = static_cast<uint32_t> (h1); //PDB cast
509 ((uint32_t *)out)[1] = static_cast<uint32_t> (h2); //PDB cast
510}
511
512// clang-format on
513// NOLINTEND
514
515#undef BIG_CONSTANT
516
517//-----------------------------------------------------------------------------
518 // \defgroup hash_murmur3
520
521} // namespace Murmur3Implementation
522
524{
525 clear();
526}
527
529Murmur3::GetHash32(const char* buffer, const std::size_t size)
530{
531 using namespace Murmur3Implementation;
532
533 MurmurHash3_x86_32_incr(buffer, size, m_hash32, (void*)&m_hash32);
534 m_size32 += static_cast<uint32_t>(size);
535 uint32_t hash;
536 MurmurHash3_x86_32_fin(m_size32, m_hash32, (void*)&hash);
537
538 return hash;
539}
540
541uint64_t
542Murmur3::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), (uint32_t*)(void*)m_hash64, m_hash64);
547 m_size64 += size;
548
549 // Simpler would be:
550 //
551 // uint64_t hash[2];
552 // MurmurHash3_x86_128_fin (m_size64, m_hash64, hash);
553 // return hash[0];
554 //
555 // but this triggers an aliasing bug in gcc-4.4 (perhaps related to
556 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39390).
557 // In ns-3, this bug produces incorrect results in static optimized
558 // builds only.
559 //
560 // Using uint32_t here avoids the bug, and continues to works with newer gcc.
561 uint32_t hash[4];
562
563 MurmurHash3_x86_128_fin(static_cast<int>(m_size64), (uint32_t*)(void*)m_hash64, hash);
564 uint64_t result = hash[1];
565 result = (result << 32) | hash[0];
566 return result;
567}
568
569void
571{
573 m_size32 = 0;
574 m_hash64[0] = m_hash64[1] = ((uint64_t)SEED << 32) | (uint32_t)SEED;
575 m_size64 = 0;
576}
577
578} // namespace Function
579
580} // namespace Hash
581
582} // namespace ns3
void clear() override
Restore initial state.
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
uint64_t m_hash64[2]
murmur3 produces 128-bit hash and state; we use just the first 64-bits.
Definition: hash-murmur3.h:117
uint32_t GetHash32(const char *buffer, const std::size_t size) override
Compute 32-bit hash of a byte buffer.
uint32_t m_hash32
Cache last hash value, and total bytes hashed (needed to finalize), for incremental hashing.
Definition: hash-murmur3.h:111
static constexpr auto SEED
Seed value.
Definition: hash-murmur3.h:104
uint64_t GetHash64(const char *buffer, const std::size_t size) override
Compute 64-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
void MurmurHash3_x86_128_fin(const std::size_t len, uint32_t *seeds, void *out)
Finalize a hash.
void MurmurHash3_x86_128_incr(const void *key, const std::size_t len, uint32_t *seeds, void *out)
Initial and incremental hash.
uint32_t getblock(const uint32_t *p, std::size_t i)
Block read.
uint64_t rotl64(uint64_t x, int8_t r)
Barrel shift (rotate) left on 64 bits.
uint32_t fmix(uint32_t h)
Finalization mix - force all bits of a hash block to avalanche.
uint32_t rotl32(uint32_t x, int8_t r)
Barrel shift (rotate) left on 32 bits.
Definition: hash-murmur3.cc:99
void MurmurHash3_x64_128(const void *key, const std::size_t len, const uint32_t seed, void *out)
Initial and incremental hash.
void MurmurHash3_x86_32(const void *key, std::size_t len, uint32_t seed, void *out)
Initial and incremental hash.
void MurmurHash3_x86_128(const void *key, const std::size_t len, uint32_t seed, void *out)
Initial and incremental hash.
void MurmurHash3_x86_32_fin(std::size_t len, uint32_t seed, void *out)
Finalize a hash.
void MurmurHash3_x86_32_incr(const void *key, std::size_t len, uint32_t seed, void *out)
Initial and incremental hash.
#define BIG_CONSTANT(x)
Unsigned long long constants.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
ns3::Hash::Function::Murmur3 declaration.
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]