Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUG: verify the OS supports avx instruction
On some systems you can disable avx registers but the gcc builtin does only checks if the cpu has the feature. Before using avx functions check the OS support with xgetbv. Closes gh-10787 Closes gh-9534
- Loading branch information
1 parent
e4d678a
commit cde5583
Showing
6 changed files
with
69 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#define _UMATHMODULE | ||
#define NPY_NO_DEPRECATED_API NPY_API_VERSION | ||
|
||
#include <Python.h> | ||
|
||
#include "npy_config.h" | ||
|
||
#define PY_ARRAY_UNIQUE_SYMBOL _npy_umathmodule_ARRAY_API | ||
#define NO_IMPORT_ARRAY | ||
|
||
#include "cpuid.h" | ||
|
||
#define XCR_XFEATURE_ENABLED_MASK 0x0 | ||
#define XSTATE_SSE 0x2 | ||
#define XSTATE_YMM 0x4 | ||
|
||
/* | ||
* verify the OS supports avx instructions | ||
* it can be disabled in some OS, e.g. with the nosavex boot option of linux | ||
*/ | ||
static NPY_INLINE | ||
int os_avx_support(void) | ||
{ | ||
#if HAVE_XGETBV | ||
/* | ||
* use bytes for xgetbv to avoid issues with compiler not knowing the | ||
* instruction | ||
*/ | ||
unsigned int eax, edx; | ||
unsigned int ecx = XCR_XFEATURE_ENABLED_MASK; | ||
__asm__("xgetbv" : "=a" (eax), "=d" (edx) : "c" (ecx)); | ||
return (eax & (XSTATE_SSE | XSTATE_YMM)) == (XSTATE_SSE | XSTATE_YMM); | ||
#else | ||
return 0; | ||
#endif | ||
} | ||
|
||
|
||
/* | ||
* Primitive cpu feature detect function | ||
* Currently only supports checking for avx on gcc compatible compilers. | ||
*/ | ||
NPY_NO_EXPORT int | ||
npy_cpu_supports(const char * feature) | ||
{ | ||
#ifdef HAVE___BUILTIN_CPU_SUPPORTS | ||
if (strcmp(feature, "avx2") == 0) { | ||
return __builtin_cpu_supports("avx2") && os_avx_support(); | ||
} | ||
else if (strcmp(feature, "avx") == 0) { | ||
return __builtin_cpu_supports("avx") && os_avx_support(); | ||
} | ||
#endif | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef _NPY_PRIVATE__CPUID_H_ | ||
#define _NPY_PRIVATE__CPUID_H_ | ||
|
||
#include <numpy/ndarraytypes.h> /* for NPY_NO_EXPORT */ | ||
|
||
NPY_NO_EXPORT int | ||
npy_cpu_supports(const char * feature); | ||
|
||
#endif |