From 588b38fd17d68d7352029f46c75fc92bf1b95bd5 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 2 Jun 2015 21:11:57 +1000 Subject: [PATCH] hash: move wang mixing funcs into seperate unit --- Makefile.am | 2 ++ hash.cpp | 27 ----------------------- hash.hpp | 4 ---- hash/wang.hpp | 30 ++++++++++++++++++++++++++ hash/wang.ipp | 51 ++++++++++++++++++++++++++++++++++++++++++++ noise/turbulence.ipp | 4 ++-- 6 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 hash/wang.hpp create mode 100644 hash/wang.ipp diff --git a/Makefile.am b/Makefile.am index 7879dfa5..db7ce754 100644 --- a/Makefile.am +++ b/Makefile.am @@ -91,6 +91,8 @@ UTIL_FILES = \ hash/sha1.hpp \ hash/sha2.cpp \ hash/sha2.hpp \ + hash/wang.hpp \ + hash/wang.ipp \ image.cpp \ image.hpp \ introspection.cpp \ diff --git a/hash.cpp b/hash.cpp index 96203723..7fadaac9 100644 --- a/hash.cpp +++ b/hash.cpp @@ -17,33 +17,6 @@ #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 // 64: 2^ 40 + 2^8 + 0xb3 // 128: 2^ 88 + 2^8 + 0x3B diff --git a/hash.hpp b/hash.hpp index e2d0f685..c7d87f09 100644 --- a/hash.hpp +++ b/hash.hpp @@ -22,10 +22,6 @@ // All hashes are unsuitable for cryptographic operations nnless noted. 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 uint32_t fnv1a32 (const void *, size_t); uint64_t fnv1a64 (const void *, size_t); diff --git a/hash/wang.hpp b/hash/wang.hpp new file mode 100644 index 00000000..c393639e --- /dev/null +++ b/hash/wang.hpp @@ -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 + */ + +#ifndef __UTIL_HASH_WANG_HPP +#define __UTIL_HASH_WANG_HPP + +#include + +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 diff --git a/hash/wang.ipp b/hash/wang.ipp new file mode 100644 index 00000000..2bc8d816 --- /dev/null +++ b/hash/wang.ipp @@ -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 + */ + +#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; +} diff --git a/noise/turbulence.ipp b/noise/turbulence.ipp index 072a274b..f0ebd8a4 100644 --- a/noise/turbulence.ipp +++ b/noise/turbulence.ipp @@ -19,7 +19,7 @@ #endif #define __UTIL_NOISE_TURBULENCE_IPP -#include "../hash.hpp" +#include "../hash/wang.hpp" namespace util { namespace noise { @@ -28,7 +28,7 @@ namespace util { namespace noise { turbulence::turbulence (seed_t _seed, vector<2,T> _scale): data (_seed), - perturb { wang64 (_seed), wang64 (wang64 (_seed)) }, + perturb { hash::wang (_seed), hash::wang (hash::wang (_seed)) }, scale (_scale) { ; }