38#if defined(INFIX_ARCH_X64)
41#elif defined(__GNUC__) || defined(__clang__)
46static uint64_t _infix_xgetbv(
void) {
48 return _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
49#elif defined(__GNUC__) || defined(__clang__)
51 __asm__ __volatile__(
"xgetbv" :
"=a"(eax),
"=d"(edx) :
"c"(0));
52 return ((uint64_t)edx << 32) | eax;
59#define XCR0_SSE (1 << 1)
60#define XCR0_AVX (1 << 2)
61#define XCR0_OPMASK (1 << 5)
62#define XCR0_ZMM_Hi256 (1 << 6)
63#define XCR0_Hi16_ZMM (1 << 7)
67#if defined(INFIX_ARCH_AARCH64) && defined(__has_include)
68#if __has_include(<sys/auxv.h>) && defined(__linux__)
71#define HWCAP_SVE (1 << 22)
73#elif __has_include(<sys/sysctl.h>) && defined(__APPLE__)
74#include <sys/sysctl.h>
78#if defined(INFIX_ARCH_X64)
79bool infix_cpu_has_avx2(
void) {
83 bool avx2_hardware =
false;
88 osxsave = (cpuInfo[2] & (1 << 27)) != 0;
90 __cpuidex(cpuInfo, 7, 0);
91 avx2_hardware = (cpuInfo[1] & (1 << 5)) != 0;
92#elif defined(__GNUC__) || defined(__clang__)
93 unsigned int eax, ebx, ecx, edx;
94 __cpuid(1, eax, ebx, ecx, edx);
95 osxsave = (ecx & (1 << 27)) != 0;
97 if (__get_cpuid_max(0, NULL) >= 7) {
98 __cpuid_count(7, 0, eax, ebx, ecx, edx);
99 avx2_hardware = (ebx & (1 << 5)) != 0;
103 if (!osxsave || !avx2_hardware)
108 uint64_t xcr0 = _infix_xgetbv();
109 return (xcr0 & (XCR0_SSE | XCR0_AVX)) == (XCR0_SSE | XCR0_AVX);
112bool infix_cpu_has_avx512f(
void) {
113 bool osxsave =
false;
114 bool avx512f_hardware =
false;
119 osxsave = (cpuInfo[2] & (1 << 27)) != 0;
121 __cpuidex(cpuInfo, 7, 0);
122 avx512f_hardware = (cpuInfo[1] & (1 << 16)) != 0;
123#elif defined(__GNUC__) || defined(__clang__)
124 unsigned int eax, ebx, ecx, edx;
125 __cpuid(1, eax, ebx, ecx, edx);
126 osxsave = (ecx & (1 << 27)) != 0;
128 if (__get_cpuid_max(0, NULL) >= 7) {
129 __cpuid_count(7, 0, eax, ebx, ecx, edx);
130 avx512f_hardware = (ebx & (1 << 16)) != 0;
134 if (!osxsave || !avx512f_hardware)
139 uint64_t xcr0 = _infix_xgetbv();
140 uint64_t required = XCR0_SSE | XCR0_AVX | XCR0_OPMASK | XCR0_ZMM_Hi256 | XCR0_Hi16_ZMM;
141 return (xcr0 & required) == required;
145#if defined(INFIX_ARCH_AARCH64)
146bool infix_cpu_has_sve(
void) {
147#if defined(__linux__) && defined(HWCAP_SVE)
148 return (getauxval(AT_HWCAP) & HWCAP_SVE) != 0;
149#elif defined(__APPLE__)
151 size_t size =
sizeof(sve_present);
152 if (sysctlbyname(
"hw.optional.arm.FEAT_SVE", &sve_present, &size, NULL, 0) == 0)
153 return sve_present == 1;
#define INFIX_PATCH
Definition infix.h:85
#define INFIX_NODISCARD
A compatibility macro for the C23 [[nodiscard]] attribute.
Definition infix.h:140
#define INFIX_MAJOR
Definition infix.h:83
#define INFIX_MINOR
Definition infix.h:84
#define INFIX_API
Symbol visibility macro.
Definition infix.h:114
The public interface for the infix FFI library.
A structure representing the semantic version of the library.
Definition infix.h:148