infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
platform.c
Go to the documentation of this file.
1
26#include "common/platform.h"
27
28#if defined(INFIX_ARCH_X64)
29#if defined(_MSC_VER)
30#include <intrin.h>
31#elif defined(__GNUC__) || defined(__clang__)
32#include <cpuid.h>
33#endif
34#endif
35#if defined(INFIX_ARCH_AARCH64) && defined(__has_include)
36#if __has_include(<sys/auxv.h>) && defined(__linux__)
37#include <sys/auxv.h>
38#ifndef HWCAP_SVE
39#define HWCAP_SVE (1 << 22)
40#endif
41#elif __has_include(<sys/sysctl.h>) && defined(__APPLE__)
42#include <sys/sysctl.h>
43#endif
44#endif
45
46#if defined(INFIX_ARCH_X64)
47bool infix_cpu_has_avx2(void) {
48#if defined(_MSC_VER)
49 int cpuInfo[4];
50 __cpuidex(cpuInfo, 7, 0);
51 return (cpuInfo[1] & (1 << 5)) != 0;
52#elif defined(__GNUC__) || defined(__clang__)
53 unsigned int eax, ebx, ecx, edx;
54 if (__get_cpuid_max(0, NULL) >= 7) {
55 __cpuid_count(7, 0, eax, ebx, ecx, edx);
56 return (ebx & (1 << 5)) != 0;
57 }
58 return false;
59#else
60 return false;
61#endif
62}
63
64bool infix_cpu_has_avx512f(void) {
65#if defined(_MSC_VER)
66 int cpuInfo[4];
67 __cpuidex(cpuInfo, 7, 0);
68 return (cpuInfo[1] & (1 << 16)) != 0;
69#elif defined(__GNUC__) || defined(__clang__)
70 unsigned int eax, ebx, ecx, edx;
71 if (__get_cpuid_max(0, NULL) >= 7) {
72 __cpuid_count(7, 0, eax, ebx, ecx, edx);
73 return (ebx & (1 << 16)) != 0;
74 }
75 return false;
76#else
77 return false;
78#endif
79}
80#endif
81
82#if defined(INFIX_ARCH_AARCH64)
83bool infix_cpu_has_sve(void) {
84#if defined(__linux__) && defined(HWCAP_SVE)
85 return (getauxval(AT_HWCAP) & HWCAP_SVE) != 0;
86#elif defined(__APPLE__)
87 int sve_present = 0;
88 size_t size = sizeof(sve_present);
89 if (sysctlbyname("hw.optional.arm.FEAT_SVE", &sve_present, &size, NULL, 0) == 0)
90 return sve_present == 1;
91 return false;
92#else
93 // Add checks for other OS (e.g., Windows on ARM) if needed.
94 return false;
95#endif
96}
97#endif
Declares internal, runtime CPU/OS feature detection functions.