1 /*===---- bmiintrin.h - BMI intrinsics -------------------------------------=== 2 * 3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 * See https://llvm.org/LICENSE.txt for license information. 5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 * 7 *===-----------------------------------------------------------------------=== 8 */ 9 10 #if !defined __X86INTRIN_H && !defined __IMMINTRIN_H 11 #error "Never use <bmiintrin.h> directly; include <x86intrin.h> instead." 12 #endif 13 14 #ifndef __BMIINTRIN_H 15 #define __BMIINTRIN_H 16 17 #define _tzcnt_u16(a) (__tzcnt_u16((a))) 18 19 #define _andn_u32(a, b) (__andn_u32((a), (b))) 20 21 /* _bextr_u32 != __bextr_u32 */ 22 #define _blsi_u32(a) (__blsi_u32((a))) 23 24 #define _blsmsk_u32(a) (__blsmsk_u32((a))) 25 26 #define _blsr_u32(a) (__blsr_u32((a))) 27 28 #define _tzcnt_u32(a) (__tzcnt_u32((a))) 29 30 /* Define the default attributes for the functions in this file. */ 31 #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi"))) 32 33 /* Allow using the tzcnt intrinsics even for non-BMI targets. Since the TZCNT 34 instruction behaves as BSF on non-BMI targets, there is code that expects 35 to use it as a potentially faster version of BSF. */ 36 #define __RELAXED_FN_ATTRS __attribute__((__always_inline__, __nodebug__)) 37 38 /// Counts the number of trailing zero bits in the operand. 39 /// 40 /// \headerfile <x86intrin.h> 41 /// 42 /// This intrinsic corresponds to the <c> TZCNT </c> instruction. 43 /// 44 /// \param __X 45 /// An unsigned 16-bit integer whose trailing zeros are to be counted. 46 /// \returns An unsigned 16-bit integer containing the number of trailing zero 47 /// bits in the operand. 48 static __inline__ unsigned short __RELAXED_FN_ATTRS 49 __tzcnt_u16(unsigned short __X) 50 { 51 return __builtin_ia32_tzcnt_u16(__X); 52 } 53 54 /// Performs a bitwise AND of the second operand with the one's 55 /// complement of the first operand. 56 /// 57 /// \headerfile <x86intrin.h> 58 /// 59 /// This intrinsic corresponds to the <c> ANDN </c> instruction. 60 /// 61 /// \param __X 62 /// An unsigned integer containing one of the operands. 63 /// \param __Y 64 /// An unsigned integer containing one of the operands. 65 /// \returns An unsigned integer containing the bitwise AND of the second 66 /// operand with the one's complement of the first operand. 67 static __inline__ unsigned int __DEFAULT_FN_ATTRS 68 __andn_u32(unsigned int __X, unsigned int __Y) 69 { 70 return ~__X & __Y; 71 } 72 73 /* AMD-specified, double-leading-underscore version of BEXTR */ 74 /// Extracts the specified bits from the first operand and returns them 75 /// in the least significant bits of the result. 76 /// 77 /// \headerfile <x86intrin.h> 78 /// 79 /// This intrinsic corresponds to the <c> BEXTR </c> instruction. 80 /// 81 /// \param __X 82 /// An unsigned integer whose bits are to be extracted. 83 /// \param __Y 84 /// An unsigned integer used to specify which bits are extracted. Bits [7:0] 85 /// specify the index of the least significant bit. Bits [15:8] specify the 86 /// number of bits to be extracted. 87 /// \returns An unsigned integer whose least significant bits contain the 88 /// extracted bits. 89 /// \see _bextr_u32 90 static __inline__ unsigned int __DEFAULT_FN_ATTRS 91 __bextr_u32(unsigned int __X, unsigned int __Y) 92 { 93 return __builtin_ia32_bextr_u32(__X, __Y); 94 } 95 96 /* Intel-specified, single-leading-underscore version of BEXTR */ 97 /// Extracts the specified bits from the first operand and returns them 98 /// in the least significant bits of the result. 99 /// 100 /// \headerfile <x86intrin.h> 101 /// 102 /// This intrinsic corresponds to the <c> BEXTR </c> instruction. 103 /// 104 /// \param __X 105 /// An unsigned integer whose bits are to be extracted. 106 /// \param __Y 107 /// An unsigned integer used to specify the index of the least significant 108 /// bit for the bits to be extracted. Bits [7:0] specify the index. 109 /// \param __Z 110 /// An unsigned integer used to specify the number of bits to be extracted. 111 /// Bits [7:0] specify the number of bits. 112 /// \returns An unsigned integer whose least significant bits contain the 113 /// extracted bits. 114 /// \see __bextr_u32 115 static __inline__ unsigned int __DEFAULT_FN_ATTRS 116 _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z) 117 { 118 return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8))); 119 } 120 121 /// Clears all bits in the source except for the least significant bit 122 /// containing a value of 1 and returns the result. 123 /// 124 /// \headerfile <x86intrin.h> 125 /// 126 /// This intrinsic corresponds to the <c> BLSI </c> instruction. 127 /// 128 /// \param __X 129 /// An unsigned integer whose bits are to be cleared. 130 /// \returns An unsigned integer containing the result of clearing the bits from 131 /// the source operand. 132 static __inline__ unsigned int __DEFAULT_FN_ATTRS 133 __blsi_u32(unsigned int __X) 134 { 135 return __X & -__X; 136 } 137 138 /// Creates a mask whose bits are set to 1, using bit 0 up to and 139 /// including the least significant bit that is set to 1 in the source 140 /// operand and returns the result. 141 /// 142 /// \headerfile <x86intrin.h> 143 /// 144 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction. 145 /// 146 /// \param __X 147 /// An unsigned integer used to create the mask. 148 /// \returns An unsigned integer containing the newly created mask. 149 static __inline__ unsigned int __DEFAULT_FN_ATTRS 150 __blsmsk_u32(unsigned int __X) 151 { 152 return __X ^ (__X - 1); 153 } 154 155 /// Clears the least significant bit that is set to 1 in the source 156 /// operand and returns the result. 157 /// 158 /// \headerfile <x86intrin.h> 159 /// 160 /// This intrinsic corresponds to the <c> BLSR </c> instruction. 161 /// 162 /// \param __X 163 /// An unsigned integer containing the operand to be cleared. 164 /// \returns An unsigned integer containing the result of clearing the source 165 /// operand. 166 static __inline__ unsigned int __DEFAULT_FN_ATTRS 167 __blsr_u32(unsigned int __X) 168 { 169 return __X & (__X - 1); 170 } 171 172 /// Counts the number of trailing zero bits in the operand. 173 /// 174 /// \headerfile <x86intrin.h> 175 /// 176 /// This intrinsic corresponds to the <c> TZCNT </c> instruction. 177 /// 178 /// \param __X 179 /// An unsigned 32-bit integer whose trailing zeros are to be counted. 180 /// \returns An unsigned 32-bit integer containing the number of trailing zero 181 /// bits in the operand. 182 static __inline__ unsigned int __RELAXED_FN_ATTRS 183 __tzcnt_u32(unsigned int __X) 184 { 185 return __builtin_ia32_tzcnt_u32(__X); 186 } 187 188 /// Counts the number of trailing zero bits in the operand. 189 /// 190 /// \headerfile <x86intrin.h> 191 /// 192 /// This intrinsic corresponds to the <c> TZCNT </c> instruction. 193 /// 194 /// \param __X 195 /// An unsigned 32-bit integer whose trailing zeros are to be counted. 196 /// \returns An 32-bit integer containing the number of trailing zero bits in 197 /// the operand. 198 static __inline__ int __RELAXED_FN_ATTRS 199 _mm_tzcnt_32(unsigned int __X) 200 { 201 return __builtin_ia32_tzcnt_u32(__X); 202 } 203 204 #ifdef __x86_64__ 205 206 #define _andn_u64(a, b) (__andn_u64((a), (b))) 207 208 /* _bextr_u64 != __bextr_u64 */ 209 #define _blsi_u64(a) (__blsi_u64((a))) 210 211 #define _blsmsk_u64(a) (__blsmsk_u64((a))) 212 213 #define _blsr_u64(a) (__blsr_u64((a))) 214 215 #define _tzcnt_u64(a) (__tzcnt_u64((a))) 216 217 /// Performs a bitwise AND of the second operand with the one's 218 /// complement of the first operand. 219 /// 220 /// \headerfile <x86intrin.h> 221 /// 222 /// This intrinsic corresponds to the <c> ANDN </c> instruction. 223 /// 224 /// \param __X 225 /// An unsigned 64-bit integer containing one of the operands. 226 /// \param __Y 227 /// An unsigned 64-bit integer containing one of the operands. 228 /// \returns An unsigned 64-bit integer containing the bitwise AND of the second 229 /// operand with the one's complement of the first operand. 230 static __inline__ unsigned long long __DEFAULT_FN_ATTRS 231 __andn_u64 (unsigned long long __X, unsigned long long __Y) 232 { 233 return ~__X & __Y; 234 } 235 236 /* AMD-specified, double-leading-underscore version of BEXTR */ 237 /// Extracts the specified bits from the first operand and returns them 238 /// in the least significant bits of the result. 239 /// 240 /// \headerfile <x86intrin.h> 241 /// 242 /// This intrinsic corresponds to the <c> BEXTR </c> instruction. 243 /// 244 /// \param __X 245 /// An unsigned 64-bit integer whose bits are to be extracted. 246 /// \param __Y 247 /// An unsigned 64-bit integer used to specify which bits are extracted. Bits 248 /// [7:0] specify the index of the least significant bit. Bits [15:8] specify 249 /// the number of bits to be extracted. 250 /// \returns An unsigned 64-bit integer whose least significant bits contain the 251 /// extracted bits. 252 /// \see _bextr_u64 253 static __inline__ unsigned long long __DEFAULT_FN_ATTRS 254 __bextr_u64(unsigned long long __X, unsigned long long __Y) 255 { 256 return __builtin_ia32_bextr_u64(__X, __Y); 257 } 258 259 /* Intel-specified, single-leading-underscore version of BEXTR */ 260 /// Extracts the specified bits from the first operand and returns them 261 /// in the least significant bits of the result. 262 /// 263 /// \headerfile <x86intrin.h> 264 /// 265 /// This intrinsic corresponds to the <c> BEXTR </c> instruction. 266 /// 267 /// \param __X 268 /// An unsigned 64-bit integer whose bits are to be extracted. 269 /// \param __Y 270 /// An unsigned integer used to specify the index of the least significant 271 /// bit for the bits to be extracted. Bits [7:0] specify the index. 272 /// \param __Z 273 /// An unsigned integer used to specify the number of bits to be extracted. 274 /// Bits [7:0] specify the number of bits. 275 /// \returns An unsigned 64-bit integer whose least significant bits contain the 276 /// extracted bits. 277 /// \see __bextr_u64 278 static __inline__ unsigned long long __DEFAULT_FN_ATTRS 279 _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z) 280 { 281 return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8))); 282 } 283 284 /// Clears all bits in the source except for the least significant bit 285 /// containing a value of 1 and returns the result. 286 /// 287 /// \headerfile <x86intrin.h> 288 /// 289 /// This intrinsic corresponds to the <c> BLSI </c> instruction. 290 /// 291 /// \param __X 292 /// An unsigned 64-bit integer whose bits are to be cleared. 293 /// \returns An unsigned 64-bit integer containing the result of clearing the 294 /// bits from the source operand. 295 static __inline__ unsigned long long __DEFAULT_FN_ATTRS 296 __blsi_u64(unsigned long long __X) 297 { 298 return __X & -__X; 299 } 300 301 /// Creates a mask whose bits are set to 1, using bit 0 up to and 302 /// including the least significant bit that is set to 1 in the source 303 /// operand and returns the result. 304 /// 305 /// \headerfile <x86intrin.h> 306 /// 307 /// This intrinsic corresponds to the <c> BLSMSK </c> instruction. 308 /// 309 /// \param __X 310 /// An unsigned 64-bit integer used to create the mask. 311 /// \returns An unsigned 64-bit integer containing the newly created mask. 312 static __inline__ unsigned long long __DEFAULT_FN_ATTRS 313 __blsmsk_u64(unsigned long long __X) 314 { 315 return __X ^ (__X - 1); 316 } 317 318 /// Clears the least significant bit that is set to 1 in the source 319 /// operand and returns the result. 320 /// 321 /// \headerfile <x86intrin.h> 322 /// 323 /// This intrinsic corresponds to the <c> BLSR </c> instruction. 324 /// 325 /// \param __X 326 /// An unsigned 64-bit integer containing the operand to be cleared. 327 /// \returns An unsigned 64-bit integer containing the result of clearing the 328 /// source operand. 329 static __inline__ unsigned long long __DEFAULT_FN_ATTRS 330 __blsr_u64(unsigned long long __X) 331 { 332 return __X & (__X - 1); 333 } 334 335 /// Counts the number of trailing zero bits in the operand. 336 /// 337 /// \headerfile <x86intrin.h> 338 /// 339 /// This intrinsic corresponds to the <c> TZCNT </c> instruction. 340 /// 341 /// \param __X 342 /// An unsigned 64-bit integer whose trailing zeros are to be counted. 343 /// \returns An unsigned 64-bit integer containing the number of trailing zero 344 /// bits in the operand. 345 static __inline__ unsigned long long __RELAXED_FN_ATTRS 346 __tzcnt_u64(unsigned long long __X) 347 { 348 return __builtin_ia32_tzcnt_u64(__X); 349 } 350 351 /// Counts the number of trailing zero bits in the operand. 352 /// 353 /// \headerfile <x86intrin.h> 354 /// 355 /// This intrinsic corresponds to the <c> TZCNT </c> instruction. 356 /// 357 /// \param __X 358 /// An unsigned 64-bit integer whose trailing zeros are to be counted. 359 /// \returns An 64-bit integer containing the number of trailing zero bits in 360 /// the operand. 361 static __inline__ long long __RELAXED_FN_ATTRS 362 _mm_tzcnt_64(unsigned long long __X) 363 { 364 return __builtin_ia32_tzcnt_u64(__X); 365 } 366 367 #endif /* __x86_64__ */ 368 369 #undef __DEFAULT_FN_ATTRS 370 #undef __RELAXED_FN_ATTRS 371 372 #endif /* __BMIINTRIN_H */ 373