core/stdarch/crates/core_arch/src/x86_64/
bmi.rs1#[cfg(test)]
13use stdarch_test::assert_instr;
14
15#[inline]
20#[target_feature(enable = "bmi1")]
21#[cfg_attr(test, assert_instr(bextr))]
22#[cfg(not(target_arch = "x86"))]
23#[stable(feature = "simd_x86", since = "1.27.0")]
24pub fn _bextr_u64(a: u64, start: u32, len: u32) -> u64 {
25 _bextr2_u64(a, ((start & 0xff) | ((len & 0xff) << 8)) as u64)
26}
27
28#[inline]
36#[target_feature(enable = "bmi1")]
37#[cfg_attr(test, assert_instr(bextr))]
38#[cfg(not(target_arch = "x86"))]
39#[stable(feature = "simd_x86", since = "1.27.0")]
40pub fn _bextr2_u64(a: u64, control: u64) -> u64 {
41 unsafe { x86_bmi_bextr_64(a, control) }
42}
43
44#[inline]
48#[target_feature(enable = "bmi1")]
49#[cfg_attr(test, assert_instr(andn))]
50#[stable(feature = "simd_x86", since = "1.27.0")]
51#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
52pub const fn _andn_u64(a: u64, b: u64) -> u64 {
53 !a & b
54}
55
56#[inline]
60#[target_feature(enable = "bmi1")]
61#[cfg_attr(test, assert_instr(blsi))]
62#[cfg(not(target_arch = "x86"))] #[stable(feature = "simd_x86", since = "1.27.0")]
64#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
65pub const fn _blsi_u64(x: u64) -> u64 {
66 x & x.wrapping_neg()
67}
68
69#[inline]
73#[target_feature(enable = "bmi1")]
74#[cfg_attr(test, assert_instr(blsmsk))]
75#[cfg(not(target_arch = "x86"))] #[stable(feature = "simd_x86", since = "1.27.0")]
77#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
78pub const fn _blsmsk_u64(x: u64) -> u64 {
79 x ^ (x.wrapping_sub(1_u64))
80}
81
82#[inline]
88#[target_feature(enable = "bmi1")]
89#[cfg_attr(test, assert_instr(blsr))]
90#[cfg(not(target_arch = "x86"))] #[stable(feature = "simd_x86", since = "1.27.0")]
92#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
93pub const fn _blsr_u64(x: u64) -> u64 {
94 x & (x.wrapping_sub(1))
95}
96
97#[inline]
103#[target_feature(enable = "bmi1")]
104#[cfg_attr(test, assert_instr(tzcnt))]
105#[stable(feature = "simd_x86", since = "1.27.0")]
106#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
107pub const fn _tzcnt_u64(x: u64) -> u64 {
108 x.trailing_zeros() as u64
109}
110
111#[inline]
117#[target_feature(enable = "bmi1")]
118#[cfg_attr(test, assert_instr(tzcnt))]
119#[stable(feature = "simd_x86", since = "1.27.0")]
120#[rustc_const_unstable(feature = "stdarch_const_x86", issue = "149298")]
121pub const fn _mm_tzcnt_64(x: u64) -> i64 {
122 x.trailing_zeros() as i64
123}
124
125unsafe extern "C" {
126 #[link_name = "llvm.x86.bmi.bextr.64"]
127 fn x86_bmi_bextr_64(x: u64, y: u64) -> u64;
128}
129
130#[cfg(test)]
131mod tests {
132 use crate::core_arch::assert_eq_const as assert_eq;
133 use stdarch_test::simd_test;
134
135 use crate::core_arch::{x86::*, x86_64::*};
136
137 #[simd_test(enable = "bmi1")]
138 fn test_bextr_u64() {
139 let r = _bextr_u64(0b0101_0000u64, 4, 4);
140 assert_eq!(r, 0b0000_0101u64);
141 }
142
143 #[simd_test(enable = "bmi1")]
144 const fn test_andn_u64() {
145 assert_eq!(_andn_u64(0, 0), 0);
146 assert_eq!(_andn_u64(0, 1), 1);
147 assert_eq!(_andn_u64(1, 0), 0);
148 assert_eq!(_andn_u64(1, 1), 0);
149
150 let r = _andn_u64(0b0000_0000u64, 0b0000_0000u64);
151 assert_eq!(r, 0b0000_0000u64);
152
153 let r = _andn_u64(0b0000_0000u64, 0b1111_1111u64);
154 assert_eq!(r, 0b1111_1111u64);
155
156 let r = _andn_u64(0b1111_1111u64, 0b0000_0000u64);
157 assert_eq!(r, 0b0000_0000u64);
158
159 let r = _andn_u64(0b1111_1111u64, 0b1111_1111u64);
160 assert_eq!(r, 0b0000_0000u64);
161
162 let r = _andn_u64(0b0100_0000u64, 0b0101_1101u64);
163 assert_eq!(r, 0b0001_1101u64);
164 }
165
166 #[simd_test(enable = "bmi1")]
167 const fn test_blsi_u64() {
168 assert_eq!(_blsi_u64(0b1101_0000u64), 0b0001_0000u64);
169 }
170
171 #[simd_test(enable = "bmi1")]
172 const fn test_blsmsk_u64() {
173 let r = _blsmsk_u64(0b0011_0000u64);
174 assert_eq!(r, 0b0001_1111u64);
175 }
176
177 #[simd_test(enable = "bmi1")]
178 const fn test_blsr_u64() {
179 let r = _blsr_u64(0b0011_0000u64);
181 assert_eq!(r, 0b0010_0000u64);
182 }
183
184 #[simd_test(enable = "bmi1")]
185 const fn test_tzcnt_u64() {
186 assert_eq!(_tzcnt_u64(0b0000_0001u64), 0u64);
187 assert_eq!(_tzcnt_u64(0b0000_0000u64), 64u64);
188 assert_eq!(_tzcnt_u64(0b1001_0000u64), 4u64);
189 }
190}