Chris@82
|
1 /*
|
Chris@82
|
2 * Copyright (c) 2003, 2007-14 Matteo Frigo
|
Chris@82
|
3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
|
Chris@82
|
4 *
|
Chris@82
|
5 * This program is free software; you can redistribute it and/or modify
|
Chris@82
|
6 * it under the terms of the GNU General Public License as published by
|
Chris@82
|
7 * the Free Software Foundation; either version 2 of the License, or
|
Chris@82
|
8 * (at your option) any later version.
|
Chris@82
|
9 *
|
Chris@82
|
10 * This program is distributed in the hope that it will be useful,
|
Chris@82
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@82
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@82
|
13 * GNU General Public License for more details.
|
Chris@82
|
14 *
|
Chris@82
|
15 * You should have received a copy of the GNU General Public License
|
Chris@82
|
16 * along with this program; if not, write to the Free Software
|
Chris@82
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Chris@82
|
18 *
|
Chris@82
|
19 */
|
Chris@82
|
20
|
Chris@82
|
21
|
Chris@82
|
22 /* this code was kindly donated by Eric J. Korpela */
|
Chris@82
|
23
|
Chris@82
|
24 #ifdef _MSC_VER
|
Chris@82
|
25 #ifndef inline
|
Chris@82
|
26 #define inline __inline
|
Chris@82
|
27 #endif
|
Chris@82
|
28 #endif
|
Chris@82
|
29
|
Chris@82
|
30 static inline int is_386()
|
Chris@82
|
31 {
|
Chris@82
|
32 #ifdef _MSC_VER
|
Chris@82
|
33 unsigned int result,tst;
|
Chris@82
|
34 _asm {
|
Chris@82
|
35 pushfd
|
Chris@82
|
36 pop eax
|
Chris@82
|
37 mov edx,eax
|
Chris@82
|
38 xor eax,40000h
|
Chris@82
|
39 push eax
|
Chris@82
|
40 popfd
|
Chris@82
|
41 pushfd
|
Chris@82
|
42 pop eax
|
Chris@82
|
43 push edx
|
Chris@82
|
44 popfd
|
Chris@82
|
45 mov tst,edx
|
Chris@82
|
46 mov result,eax
|
Chris@82
|
47 }
|
Chris@82
|
48 #else
|
Chris@82
|
49 register unsigned int result,tst;
|
Chris@82
|
50 __asm__ (
|
Chris@82
|
51 "pushfl\n\t"
|
Chris@82
|
52 "popl %0\n\t"
|
Chris@82
|
53 "movl %0,%1\n\t"
|
Chris@82
|
54 "xorl $0x40000,%0\n\t"
|
Chris@82
|
55 "pushl %0\n\t"
|
Chris@82
|
56 "popfl\n\t"
|
Chris@82
|
57 "pushfl\n\t"
|
Chris@82
|
58 "popl %0\n\t"
|
Chris@82
|
59 "pushl %1\n\t"
|
Chris@82
|
60 "popfl"
|
Chris@82
|
61 : "=r" (result), "=r" (tst) /* output */
|
Chris@82
|
62 : /* no inputs */
|
Chris@82
|
63 );
|
Chris@82
|
64 #endif
|
Chris@82
|
65 return (result == tst);
|
Chris@82
|
66 }
|
Chris@82
|
67
|
Chris@82
|
68 static inline int has_cpuid()
|
Chris@82
|
69 {
|
Chris@82
|
70 #ifdef _MSC_VER
|
Chris@82
|
71 unsigned int result,tst;
|
Chris@82
|
72 _asm {
|
Chris@82
|
73 pushfd
|
Chris@82
|
74 pop eax
|
Chris@82
|
75 mov edx,eax
|
Chris@82
|
76 xor eax,200000h
|
Chris@82
|
77 push eax
|
Chris@82
|
78 popfd
|
Chris@82
|
79 pushfd
|
Chris@82
|
80 pop eax
|
Chris@82
|
81 push edx
|
Chris@82
|
82 popfd
|
Chris@82
|
83 mov tst,edx
|
Chris@82
|
84 mov result,eax
|
Chris@82
|
85 }
|
Chris@82
|
86 #else
|
Chris@82
|
87 register unsigned int result,tst;
|
Chris@82
|
88 __asm__ (
|
Chris@82
|
89 "pushfl\n\t"
|
Chris@82
|
90 "pop %0\n\t"
|
Chris@82
|
91 "movl %0,%1\n\t"
|
Chris@82
|
92 "xorl $0x200000,%0\n\t"
|
Chris@82
|
93 "pushl %0\n\t"
|
Chris@82
|
94 "popfl\n\t"
|
Chris@82
|
95 "pushfl\n\t"
|
Chris@82
|
96 "popl %0\n\t"
|
Chris@82
|
97 "pushl %1\n\t"
|
Chris@82
|
98 "popfl"
|
Chris@82
|
99 : "=r" (result), "=r" (tst) /* output */
|
Chris@82
|
100 : /* no inputs */
|
Chris@82
|
101 );
|
Chris@82
|
102 #endif
|
Chris@82
|
103 return (result != tst);
|
Chris@82
|
104 }
|
Chris@82
|
105
|
Chris@82
|
106 /* cpuid version to get all registers. Donated by Erik Lindahl from Gromacs. */
|
Chris@82
|
107 static inline void
|
Chris@82
|
108 cpuid_all(int level, int ecxval, int *eax, int *ebx, int *ecx, int *edx)
|
Chris@82
|
109 {
|
Chris@82
|
110 #if (defined _MSC_VER)
|
Chris@82
|
111 int CPUInfo[4];
|
Chris@82
|
112
|
Chris@82
|
113 # if (_MSC_VER > 1500) || (_MSC_VER == 1500 & _MSC_FULL_VER >= 150030729)
|
Chris@82
|
114 /* MSVC 9.0 SP1 or later */
|
Chris@82
|
115 __cpuidex(CPUInfo, level, ecxval);
|
Chris@82
|
116 # else
|
Chris@82
|
117 __cpuid(CPUInfo, level);
|
Chris@82
|
118 /* Set an error code if the user wanted a non-zero ecxval, since we did not have cpuidex */
|
Chris@82
|
119 # endif
|
Chris@82
|
120 *eax = CPUInfo[0];
|
Chris@82
|
121 *ebx = CPUInfo[1];
|
Chris@82
|
122 *ecx = CPUInfo[2];
|
Chris@82
|
123 *edx = CPUInfo[3];
|
Chris@82
|
124
|
Chris@82
|
125 #else
|
Chris@82
|
126 /* Not MSVC */
|
Chris@82
|
127 *eax = level;
|
Chris@82
|
128 *ecx = ecxval;
|
Chris@82
|
129 *ebx = 0;
|
Chris@82
|
130 *edx = 0;
|
Chris@82
|
131 /* Avoid clobbering global offset table in 32-bit pic code (ebx) */
|
Chris@82
|
132 # if defined(__PIC__)
|
Chris@82
|
133 __asm__ ("xchgl %%ebx, %1 \n\t"
|
Chris@82
|
134 "cpuid \n\t"
|
Chris@82
|
135 "xchgl %%ebx, %1 \n\t"
|
Chris@82
|
136 : "+a" (*eax), "+r" (*ebx), "+c" (*ecx), "+d" (*edx));
|
Chris@82
|
137 # else
|
Chris@82
|
138 /* No need to save ebx if we are not in pic mode */
|
Chris@82
|
139 __asm__ ("cpuid \n\t"
|
Chris@82
|
140 : "+a" (*eax), "+b" (*ebx), "+c" (*ecx), "+d" (*edx));
|
Chris@82
|
141 # endif
|
Chris@82
|
142 #endif
|
Chris@82
|
143 }
|
Chris@82
|
144
|
Chris@82
|
145 static inline int cpuid_edx(int op)
|
Chris@82
|
146 {
|
Chris@82
|
147 # ifdef _MSC_VER
|
Chris@82
|
148 int result;
|
Chris@82
|
149 _asm {
|
Chris@82
|
150 push ebx
|
Chris@82
|
151 mov eax,op
|
Chris@82
|
152 cpuid
|
Chris@82
|
153 mov result,edx
|
Chris@82
|
154 pop ebx
|
Chris@82
|
155 }
|
Chris@82
|
156 return result;
|
Chris@82
|
157 # else
|
Chris@82
|
158 int eax, ecx, edx;
|
Chris@82
|
159
|
Chris@82
|
160 __asm__("push %%ebx\n\tcpuid\n\tpop %%ebx"
|
Chris@82
|
161 : "=a" (eax), "=c" (ecx), "=d" (edx)
|
Chris@82
|
162 : "a" (op));
|
Chris@82
|
163 return edx;
|
Chris@82
|
164 # endif
|
Chris@82
|
165 }
|
Chris@82
|
166
|
Chris@82
|
167 static inline int cpuid_ecx(int op)
|
Chris@82
|
168 {
|
Chris@82
|
169 # ifdef _MSC_VER
|
Chris@82
|
170 int result;
|
Chris@82
|
171 _asm {
|
Chris@82
|
172 push ebx
|
Chris@82
|
173 mov eax,op
|
Chris@82
|
174 cpuid
|
Chris@82
|
175 mov result,ecx
|
Chris@82
|
176 pop ebx
|
Chris@82
|
177 }
|
Chris@82
|
178 return result;
|
Chris@82
|
179 # else
|
Chris@82
|
180 int eax, ecx, edx;
|
Chris@82
|
181
|
Chris@82
|
182 __asm__("push %%ebx\n\tcpuid\n\tpop %%ebx"
|
Chris@82
|
183 : "=a" (eax), "=c" (ecx), "=d" (edx)
|
Chris@82
|
184 : "a" (op));
|
Chris@82
|
185 return ecx;
|
Chris@82
|
186 # endif
|
Chris@82
|
187 }
|
Chris@82
|
188
|
Chris@82
|
189 static inline int xgetbv_eax(int op)
|
Chris@82
|
190 {
|
Chris@82
|
191 # ifdef _MSC_VER
|
Chris@82
|
192 int veax, vedx;
|
Chris@82
|
193 _asm {
|
Chris@82
|
194 mov ecx,op
|
Chris@82
|
195 # if defined(__INTEL_COMPILER) || (_MSC_VER >= 1600)
|
Chris@82
|
196 xgetbv
|
Chris@82
|
197 # else
|
Chris@82
|
198 __emit 15
|
Chris@82
|
199 __emit 1
|
Chris@82
|
200 __emit 208
|
Chris@82
|
201 # endif
|
Chris@82
|
202 mov veax,eax
|
Chris@82
|
203 mov vedx,edx
|
Chris@82
|
204 }
|
Chris@82
|
205 return veax;
|
Chris@82
|
206 # else
|
Chris@82
|
207 int eax, edx;
|
Chris@82
|
208 __asm__ (".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c" (op));
|
Chris@82
|
209 return eax;
|
Chris@82
|
210 #endif
|
Chris@82
|
211 }
|