Debian Codegraph @

ext/zlib-ng/arch/arm/arm_features.c

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