https://en.wikipedia.org/wiki/CPUID
Example to check if AVX is available.
#include <intrin.h>
void __cdecl exit(int status);
int printf(const char * restrict format, ...);
#pragma comment(lib, "msvcrt.lib")
//#pragma comment(linker,"/subsystem:console,5.1")
void __cdecl mainCRTStartup(void)
{
int __cdecl main(void);
void __cdecl exit(int status);
exit(main());
}
int CheckAVX(void) {
int r[4]; // EAX, EBX, ECX and EDX
_cpuid(r, 1);
return (r[2] & 0x10000000) ? 1 : 0;
}
int main(void)
{
printf("AVX support: %s\n", CheckAVX()?"Yes":"No");
exit(0);
}
Why not some string too#include <intrin.h>
void __cdecl exit(int status);
int printf(const char * restrict format, ...);
#pragma comment(lib, "msvcrt.lib")
//#pragma comment(linker,"/subsystem:console,5.1")
void __cdecl mainCRTStartup(void)
{
int __cdecl main(void);
void __cdecl exit(int status);
exit(main());
}
//" AuthcAMDenti" -> "AuthenticAMD"
char *CheckVendor(void) {
static char s[17];
_cpuid((int*)s, 0);
*(int*)s = *(int*)(s+4);
*(int*)(s+4) = *(int*)(s+12);
*(s+12) = 0;
return s;
}
char *CheckBrand(void) {
static char s[50];
_cpuid((int*)s, 0x80000000);
if (*(unsigned int*)s >= 0x80000004) {
_cpuid((int*)s, 0x80000002);
_cpuid((int*)(s+16), 0x80000003);
_cpuid((int*)(s+32), 0x80000004);
}
return s;
}
int main(void)
{
printf("%s\n",CheckVendor());
printf("%s\n",CheckBrand());
exit(0);
}
Hi TimoVJL:
Windows 10 64 bit, Pelles C 10.00.6.
CpuIdTestAVX64.ppj from the .zip in the Pelles C IDE.
Quote
Output:
Project build started
Project build ended in complete failure
Project:
Building CpuIdTestAVX.obj.
Building CpuIdTestAVX64.exe.
POLINK: fatal error: File not found: 'msvcrt.lib'.
*** Error code: 1 ***
Done.
Hi Robert,
Here is how to build the 64-bit version of msvcrt.lib :
\PellesC\bin\polib.exe /OUT:msvcrt.lib /MACHINE:x64 \Windows\System32\msvcrt.dll
Timo's code modified to be compiled with standard PellesC configuration:
#include <stdio.h>
#include <stdlib.h>
#include <intrin.h>
//" AuthcAMDenti" -> "AuthenticAMD"
char *CheckVendor(void)
{
static char s[17];
_cpuid((int *)s, 0);
*(int *)s = *(int *)(s + 4);
*(int *)(s + 4) = *(int *)(s + 12);
*(s + 12) = 0;
return s;
}
char *CheckBrand(void)
{
static char s[50];
_cpuid((int *)s, 0x80000000);
if (*(unsigned int *)s >= 0x80000004)
{
_cpuid((int *)s, 0x80000002);
_cpuid((int *)(s + 16), 0x80000003);
_cpuid((int *)(s + 32), 0x80000004);
}
return s;
}
int CheckAVX(void)
{
int r[4]; // EAX, EBX, ECX and EDX
_cpuid(r, 1);
return (r[2] & 0x10000000) ? 1 : 0;
}
int main(void)
{
printf("%s\n", CheckVendor());
printf("%s\n", CheckBrand());
printf("AVX support: %s\n", CheckAVX()? "Yes" : "No");
exit(0);
}
Project attached.
For checking some common CPU features, the following is also possible (with Pelles C):
#include <stdio.h>
#include <cpufeat.h>
int main(void)
{
printf("The current processor support:\n");
if (__cpu_feature_mmx()) printf("- MMX instructions\n");
if (__cpu_feature_sse()) printf("- SSE instructions\n");
if (__cpu_feature_sse2()) printf("- SSE2 instructions\n");
if (__cpu_feature_sse3()) printf("- SSE3 instructions\n");
if (__cpu_feature_ssse3()) printf("- SSSE3 instructions\n");
if (__cpu_feature_sse41()) printf("- SSE41 instructions\n");
if (__cpu_feature_sse42()) printf("- SSE42 instructions\n");
if (__cpu_feature_avx()) printf("- AVX instructions\n");
if (__cpu_feature_avx2()) printf("- AVX2 instructions\n");
if (__cpu_feature_avx512f()) printf("- AVX512F instructions\n");
}
Thank you, Vortex, frankie and Pelle. Very interesting and useful information.
I completely missed the introduction of cpufeat.h even though it is prominent on the splash page of the Help file. I must pay better attention. Focus !
Like the poet said: "shit happens" ... ;D