Craig Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 1 | /*===---- 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 Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 28 | #ifndef __BMIINTRIN_H |
| 29 | #define __BMIINTRIN_H |
| 30 | |
Sanjay Patel | 1585fb9 | 2014-05-28 20:26:57 +0000 | [diff] [blame] | 31 | #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 Christopher | 4d185168 | 2015-06-17 07:09:20 +0000 | [diff] [blame] | 39 | /* Define the default attributes for the functions in this file. */ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 40 | #define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__, __target__("bmi"))) |
Eric Christopher | 4d185168 | 2015-06-17 07:09:20 +0000 | [diff] [blame] | 41 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 42 | static __inline__ unsigned short __DEFAULT_FN_ATTRS |
Craig Topper | 6490bdc | 2012-07-02 06:52:51 +0000 | [diff] [blame] | 43 | __tzcnt_u16(unsigned short __X) |
Craig Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 44 | { |
Craig Topper | a52e0d7 | 2014-11-01 22:50:54 +0000 | [diff] [blame] | 45 | return __X ? __builtin_ctzs(__X) : 16; |
Craig Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 48 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 49 | __andn_u32(unsigned int __X, unsigned int __Y) |
| 50 | { |
| 51 | return ~__X & __Y; |
| 52 | } |
| 53 | |
Sanjay Patel | 1585fb9 | 2014-05-28 20:26:57 +0000 | [diff] [blame] | 54 | /* AMD-specified, double-leading-underscore version of BEXTR */ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 55 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 56 | __bextr_u32(unsigned int __X, unsigned int __Y) |
| 57 | { |
| 58 | return __builtin_ia32_bextr_u32(__X, __Y); |
| 59 | } |
| 60 | |
Sanjay Patel | 1585fb9 | 2014-05-28 20:26:57 +0000 | [diff] [blame] | 61 | /* Intel-specified, single-leading-underscore version of BEXTR */ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 62 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
Sanjay Patel | 1585fb9 | 2014-05-28 20:26:57 +0000 | [diff] [blame] | 63 | _bextr_u32(unsigned int __X, unsigned int __Y, unsigned int __Z) |
| 64 | { |
| 65 | return __builtin_ia32_bextr_u32 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8))); |
| 66 | } |
| 67 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 68 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 69 | __blsi_u32(unsigned int __X) |
| 70 | { |
| 71 | return __X & -__X; |
| 72 | } |
| 73 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 74 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 75 | __blsmsk_u32(unsigned int __X) |
| 76 | { |
| 77 | return __X ^ (__X - 1); |
| 78 | } |
| 79 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 80 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 81 | __blsr_u32(unsigned int __X) |
| 82 | { |
| 83 | return __X & (__X - 1); |
| 84 | } |
| 85 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 86 | static __inline__ unsigned int __DEFAULT_FN_ATTRS |
Craig Topper | 6490bdc | 2012-07-02 06:52:51 +0000 | [diff] [blame] | 87 | __tzcnt_u32(unsigned int __X) |
Craig Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 88 | { |
Craig Topper | a52e0d7 | 2014-11-01 22:50:54 +0000 | [diff] [blame] | 89 | return __X ? __builtin_ctz(__X) : 32; |
Craig Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | #ifdef __x86_64__ |
Sanjay Patel | 1585fb9 | 2014-05-28 20:26:57 +0000 | [diff] [blame] | 93 | |
| 94 | #define _andn_u64(a, b) (__andn_u64((a), (b))) |
| 95 | /* _bextr_u64 != __bextr_u64 */ |
| 96 | #define _blsi_u64(a) (__blsi_u64((a))) |
| 97 | #define _blsmsk_u64(a) (__blsmsk_u64((a))) |
| 98 | #define _blsr_u64(a) (__blsr_u64((a))) |
| 99 | #define _tzcnt_u64(a) (__tzcnt_u64((a))) |
| 100 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 101 | static __inline__ unsigned long long __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 102 | __andn_u64 (unsigned long long __X, unsigned long long __Y) |
| 103 | { |
| 104 | return ~__X & __Y; |
| 105 | } |
| 106 | |
Sanjay Patel | 1585fb9 | 2014-05-28 20:26:57 +0000 | [diff] [blame] | 107 | /* AMD-specified, double-leading-underscore version of BEXTR */ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 108 | static __inline__ unsigned long long __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 109 | __bextr_u64(unsigned long long __X, unsigned long long __Y) |
| 110 | { |
| 111 | return __builtin_ia32_bextr_u64(__X, __Y); |
| 112 | } |
| 113 | |
Sanjay Patel | 1585fb9 | 2014-05-28 20:26:57 +0000 | [diff] [blame] | 114 | /* Intel-specified, single-leading-underscore version of BEXTR */ |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 115 | static __inline__ unsigned long long __DEFAULT_FN_ATTRS |
Sanjay Patel | 1585fb9 | 2014-05-28 20:26:57 +0000 | [diff] [blame] | 116 | _bextr_u64(unsigned long long __X, unsigned int __Y, unsigned int __Z) |
| 117 | { |
| 118 | return __builtin_ia32_bextr_u64 (__X, ((__Y & 0xff) | ((__Z & 0xff) << 8))); |
| 119 | } |
| 120 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 121 | static __inline__ unsigned long long __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 122 | __blsi_u64(unsigned long long __X) |
| 123 | { |
| 124 | return __X & -__X; |
| 125 | } |
| 126 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 127 | static __inline__ unsigned long long __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 128 | __blsmsk_u64(unsigned long long __X) |
| 129 | { |
| 130 | return __X ^ (__X - 1); |
| 131 | } |
| 132 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 133 | static __inline__ unsigned long long __DEFAULT_FN_ATTRS |
Craig Topper | a06d4a1 | 2011-12-25 07:27:12 +0000 | [diff] [blame] | 134 | __blsr_u64(unsigned long long __X) |
| 135 | { |
| 136 | return __X & (__X - 1); |
| 137 | } |
| 138 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 139 | static __inline__ unsigned long long __DEFAULT_FN_ATTRS |
Craig Topper | 6490bdc | 2012-07-02 06:52:51 +0000 | [diff] [blame] | 140 | __tzcnt_u64(unsigned long long __X) |
Craig Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 141 | { |
Craig Topper | a52e0d7 | 2014-11-01 22:50:54 +0000 | [diff] [blame] | 142 | return __X ? __builtin_ctzll(__X) : 64; |
Craig Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 143 | } |
Sanjay Patel | 1585fb9 | 2014-05-28 20:26:57 +0000 | [diff] [blame] | 144 | |
| 145 | #endif /* __x86_64__ */ |
Craig Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 146 | |
Michael Kuperstein | e45af54 | 2015-06-30 13:36:19 +0000 | [diff] [blame^] | 147 | #undef __DEFAULT_FN_ATTRS |
Eric Christopher | 4d185168 | 2015-06-17 07:09:20 +0000 | [diff] [blame] | 148 | |
Craig Topper | f2855ad | 2011-12-25 06:25:37 +0000 | [diff] [blame] | 149 | #endif /* __BMIINTRIN_H */ |