master
  1/*===---- lzcntintrin.h - LZCNT 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 <lzcntintrin.h> directly; include <x86intrin.h> instead."
 12#endif
 13
 14#ifndef __LZCNTINTRIN_H
 15#define __LZCNTINTRIN_H
 16
 17/* Define the default attributes for the functions in this file.
 18   Allow using the lzcnt intrinsics even for non-LZCNT targets. Since the LZCNT
 19   intrinsics are mapped to llvm.ctlz.*, false, which can be lowered to BSR on
 20   non-LZCNT targets with zero-value input handled correctly. */
 21#if defined(__cplusplus) && (__cplusplus >= 201103L)
 22#define __DEFAULT_FN_ATTRS                                                     \
 23  __attribute__((__always_inline__, __nodebug__)) constexpr
 24#else
 25#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
 26#endif
 27
 28#ifndef _MSC_VER
 29/// Counts the number of leading zero bits in the operand.
 30///
 31/// \headerfile <x86intrin.h>
 32///
 33/// This intrinsic corresponds to the \c LZCNT instruction.
 34///
 35/// \param __X
 36///    An unsigned 16-bit integer whose leading zeros are to be counted.
 37/// \returns An unsigned 16-bit integer containing the number of leading zero
 38///    bits in the operand.
 39#define __lzcnt16(X) __builtin_ia32_lzcnt_u16((unsigned short)(X))
 40#endif // _MSC_VER
 41
 42/// Counts the number of leading zero bits in the operand.
 43///
 44/// \headerfile <x86intrin.h>
 45///
 46/// This intrinsic corresponds to the \c LZCNT instruction.
 47///
 48/// \param __X
 49///    An unsigned 32-bit integer whose leading zeros are to be counted.
 50/// \returns An unsigned 32-bit integer containing the number of leading zero
 51///    bits in the operand.
 52/// \see _lzcnt_u32
 53static __inline__ unsigned int __DEFAULT_FN_ATTRS
 54__lzcnt32(unsigned int __X) {
 55  return __builtin_ia32_lzcnt_u32(__X);
 56}
 57
 58/// Counts the number of leading zero bits in the operand.
 59///
 60/// \headerfile <x86intrin.h>
 61///
 62/// This intrinsic corresponds to the \c LZCNT instruction.
 63///
 64/// \param __X
 65///    An unsigned 32-bit integer whose leading zeros are to be counted.
 66/// \returns An unsigned 32-bit integer containing the number of leading zero
 67///    bits in the operand.
 68/// \see __lzcnt32
 69static __inline__ unsigned int __DEFAULT_FN_ATTRS
 70_lzcnt_u32(unsigned int __X) {
 71  return __builtin_ia32_lzcnt_u32(__X);
 72}
 73
 74#ifdef __x86_64__
 75#ifndef _MSC_VER
 76/// 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/// \see _lzcnt_u64
 87#define __lzcnt64(X) __builtin_ia32_lzcnt_u64((unsigned long long)(X))
 88#endif // _MSC_VER
 89
 90/// Counts the number of leading zero bits in the operand.
 91///
 92/// \headerfile <x86intrin.h>
 93///
 94/// This intrinsic corresponds to the \c LZCNT instruction.
 95///
 96/// \param __X
 97///    An unsigned 64-bit integer whose leading zeros are to be counted.
 98/// \returns An unsigned 64-bit integer containing the number of leading zero
 99///    bits in the operand.
100/// \see __lzcnt64
101static __inline__ unsigned long long __DEFAULT_FN_ATTRS
102_lzcnt_u64(unsigned long long __X) {
103  return __builtin_ia32_lzcnt_u64(__X);
104}
105#endif
106
107#undef __DEFAULT_FN_ATTRS
108
109#endif /* __LZCNTINTRIN_H */