bmiintrin.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 __BMI__
  27. # error "BMI instruction set not enabled"
  28. #endif /* __BMI__ */
  29. #ifndef __BMIINTRIN_H
  30. #define __BMIINTRIN_H
  31. #define _tzcnt_u16(a) (__tzcnt_u16((a)))
  32. #define _andn_u32(a, b) (__andn_u32((a), (b)))
  33. /* _bextr_u32 != __bextr_u32 */
  34. #define _blsi_u32(a) (__blsi_u32((a)))
  35. #define _blsmsk_u32(a) (__blsmsk_u32((a)))
  36. #define _blsr_u32(a) (__blsr_u32((a)))
  37. #define _tzcnt_u32(a) (__tzcnt_u32((a)))
  38. /* Define the default attributes for the functions in this file. */
  39. #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
  40. static __inline__ unsigned short __DEFAULT_FN_ATTRS
  41. __tzcnt_u16(unsigned short __X)
  42. {
  43. return __X ? __builtin_ctzs(__X) : 16;
  44. }
  45. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  46. __andn_u32(unsigned int __X, unsigned int __Y)
  47. {
  48. return ~__X & __Y;
  49. }
  50. /* AMD-specified, double-leading-underscore version of BEXTR */
  51. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  52. __bextr_u32(unsigned int __X, unsigned int __Y)
  53. {
  54. return __builtin_ia32_bextr_u32(__X, __Y);
  55. }
  56. /* Intel-specified, single-leading-underscore version of BEXTR */
  57. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  58. _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
  59. {
  60. return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
  61. }
  62. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  63. __blsi_u32(unsigned int __X)
  64. {
  65. return __X & -__X;
  66. }
  67. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  68. __blsmsk_u32(unsigned int __X)
  69. {
  70. return __X ^ (__X - 1);
  71. }
  72. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  73. __blsr_u32(unsigned int __X)
  74. {
  75. return __X & (__X - 1);
  76. }
  77. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  78. __tzcnt_u32(unsigned int __X)
  79. {
  80. return __X ? __builtin_ctz(__X) : 32;
  81. }
  82. #ifdef __x86_64__
  83. #define _andn_u64(a, b) (__andn_u64((a), (b)))
  84. /* _bextr_u64 != __bextr_u64 */
  85. #define _blsi_u64(a) (__blsi_u64((a)))
  86. #define _blsmsk_u64(a) (__blsmsk_u64((a)))
  87. #define _blsr_u64(a) (__blsr_u64((a)))
  88. #define _tzcnt_u64(a) (__tzcnt_u64((a)))
  89. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  90. __andn_u64 (unsigned long long __X, unsigned long long __Y)
  91. {
  92. return ~__X & __Y;
  93. }
  94. /* AMD-specified, double-leading-underscore version of BEXTR */
  95. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  96. __bextr_u64(unsigned long long __X, unsigned long long __Y)
  97. {
  98. return __builtin_ia32_bextr_u64(__X, __Y);
  99. }
  100. /* Intel-specified, single-leading-underscore version of BEXTR */
  101. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  102. _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
  103. {
  104. return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
  105. }
  106. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  107. __blsi_u64(unsigned long long __X)
  108. {
  109. return __X & -__X;
  110. }
  111. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  112. __blsmsk_u64(unsigned long long __X)
  113. {
  114. return __X ^ (__X - 1);
  115. }
  116. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  117. __blsr_u64(unsigned long long __X)
  118. {
  119. return __X & (__X - 1);
  120. }
  121. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  122. __tzcnt_u64(unsigned long long __X)
  123. {
  124. return __X ? __builtin_ctzll(__X) : 64;
  125. }
  126. #endif /* __x86_64__ */
  127. #undef __DEFAULT_FN_ATTRS
  128. #endif /* __BMIINTRIN_H */