1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
1,168c1,187
< /* CpuArch.c -- CPU specific code
< 2010-10-26: Igor Pavlov : Public domain */
<
< #include "CpuArch.h"
<
< #ifdef MY_CPU_X86_OR_AMD64
<
< #if (defined(_MSC_VER) && !defined(MY_CPU_AMD64)) || defined(__GNUC__)
< #define USE_ASM
< #endif
<
< #if defined(USE_ASM) && !defined(MY_CPU_AMD64)
< static UInt32 CheckFlag(UInt32 flag)
< {
< #ifdef _MSC_VER
< __asm pushfd;
< __asm pop EAX;
< __asm mov EDX, EAX;
< __asm xor EAX, flag;
< __asm push EAX;
< __asm popfd;
< __asm pushfd;
< __asm pop EAX;
< __asm xor EAX, EDX;
< __asm push EDX;
< __asm popfd;
< __asm and flag, EAX;
< #else
< __asm__ __volatile__ (
< "pushf\n\t"
< "pop %%EAX\n\t"
< "movl %%EAX,%%EDX\n\t"
< "xorl %0,%%EAX\n\t"
< "push %%EAX\n\t"
< "popf\n\t"
< "pushf\n\t"
< "pop %%EAX\n\t"
< "xorl %%EDX,%%EAX\n\t"
< "push %%EDX\n\t"
< "popf\n\t"
< "andl %%EAX, %0\n\t":
< "=c" (flag) : "c" (flag));
< #endif
< return flag;
< }
< #define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
< #else
< #define CHECK_CPUID_IS_SUPPORTED
< #endif
<
< static void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d)
< {
< #ifdef USE_ASM
<
< #ifdef _MSC_VER
<
< UInt32 a2, b2, c2, d2;
< __asm xor EBX, EBX;
< __asm xor ECX, ECX;
< __asm xor EDX, EDX;
< __asm mov EAX, function;
< __asm cpuid;
< __asm mov a2, EAX;
< __asm mov b2, EBX;
< __asm mov c2, ECX;
< __asm mov d2, EDX;
<
< *a = a2;
< *b = b2;
< *c = c2;
< *d = d2;
<
< #else
<
< __asm__ __volatile__ (
< "cpuid"
< : "=a" (*a) ,
< "=b" (*b) ,
< "=c" (*c) ,
< "=d" (*d)
< : "0" (function)) ;
<
< #endif
<
< #else
<
< int CPUInfo[4];
< __cpuid(CPUInfo, function);
< *a = CPUInfo[0];
< *b = CPUInfo[1];
< *c = CPUInfo[2];
< *d = CPUInfo[3];
<
< #endif
< }
<
< Bool x86cpuid_CheckAndRead(Cx86cpuid *p)
< {
< CHECK_CPUID_IS_SUPPORTED
< MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]);
< MyCPUID(1, &p->ver, &p->b, &p->c, &p->d);
< return True;
< }
<
< static UInt32 kVendors[][3] =
< {
< { 0x756E6547, 0x49656E69, 0x6C65746E},
< { 0x68747541, 0x69746E65, 0x444D4163},
< { 0x746E6543, 0x48727561, 0x736C7561}
< };
<
< int x86cpuid_GetFirm(const Cx86cpuid *p)
< {
< unsigned i;
< for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++)
< {
< const UInt32 *v = kVendors[i];
< if (v[0] == p->vendor[0] &&
< v[1] == p->vendor[1] &&
< v[2] == p->vendor[2])
< return (int)i;
< }
< return -1;
< }
<
< Bool CPU_Is_InOrder()
< {
< Cx86cpuid p;
< int firm;
< UInt32 family, model;
< if (!x86cpuid_CheckAndRead(&p))
< return True;
< family = x86cpuid_GetFamily(&p);
< model = x86cpuid_GetModel(&p);
< firm = x86cpuid_GetFirm(&p);
< switch (firm)
< {
< case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && model == 0x100C));
< case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA)));
< case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF));
< }
< return True;
< }
<
< #if !defined(MY_CPU_AMD64) && defined(_WIN32)
< static Bool CPU_Sys_Is_SSE_Supported()
< {
< OSVERSIONINFO vi;
< vi.dwOSVersionInfoSize = sizeof(vi);
< if (!GetVersionEx(&vi))
< return False;
< return (vi.dwMajorVersion >= 5);
< }
< #define CHECK_SYS_SSE_SUPPORT if (!CPU_Sys_Is_SSE_Supported()) return False;
< #else
< #define CHECK_SYS_SSE_SUPPORT
< #endif
<
< Bool CPU_Is_Aes_Supported()
< {
< Cx86cpuid p;
< CHECK_SYS_SSE_SUPPORT
< if (!x86cpuid_CheckAndRead(&p))
< return False;
< return (p.c >> 25) & 1;
< }
<
< #endif
---
> /* CpuArch.c -- CPU specific code
> 2010-10-26: Igor Pavlov : Public domain */
>
> #include "CpuArch.h"
>
> #ifdef MY_CPU_X86_OR_AMD64
>
> #if (defined(_MSC_VER) && !defined(MY_CPU_AMD64)) || defined(__GNUC__)
> #define USE_ASM
> #endif
>
> #if defined(USE_ASM) && !defined(MY_CPU_AMD64)
> static UInt32 CheckFlag(UInt32 flag)
> {
> #ifdef _MSC_VER
> __asm pushfd;
> __asm pop EAX;
> __asm mov EDX, EAX;
> __asm xor EAX, flag;
> __asm push EAX;
> __asm popfd;
> __asm pushfd;
> __asm pop EAX;
> __asm xor EAX, EDX;
> __asm push EDX;
> __asm popfd;
> __asm and flag, EAX;
> #else
> __asm__ __volatile__ (
> "pushf\n\t"
> "pop %%EAX\n\t"
> "movl %%EAX,%%EDX\n\t"
> "xorl %0,%%EAX\n\t"
> "push %%EAX\n\t"
> "popf\n\t"
> "pushf\n\t"
> "pop %%EAX\n\t"
> "xorl %%EDX,%%EAX\n\t"
> "push %%EDX\n\t"
> "popf\n\t"
> "andl %%EAX, %0\n\t":
> "=c" (flag) : "c" (flag):
> "%eax", "%edx" );
> #endif
> return flag;
> }
> #define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
> #else
> #define CHECK_CPUID_IS_SUPPORTED
> #endif
>
> static void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d)
> {
> #ifdef USE_ASM
>
> #ifdef _MSC_VER
>
> UInt32 a2, b2, c2, d2;
> __asm xor EBX, EBX;
> __asm xor ECX, ECX;
> __asm xor EDX, EDX;
> __asm mov EAX, function;
> __asm cpuid;
> __asm mov a2, EAX;
> __asm mov b2, EBX;
> __asm mov c2, ECX;
> __asm mov d2, EDX;
>
> *a = a2;
> *b = b2;
> *c = c2;
> *d = d2;
>
> #else
>
> #if defined(MY_CPU_AMD64)
>
> __asm__ __volatile__ (
> "mov %%rbx, %%rdi\n"
> "cpuid\n"
> "xchg %%rdi, %%rbx\n"
> : "=a" (*a) ,
> "=D" (*b) ,
> "=c" (*c) ,
> "=d" (*d)
> : "0" (function)) ;
>
> #else
>
> __asm__ __volatile__ (
> "mov %%ebx, %%edi\n"
> "cpuid\n"
> "xchg %%edi, %%ebx\n"
> : "=a" (*a) ,
> "=D" (*b) ,
> "=c" (*c) ,
> "=d" (*d)
> : "0" (function)) ;
>
> #endif
>
> #endif
>
> #else
>
> int CPUInfo[4];
> __cpuid(CPUInfo, function);
> *a = CPUInfo[0];
> *b = CPUInfo[1];
> *c = CPUInfo[2];
> *d = CPUInfo[3];
>
> #endif
> }
>
> Bool x86cpuid_CheckAndRead(Cx86cpuid *p)
> {
> CHECK_CPUID_IS_SUPPORTED
> MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]);
> MyCPUID(1, &p->ver, &p->b, &p->c, &p->d);
> return True;
> }
>
> static UInt32 kVendors[][3] =
> {
> { 0x756E6547, 0x49656E69, 0x6C65746E},
> { 0x68747541, 0x69746E65, 0x444D4163},
> { 0x746E6543, 0x48727561, 0x736C7561}
> };
>
> int x86cpuid_GetFirm(const Cx86cpuid *p)
> {
> unsigned i;
> for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++)
> {
> const UInt32 *v = kVendors[i];
> if (v[0] == p->vendor[0] &&
> v[1] == p->vendor[1] &&
> v[2] == p->vendor[2])
> return (int)i;
> }
> return -1;
> }
>
> Bool CPU_Is_InOrder()
> {
> Cx86cpuid p;
> int firm;
> UInt32 family, model;
> if (!x86cpuid_CheckAndRead(&p))
> return True;
> family = x86cpuid_GetFamily(&p);
> model = x86cpuid_GetModel(&p);
> firm = x86cpuid_GetFirm(&p);
> switch (firm)
> {
> case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && model == 0x100C));
> case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA)));
> case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF));
> }
> return True;
> }
>
> #if !defined(MY_CPU_AMD64) && defined(_WIN32)
> static Bool CPU_Sys_Is_SSE_Supported()
> {
> OSVERSIONINFO vi;
> vi.dwOSVersionInfoSize = sizeof(vi);
> if (!GetVersionEx(&vi))
> return False;
> return (vi.dwMajorVersion >= 5);
> }
> #define CHECK_SYS_SSE_SUPPORT if (!CPU_Sys_Is_SSE_Supported()) return False;
> #else
> #define CHECK_SYS_SSE_SUPPORT
> #endif
>
> Bool CPU_Is_Aes_Supported()
> {
> Cx86cpuid p;
> CHECK_SYS_SSE_SUPPORT
> if (!x86cpuid_CheckAndRead(&p))
> return False;
> return (p.c >> 25) & 1;
> }
>
> #endif
|