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

#[cfg(RUSTC_IS_NIGHTLY)]
mod intrinsics {
    use alg;
    use int::Int;

    #[allow(dead_code)]
    extern "platform-intrinsic" {
        fn x86_bmi2_bzhi_32(x: u32, y: u32) -> u32;
        fn x86_bmi2_bzhi_64(x: u64, y: u64) -> u64;
    }

    #[inline]
    pub unsafe fn bzhi<T: Int>(x: T, y: T) -> T {
        match T::bit_size().to_u8() {
            32 => T::from_u32(x86_bmi2_bzhi_32(x.to_u32(), y.to_u32())),
            64 => T::from_u64(x86_bmi2_bzhi_64(x.to_u64(), y.to_u64())),
            _ => alg::x86::bmi2::bzhi(x, y),
        }
    }

} // mod intrinsics

/// Zero the high bits of `x` at position >= `bit_position`.
///
/// # Panics
///
/// If `bit_position >= bit_size()` the behavior is undefined (panics in debug
/// builds).
///
/// # Assembly Instructions
///
/// - [`BZHI`](http://www.felixcloutier.com/x86/BZHI.html):
///   - Description: Zero high bits starting with specified bit position.
///   - Architecture: x86.
///   - Instruction set: BMI2.
///   - Registers: 32/64 bit.
///
/// # Example
///
/// ```
/// use bitintr::x86::bmi2::*;
///
/// let n = 0b1111_0010u32;
/// let s = 0b0001_0010u32;
/// assert_eq!(bzhi(n, 5), s);
/// assert_eq!(n.bzhi(5), s);
/// ```
#[inline]
pub fn bzhi<T: Int>(x: T, bit_position: T) -> T {
    #[cfg(RUSTC_IS_NIGHTLY)]
    {
        if cfg!(target_feature = "bmi2") {
            debug_assert!(bit_position < T::bit_size());
            unsafe { intrinsics::bzhi(x, bit_position) }
        } else {
            alg::x86::bmi2::bzhi(x, bit_position)
        }
    }
    #[cfg(not(RUSTC_IS_NIGHTLY))]
    {
        alg::x86::bmi2::bzhi(x, bit_position)
    }
}

/// Method version of [`bzhi`](fn.bzhi.html).
pub trait BZHI {
    #[inline]
    fn bzhi(self, Self) -> Self;
}

impl<T: Int> BZHI for T {
    #[inline]
    fn bzhi(self, y: Self) -> Self {
        bzhi(self, y)
    }
}