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