1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! `Murmur3`, a suite of non-cryptographic hash functions that was used for hash-based lookups.
//!
//! by Austin Appleby (aappleby (AT) gmail)
//!
//! https://sites.google.com/site/murmurhash/
//!
//! # Note
//!
//! The x86 and x64 versions do _not_ produce the same results, as the
//! algorithms are optimized for their respective platforms. You can still
//! compile and run any of them on any platform, but your performance with the
//! non-native version will be less than optimal.
//!
//! # Example
//!
//! ```
//! use std::hash::{Hash, Hasher};
//!
//! use fasthash::{murmur3, Murmur3Hasher};
//!
//! fn hash<T: Hash>(t: &T) -> u64 {
//!     let mut s: Murmur3Hasher = Default::default();
//!     t.hash(&mut s);
//!     s.finish()
//! }
//!
//! let h = murmur3::hash128(b"hello world\xff").low64();
//!
//! assert_eq!(h, hash(&"hello world"));
//! ```
//!
#![allow(non_camel_case_types)]
use std::mem;
use std::os::raw::c_void;

use extprim::u128::u128;

use ffi;

use hasher::{FastHash, FastHasher};

/// `MurmurHash3` 32-bit hash functions
pub struct Murmur3_x86_32 {}

impl FastHash for Murmur3_x86_32 {
    type Value = u32;
    type Seed = u32;

    #[inline]
    fn hash_with_seed<T: AsRef<[u8]>>(bytes: &T, seed: u32) -> u32 {
        unsafe {
            let mut hash = 0_u32;

            ffi::MurmurHash3_x86_32(bytes.as_ref().as_ptr() as *const c_void,
                                    bytes.as_ref().len() as i32,
                                    seed,
                                    mem::transmute(&mut hash));

            hash
        }
    }
}

impl_hasher!(Murmur3Hasher_x86_32, Murmur3_x86_32);

/// `MurmurHash3` 128-bit hash functions for 32-bit processors
pub struct Murmur3_x86_128 {}

impl FastHash for Murmur3_x86_128 {
    type Value = u128;
    type Seed = u32;

    #[inline]
    fn hash_with_seed<T: AsRef<[u8]>>(bytes: &T, seed: u32) -> u128 {
        unsafe {
            let mut hash = u128::zero();

            ffi::MurmurHash3_x86_128(bytes.as_ref().as_ptr() as *const c_void,
                                     bytes.as_ref().len() as i32,
                                     seed,
                                     mem::transmute(&mut hash));

            hash
        }
    }
}

impl_hasher_ext!(Murmur3Hasher_x86_128, Murmur3_x86_128);

/// `MurmurHash3` 128-bit hash functions for 64-bit processors
pub struct Murmur3_x64_128 {}

impl FastHash for Murmur3_x64_128 {
    type Value = u128;
    type Seed = u32;

    #[inline]
    fn hash_with_seed<T: AsRef<[u8]>>(bytes: &T, seed: u32) -> u128 {
        unsafe {
            let mut hash = u128::zero();

            ffi::MurmurHash3_x64_128(bytes.as_ref().as_ptr() as *const c_void,
                                     bytes.as_ref().len() as i32,
                                     seed,
                                     mem::transmute(&mut hash));

            hash
        }
    }
}

impl_hasher_ext!(Murmur3Hasher_x64_128, Murmur3_x64_128);

/// `MurmurHash3` 32-bit hash functions for a byte array.
#[inline]
pub fn hash32<T: AsRef<[u8]>>(v: &T) -> u32 {
    Murmur3_x86_32::hash(v)
}

/// `MurmurHash3` 32-bit hash functions for a byte array.
/// For convenience, a 32-bit seed is also hashed into the result.
#[inline]
pub fn hash32_with_seed<T: AsRef<[u8]>>(v: &T, seed: u32) -> u32 {
    Murmur3_x86_32::hash_with_seed(v, seed)
}

/// `MurmurHash3` 128-bit hash functions for a byte array.
#[inline]
pub fn hash128<T: AsRef<[u8]>>(v: &T) -> u128 {
    Murmur3_x64_128::hash(v)
}

/// `MurmurHash3` 128-bit hash functions for a byte array.
/// For convenience, a 32-bit seed is also hashed into the result.
#[inline]
pub fn hash128_with_seed<T: AsRef<[u8]>>(v: &T, seed: u32) -> u128 {
    Murmur3_x64_128::hash_with_seed(v, seed)
}


#[cfg(test)]
mod tests {
    use std::hash::Hasher;

    use extprim::u128::u128;

    use hasher::{FastHash, FastHasher, HasherExt};
    use super::*;

    #[test]
    fn test_murmur3_x86_32() {
        assert_eq!(Murmur3_x86_32::hash(b"hello"), 613153351);
        assert_eq!(Murmur3_x86_32::hash_with_seed(b"hello", 123), 1573043710);
        assert_eq!(Murmur3_x86_32::hash(b"helloworld"), 2687965642);

        let mut h = Murmur3Hasher_x86_32::new();

        h.write(b"hello");
        assert_eq!(h.finish(), 613153351);

        h.write(b"world");
        assert_eq!(h.finish(), 2687965642);
    }

    #[test]
    fn test_murmur3_x86_128() {
        assert_eq!(Murmur3_x86_128::hash(b"hello"),
                   u128::from_parts(11158567162092401078, 15821672119091348640));
        assert_eq!(Murmur3_x86_128::hash_with_seed(b"hello", 123),
                   u128::from_parts(2149221405153268091, 10130600740778964073));
        assert_eq!(Murmur3_x86_128::hash(b"helloworld"),
                   u128::from_parts(4510970894511742178, 13166749202678098166));

        let mut h = Murmur3Hasher_x86_128::new();

        h.write(b"hello");
        assert_eq!(h.finish_ext(),
                   u128::from_parts(11158567162092401078, 15821672119091348640));

        h.write(b"world");
        assert_eq!(h.finish_ext(),
                   u128::from_parts(4510970894511742178, 13166749202678098166));
    }

    #[test]
    fn test_murmur3_x64_128() {
        assert_eq!(Murmur3_x64_128::hash(b"hello"),
                   u128::from_parts(6565844092913065241, 14688674573012802306));
        assert_eq!(Murmur3_x64_128::hash_with_seed(b"hello", 123),
                   u128::from_parts(1043184066639555970, 3016954156110693643));
        assert_eq!(Murmur3_x64_128::hash(b"helloworld"),
                   u128::from_parts(11724578221562109303, 10256632503372987514));

        let mut h = Murmur3Hasher_x64_128::new();

        h.write(b"hello");
        assert_eq!(h.finish_ext(),
                   u128::from_parts(6565844092913065241, 14688674573012802306));

        h.write(b"world");
        assert_eq!(h.finish_ext(),
                   u128::from_parts(11724578221562109303, 10256632503372987514));
    }
}