1/* Copyright (C) 2009-2015 Free Software Foundation, Inc.
2
3   This file is part of GCC.
4
5   GCC is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 3, or (at your option)
8   any later version.
9
10   GCC is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   Under Section 7 of GPL version 3, you are granted additional
16   permissions described in the GCC Runtime Library Exception, version
17   3.1, as published by the Free Software Foundation.
18
19   You should have received a copy of the GNU General Public License and
20   a copy of the GCC Runtime Library Exception along with this program;
21   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
22   <http://www.gnu.org/licenses/>.  */
23
24#ifndef _X86INTRIN_H_INCLUDED
25# error "Never use <ia32intrin.h> directly; include <x86intrin.h> instead."
26#endif
27
28/* 32bit bsf */
29extern __inline int
30__attribute__((__gnu_inline__, __always_inline__, __artificial__))
31__bsfd (int __X)
32{
33  return __builtin_ctz (__X);
34}
35
36/* 32bit bsr */
37extern __inline int
38__attribute__((__gnu_inline__, __always_inline__, __artificial__))
39__bsrd (int __X)
40{
41  return __builtin_ia32_bsrsi (__X);
42}
43
44/* 32bit bswap */
45extern __inline int
46__attribute__((__gnu_inline__, __always_inline__, __artificial__))
47__bswapd (int __X)
48{
49  return __builtin_bswap32 (__X);
50}
51
52#ifndef __SSE4_2__
53#pragma GCC push_options
54#pragma GCC target("sse4.2")
55#define __DISABLE_SSE4_2__
56#endif /* __SSE4_2__ */
57
58/* 32bit accumulate CRC32 (polynomial 0x11EDC6F41) value.  */
59extern __inline unsigned int
60__attribute__((__gnu_inline__, __always_inline__, __artificial__))
61__crc32b (unsigned int __C, unsigned char __V)
62{
63  return __builtin_ia32_crc32qi (__C, __V);
64}
65
66extern __inline unsigned int
67__attribute__((__gnu_inline__, __always_inline__, __artificial__))
68__crc32w (unsigned int __C, unsigned short __V)
69{
70  return __builtin_ia32_crc32hi (__C, __V);
71}
72
73extern __inline unsigned int
74__attribute__((__gnu_inline__, __always_inline__, __artificial__))
75__crc32d (unsigned int __C, unsigned int __V)
76{
77  return __builtin_ia32_crc32si (__C, __V);
78}
79
80#ifdef __DISABLE_SSE4_2__
81#undef __DISABLE_SSE4_2__
82#pragma GCC pop_options
83#endif /* __DISABLE_SSE4_2__ */
84
85/* 32bit popcnt */
86extern __inline int
87__attribute__((__gnu_inline__, __always_inline__, __artificial__))
88__popcntd (unsigned int __X)
89{
90  return __builtin_popcount (__X);
91}
92
93/* rdpmc */
94extern __inline unsigned long long
95__attribute__((__gnu_inline__, __always_inline__, __artificial__))
96__rdpmc (int __S)
97{
98  return __builtin_ia32_rdpmc (__S);
99}
100
101/* rdtsc */
102extern __inline unsigned long long
103__attribute__((__gnu_inline__, __always_inline__, __artificial__))
104__rdtsc (void)
105{
106  return __builtin_ia32_rdtsc ();
107}
108
109/* rdtscp */
110extern __inline unsigned long long
111__attribute__((__gnu_inline__, __always_inline__, __artificial__))
112__rdtscp (unsigned int *__A)
113{
114  return __builtin_ia32_rdtscp (__A);
115}
116
117/* 8bit rol */
118extern __inline unsigned char
119__attribute__((__gnu_inline__, __always_inline__, __artificial__))
120__rolb (unsigned char __X, int __C)
121{
122  return __builtin_ia32_rolqi (__X, __C);
123}
124
125/* 16bit rol */
126extern __inline unsigned short
127__attribute__((__gnu_inline__, __always_inline__, __artificial__))
128__rolw (unsigned short __X, int __C)
129{
130  return __builtin_ia32_rolhi (__X, __C);
131}
132
133/* 32bit rol */
134extern __inline unsigned int
135__attribute__((__gnu_inline__, __always_inline__, __artificial__))
136__rold (unsigned int __X, int __C)
137{
138  return (__X << __C) | (__X >> (32 - __C));
139}
140
141/* 8bit ror */
142extern __inline unsigned char
143__attribute__((__gnu_inline__, __always_inline__, __artificial__))
144__rorb (unsigned char __X, int __C)
145{
146  return __builtin_ia32_rorqi (__X, __C);
147}
148
149/* 16bit ror */
150extern __inline unsigned short
151__attribute__((__gnu_inline__, __always_inline__, __artificial__))
152__rorw (unsigned short __X, int __C)
153{
154  return __builtin_ia32_rorhi (__X, __C);
155}
156
157/* 32bit ror */
158extern __inline unsigned int
159__attribute__((__gnu_inline__, __always_inline__, __artificial__))
160__rord (unsigned int __X, int __C)
161{
162  return (__X >> __C) | (__X << (32 - __C));
163}
164
165/* Pause */
166extern __inline void
167__attribute__((__gnu_inline__, __always_inline__, __artificial__))
168__pause (void)
169{
170  __builtin_ia32_pause ();
171}
172
173#ifdef __x86_64__
174/* 64bit bsf */
175extern __inline int
176__attribute__((__gnu_inline__, __always_inline__, __artificial__))
177__bsfq (long long __X)
178{
179  return __builtin_ctzll (__X);
180}
181
182/* 64bit bsr */
183extern __inline int
184__attribute__((__gnu_inline__, __always_inline__, __artificial__))
185__bsrq (long long __X)
186{
187  return __builtin_ia32_bsrdi (__X);
188}
189
190/* 64bit bswap */
191extern __inline long long
192__attribute__((__gnu_inline__, __always_inline__, __artificial__))
193__bswapq (long long __X)
194{
195  return __builtin_bswap64 (__X);
196}
197
198#ifndef __SSE4_2__
199#pragma GCC push_options
200#pragma GCC target("sse4.2")
201#define __DISABLE_SSE4_2__
202#endif /* __SSE4_2__ */
203
204/* 64bit accumulate CRC32 (polynomial 0x11EDC6F41) value.  */
205extern __inline unsigned long long
206__attribute__((__gnu_inline__, __always_inline__, __artificial__))
207__crc32q (unsigned long long __C, unsigned long long __V)
208{
209  return __builtin_ia32_crc32di (__C, __V);
210}
211
212#ifdef __DISABLE_SSE4_2__
213#undef __DISABLE_SSE4_2__
214#pragma GCC pop_options
215#endif /* __DISABLE_SSE4_2__ */
216
217/* 64bit popcnt */
218extern __inline long long
219__attribute__((__gnu_inline__, __always_inline__, __artificial__))
220__popcntq (unsigned long long __X)
221{
222  return __builtin_popcountll (__X);
223}
224
225/* 64bit rol */
226extern __inline unsigned long long
227__attribute__((__gnu_inline__, __always_inline__, __artificial__))
228__rolq (unsigned long long __X, int __C)
229{
230  return (__X << __C) | (__X >> (64 - __C));
231}
232
233/* 64bit ror */
234extern __inline unsigned long long
235__attribute__((__gnu_inline__, __always_inline__, __artificial__))
236__rorq (unsigned long long __X, int __C)
237{
238  return (__X >> __C) | (__X << (64 - __C));
239}
240
241/* Read flags register */
242extern __inline unsigned long long
243__attribute__((__gnu_inline__, __always_inline__, __artificial__))
244__readeflags (void)
245{
246  return __builtin_ia32_readeflags_u64 ();
247}
248
249/* Write flags register */
250extern __inline void
251__attribute__((__gnu_inline__, __always_inline__, __artificial__))
252__writeeflags (unsigned long long X)
253{
254  __builtin_ia32_writeeflags_u64 (X);
255}
256
257#define _bswap64(a)		__bswapq(a)
258#define _popcnt64(a)		__popcntq(a)
259#else
260
261/* Read flags register */
262extern __inline unsigned int
263__attribute__((__gnu_inline__, __always_inline__, __artificial__))
264__readeflags (void)
265{
266  return __builtin_ia32_readeflags_u32 ();
267}
268
269/* Write flags register */
270extern __inline void
271__attribute__((__gnu_inline__, __always_inline__, __artificial__))
272__writeeflags (unsigned int X)
273{
274  __builtin_ia32_writeeflags_u32 (X);
275}
276
277#endif
278
279/* On LP64 systems, longs are 64-bit.  Use the appropriate rotate
280 * function.  */
281#ifdef __LP64__
282#define _lrotl(a,b)		__rolq((a), (b))
283#define _lrotr(a,b)		__rorq((a), (b))
284#else
285#define _lrotl(a,b)		__rold((a), (b))
286#define _lrotr(a,b)		__rord((a), (b))
287#endif
288
289#define _bit_scan_forward(a)	__bsfd(a)
290#define _bit_scan_reverse(a)	__bsrd(a)
291#define _bswap(a)		__bswapd(a)
292#define _popcnt32(a)		__popcntd(a)
293#define _rdpmc(a)		__rdpmc(a)
294#define _rdtsc()		__rdtsc()
295#define _rdtscp(a)		__rdtscp(a)
296#define _rotwl(a,b)		__rolw((a), (b))
297#define _rotwr(a,b)		__rorw((a), (b))
298#define _rotl(a,b)		__rold((a), (b))
299#define _rotr(a,b)		__rord((a), (b))
300