| 1 |
#![cfg(target_feature = "bmi1")] |
| 2 |
|
| 3 |
use super::*; |
| 4 |
|
| 5 |
/// Bitwise `(!a) & b` for `u32` |
| 6 |
/// |
| 7 |
/// * **Intrinsic:** [`_andn_u32`] |
| 8 |
/// * **Assembly:** `andn r32, r32, r32` |
| 9 |
#[must_use] |
| 10 |
#[inline(always)] |
| 11 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 12 |
pub fn bitandnot_u32(a: u32, b: u32) -> u32 { |
| 13 |
unsafe { _andn_u32(a, b) } |
| 14 |
} |
| 15 |
|
| 16 |
/// Bitwise `(!a) & b` for `u64` |
| 17 |
/// |
| 18 |
/// * **Intrinsic:** [`_andn_u64`] |
| 19 |
/// * **Assembly:** `andn r64, r64, r64` |
| 20 |
#[must_use] |
| 21 |
#[inline(always)] |
| 22 |
#[cfg(target_arch = "x86_64")] |
| 23 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 24 |
pub fn bitandnot_u64(a: u64, b: u64) -> u64 { |
| 25 |
unsafe { _andn_u64(a, b) } |
| 26 |
} |
| 27 |
|
| 28 |
/// Extract a span of bits from the `u32`, start and len style. |
| 29 |
/// |
| 30 |
/// * **Intrinsic:** [`_bextr_u32`] |
| 31 |
/// * **Assembly:** `bextr r32, r32, r32` |
| 32 |
#[must_use] |
| 33 |
#[inline(always)] |
| 34 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 35 |
pub fn bit_extract_u32(a: u32, start: u32, len: u32) -> u32 { |
| 36 |
unsafe { _bextr_u32(a, start, len) } |
| 37 |
} |
| 38 |
|
| 39 |
/// Extract a span of bits from the `u64`, start and len style. |
| 40 |
/// |
| 41 |
/// * **Intrinsic:** [`_bextr_u64`] |
| 42 |
/// * **Assembly:** `bextr r64, r64, r64` |
| 43 |
#[must_use] |
| 44 |
#[inline(always)] |
| 45 |
#[cfg(target_arch = "x86_64")] |
| 46 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 47 |
pub fn bit_extract_u64(a: u64, start: u32, len: u32) -> u64 { |
| 48 |
unsafe { _bextr_u64(a, start, len) } |
| 49 |
} |
| 50 |
|
| 51 |
/// Extract a span of bits from the `u32`, control value style. |
| 52 |
/// |
| 53 |
/// * Bits 0 through 7: start position. |
| 54 |
/// * Bits 8 through 15: span length. |
| 55 |
/// |
| 56 |
/// * **Intrinsic:** [`_bextr2_u32`] |
| 57 |
/// * **Assembly:** `bextr r32, r32, r32` |
| 58 |
#[must_use] |
| 59 |
#[inline(always)] |
| 60 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 61 |
pub fn bit_extract2_u32(a: u32, control: u32) -> u32 { |
| 62 |
unsafe { _bextr2_u32(a, control) } |
| 63 |
} |
| 64 |
|
| 65 |
/// Extract a span of bits from the `u64`, control value style. |
| 66 |
/// |
| 67 |
/// * Bits 0 through 7: start position. |
| 68 |
/// * Bits 8 through 15: span length. |
| 69 |
/// |
| 70 |
/// * **Intrinsic:** [`_bextr2_u64`] |
| 71 |
/// * **Assembly:** `bextr r64, r64, r64` |
| 72 |
#[must_use] |
| 73 |
#[inline(always)] |
| 74 |
#[cfg(target_arch = "x86_64")] |
| 75 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 76 |
pub fn bit_extract2_u64(a: u64, control: u64) -> u64 { |
| 77 |
unsafe { _bextr2_u64(a, control) } |
| 78 |
} |
| 79 |
|
| 80 |
/// Gets the *value* of the lowest set bit in a `u32`. |
| 81 |
/// |
| 82 |
/// If the input is 0 you get 0 back. |
| 83 |
/// |
| 84 |
/// * Formula: `(!a) & a` |
| 85 |
/// |
| 86 |
/// * **Intrinsic:** [`_blsi_u32`] |
| 87 |
/// * **Assembly:** `blsi r32, r32` |
| 88 |
#[must_use] |
| 89 |
#[inline(always)] |
| 90 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 91 |
pub fn bit_lowest_set_value_u32(a: u32) -> u32 { |
| 92 |
unsafe { _blsi_u32(a) } |
| 93 |
} |
| 94 |
|
| 95 |
/// Gets the *value* of the lowest set bit in a `u64`. |
| 96 |
/// |
| 97 |
/// If the input is 0 you get 0 back. |
| 98 |
/// |
| 99 |
/// * Formula: `(!a) & a` |
| 100 |
/// |
| 101 |
/// * **Intrinsic:** [`_blsi_u64`] |
| 102 |
/// * **Assembly:** `blsi r64, r64` |
| 103 |
#[must_use] |
| 104 |
#[inline(always)] |
| 105 |
#[cfg(target_arch = "x86_64")] |
| 106 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 107 |
pub fn bit_lowest_set_value_u64(a: u64) -> u64 { |
| 108 |
unsafe { _blsi_u64(a) } |
| 109 |
} |
| 110 |
|
| 111 |
/// Gets the mask of all bits up to and including the lowest set bit in a `u32`. |
| 112 |
/// |
| 113 |
/// If the input is 0, you get `u32::MAX` |
| 114 |
/// |
| 115 |
/// * Formula: `(a - 1) ^ a` |
| 116 |
/// |
| 117 |
/// * **Intrinsic:** [`_blsmsk_u32`] |
| 118 |
/// * **Assembly:** `blsmsk r32, r32` |
| 119 |
#[must_use] |
| 120 |
#[inline(always)] |
| 121 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 122 |
pub fn bit_lowest_set_mask_u32(a: u32) -> u32 { |
| 123 |
unsafe { _blsmsk_u32(a) } |
| 124 |
} |
| 125 |
|
| 126 |
/// Gets the mask of all bits up to and including the lowest set bit in a `u64`. |
| 127 |
/// |
| 128 |
/// If the input is 0, you get `u64::MAX` |
| 129 |
/// |
| 130 |
/// * Formula: `(a - 1) ^ a` |
| 131 |
/// |
| 132 |
/// * **Intrinsic:** [`_blsmsk_u64`] |
| 133 |
/// * **Assembly:** `blsmsk r64, r64` |
| 134 |
#[must_use] |
| 135 |
#[inline(always)] |
| 136 |
#[cfg(target_arch = "x86_64")] |
| 137 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 138 |
pub fn bit_lowest_set_mask_u64(a: u64) -> u64 { |
| 139 |
unsafe { _blsmsk_u64(a) } |
| 140 |
} |
| 141 |
|
| 142 |
/// Resets (clears) the lowest set bit. |
| 143 |
/// |
| 144 |
/// If the input is 0 you get 0 back. |
| 145 |
/// |
| 146 |
/// * Formula: `(a - 1) & a` |
| 147 |
/// |
| 148 |
/// * **Intrinsic:** [`_blsr_u32`] |
| 149 |
/// * **Assembly:** `blsr r32, r32` |
| 150 |
#[must_use] |
| 151 |
#[inline(always)] |
| 152 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 153 |
pub fn bit_lowest_set_reset_u32(a: u32) -> u32 { |
| 154 |
unsafe { _blsr_u32(a) } |
| 155 |
} |
| 156 |
|
| 157 |
/// Resets (clears) the lowest set bit. |
| 158 |
/// |
| 159 |
/// If the input is 0 you get 0 back. |
| 160 |
/// |
| 161 |
/// * Formula: `(a - 1) & a` |
| 162 |
/// |
| 163 |
/// * **Intrinsic:** [`_blsr_u64`] |
| 164 |
/// * **Assembly:** `blsr r64, r64` |
| 165 |
#[must_use] |
| 166 |
#[inline(always)] |
| 167 |
#[cfg(target_arch = "x86_64")] |
| 168 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 169 |
pub fn bit_lowest_set_reset_u64(a: u64) -> u64 { |
| 170 |
unsafe { _blsr_u64(a) } |
| 171 |
} |
| 172 |
|
| 173 |
/// Counts the number of trailing zero bits in a `u32`. |
| 174 |
/// |
| 175 |
/// An input of 0 gives 32. |
| 176 |
/// |
| 177 |
/// * **Intrinsic:** [`_tzcnt_u32`] |
| 178 |
/// * **Assembly:** `tzcnt r32, r32` |
| 179 |
#[must_use] |
| 180 |
#[inline(always)] |
| 181 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 182 |
pub fn trailing_zero_count_u32(a: u32) -> u32 { |
| 183 |
unsafe { _tzcnt_u32(a) } |
| 184 |
} |
| 185 |
|
| 186 |
/// Counts the number of trailing zero bits in a `u64`. |
| 187 |
/// |
| 188 |
/// An input of 0 gives 64. |
| 189 |
/// |
| 190 |
/// * **Intrinsic:** [`_tzcnt_u64`] |
| 191 |
/// * **Assembly:** `tzcnt r64, r64` |
| 192 |
#[must_use] |
| 193 |
#[inline(always)] |
| 194 |
#[cfg(target_arch = "x86_64")] |
| 195 |
#[cfg_attr(docsrs, doc(cfg(target_feature = "bmi1")))] |
| 196 |
pub fn trailing_zero_count_u64(a: u64) -> u64 { |
| 197 |
unsafe { _tzcnt_u64(a) } |
| 198 |
} |
| 199 |
|