hash: move wang mixing funcs into seperate unit

This commit is contained in:
Danny Robson 2015-06-02 21:11:57 +10:00
parent 7dcba618c0
commit 588b38fd17
6 changed files with 85 additions and 33 deletions

View File

@ -91,6 +91,8 @@ UTIL_FILES = \
hash/sha1.hpp \ hash/sha1.hpp \
hash/sha2.cpp \ hash/sha2.cpp \
hash/sha2.hpp \ hash/sha2.hpp \
hash/wang.hpp \
hash/wang.ipp \
image.cpp \ image.cpp \
image.hpp \ image.hpp \
introspection.cpp \ introspection.cpp \

View File

@ -17,33 +17,6 @@
#include "hash.hpp" #include "hash.hpp"
uint32_t
util::wang32 (uint32_t key) {
uint32_t c2 = 0x27d4eb2d; // a prime or an odd constant
key = (key ^ 61) ^ (key >> 16);
key = key + (key << 3);
key = key ^ (key >> 4);
key = key * c2;
key = key ^ (key >> 15);
return key;
}
uint64_t
util::wang64 (uint64_t key) {
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;
}
// 32: 2^ 24 + 2^8 + 0x93 // 32: 2^ 24 + 2^8 + 0x93
// 64: 2^ 40 + 2^8 + 0xb3 // 64: 2^ 40 + 2^8 + 0xb3
// 128: 2^ 88 + 2^8 + 0x3B // 128: 2^ 88 + 2^8 + 0x3B

View File

@ -22,10 +22,6 @@
// All hashes are unsuitable for cryptographic operations nnless noted. // All hashes are unsuitable for cryptographic operations nnless noted.
namespace util { namespace util {
// Fast integer mixing operations by Thomas Wang.
uint32_t wang32 (uint32_t key);
uint64_t wang64 (uint64_t key);
// Fast and general hashing using FNV-1a // Fast and general hashing using FNV-1a
uint32_t fnv1a32 (const void *, size_t); uint32_t fnv1a32 (const void *, size_t);
uint64_t fnv1a64 (const void *, size_t); uint64_t fnv1a64 (const void *, size_t);

30
hash/wang.hpp Normal file
View File

@ -0,0 +1,30 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_HASH_WANG_HPP
#define __UTIL_HASH_WANG_HPP
#include <cstdint>
namespace util { namespace hash {
// Thomas Wang's integer mixing functions, ca 2007
constexpr uint32_t wang (uint32_t);
constexpr uint64_t wang (uint64_t);
} }
#include "wang.ipp"
#endif

51
hash/wang.ipp Normal file
View File

@ -0,0 +1,51 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_HASH_WANG_IPP
#error
#endif
#define __UTIL_HASH_WANG_IPP
//-----------------------------------------------------------------------------
constexpr uint32_t
util::hash::wang (uint32_t key) {
// 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;
}
//-----------------------------------------------------------------------------
constexpr uint64_t
util::hash::wang (uint64_t key) {
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;
}

View File

@ -19,7 +19,7 @@
#endif #endif
#define __UTIL_NOISE_TURBULENCE_IPP #define __UTIL_NOISE_TURBULENCE_IPP
#include "../hash.hpp" #include "../hash/wang.hpp"
namespace util { namespace noise { namespace util { namespace noise {
@ -28,7 +28,7 @@ namespace util { namespace noise {
turbulence<T,D,P>::turbulence (seed_t _seed, turbulence<T,D,P>::turbulence (seed_t _seed,
vector<2,T> _scale): vector<2,T> _scale):
data (_seed), data (_seed),
perturb { wang64 (_seed), wang64 (wang64 (_seed)) }, perturb { hash::wang (_seed), hash::wang (hash::wang (_seed)) },
scale (_scale) scale (_scale)
{ ; } { ; }