// Implementation: Demetri Spanos (github.com/demetri/scribbles)
// Jai Port: Jesse Coyle (github.com/Zilarrezko)

MurMur_Seed : u32 : 0xa3c91521;

murmur32 :: inline (s: string, seed: u32 = MurMur_Seed) -> u32 {
    return murmur32(s.data, s.count, seed);
}

murmur32 :: inline (x: $T, seed: u32 = MurMur_Seed) -> u32 {
    d: []u8 = ---;
    d.data = cast(*u8)*x;
    d.count = size_of(T);
    return murmur32(d.data, d.count, seed);
}

murmur32 :: (key: *void, len: int, seed: u32 = MurMur_Seed) -> u32 {
    scrambler :: (k: u32) -> u32 #expand {
        c1: u32 : 0xcc9e2d51;
        c2: u32 : 0x1b873593;
        r1: int : 15;
        k = k*c1;
        k = k <<< r1;
        k = k*c2;
        return k;
    }

    h: u32 = seed;
    tail: *u8 = cast(*u8)key + (len/4)*4;
    p: *u32 = cast(*u32)key;

    while cast(*u8)p < tail {
        k: u32 = <<p;
        k = scrambler(k);
        h = h^k;
        h = h <<< 13;
        h = h*5 + 0xe6546b64;
        p += 1;
    }

    t: u32;
    if len & 3 == {
        case 3;
            t ^= cast(u32)(tail[2]) << 16;
            #through;
        case 2;
            t ^= cast(u32)(tail[1]) << 8;
            #through;
        case 1;
            t ^= cast(u32)(tail[0]);
            t = scrambler(t);
            h = h^t;
    }
    h ^= cast,trunc(u32)len;
    h ^= h >> 16; h *= 0x85ebca6b;
    h ^= h >> 13; h *= 0xc2b2ae35;
    h ^= h >> 16;
    return h;
}
