Skip to content

Commit

Permalink
cpuinfo: Added SDL_HasAVX512F().
Browse files Browse the repository at this point in the history
This checks for the "foundation" AVX-512 instructions (that all AVX-512
compatible CPUs support).
  • Loading branch information
icculus committed May 21, 2018
1 parent 4718791 commit 4df859c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/SDL_cpuinfo.h
Expand Up @@ -159,6 +159,11 @@ extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX(void);
*/
extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX2(void);

/**
* This function returns true if the CPU has AVX-512F (foundation) features.
*/
extern DECLSPEC SDL_bool SDLCALL SDL_HasAVX512F(void);

/**
* This function returns true if the CPU has NEON (ARM SIMD) features.
*/
Expand Down
28 changes: 27 additions & 1 deletion src/cpuinfo/SDL_cpuinfo.c
Expand Up @@ -89,6 +89,7 @@
#define CPU_HAS_AVX (1 << 9)
#define CPU_HAS_AVX2 (1 << 10)
#define CPU_HAS_NEON (1 << 11)
#define CPU_HAS_AVX512F (1 << 12)

#if SDL_ALTIVEC_BLITTERS && HAVE_SETJMP && !__MACOSX__ && !__OpenBSD__
/* This is the brute force way of detecting instruction sets...
Expand Down Expand Up @@ -247,6 +248,7 @@ CPU_haveCPUID(void)
static int CPU_CPUIDFeatures[4];
static int CPU_CPUIDMaxFunction = 0;
static SDL_bool CPU_OSSavesYMM = SDL_FALSE;
static SDL_bool CPU_OSSavesZMM = SDL_FALSE;

static void
CPU_calcCPUIDFeatures(void)
Expand All @@ -267,7 +269,7 @@ CPU_calcCPUIDFeatures(void)

/* Check to make sure we can call xgetbv */
if (c & 0x08000000) {
/* Call xgetbv to see if YMM register state is saved */
/* Call xgetbv to see if YMM (etc) register state is saved */
#if defined(__GNUC__) && (defined(i386) || defined(__x86_64__))
__asm__(".byte 0x0f, 0x01, 0xd0" : "=a" (a) : "c" (0) : "%edx");
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) && (_MSC_FULL_VER >= 160040219) /* VS2010 SP1 */
Expand All @@ -281,6 +283,7 @@ CPU_calcCPUIDFeatures(void)
}
#endif
CPU_OSSavesYMM = ((a & 6) == 6) ? SDL_TRUE : SDL_FALSE;
CPU_OSSavesZMM = (CPU_OSSavesYMM && ((a & 0xe0) == 0xe0)) ? SDL_TRUE : SDL_FALSE;
}
}
}
Expand Down Expand Up @@ -401,6 +404,18 @@ CPU_haveAVX2(void)
return 0;
}

static int
CPU_haveAVX512F(void)
{
if (CPU_OSSavesZMM && (CPU_CPUIDMaxFunction >= 7)) {
int a, b, c, d;
(void) a; (void) b; (void) c; (void) d; /* compiler warnings... */
cpuid(7, a, b, c, d);
return (b & 0x00010000);
}
return 0;
}

static int SDL_CPUCount = 0;

int
Expand Down Expand Up @@ -624,6 +639,10 @@ SDL_GetCPUFeatures(void)
SDL_CPUFeatures |= CPU_HAS_AVX2;
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
}
if (CPU_haveAVX512F()) {
SDL_CPUFeatures |= CPU_HAS_AVX512F;
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 64);
}
if (CPU_haveNEON()) {
SDL_CPUFeatures |= CPU_HAS_NEON;
SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
Expand Down Expand Up @@ -699,6 +718,12 @@ SDL_HasAVX2(void)
return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX2);
}

SDL_bool
SDL_HasAVX512F(void)
{
return CPU_FEATURE_AVAILABLE(CPU_HAS_AVX512F);
}

SDL_bool
SDL_HasNEON(void)
{
Expand Down Expand Up @@ -819,6 +844,7 @@ main()
printf("SSE4.2: %d\n", SDL_HasSSE42());
printf("AVX: %d\n", SDL_HasAVX());
printf("AVX2: %d\n", SDL_HasAVX2());
printf("AVX-512F: %d\n", SDL_HasAVX512F());
printf("NEON: %d\n", SDL_HasNEON());
printf("RAM: %d MB\n", SDL_GetSystemRAM());
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Expand Up @@ -671,3 +671,4 @@
#define SDL_log10f SDL_log10f_REAL
#define SDL_GameControllerMappingForDeviceIndex SDL_GameControllerMappingForDeviceIndex_REAL
#define SDL_LinuxSetThreadPriority SDL_LinuxSetThreadPriority_REAL
#define SDL_HasAVX512F SDL_HasAVX512F_REAL
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_procs.h
Expand Up @@ -711,3 +711,4 @@ SDL_DYNAPI_PROC(char*,SDL_GameControllerMappingForDeviceIndex,(int a),(a),return
#ifdef __LINUX__
SDL_DYNAPI_PROC(int,SDL_LinuxSetThreadPriority,(Sint64 a, int b),(a,b),return)
#endif
SDL_DYNAPI_PROC(SDL_bool,SDL_HasAVX512F,(void),(),return)
1 change: 1 addition & 0 deletions test/testplatform.c
Expand Up @@ -380,6 +380,7 @@ TestCPUInfo(SDL_bool verbose)
SDL_Log("SSE4.2 %s\n", SDL_HasSSE42()? "detected" : "not detected");
SDL_Log("AVX %s\n", SDL_HasAVX()? "detected" : "not detected");
SDL_Log("AVX2 %s\n", SDL_HasAVX2()? "detected" : "not detected");
SDL_Log("AVX-512F %s\n", SDL_HasAVX512F()? "detected" : "not detected");
SDL_Log("NEON %s\n", SDL_HasNEON()? "detected" : "not detected");
SDL_Log("System RAM %d MB\n", SDL_GetSystemRAM());
}
Expand Down

0 comments on commit 4df859c

Please sign in to comment.