A C implementation of MurmurHash3, Austin Appleby's non-cryptographic hash function. Ported from the original C++.
Fast and well-distributed — suitable for hash tables, Bloom filters, sharding, and similar use cases. Not suitable for cryptographic purposes.
#include "murmur3.h"
void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out);
void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out);
void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out);| Function | Target arch | Output | When to use |
|---|---|---|---|
MurmurHash3_x86_32 |
32-bit | 4 bytes | Small keys, lowest latency |
MurmurHash3_x86_128 |
32-bit | 16 bytes | 32-bit systems, wider hash needed |
MurmurHash3_x64_128 |
64-bit | 16 bytes | 64-bit systems — highest throughput |
Parameters:
key— pointer to the data to hash;len— length in bytes;seed— arbitrary seed value (lets you derive different hashes from the same input);out— output buffer (4 bytes for_32, 16 bytes for_128).
The makefile provides the following targets.
Compiles example.c together with murmur3.c into the example binary.
make
./example "hello world"Builds and immediately runs the tests binary from test.c.
make testsPlaces an object file into build/ and produces libmurmur3.a in the repo root.
make staticOutput: libmurmur3.a.
Compiles with -fPIC and produces libmurmur3.so.
make sharedOn macOS the
-Wl,--export-dynamicflag isn't supported by the system linker. Build manually if needed:cc -fPIC -O3 -c murmur3.c cc -dynamiclib murmur3.o -o libmurmur3.dylib
Removes example, *.o, and *.so.
example.c:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "murmur3.h"
int main(int argc, char **argv) {
uint32_t hash[4];
uint32_t seed = 42;
const char *input = argc > 1 ? argv[1] : "hello world";
size_t len = strlen(input);
MurmurHash3_x86_32(input, len, seed, hash);
printf("x86_32: %08x\n", hash[0]);
MurmurHash3_x86_128(input, len, seed, hash);
printf("x86_128: %08x %08x %08x %08x\n",
hash[0], hash[1], hash[2], hash[3]);
MurmurHash3_x64_128(input, len, seed, hash);
printf("x64_128: %08x %08x %08x %08x\n",
hash[0], hash[1], hash[2], hash[3]);
return 0;
}Build and run directly:
cc -O3 example.c murmur3.c -o example
./example "murmur"make static
cc -O3 example.c -L. -lmurmur3 -o examplemake shared
cc -O3 example.c -L. -lmurmur3 -o example
LD_LIBRARY_PATH=. ./example "murmur" # Linux
DYLD_LIBRARY_PATH=. ./example "murmur" # macOSThe code targets x86/x86_64: it assumes little-endian byte order and cheap unaligned reads. On other architectures (older ARM, big-endian systems) murmur3.c may need adjustments.
Public domain. MurmurHash3 was written by Austin Appleby; the C port is by Peter Scott.