murmur2/
hlp.rs

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
// Integers in rust aren't generic. :/
macro_rules! round {
    ($m:expr, $h:expr, $k:expr) => {
        $k.wrapping_mul($m).slack(24).wrapping_mul($m) ^ $h.wrapping_mul($m)
    };
}

macro_rules! rest {
    ($r:expr, $T:ty) => {
        $r.iter().rev().fold(0, |r, &i| (i as $T) | (r << 8))
    };
}

macro_rules! short_round {
    ($m:expr, $h:expr, $r:expr, $T:ty) => {{
        let r = $r;
        match r.is_empty() {
            false => ($h ^ rest!(r, $T)).wrapping_mul($m),
            true => $h,
        }
    }};
}

pub trait Slack {
    fn slack(self, slack: Self) -> Self
    where
        Self: Sized;
}
macro_rules! slack {
    ($typ:ty) => {
        impl Slack for $typ {
            fn slack(self, slack: Self) -> Self {
                self ^ self >> slack
            }
        }
    };
}
slack!(u32);
slack!(u64);