lzcntintrin.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*===---- lzcntintrin.h - LZCNT 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 <lzcntintrin.h> directly; include <x86intrin.h> instead."
  25. #endif
  26. #ifndef __LZCNTINTRIN_H
  27. #define __LZCNTINTRIN_H
  28. /* Define the default attributes for the functions in this file. */
  29. #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("lzcnt")))
  30. /// \brief Counts the number of leading zero bits in the operand.
  31. ///
  32. /// \headerfile <x86intrin.h>
  33. ///
  34. /// This intrinsic corresponds to the \c LZCNT instruction.
  35. ///
  36. /// \param __X
  37. /// An unsigned 16-bit integer whose leading zeros are to be counted.
  38. /// \returns An unsigned 16-bit integer containing the number of leading zero
  39. /// bits in the operand.
  40. static __inline__ unsigned short __DEFAULT_FN_ATTRS
  41. __lzcnt16(unsigned short __X)
  42. {
  43. return __X ? __builtin_clzs(__X) : 16;
  44. }
  45. /// \brief Counts the number of leading zero bits in the operand.
  46. ///
  47. /// \headerfile <x86intrin.h>
  48. ///
  49. /// This intrinsic corresponds to the \c LZCNT instruction.
  50. ///
  51. /// \param __X
  52. /// An unsigned 32-bit integer whose leading zeros are to be counted.
  53. /// \returns An unsigned 32-bit integer containing the number of leading zero
  54. /// bits in the operand.
  55. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  56. __lzcnt32(unsigned int __X)
  57. {
  58. return __X ? __builtin_clz(__X) : 32;
  59. }
  60. /// \brief Counts the number of leading zero bits in the operand.
  61. ///
  62. /// \headerfile <x86intrin.h>
  63. ///
  64. /// This intrinsic corresponds to the \c LZCNT instruction.
  65. ///
  66. /// \param __X
  67. /// An unsigned 32-bit integer whose leading zeros are to be counted.
  68. /// \returns An unsigned 32-bit integer containing the number of leading zero
  69. /// bits in the operand.
  70. static __inline__ unsigned int __DEFAULT_FN_ATTRS
  71. _lzcnt_u32(unsigned int __X)
  72. {
  73. return __X ? __builtin_clz(__X) : 32;
  74. }
  75. #ifdef __x86_64__
  76. /// \brief Counts the number of leading zero bits in the operand.
  77. ///
  78. /// \headerfile <x86intrin.h>
  79. ///
  80. /// This intrinsic corresponds to the \c LZCNT instruction.
  81. ///
  82. /// \param __X
  83. /// An unsigned 64-bit integer whose leading zeros are to be counted.
  84. /// \returns An unsigned 64-bit integer containing the number of leading zero
  85. /// bits in the operand.
  86. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  87. __lzcnt64(unsigned long long __X)
  88. {
  89. return __X ? __builtin_clzll(__X) : 64;
  90. }
  91. /// \brief Counts the number of leading zero bits in the operand.
  92. ///
  93. /// \headerfile <x86intrin.h>
  94. ///
  95. /// This intrinsic corresponds to the \c LZCNT instruction.
  96. ///
  97. /// \param __X
  98. /// An unsigned 64-bit integer whose leading zeros are to be counted.
  99. /// \returns An unsigned 64-bit integer containing the number of leading zero
  100. /// bits in the operand.
  101. static __inline__ unsigned long long __DEFAULT_FN_ATTRS
  102. _lzcnt_u64(unsigned long long __X)
  103. {
  104. return __X ? __builtin_clzll(__X) : 64;
  105. }
  106. #endif
  107. #undef __DEFAULT_FN_ATTRS
  108. #endif /* __LZCNTINTRIN_H */