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