/* * 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 2015 Danny Robson */ #include "murmur2.hpp" #include "../../debug.hpp" #include "common.hpp" /////////////////////////////////////////////////////////////////////////////// uint32_t util::hash::murmur2::hash_32 (const void *restrict key, size_t len, uint32_t seed) { CHECK (key); // setup constexpr auto m = detail::constants::m; uint32_t h = seed ^ uint32_t (len); // body auto cursor = reinterpret_cast (key); auto last = cursor + len / sizeof (uint32_t); for (; cursor < last; ++cursor) h = mix (h, *cursor); // tail if (len % sizeof (uint32_t)) { h ^= murmur::tail (cursor, len); h *= m; } // finalise h ^= h >> 13; h *= m; h ^= h >> 15; return h; } //----------------------------------------------------------------------------- uint64_t util::hash::murmur2::hash_64 (const void *restrict key, size_t len, uint64_t seed) { // setup constexpr auto m = detail::constants::m; constexpr auto r = detail::constants::r; uint64_t h = seed ^ (len * m); // body auto cursor = reinterpret_cast (key); auto last = cursor + len / sizeof (uint64_t); for (; cursor < last; ++cursor) h = mix (h, *cursor); // tail if (len % sizeof (uint64_t)) { h ^= murmur::tail (cursor, len); h *= m; } // finalise h ^= h >> r; h *= m; h ^= h >> r; return h; }