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
use int::Int;

/// Counts the leading most significant zero bits.
///
/// When the operand is zero, it returns its size in bits.
///
/// **Keywords**: Count leading zeros, bit scan reverse, find last set.
///
/// # Intrinsic (when available: ABM / SSE4.2)
///
/// [`LZCNT`](http://www.felixcloutier.com/x86/LZCNT.html): Count the number of
/// leading zero bits (supports 16/32/64 bit registers).
///
/// Note: This instruction is officialy part of BMI1 but Intel and AMD CPUs
/// advertise it as being part of ABM.
///
/// # Example
/// ```
/// use bitintr::abm::lzcnt;
///
/// assert_eq!(lzcnt(0b0101_1010u16), 9u16);
/// ```
pub fn lzcnt<T: Int>(x: T) -> T {
    x.leading_zeros() // TODO: software emulation
}

pub trait LZCNT {
    fn lzcnt(self) -> Self;
}

impl<T: Int> LZCNT for T {
    fn lzcnt(self) -> T {
        lzcnt(self)
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn lzcnt_test() {
        {
            use alg::abm::lzcnt::*;
            assert_eq!(lzcnt(0b0101_1010u16), 9u16);
            assert_eq!(LZCNT::lzcnt(0b0101_1010u16), 9u16);
            assert_eq!(0b0101_1010u16.lzcnt(), 9u16);
        }
    }
}