bmiintrin.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*===---- bmiintrin.h - BMI intrinsics -------------------------------------===
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. *
  21. *===-----------------------------------------------------------------------===
  22. */
  23. #if !defined __X86INTRIN_H && !defined __IMMINTRIN_H
  24. #error "Never use <bmiintrin.h> directly; include <x86intrin.h> instead."
  25. #endif
  26. #ifndef __BMIINTRIN_H
  27. #define __BMIINTRIN_H
  28. #define _tzcnt_u16(a) (__tzcnt_u16((a)))
  29. #define _andn_u32(a, b) (__andn_u32((a), (b)))
  30. /* _bextr_u32 != __bextr_u32 */
  31. #define _blsi_u32(a) (__blsi_u32((a)))
  32. #define _blsmsk_u32(a) (__blsmsk_u32((a)))
  33. #define _blsr_u32(a) (__blsr_u32((a)))
  34. #define _tzcnt_u32(a) (__tzcnt_u32((a)))
  35. /* Define the default attributes for the functions in this file. */
  36. #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
  37. /* Allow using the tzcnt intrinsics even for non-BMI targets. Since the TZCNT
  38. instruction behaves as BSF on non-BMI targets, there is code that expects
  39. to use it as a potentially faster version of BSF. */
  40. #define __RELAXED_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
  41. /// \brief Counts the number of trailing zero bits in the operand.
  42. ///
  43. /// \headerfile <x86intrin.h>
  44. ///
  45. /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
  46. ///
  47. /// \param __X
  48. /// An unsigned 16-bit integer whose trailing zeros are to be counted.
  49. /// \returns An unsigned 16-bit integer containing the number of trailing zero
  50. /// bits in the operand.
  51. static __inline__ unsigned short __RELAXED_FN_ATTRS
  52. __tzcnt_u16(unsigned short __X)
  53. {
  54. return __X ? __builtin_ctzs(__X) : 16;
  55. }
  56. /// \brief Performs a bitwise AND of the second operand with the one's
  57. /// complement of the first operand.
  58. ///
  59. /// \headerfile <x86intrin.h>
  60. ///
  61. /// This intrinsic corresponds to the <c> ANDN </c> instruction.
  62. ///
  63. /// \param __X
  64. /// An unsigned integer containing one of the operands.
  65. /// \param __Y
  66. /// An unsigned integer containing one of the operands.
  67. /// \returns An unsigned integer containing the bitwise AND of the second
  68. /// operand with the one's complement of the first operand.
  69. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  70. __andn_u32(unsigned int __X, unsigned int __Y)
  71. {
  72. return ~__X & __Y;
  73. }
  74. /* AMD-specified, double-leading-underscore version of BEXTR */
  75. /// \brief Extracts the specified bits from the first operand and returns them
  76. /// in the least significant bits of the result.
  77. ///
  78. /// \headerfile <x86intrin.h>
  79. ///
  80. /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
  81. ///
  82. /// \param __X
  83. /// An unsigned integer whose bits are to be extracted.
  84. /// \param __Y
  85. /// An unsigned integer used to specify which bits are extracted. Bits [7:0]
  86. /// specify the index of the least significant bit. Bits [15:8] specify the
  87. /// number of bits to be extracted.
  88. /// \returns An unsigned integer whose least significant bits contain the
  89. /// extracted bits.
  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. /* Intel-specified, single-leading-underscore version of BEXTR */
  96. /// \brief Extracts the specified bits from the first operand and returns them
  97. /// in the least significant bits of the result.
  98. ///
  99. /// \headerfile <x86intrin.h>
  100. ///
  101. /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
  102. ///
  103. /// \param __X
  104. /// An unsigned integer whose bits are to be extracted.
  105. /// \param __Y
  106. /// An unsigned integer used to specify the index of the least significant
  107. /// bit for the bits to be extracted. Bits [7:0] specify the index.
  108. /// \param __Z
  109. /// An unsigned integer used to specify the number of bits to be extracted.
  110. /// Bits [7:0] specify the number of bits.
  111. /// \returns An unsigned integer whose least significant bits contain the
  112. /// extracted bits.
  113. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  114. _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
  115. {
  116. return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
  117. }
  118. /// \brief Clears all bits in the source except for the least significant bit
  119. /// containing a value of 1 and returns the result.
  120. ///
  121. /// \headerfile <x86intrin.h>
  122. ///
  123. /// This intrinsic corresponds to the <c> BLSI </c> instruction.
  124. ///
  125. /// \param __X
  126. /// An unsigned integer whose bits are to be cleared.
  127. /// \returns An unsigned integer containing the result of clearing the bits from
  128. /// the source operand.
  129. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  130. __blsi_u32(unsigned int __X)
  131. {
  132. return __X & -__X;
  133. }
  134. /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
  135. /// including the least significant bit that is set to 1 in the source
  136. /// operand and returns the result.
  137. ///
  138. /// \headerfile <x86intrin.h>
  139. ///
  140. /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
  141. ///
  142. /// \param __X
  143. /// An unsigned integer used to create the mask.
  144. /// \returns An unsigned integer containing the newly created mask.
  145. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  146. __blsmsk_u32(unsigned int __X)
  147. {
  148. return __X ^ (__X - 1);
  149. }
  150. /// \brief Clears the least significant bit that is set to 1 in the source
  151. /// operand and returns the result.
  152. ///
  153. /// \headerfile <x86intrin.h>
  154. ///
  155. /// This intrinsic corresponds to the <c> BLSR </c> instruction.
  156. ///
  157. /// \param __X
  158. /// An unsigned integer containing the operand to be cleared.
  159. /// \returns An unsigned integer containing the result of clearing the source
  160. /// operand.
  161. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  162. __blsr_u32(unsigned int __X)
  163. {
  164. return __X & (__X - 1);
  165. }
  166. /// \brief Counts the number of trailing zero bits in the operand.
  167. ///
  168. /// \headerfile <x86intrin.h>
  169. ///
  170. /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
  171. ///
  172. /// \param __X
  173. /// An unsigned 32-bit integer whose trailing zeros are to be counted.
  174. /// \returns An unsigned 32-bit integer containing the number of trailing zero
  175. /// bits in the operand.
  176. static __inline__ unsigned int __RELAXED_FN_ATTRS
  177. __tzcnt_u32(unsigned int __X)
  178. {
  179. return __X ? __builtin_ctz(__X) : 32;
  180. }
  181. /// \brief Counts the number of trailing zero bits in the operand.
  182. ///
  183. /// \headerfile <x86intrin.h>
  184. ///
  185. /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
  186. ///
  187. /// \param __X
  188. /// An unsigned 32-bit integer whose trailing zeros are to be counted.
  189. /// \returns An 32-bit integer containing the number of trailing zero bits in
  190. /// the operand.
  191. static __inline__ int __RELAXED_FN_ATTRS
  192. _mm_tzcnt_32(unsigned int __X)
  193. {
  194. return __X ? __builtin_ctz(__X) : 32;
  195. }
  196. #ifdef __x86_64__
  197. #define _andn_u64(a, b) (__andn_u64((a), (b)))
  198. /* _bextr_u64 != __bextr_u64 */
  199. #define _blsi_u64(a) (__blsi_u64((a)))
  200. #define _blsmsk_u64(a) (__blsmsk_u64((a)))
  201. #define _blsr_u64(a) (__blsr_u64((a)))
  202. #define _tzcnt_u64(a) (__tzcnt_u64((a)))
  203. /// \brief Performs a bitwise AND of the second operand with the one's
  204. /// complement of the first operand.
  205. ///
  206. /// \headerfile <x86intrin.h>
  207. ///
  208. /// This intrinsic corresponds to the <c> ANDN </c> instruction.
  209. ///
  210. /// \param __X
  211. /// An unsigned 64-bit integer containing one of the operands.
  212. /// \param __Y
  213. /// An unsigned 64-bit integer containing one of the operands.
  214. /// \returns An unsigned 64-bit integer containing the bitwise AND of the second
  215. /// operand with the one's complement of the first operand.
  216. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  217. __andn_u64 (unsigned long long __X, unsigned long long __Y)
  218. {
  219. return ~__X & __Y;
  220. }
  221. /* AMD-specified, double-leading-underscore version of BEXTR */
  222. /// \brief Extracts the specified bits from the first operand and returns them
  223. /// in the least significant bits of the result.
  224. ///
  225. /// \headerfile <x86intrin.h>
  226. ///
  227. /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
  228. ///
  229. /// \param __X
  230. /// An unsigned 64-bit integer whose bits are to be extracted.
  231. /// \param __Y
  232. /// An unsigned 64-bit integer used to specify which bits are extracted. Bits
  233. /// [7:0] specify the index of the least significant bit. Bits [15:8] specify
  234. /// the number of bits to be extracted.
  235. /// \returns An unsigned 64-bit integer whose least significant bits contain the
  236. /// extracted bits.
  237. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  238. __bextr_u64(unsigned long long __X, unsigned long long __Y)
  239. {
  240. return __builtin_ia32_bextr_u64(__X, __Y);
  241. }
  242. /* Intel-specified, single-leading-underscore version of BEXTR */
  243. /// \brief Extracts the specified bits from the first operand and returns them
  244. /// in the least significant bits of the result.
  245. ///
  246. /// \headerfile <x86intrin.h>
  247. ///
  248. /// This intrinsic corresponds to the <c> BEXTR </c> instruction.
  249. ///
  250. /// \param __X
  251. /// An unsigned 64-bit integer whose bits are to be extracted.
  252. /// \param __Y
  253. /// An unsigned integer used to specify the index of the least significant
  254. /// bit for the bits to be extracted. Bits [7:0] specify the index.
  255. /// \param __Z
  256. /// An unsigned integer used to specify the number of bits to be extracted.
  257. /// Bits [7:0] specify the number of bits.
  258. /// \returns An unsigned 64-bit integer whose least significant bits contain the
  259. /// extracted bits.
  260. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  261. _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
  262. {
  263. return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
  264. }
  265. /// \brief Clears all bits in the source except for the least significant bit
  266. /// containing a value of 1 and returns the result.
  267. ///
  268. /// \headerfile <x86intrin.h>
  269. ///
  270. /// This intrinsic corresponds to the <c> BLSI </c> instruction.
  271. ///
  272. /// \param __X
  273. /// An unsigned 64-bit integer whose bits are to be cleared.
  274. /// \returns An unsigned 64-bit integer containing the result of clearing the
  275. /// bits from the source operand.
  276. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  277. __blsi_u64(unsigned long long __X)
  278. {
  279. return __X & -__X;
  280. }
  281. /// \brief Creates a mask whose bits are set to 1, using bit 0 up to and
  282. /// including the least significant bit that is set to 1 in the source
  283. /// operand and returns the result.
  284. ///
  285. /// \headerfile <x86intrin.h>
  286. ///
  287. /// This intrinsic corresponds to the <c> BLSMSK </c> instruction.
  288. ///
  289. /// \param __X
  290. /// An unsigned 64-bit integer used to create the mask.
  291. /// \returns An unsigned 64-bit integer containing the newly created mask.
  292. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  293. __blsmsk_u64(unsigned long long __X)
  294. {
  295. return __X ^ (__X - 1);
  296. }
  297. /// \brief Clears the least significant bit that is set to 1 in the source
  298. /// operand and returns the result.
  299. ///
  300. /// \headerfile <x86intrin.h>
  301. ///
  302. /// This intrinsic corresponds to the <c> BLSR </c> instruction.
  303. ///
  304. /// \param __X
  305. /// An unsigned 64-bit integer containing the operand to be cleared.
  306. /// \returns An unsigned 64-bit integer containing the result of clearing the
  307. /// source operand.
  308. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  309. __blsr_u64(unsigned long long __X)
  310. {
  311. return __X & (__X - 1);
  312. }
  313. /// \brief Counts the number of trailing zero bits in the operand.
  314. ///
  315. /// \headerfile <x86intrin.h>
  316. ///
  317. /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
  318. ///
  319. /// \param __X
  320. /// An unsigned 64-bit integer whose trailing zeros are to be counted.
  321. /// \returns An unsigned 64-bit integer containing the number of trailing zero
  322. /// bits in the operand.
  323. static __inline__ unsigned long long __RELAXED_FN_ATTRS
  324. __tzcnt_u64(unsigned long long __X)
  325. {
  326. return __X ? __builtin_ctzll(__X) : 64;
  327. }
  328. /// \brief Counts the number of trailing zero bits in the operand.
  329. ///
  330. /// \headerfile <x86intrin.h>
  331. ///
  332. /// This intrinsic corresponds to the <c> TZCNT </c> instruction.
  333. ///
  334. /// \param __X
  335. /// An unsigned 64-bit integer whose trailing zeros are to be counted.
  336. /// \returns An 64-bit integer containing the number of trailing zero bits in
  337. /// the operand.
  338. static __inline__ long long __RELAXED_FN_ATTRS
  339. _mm_tzcnt_64(unsigned long long __X)
  340. {
  341. return __X ? __builtin_ctzll(__X) : 64;
  342. }
  343. #endif /* __x86_64__ */
  344. #undef __DEFAULT_FN_ATTRS
  345. #undef __RELAXED_FN_ATTRS
  346. #endif /* __BMIINTRIN_H */