blob: 0aa94aecda5b8042da5ad261bb3bc0b815360039 [file] [log] [blame]
Logan Chiendf4f7662019-09-04 16:45:23 -07001/*===---- popcntintrin.h - POPCNT 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#ifndef __POPCNTINTRIN_H
11#define __POPCNTINTRIN_H
12
13/* Define the default attributes for the functions in this file. */
14#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("popcnt")))
15
Sasha Smundak4b1f33a2021-01-11 15:05:07 -080016#if defined(__cplusplus) && (__cplusplus >= 201103L)
17#define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS constexpr
18#else
19#define __DEFAULT_FN_ATTRS_CONSTEXPR __DEFAULT_FN_ATTRS
20#endif
21
Logan Chiendf4f7662019-09-04 16:45:23 -070022/// Counts the number of bits in the source operand having a value of 1.
23///
24/// \headerfile <x86intrin.h>
25///
26/// This intrinsic corresponds to the <c> POPCNT </c> instruction.
27///
28/// \param __A
29/// An unsigned 32-bit integer operand.
30/// \returns A 32-bit integer containing the number of bits with value 1 in the
31/// source operand.
Sasha Smundak4b1f33a2021-01-11 15:05:07 -080032static __inline__ int __DEFAULT_FN_ATTRS_CONSTEXPR
Logan Chiendf4f7662019-09-04 16:45:23 -070033_mm_popcnt_u32(unsigned int __A)
34{
35 return __builtin_popcount(__A);
36}
37
38#ifdef __x86_64__
39/// Counts the number of bits in the source operand having a value of 1.
40///
41/// \headerfile <x86intrin.h>
42///
43/// This intrinsic corresponds to the <c> POPCNT </c> instruction.
44///
45/// \param __A
46/// An unsigned 64-bit integer operand.
47/// \returns A 64-bit integer containing the number of bits with value 1 in the
48/// source operand.
Sasha Smundak4b1f33a2021-01-11 15:05:07 -080049static __inline__ long long __DEFAULT_FN_ATTRS_CONSTEXPR
Logan Chiendf4f7662019-09-04 16:45:23 -070050_mm_popcnt_u64(unsigned long long __A)
51{
52 return __builtin_popcountll(__A);
53}
54#endif /* __x86_64__ */
55
56#undef __DEFAULT_FN_ATTRS
Sasha Smundak4b1f33a2021-01-11 15:05:07 -080057#undef __DEFAULT_FN_ATTRS_CONSTEXPR
Logan Chiendf4f7662019-09-04 16:45:23 -070058
59#endif /* __POPCNTINTRIN_H */