2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / config / i386 / cpuid.h
blobce406c5404a14c96198639d0d6afd5c98d5fc38b
1 /*
2 * Copyright (C) 2007, 2008 Free Software Foundation, Inc.
4 * This file is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2, or (at your option) any
7 * later version.
8 *
9 * In addition to the permissions in the GNU General Public License, the
10 * Free Software Foundation gives you unlimited permission to link the
11 * compiled version of this file with other programs, and to distribute
12 * those programs without any restriction coming from the use of this
13 * file. (The General Public License restrictions do apply in other
14 * respects; for example, they cover modification of the file, and
15 * distribution when not linked into another program.)
17 * This file is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
27 * As a special exception, if you link this library with files
28 * compiled with GCC to produce an executable, this does not cause
29 * the resulting executable to be covered by the GNU General Public License.
30 * This exception does not however invalidate any other reasons why
31 * the executable file might be covered by the GNU General Public License.
34 /* %ecx */
35 #define bit_SSE3 (1 << 0)
36 #define bit_PCLMUL (1 << 1)
37 #define bit_SSSE3 (1 << 9)
38 #define bit_CMPXCHG16B (1 << 13)
39 #define bit_SSE4_1 (1 << 19)
40 #define bit_SSE4_2 (1 << 20)
41 #define bit_POPCNT (1 << 23)
42 #define bit_AES (1 << 25)
44 /* %edx */
45 #define bit_CMPXCHG8B (1 << 8)
46 #define bit_CMOV (1 << 15)
47 #define bit_MMX (1 << 23)
48 #define bit_FXSAVE (1 << 24)
49 #define bit_SSE (1 << 25)
50 #define bit_SSE2 (1 << 26)
52 /* Extended Features */
53 /* %ecx */
54 #define bit_LAHF_LM (1 << 0)
55 #define bit_SSE4a (1 << 6)
56 #define bit_SSE5 (1 << 11)
58 /* %edx */
59 #define bit_LM (1 << 29)
60 #define bit_3DNOWP (1 << 30)
61 #define bit_3DNOW (1 << 31)
64 #if defined(__i386__) && defined(__PIC__)
65 /* %ebx may be the PIC register. */
66 #if __GNUC__ >= 3
67 #define __cpuid(level, a, b, c, d) \
68 __asm__ ("xchg{l}\t{%%}ebx, %1\n\t" \
69 "cpuid\n\t" \
70 "xchg{l}\t{%%}ebx, %1\n\t" \
71 : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
72 : "0" (level))
73 #else
74 /* Host GCCs older than 3.0 weren't supporting Intel asm syntax
75 nor alternatives in i386 code. */
76 #define __cpuid(level, a, b, c, d) \
77 __asm__ ("xchgl\t%%ebx, %1\n\t" \
78 "cpuid\n\t" \
79 "xchgl\t%%ebx, %1\n\t" \
80 : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
81 : "0" (level))
82 #endif
83 #else
84 #define __cpuid(level, a, b, c, d) \
85 __asm__ ("cpuid\n\t" \
86 : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
87 : "0" (level))
88 #endif
90 /* Return highest supported input value for cpuid instruction. ext can
91 be either 0x0 or 0x8000000 to return highest supported value for
92 basic or extended cpuid information. Function returns 0 if cpuid
93 is not supported or whatever cpuid returns in eax register. If sig
94 pointer is non-null, then first four bytes of the signature
95 (as found in ebx register) are returned in location pointed by sig. */
97 static __inline unsigned int
98 __get_cpuid_max (unsigned int __ext, unsigned int *__sig)
100 unsigned int __eax, __ebx, __ecx, __edx;
102 #ifndef __x86_64__
103 #if __GNUC__ >= 3
104 /* See if we can use cpuid. On AMD64 we always can. */
105 __asm__ ("pushf{l|d}\n\t"
106 "pushf{l|d}\n\t"
107 "pop{l}\t%0\n\t"
108 "mov{l}\t{%0, %1|%1, %0}\n\t"
109 "xor{l}\t{%2, %0|%0, %2}\n\t"
110 "push{l}\t%0\n\t"
111 "popf{l|d}\n\t"
112 "pushf{l|d}\n\t"
113 "pop{l}\t%0\n\t"
114 "popf{l|d}\n\t"
115 : "=&r" (__eax), "=&r" (__ebx)
116 : "i" (0x00200000));
117 #else
118 /* Host GCCs older than 3.0 weren't supporting Intel asm syntax
119 nor alternatives in i386 code. */
120 __asm__ ("pushfl\n\t"
121 "pushfl\n\t"
122 "popl\t%0\n\t"
123 "movl\t%0, %1\n\t"
124 "xorl\t%2, %0\n\t"
125 "pushl\t%0\n\t"
126 "popfl\n\t"
127 "pushfl\n\t"
128 "popl\t%0\n\t"
129 "popfl\n\t"
130 : "=&r" (__eax), "=&r" (__ebx)
131 : "i" (0x00200000));
132 #endif
134 if (!((__eax ^ __ebx) & 0x00200000))
135 return 0;
136 #endif
138 /* Host supports cpuid. Return highest supported cpuid input value. */
139 __cpuid (__ext, __eax, __ebx, __ecx, __edx);
141 if (__sig)
142 *__sig = __ebx;
144 return __eax;
147 /* Return cpuid data for requested cpuid level, as found in returned
148 eax, ebx, ecx and edx registers. The function checks if cpuid is
149 supported and returns 1 for valid cpuid information or 0 for
150 unsupported cpuid level. All pointers are required to be non-null. */
152 static __inline int
153 __get_cpuid (unsigned int __level,
154 unsigned int *__eax, unsigned int *__ebx,
155 unsigned int *__ecx, unsigned int *__edx)
157 unsigned int __ext = __level & 0x80000000;
159 if (__get_cpuid_max (__ext, 0) < __level)
160 return 0;
162 __cpuid (__level, *__eax, *__ebx, *__ecx, *__edx);
163 return 1;