/*-1* Copyright (c) 2014 Dag-Erling Smørgrav2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/hash.h>27#include <sys/endian.h>28#include <sys/stdint.h>29#include <sys/types.h>3031#define rol32(i32, n) ((i32) << (n) | (i32) >> (32 - (n)))3233/*34* Simple implementation of the Murmur3-32 hash function.35*36* This implementation is slow but safe. It can be made significantly37* faster if the caller guarantees that the input is correctly aligned for38* 32-bit reads, and slightly faster yet if the caller guarantees that the39* length of the input is always a multiple of 4 bytes.40*/41uint32_t42murmur3_32_hash(const void *data, size_t len, uint32_t seed)43{44const uint8_t *bytes;45uint32_t hash, k;46size_t res;4748/* initialization */49bytes = data;50res = len;51hash = seed;5253/* main loop */54while (res >= 4) {55/* replace with le32toh() if input is aligned */56k = le32dec(bytes);57bytes += 4;58res -= 4;59k *= 0xcc9e2d51;60k = rol32(k, 15);61k *= 0x1b873593;62hash ^= k;63hash = rol32(hash, 13);64hash *= 5;65hash += 0xe6546b64;66}6768/* remainder */69/* remove if input length is a multiple of 4 */70if (res > 0) {71k = 0;72switch (res) {73case 3:74k |= bytes[2] << 16;75case 2:76k |= bytes[1] << 8;77case 1:78k |= bytes[0];79k *= 0xcc9e2d51;80k = rol32(k, 15);81k *= 0x1b873593;82hash ^= k;83break;84}85}8687/* finalize */88hash ^= (uint32_t)len;89hash ^= hash >> 16;90hash *= 0x85ebca6b;91hash ^= hash >> 13;92hash *= 0xc2b2ae35;93hash ^= hash >> 16;94return (hash);95}9697/*98* Simplified version of the above optimized for aligned sequences of99* 32-bit words. The count argument is the number of words, not the100* length in bytes.101*/102uint32_t103murmur3_32_hash32(const uint32_t *data, size_t count, uint32_t seed)104{105uint32_t hash, k;106size_t res;107108/* iterate */109for (res = count, hash = seed; res > 0; res--, data++) {110k = le32toh(*data);111k *= 0xcc9e2d51;112k = rol32(k, 15);113k *= 0x1b873593;114hash ^= k;115hash = rol32(hash, 13);116hash *= 5;117hash += 0xe6546b64;118}119120/* finalize */121hash ^= (uint32_t)count;122hash ^= hash >> 16;123hash *= 0x85ebca6b;124hash ^= hash >> 13;125hash *= 0xc2b2ae35;126hash ^= hash >> 16;127return (hash);128}129130131