#![allow(unsafe_code)]
#[inline]
pub(crate) fn pext_u32(x: u32, mask: u32) -> u32 {
#[cfg(all(target_arch = "x86_64", target_feature = "bmi2"))]
{
return unsafe { core::arch::x86_64::_pext_u32(x, mask) };
}
#[cfg(all(
target_arch = "x86_64",
not(target_feature = "bmi2"),
feature = "runtime-detect",
))]
{
if std::is_x86_feature_detected!("bmi2") {
return unsafe { core::arch::x86_64::_pext_u32(x, mask) };
}
}
pext_u32_swar(x, mask)
}
#[inline]
pub(crate) fn pdep_u32(x: u32, mask: u32) -> u32 {
#[cfg(all(target_arch = "x86_64", target_feature = "bmi2"))]
{
return unsafe { core::arch::x86_64::_pdep_u32(x, mask) };
}
#[cfg(all(
target_arch = "x86_64",
not(target_feature = "bmi2"),
feature = "runtime-detect",
))]
{
if std::is_x86_feature_detected!("bmi2") {
return unsafe { core::arch::x86_64::_pdep_u32(x, mask) };
}
}
pdep_u32_swar(x, mask)
}
#[inline]
pub(crate) const fn pext_u32_swar(x: u32, mut mask: u32) -> u32 {
let mut res = 0u32;
let mut bb = 1u32;
while mask != 0 {
let lowest = mask & mask.wrapping_neg();
if (x & lowest) != 0 { res |= bb; }
mask ^= lowest;
bb <<= 1;
}
res
}
#[inline]
pub(crate) const fn pdep_u32_swar(x: u32, mut mask: u32) -> u32 {
let mut res = 0u32;
let mut bb = 1u32;
while mask != 0 {
let lowest = mask & mask.wrapping_neg();
if (x & bb) != 0 { res |= lowest; }
mask ^= lowest;
bb <<= 1;
}
res
}
#[inline]
pub(crate) fn pext_u64(x: u64, mask: u64) -> u64 {
#[cfg(all(target_arch = "x86_64", target_feature = "bmi2"))]
{
return unsafe { core::arch::x86_64::_pext_u64(x, mask) };
}
#[cfg(all(
target_arch = "x86_64",
not(target_feature = "bmi2"),
feature = "runtime-detect",
))]
{
if std::is_x86_feature_detected!("bmi2") {
return unsafe { core::arch::x86_64::_pext_u64(x, mask) };
}
}
pext_u64_swar(x, mask)
}
#[inline]
pub(crate) fn pdep_u64(x: u64, mask: u64) -> u64 {
#[cfg(all(target_arch = "x86_64", target_feature = "bmi2"))]
{
return unsafe { core::arch::x86_64::_pdep_u64(x, mask) };
}
#[cfg(all(
target_arch = "x86_64",
not(target_feature = "bmi2"),
feature = "runtime-detect",
))]
{
if std::is_x86_feature_detected!("bmi2") {
return unsafe { core::arch::x86_64::_pdep_u64(x, mask) };
}
}
pdep_u64_swar(x, mask)
}
#[inline]
pub(crate) const fn pext_u64_swar(x: u64, mut mask: u64) -> u64 {
let mut res = 0u64;
let mut bb = 1u64;
while mask != 0 {
let lowest = mask & mask.wrapping_neg();
if (x & lowest) != 0 { res |= bb; }
mask ^= lowest;
bb <<= 1;
}
res
}
#[inline]
pub(crate) const fn pdep_u64_swar(x: u64, mut mask: u64) -> u64 {
let mut res = 0u64;
let mut bb = 1u64;
while mask != 0 {
let lowest = mask & mask.wrapping_neg();
if (x & bb) != 0 { res |= lowest; }
mask ^= lowest;
bb <<= 1;
}
res
}