blob: f4ddce9d0e6834ae11ce88a9341aae4778a235e6 [file] [log] [blame]
Logan Chien2833ffb2018-10-09 10:03:24 +08001/*===---- lzcntintrin.h - LZCNT intrinsics ---------------------------------===
2 *
Logan Chiendf4f7662019-09-04 16:45:23 -07003 * 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
Logan Chien2833ffb2018-10-09 10:03:24 +08006 *
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#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("lzcnt")))
19
Logan Chiendbcf4122019-03-21 10:50:25 +080020#ifndef _MSC_VER
Logan Chien55afb0a2018-10-15 10:42:14 +080021/// Counts the number of leading zero bits in the operand.
22///
23/// \headerfile <x86intrin.h>
24///
25/// This intrinsic corresponds to the \c LZCNT instruction.
26///
27/// \param __X
28/// An unsigned 16-bit integer whose leading zeros are to be counted.
29/// \returns An unsigned 16-bit integer containing the number of leading zero
30/// bits in the operand.
Logan Chiendbcf4122019-03-21 10:50:25 +080031#define __lzcnt16(X) __builtin_ia32_lzcnt_u16((unsigned short)(X))
32#endif // _MSC_VER
Logan Chien2833ffb2018-10-09 10:03:24 +080033
Logan Chien55afb0a2018-10-15 10:42:14 +080034/// Counts the number of leading zero bits in the operand.
35///
36/// \headerfile <x86intrin.h>
37///
38/// This intrinsic corresponds to the \c LZCNT instruction.
39///
40/// \param __X
41/// An unsigned 32-bit integer whose leading zeros are to be counted.
42/// \returns An unsigned 32-bit integer containing the number of leading zero
43/// bits in the operand.
44/// \see _lzcnt_u32
Logan Chien2833ffb2018-10-09 10:03:24 +080045static __inline__ unsigned int __DEFAULT_FN_ATTRS
46__lzcnt32(unsigned int __X)
47{
Logan Chienb0c84022018-11-09 16:19:54 +080048 return __builtin_ia32_lzcnt_u32(__X);
Logan Chien2833ffb2018-10-09 10:03:24 +080049}
50
Logan Chien55afb0a2018-10-15 10:42:14 +080051/// Counts the number of leading zero bits in the operand.
52///
53/// \headerfile <x86intrin.h>
54///
55/// This intrinsic corresponds to the \c LZCNT instruction.
56///
57/// \param __X
58/// An unsigned 32-bit integer whose leading zeros are to be counted.
59/// \returns An unsigned 32-bit integer containing the number of leading zero
60/// bits in the operand.
61/// \see __lzcnt32
Logan Chien2833ffb2018-10-09 10:03:24 +080062static __inline__ unsigned int __DEFAULT_FN_ATTRS
63_lzcnt_u32(unsigned int __X)
64{
Logan Chienb0c84022018-11-09 16:19:54 +080065 return __builtin_ia32_lzcnt_u32(__X);
Logan Chien2833ffb2018-10-09 10:03:24 +080066}
67
68#ifdef __x86_64__
Logan Chiendbcf4122019-03-21 10:50:25 +080069#ifndef _MSC_VER
Logan Chien55afb0a2018-10-15 10:42:14 +080070/// Counts the number of leading zero bits in the operand.
71///
72/// \headerfile <x86intrin.h>
73///
74/// This intrinsic corresponds to the \c LZCNT instruction.
75///
76/// \param __X
77/// An unsigned 64-bit integer whose leading zeros are to be counted.
78/// \returns An unsigned 64-bit integer containing the number of leading zero
79/// bits in the operand.
80/// \see _lzcnt_u64
Logan Chiendbcf4122019-03-21 10:50:25 +080081#define __lzcnt64(X) __builtin_ia32_lzcnt_u64((unsigned long long)(X))
82#endif // _MSC_VER
Logan Chien2833ffb2018-10-09 10:03:24 +080083
Logan Chien55afb0a2018-10-15 10:42:14 +080084/// Counts the number of leading zero bits in the operand.
85///
86/// \headerfile <x86intrin.h>
87///
88/// This intrinsic corresponds to the \c LZCNT instruction.
89///
90/// \param __X
91/// An unsigned 64-bit integer whose leading zeros are to be counted.
92/// \returns An unsigned 64-bit integer containing the number of leading zero
93/// bits in the operand.
94/// \see __lzcnt64
Logan Chien2833ffb2018-10-09 10:03:24 +080095static __inline__ unsigned long long __DEFAULT_FN_ATTRS
96_lzcnt_u64(unsigned long long __X)
97{
Logan Chienb0c84022018-11-09 16:19:54 +080098 return __builtin_ia32_lzcnt_u64(__X);
Logan Chien2833ffb2018-10-09 10:03:24 +080099}
100#endif
101
102#undef __DEFAULT_FN_ATTRS
103
104#endif /* __LZCNTINTRIN_H */