Skip to content
 
 

Latest commit

 

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

murmur3

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.

API

#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).

Building

The makefile provides the following targets.

make / make all — build the example

Compiles example.c together with murmur3.c into the example binary.

make
./example "hello world"

make tests — run the test suite

Builds and immediately runs the tests binary from test.c.

make tests

make static — static library

Places an object file into build/ and produces libmurmur3.a in the repo root.

make static

Output: libmurmur3.a.

make shared — shared library

Compiles with -fPIC and produces libmurmur3.so.

make shared

On macOS the -Wl,--export-dynamic flag isn't supported by the system linker. Build manually if needed:

cc -fPIC -O3 -c murmur3.c
cc -dynamiclib murmur3.o -o libmurmur3.dylib

make clean

Removes example, *.o, and *.so.

Usage example

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"

Linking against the static library

make static
cc -O3 example.c -L. -lmurmur3 -o example

Linking against the shared library

make shared
cc -O3 example.c -L. -lmurmur3 -o example
LD_LIBRARY_PATH=. ./example "murmur"     # Linux
DYLD_LIBRARY_PATH=. ./example "murmur"   # macOS

Portability

The 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.

License

Public domain. MurmurHash3 was written by Austin Appleby; the C port is by Peter Scott.

About

Murmur3 hash in C

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages