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

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

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

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

} // mod intrinsics

/// Parallel bits deposit.
///
/// Scatter contiguous low order bits of `x` to the result at the positions
/// specified by the `mask_`.
///
/// All other bits (bits not set in the mask) of the result are set to zero.
///
/// **Keywords**: Parallel bits deposit, scatter bits.
///
/// # Assembly Instructions
///
/// - [`PDEP`](http://www.felixcloutier.com/x86/PDEP.html):
///   - Description: Parallel bits deposit.
///   - Architecture: x86.
///   - Instruction set: BMI2.
///   - Registers: 32/64 bit.
///
/// # Example
/// ```
/// use bitintr::x86::bmi2::*;
/// let n  = 0b1011_1110_1001_0011u16;
///
/// let m0 = 0b0110_0011_1000_0101u16;
/// let s0 = 0b0000_0010_0000_0101u16;
///
/// let m1 = 0b1110_1011_1110_1111u16;
/// let s1 = 0b1110_1001_0010_0011u16;
///
/// assert_eq!(pdep(n, m0), s0);
/// assert_eq!(n.pdep(m1), s1);
/// ```
#[inline]
pub fn pdep<T: Int>(x: T, mask: T) -> T {
    #[cfg(RUSTC_IS_NIGHTLY)]
    {
        if cfg!(target_feature = "bmi2") {
            unsafe { intrinsics::pdep(x, mask) }
        } else {
            alg::x86::bmi2::pdep(x, mask)
        }
    }
    #[cfg(not(RUSTC_IS_NIGHTLY))]
    {
        alg::x86::bmi2::pdep(x, mask)
    }
}

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

impl<T: Int> PDEP for T {
    #[inline]
    fn pdep(self, mask: Self) -> Self {
        pdep(self, mask)
    }
}