| 1 |
#include "../../zbuild.h" |
| 2 |
#include "arm_features.h" |
| 3 |
|
| 4 |
#if defined(__linux__) && defined(HAVE_SYS_AUXV_H) |
| 5 |
# include <sys/auxv.h> |
| 6 |
# ifdef ARM_ASM_HWCAP |
| 7 |
# include <asm/hwcap.h> |
| 8 |
# endif |
| 9 |
#elif defined(__FreeBSD__) && defined(__aarch64__) |
| 10 |
# include <machine/armreg.h> |
| 11 |
# ifndef ID_AA64ISAR0_CRC32_VAL |
| 12 |
# define ID_AA64ISAR0_CRC32_VAL ID_AA64ISAR0_CRC32 |
| 13 |
# endif |
| 14 |
#elif defined(__APPLE__) |
| 15 |
# if !defined(_DARWIN_C_SOURCE) |
| 16 |
# define _DARWIN_C_SOURCE /* enable types aliases (eg u_int) */ |
| 17 |
# endif |
| 18 |
# include <sys/sysctl.h> |
| 19 |
#elif defined(_WIN32) |
| 20 |
# include <windows.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
static int arm_has_crc32() { |
| 24 |
#if defined(__linux__) && defined(ARM_AUXV_HAS_CRC32) |
| 25 |
# ifdef HWCAP_CRC32 |
| 26 |
return (getauxval(AT_HWCAP) & HWCAP_CRC32) != 0 ? 1 : 0; |
| 27 |
# else |
| 28 |
return (getauxval(AT_HWCAP2) & HWCAP2_CRC32) != 0 ? 1 : 0; |
| 29 |
# endif |
| 30 |
#elif defined(__FreeBSD__) && defined(__aarch64__) |
| 31 |
return getenv("QEMU_EMULATING") == NULL |
| 32 |
&& ID_AA64ISAR0_CRC32_VAL(READ_SPECIALREG(id_aa64isar0_el1)) >= ID_AA64ISAR0_CRC32_BASE; |
| 33 |
#elif defined(__APPLE__) |
| 34 |
int hascrc32; |
| 35 |
size_t size = sizeof(hascrc32); |
| 36 |
return sysctlbyname("hw.optional.armv8_crc32", &hascrc32, &size, NULL, 0) == 0 |
| 37 |
&& hascrc32 == 1; |
| 38 |
#elif defined(_WIN32) |
| 39 |
return IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE); |
| 40 |
#elif defined(ARM_NOCHECK_ACLE) |
| 41 |
return 1; |
| 42 |
#else |
| 43 |
return 0; |
| 44 |
#endif |
| 45 |
} |
| 46 |
|
| 47 |
/* AArch64 has neon. */ |
| 48 |
#if !defined(__aarch64__) && !defined(_M_ARM64) && !defined(_M_ARM64EC) |
| 49 |
static inline int arm_has_neon() { |
| 50 |
#if defined(__linux__) && defined(ARM_AUXV_HAS_NEON) |
| 51 |
# ifdef HWCAP_ARM_NEON |
| 52 |
return (getauxval(AT_HWCAP) & HWCAP_ARM_NEON) != 0 ? 1 : 0; |
| 53 |
# else |
| 54 |
return (getauxval(AT_HWCAP) & HWCAP_NEON) != 0 ? 1 : 0; |
| 55 |
# endif |
| 56 |
#elif defined(__APPLE__) |
| 57 |
int hasneon; |
| 58 |
size_t size = sizeof(hasneon); |
| 59 |
return sysctlbyname("hw.optional.neon", &hasneon, &size, NULL, 0) == 0 |
| 60 |
&& hasneon == 1; |
| 61 |
#elif defined(_M_ARM) && defined(WINAPI_FAMILY_PARTITION) |
| 62 |
# if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP) |
| 63 |
return 1; /* Always supported */ |
| 64 |
# endif |
| 65 |
#endif |
| 66 |
|
| 67 |
#if defined(ARM_NOCHECK_NEON) |
| 68 |
return 1; |
| 69 |
#else |
| 70 |
return 0; |
| 71 |
#endif |
| 72 |
} |
| 73 |
#endif |
| 74 |
|
| 75 |
/* AArch64 does not have ARMv6 SIMD. */ |
| 76 |
#if !defined(__aarch64__) && !defined(_M_ARM64) && !defined(_M_ARM64EC) |
| 77 |
static inline int arm_has_simd() { |
| 78 |
#if defined(__linux__) && defined(HAVE_SYS_AUXV_H) |
| 79 |
const char *platform = (const char *)getauxval(AT_PLATFORM); |
| 80 |
return strncmp(platform, "v6l", 3) == 0 |
| 81 |
|| strncmp(platform, "v7l", 3) == 0 |
| 82 |
|| strncmp(platform, "v8l", 3) == 0; |
| 83 |
#elif defined(ARM_NOCHECK_SIMD) |
| 84 |
return 1; |
| 85 |
#else |
| 86 |
return 0; |
| 87 |
#endif |
| 88 |
} |
| 89 |
#endif |
| 90 |
|
| 91 |
void Z_INTERNAL arm_check_features(struct arm_cpu_features *features) { |
| 92 |
#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) |
| 93 |
features->has_simd = 0; /* never available */ |
| 94 |
features->has_neon = 1; /* always available */ |
| 95 |
#else |
| 96 |
features->has_simd = arm_has_simd(); |
| 97 |
features->has_neon = arm_has_neon(); |
| 98 |
#endif |
| 99 |
features->has_crc32 = arm_has_crc32(); |
| 100 |
} |