blob: da98792d830752c9675ade961ea105b40b9e78de [file] [log] [blame]
Craig Topperf2855ad2011-12-25 06:25:37 +00001/*===---- 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
24#if !defined __X86INTRIN_H && !defined __IMMINTRIN_H
25#error "Never use <bmiintrin.h> directly; include <x86intrin.h> instead."
26#endif
27
Craig Topperf2855ad2011-12-25 06:25:37 +000028#ifndef __BMIINTRIN_H
29#define __BMIINTRIN_H
30
Sanjay Patel1585fb92014-05-28 20:26:57 +000031#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
Eric Christopher4d1851682015-06-17 07:09:20 +000039/* Define the default attributes for the functions in this file. */
Michael Kupersteine45af542015-06-30 13:36:19 +000040#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi")))
Eric Christopher4d1851682015-06-17 07:09:20 +000041
Hans Wennborg1acf9552015-11-17 18:46:48 +000042/* Allow using the tzcnt intrinsics even for non-BMI targets. Since the TZCNT
43 instruction behaves as BSF on non-BMI targets, there is code that expects
44 to use it as a potentially faster version of BSF. */
45#define __RELAXED_FN_ATTRS __attribute__((__always_inline__, __nodebug__))
46
47static __inline__ unsigned short __RELAXED_FN_ATTRS
Craig Topper6490bdc2012-07-02 06:52:51 +000048__tzcnt_u16(unsigned short __X)
Craig Topperf2855ad2011-12-25 06:25:37 +000049{
Craig Toppera52e0d72014-11-01 22:50:54 +000050 return __X ? __builtin_ctzs(__X) : 16;
Craig Topperf2855ad2011-12-25 06:25:37 +000051}
52
Michael Kupersteine45af542015-06-30 13:36:19 +000053static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +000054__andn_u32(unsigned int __X, unsigned int __Y)
55{
56 return ~__X & __Y;
57}
58
Sanjay Patel1585fb92014-05-28 20:26:57 +000059/* AMD-specified, double-leading-underscore version of BEXTR */
Michael Kupersteine45af542015-06-30 13:36:19 +000060static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +000061__bextr_u32(unsigned int __X, unsigned int __Y)
62{
63 return __builtin_ia32_bextr_u32(__X, __Y);
64}
65
Sanjay Patel1585fb92014-05-28 20:26:57 +000066/* Intel-specified, single-leading-underscore version of BEXTR */
Michael Kupersteine45af542015-06-30 13:36:19 +000067static __inline__ unsigned int __DEFAULT_FN_ATTRS
Sanjay Patel1585fb92014-05-28 20:26:57 +000068_bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z)
69{
70 return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
71}
72
Michael Kupersteine45af542015-06-30 13:36:19 +000073static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +000074__blsi_u32(unsigned int __X)
75{
76 return __X & -__X;
77}
78
Michael Kupersteine45af542015-06-30 13:36:19 +000079static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +000080__blsmsk_u32(unsigned int __X)
81{
82 return __X ^ (__X - 1);
83}
84
Michael Kupersteine45af542015-06-30 13:36:19 +000085static __inline__ unsigned int __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +000086__blsr_u32(unsigned int __X)
87{
88 return __X & (__X - 1);
89}
90
Hans Wennborg1acf9552015-11-17 18:46:48 +000091static __inline__ unsigned int __RELAXED_FN_ATTRS
Craig Topper6490bdc2012-07-02 06:52:51 +000092__tzcnt_u32(unsigned int __X)
Craig Topperf2855ad2011-12-25 06:25:37 +000093{
Craig Toppera52e0d72014-11-01 22:50:54 +000094 return __X ? __builtin_ctz(__X) : 32;
Craig Topperf2855ad2011-12-25 06:25:37 +000095}
96
97#ifdef __x86_64__
Sanjay Patel1585fb92014-05-28 20:26:57 +000098
99#define _andn_u64(a, b) (__andn_u64((a), (b)))
100/* _bextr_u64 != __bextr_u64 */
101#define _blsi_u64(a) (__blsi_u64((a)))
102#define _blsmsk_u64(a) (__blsmsk_u64((a)))
103#define _blsr_u64(a) (__blsr_u64((a)))
104#define _tzcnt_u64(a) (__tzcnt_u64((a)))
105
Michael Kupersteine45af542015-06-30 13:36:19 +0000106static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000107__andn_u64 (unsigned long long __X, unsigned long long __Y)
108{
109 return ~__X & __Y;
110}
111
Sanjay Patel1585fb92014-05-28 20:26:57 +0000112/* AMD-specified, double-leading-underscore version of BEXTR */
Michael Kupersteine45af542015-06-30 13:36:19 +0000113static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000114__bextr_u64(unsigned long long __X, unsigned long long __Y)
115{
116 return __builtin_ia32_bextr_u64(__X, __Y);
117}
118
Sanjay Patel1585fb92014-05-28 20:26:57 +0000119/* Intel-specified, single-leading-underscore version of BEXTR */
Michael Kupersteine45af542015-06-30 13:36:19 +0000120static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Sanjay Patel1585fb92014-05-28 20:26:57 +0000121_bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z)
122{
123 return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8)));
124}
125
Michael Kupersteine45af542015-06-30 13:36:19 +0000126static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000127__blsi_u64(unsigned long long __X)
128{
129 return __X & -__X;
130}
131
Michael Kupersteine45af542015-06-30 13:36:19 +0000132static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000133__blsmsk_u64(unsigned long long __X)
134{
135 return __X ^ (__X - 1);
136}
137
Michael Kupersteine45af542015-06-30 13:36:19 +0000138static __inline__ unsigned long long __DEFAULT_FN_ATTRS
Craig Toppera06d4a12011-12-25 07:27:12 +0000139__blsr_u64(unsigned long long __X)
140{
141 return __X & (__X - 1);
142}
143
Hans Wennborg1acf9552015-11-17 18:46:48 +0000144static __inline__ unsigned long long __RELAXED_FN_ATTRS
Craig Topper6490bdc2012-07-02 06:52:51 +0000145__tzcnt_u64(unsigned long long __X)
Craig Topperf2855ad2011-12-25 06:25:37 +0000146{
Craig Toppera52e0d72014-11-01 22:50:54 +0000147 return __X ? __builtin_ctzll(__X) : 64;
Craig Topperf2855ad2011-12-25 06:25:37 +0000148}
Sanjay Patel1585fb92014-05-28 20:26:57 +0000149
150#endif /* __x86_64__ */
Craig Topperf2855ad2011-12-25 06:25:37 +0000151
Michael Kupersteine45af542015-06-30 13:36:19 +0000152#undef __DEFAULT_FN_ATTRS
Hans Wennborg1acf9552015-11-17 18:46:48 +0000153#undef __RELAXED_FN_ATTRS
Eric Christopher4d1851682015-06-17 07:09:20 +0000154
Craig Topperf2855ad2011-12-25 06:25:37 +0000155#endif /* __BMIINTRIN_H */