1 /* CpuArch.c -- CPU specific code
2 2018-02-18: Igor Pavlov : Public domain */
8 #ifdef MY_CPU_X86_OR_AMD64
10 #if (defined(_MSC_VER) && !defined(MY_CPU_AMD64)) || defined(__GNUC__)
14 #if !defined(USE_ASM) && _MSC_VER >= 1500
18 #if defined(USE_ASM) && !defined(MY_CPU_AMD64)
19 static UInt32 CheckFlag(UInt32 flag)
35 __asm__ __volatile__ (
38 "movl %%EAX,%%EDX\n\t"
44 "xorl %%EDX,%%EAX\n\t"
48 "=c" (flag) : "c" (flag) :
53 #define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
55 #define CHECK_CPUID_IS_SUPPORTED
58 void MyCPUID(UInt32 function, UInt32 *a, UInt32 *b, UInt32 *c, UInt32 *d)
64 UInt32 a2, b2, c2, d2;
68 __asm mov EAX, function;
82 __asm__ __volatile__ (
83 #if defined(MY_CPU_AMD64) && defined(__PIC__)
89 #elif defined(MY_CPU_X86) && defined(__PIC__)
109 __cpuid(CPUInfo, function);
118 BoolInt x86cpuid_CheckAndRead(Cx86cpuid *p)
120 CHECK_CPUID_IS_SUPPORTED
121 MyCPUID(0, &p->maxFunc, &p->vendor[0], &p->vendor[2], &p->vendor[1]);
122 MyCPUID(1, &p->ver, &p->b, &p->c, &p->d);
126 static const UInt32 kVendors[][3] =
128 { 0x756E6547, 0x49656E69, 0x6C65746E},
129 { 0x68747541, 0x69746E65, 0x444D4163},
130 { 0x746E6543, 0x48727561, 0x736C7561}
133 int x86cpuid_GetFirm(const Cx86cpuid *p)
136 for (i = 0; i < sizeof(kVendors) / sizeof(kVendors[i]); i++)
138 const UInt32 *v = kVendors[i];
139 if (v[0] == p->vendor[0] &&
140 v[1] == p->vendor[1] &&
141 v[2] == p->vendor[2])
147 BoolInt CPU_Is_InOrder()
151 UInt32 family, model;
152 if (!x86cpuid_CheckAndRead(&p))
155 family = x86cpuid_GetFamily(p.ver);
156 model = x86cpuid_GetModel(p.ver);
158 firm = x86cpuid_GetFirm(&p);
162 case CPU_FIRM_INTEL: return (family < 6 || (family == 6 && (
163 /* In-Order Atom CPU */
164 model == 0x1C /* 45 nm, N4xx, D4xx, N5xx, D5xx, 230, 330 */
165 || model == 0x26 /* 45 nm, Z6xx */
166 || model == 0x27 /* 32 nm, Z2460 */
167 || model == 0x35 /* 32 nm, Z2760 */
168 || model == 0x36 /* 32 nm, N2xxx, D2xxx */
170 case CPU_FIRM_AMD: return (family < 5 || (family == 5 && (model < 6 || model == 0xA)));
171 case CPU_FIRM_VIA: return (family < 6 || (family == 6 && model < 0xF));
176 #if !defined(MY_CPU_AMD64) && defined(_WIN32)
178 static BoolInt CPU_Sys_Is_SSE_Supported()
181 vi.dwOSVersionInfoSize = sizeof(vi);
182 if (!GetVersionEx(&vi))
184 return (vi.dwMajorVersion >= 5);
186 #define CHECK_SYS_SSE_SUPPORT if (!CPU_Sys_Is_SSE_Supported()) return False;
188 #define CHECK_SYS_SSE_SUPPORT
191 BoolInt CPU_Is_Aes_Supported()
194 CHECK_SYS_SSE_SUPPORT
195 if (!x86cpuid_CheckAndRead(&p))
197 return (p.c >> 25) & 1;
200 BoolInt CPU_IsSupported_PageGB()
203 if (!x86cpuid_CheckAndRead(&cpuid))
207 MyCPUID(0x80000000, &d[0], &d[1], &d[2], &d[3]);
208 if (d[0] < 0x80000001)
213 MyCPUID(0x80000001, &d[0], &d[1], &d[2], &d[3]);
214 return (d[3] >> 26) & 1;