2015-06-02 21:11:57 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-06-02 21:11:57 +10:00
|
|
|
*
|
|
|
|
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_HASH_WANG_HPP
|
|
|
|
#define __UTIL_HASH_WANG_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::hash {
|
2015-06-02 21:11:57 +10:00
|
|
|
// Thomas Wang's integer mixing functions, ca 2007
|
2018-01-13 13:48:58 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
constexpr uint32_t
|
2018-01-14 17:13:21 +11:00
|
|
|
wang (uint32_t key)
|
|
|
|
{
|
2018-01-13 13:48:58 +11:00
|
|
|
// a prime or an odd constant
|
|
|
|
constexpr uint32_t c2 = 0x27d4eb2d;
|
|
|
|
|
|
|
|
key = (key ^ 61) ^ (key >> 16);
|
|
|
|
key = key + (key << 3);
|
|
|
|
key = key ^ (key >> 4);
|
|
|
|
key = key * c2;
|
|
|
|
key = key ^ (key >> 15);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
2015-06-02 21:11:57 +10:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
constexpr uint64_t
|
2018-01-14 17:13:21 +11:00
|
|
|
wang (uint64_t key)
|
|
|
|
{
|
2018-01-13 13:48:58 +11:00
|
|
|
key = ~key + (key << 21); // key = (key << 21) - key - 1;
|
|
|
|
key = key ^ (key >> 24);
|
|
|
|
key = (key + (key << 3)) + (key << 8); // key * 265
|
|
|
|
key = key ^ (key >> 14);
|
|
|
|
key = (key + (key << 2)) + (key << 4); // key * 21
|
|
|
|
key = key ^ (key >> 28);
|
|
|
|
key = key + (key << 31);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 21:11:57 +10:00
|
|
|
|
|
|
|
#endif
|