1 /* CpuArch.c -- CPU specific code
2 2010-10-26: Igor Pavlov : Public domain */
6 #ifdef MY_CPU_X86_OR_AMD64
8 #if (defined(_MSC_VER) && !defined(MY_CPU_AMD64)) || defined(__GNUC__)
12 #if defined(USE_ASM) && !defined(MY_CPU_AMD64)
13 static UInt32
CheckFlag(UInt32 flag
)
29 __asm__
__volatile__ (
32 "movl %%EAX,%%EDX\n\t"
38 "xorl %%EDX,%%EAX\n\t"
42 "=c" (flag
) : "c" (flag
):
47 #define CHECK_CPUID_IS_SUPPORTED if (CheckFlag(1 << 18) == 0 || CheckFlag(1 << 21) == 0) return False;
49 #define CHECK_CPUID_IS_SUPPORTED
52 static void MyCPUID(UInt32 function
, UInt32
*a
, UInt32
*b
, UInt32
*c
, UInt32
*d
)
58 UInt32 a2
, b2
, c2
, d2
;
62 __asm mov EAX
, function
;
76 #if defined(MY_CPU_AMD64)
78 __asm__
__volatile__ (
90 __asm__
__volatile__ (
107 __cpuid(CPUInfo
, function
);
116 Bool
x86cpuid_CheckAndRead(Cx86cpuid
*p
)
118 CHECK_CPUID_IS_SUPPORTED
119 MyCPUID(0, &p
->maxFunc
, &p
->vendor
[0], &p
->vendor
[2], &p
->vendor
[1]);
120 MyCPUID(1, &p
->ver
, &p
->b
, &p
->c
, &p
->d
);
124 static UInt32 kVendors
[][3] =
126 { 0x756E6547, 0x49656E69, 0x6C65746E},
127 { 0x68747541, 0x69746E65, 0x444D4163},
128 { 0x746E6543, 0x48727561, 0x736C7561}
131 int x86cpuid_GetFirm(const Cx86cpuid
*p
)
134 for (i
= 0; i
< sizeof(kVendors
) / sizeof(kVendors
[i
]); i
++)
136 const UInt32
*v
= kVendors
[i
];
137 if (v
[0] == p
->vendor
[0] &&
138 v
[1] == p
->vendor
[1] &&
139 v
[2] == p
->vendor
[2])
145 Bool
CPU_Is_InOrder()
149 UInt32 family
, model
;
150 if (!x86cpuid_CheckAndRead(&p
))
152 family
= x86cpuid_GetFamily(&p
);
153 model
= x86cpuid_GetModel(&p
);
154 firm
= x86cpuid_GetFirm(&p
);
157 case CPU_FIRM_INTEL
: return (family
< 6 || (family
== 6 && model
== 0x100C));
158 case CPU_FIRM_AMD
: return (family
< 5 || (family
== 5 && (model
< 6 || model
== 0xA)));
159 case CPU_FIRM_VIA
: return (family
< 6 || (family
== 6 && model
< 0xF));
164 #if !defined(MY_CPU_AMD64) && defined(_WIN32)
165 static Bool
CPU_Sys_Is_SSE_Supported()
168 vi
.dwOSVersionInfoSize
= sizeof(vi
);
169 if (!GetVersionEx(&vi
))
171 return (vi
.dwMajorVersion
>= 5);
173 #define CHECK_SYS_SSE_SUPPORT if (!CPU_Sys_Is_SSE_Supported()) return False;
175 #define CHECK_SYS_SSE_SUPPORT
178 Bool
CPU_Is_Aes_Supported()
181 CHECK_SYS_SSE_SUPPORT
182 if (!x86cpuid_CheckAndRead(&p
))
184 return (p
.c
>> 25) & 1;