Path: blob/master/thirdparty/sdl/stdlib/SDL_murmur3.c
9903 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122// Public domain murmur3 32-bit hash algorithm23//24// Adapted from: https://en.wikipedia.org/wiki/MurmurHash2526static SDL_INLINE Uint32 murmur_32_scramble(Uint32 k)27{28k *= 0xcc9e2d51;29k = (k << 15) | (k >> 17);30k *= 0x1b873593;31return k;32}3334Uint32 SDLCALL SDL_murmur3_32(const void *data, size_t len, Uint32 seed)35{36const Uint8 *bytes = (const Uint8 *)data;37Uint32 hash = seed;38Uint32 k;3940// Read in groups of 4.41if ((((uintptr_t)bytes) & 3) == 0) {42// We can do aligned 32-bit reads43for (size_t i = len >> 2; i--; ) {44k = *(const Uint32 *)bytes;45k = SDL_Swap32LE(k);46bytes += sizeof(Uint32);47hash ^= murmur_32_scramble(k);48hash = (hash << 13) | (hash >> 19);49hash = hash * 5 + 0xe6546b64;50}51} else {52for (size_t i = len >> 2; i--; ) {53SDL_memcpy(&k, bytes, sizeof(Uint32));54k = SDL_Swap32LE(k);55bytes += sizeof(Uint32);56hash ^= murmur_32_scramble(k);57hash = (hash << 13) | (hash >> 19);58hash = hash * 5 + 0xe6546b64;59}60}6162// Read the rest.63size_t left = (len & 3);64if (left) {65k = 0;66for (size_t i = left; i--; ) {67k <<= 8;68k |= bytes[i];69}7071// A swap is *not* necessary here because the preceding loop already72// places the low bytes in the low places according to whatever endianness73// we use. Swaps only apply when the memory is copied in a chunk.74hash ^= murmur_32_scramble(k);75}7677/* Finalize. */78hash ^= len;79hash ^= hash >> 16;80hash *= 0x85ebca6b;81hash ^= hash >> 13;82hash *= 0xc2b2ae35;83hash ^= hash >> 16;8485return hash;86}878889