Pelles C forum

C language => Tips & tricks => Topic started by: TimoVJL on January 23, 2017, 05:23:33 PM

Title: Check with _cpuid()
Post by: TimoVJL on January 23, 2017, 05:23:33 PM
https://en.wikipedia.org/wiki/CPUID

Example to check if AVX is available.
Code: [Select]
#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
Code: [Select]
#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);
}
Title: Re: Check with _cpuid()
Post by: Robert on May 16, 2021, 06:59:27 AM
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.

Title: Re: Check with _cpuid()
Post by: Vortex on May 16, 2021, 12:49:38 PM
Hi Robert,

Here is how to build the 64-bit version of msvcrt.lib :

Code: [Select]
\PellesC\bin\polib.exe /OUT:msvcrt.lib /MACHINE:x64 \Windows\System32\msvcrt.dll
Title: Re: Check with _cpuid()
Post by: frankie on May 16, 2021, 02:11:42 PM
Timo's code modified to be compiled with standard PellesC configuration:
Code: [Select]
#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.
Title: Re: Check with _cpuid()
Post by: Pelle on May 16, 2021, 05:05:27 PM
For checking some common CPU features, the following is also possible (with Pelles C):

Code: [Select]
#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");
}
Title: Re: Check with _cpuid()
Post by: Robert on May 16, 2021, 09:56:39 PM
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 !
Title: Re: Check with _cpuid()
Post by: Pelle on May 17, 2021, 04:02:32 PM
Like the poet said: "shit happens" ...  ;D