FFmpeg
cpu.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/cpu.h"
20 #include "libavutil/cpu_internal.h"
21 #include "config.h"
22 
23 #if HAVE_GETAUXVAL || HAVE_ELF_AUX_INFO
24 #include <stdint.h>
25 #include <sys/auxv.h>
26 
27 #define HWCAP_AARCH64_CRC32 (1 << 7)
28 #define HWCAP_AARCH64_ASIMDDP (1 << 20)
29 #define HWCAP_AARCH64_SVE (1 << 22)
30 #define HWCAP2_AARCH64_SVE2 (1 << 1)
31 #define HWCAP2_AARCH64_I8MM (1 << 13)
32 #define HWCAP2_AARCH64_SME (1 << 23)
33 #define HWCAP2_AARCH64_SME_I16I64 (1 << 24)
34 #define HWCAP2_AARCH64_SME2 (1ULL << 37)
35 
36 static int detect_flags(void)
37 {
38  int flags = 0;
39 
40  unsigned long hwcap = ff_getauxval(AT_HWCAP);
41  unsigned long hwcap2 = ff_getauxval(AT_HWCAP2);
42 
43  if (hwcap & HWCAP_AARCH64_CRC32)
45  if (hwcap & HWCAP_AARCH64_ASIMDDP)
47  if (hwcap & HWCAP_AARCH64_SVE)
49  if (hwcap2 & HWCAP2_AARCH64_SVE2)
51  if (hwcap2 & HWCAP2_AARCH64_I8MM)
53  if (hwcap2 & HWCAP2_AARCH64_SME)
55  if (hwcap2 & HWCAP2_AARCH64_SME_I16I64)
57  if (hwcap2 & HWCAP2_AARCH64_SME2)
59 
60  return flags;
61 }
62 
63 #elif defined(__APPLE__) && HAVE_SYSCTLBYNAME
64 #include <sys/sysctl.h>
65 
66 static int have_feature(const char *feature) {
67  uint32_t value = 0;
68  size_t size = sizeof(value);
69  if (!sysctlbyname(feature, &value, &size, NULL, 0))
70  return value;
71  return 0;
72 }
73 
74 static int detect_flags(void)
75 {
76  int flags = 0;
77 
78  if (have_feature("hw.optional.arm.FEAT_DotProd"))
80  if (have_feature("hw.optional.arm.FEAT_I8MM"))
82  if (have_feature("hw.optional.arm.FEAT_SME"))
84  if (have_feature("hw.optional.arm.FEAT_SME_I16I64"))
86  if (have_feature("hw.optional.armv8_crc32"))
88  if (have_feature("hw.optional.arm.FEAT_SME2"))
90 
91  return flags;
92 }
93 
94 #elif defined(__OpenBSD__)
95 #include <machine/armreg.h>
96 #include <machine/cpu.h>
97 #include <sys/types.h>
98 #include <sys/sysctl.h>
99 
100 static int detect_flags(void)
101 {
102  int flags = 0;
103 
104 #ifdef CPU_ID_AA64ISAR0
105  int mib[2];
106  uint64_t isar0;
107  uint64_t isar1;
108  size_t len;
109 
110  mib[0] = CTL_MACHDEP;
111  mib[1] = CPU_ID_AA64ISAR0;
112  len = sizeof(isar0);
113  if (sysctl(mib, 2, &isar0, &len, NULL, 0) != -1) {
114  if (ID_AA64ISAR0_DP(isar0) >= ID_AA64ISAR0_DP_IMPL)
116  if (ID_AA64ISAR0_CRC32(isar0) >= ID_AA64ISAR0_CRC32_BASE)
118  }
119 
120  mib[0] = CTL_MACHDEP;
121  mib[1] = CPU_ID_AA64ISAR1;
122  len = sizeof(isar1);
123  if (sysctl(mib, 2, &isar1, &len, NULL, 0) != -1) {
124 #ifdef ID_AA64ISAR1_I8MM_IMPL
125  if (ID_AA64ISAR1_I8MM(isar1) >= ID_AA64ISAR1_I8MM_IMPL)
127 #endif
128  }
129 #endif
130 
131  return flags;
132 }
133 
134 #elif defined(_WIN32)
135 #include <windows.h>
136 
137 static int detect_flags(void)
138 {
139  int flags = 0;
140 #ifdef PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE
141  if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
143 #endif
144 #ifdef PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE
145  if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
147 #endif
148 #ifdef PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE
149  if (IsProcessorFeaturePresent(PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE))
151 #endif
152 #ifdef PF_ARM_SVE_INSTRUCTIONS_AVAILABLE
153  if (IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE))
155 #endif
156 #ifdef PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE
157  if (IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE))
159 #endif
160 #ifdef PF_ARM_SME_INSTRUCTIONS_AVAILABLE
161  if (IsProcessorFeaturePresent(PF_ARM_SME_INSTRUCTIONS_AVAILABLE))
163 #endif
164 #ifdef PF_ARM_SME_I16I64_INSTRUCTIONS_AVAILABLE
165  if (IsProcessorFeaturePresent(PF_ARM_SME_I16I64_INSTRUCTIONS_AVAILABLE))
167 #endif
168 #ifdef PF_ARM_SME2_INSTRUCTIONS_AVAILABLE
169  if (IsProcessorFeaturePresent(PF_ARM_SME2_INSTRUCTIONS_AVAILABLE))
171 #endif
172  return flags;
173 }
174 #else
175 
176 static int detect_flags(void)
177 {
178  return 0;
179 }
180 
181 #endif
182 
184 {
185  int flags = AV_CPU_FLAG_ARMV8 * HAVE_ARMV8 |
186  AV_CPU_FLAG_NEON * HAVE_NEON;
187 
188 #ifdef __ARM_FEATURE_DOTPROD
190 #endif
191 #ifdef __ARM_FEATURE_MATMUL_INT8
193 #endif
194 #ifdef __ARM_FEATURE_SVE
196 #endif
197 #ifdef __ARM_FEATURE_SVE2
199 #endif
200 #ifdef __ARM_FEATURE_SME
202 #endif
203 #ifdef __ARM_FEATURE_CRC32
205 #endif
206 #ifdef __ARM_FEATURE_SME_I16I64
208 #endif
209 #ifdef __ARM_FEATURE_SME2
211 #endif
212 
213  flags |= detect_flags();
214 
215  return flags;
216 }
217 
219 {
220  int flags = av_get_cpu_flags();
221 
222  if (flags & AV_CPU_FLAG_NEON)
223  return 16;
224 
225  return 8;
226 }
flags
const SwsFlags flags[]
Definition: swscale.c:61
AT_HWCAP
#define AT_HWCAP
Definition: cpu.c:50
AV_CPU_FLAG_SVE2
#define AV_CPU_FLAG_SVE2
Definition: cpu.h:79
AV_CPU_FLAG_SVE
#define AV_CPU_FLAG_SVE
Definition: cpu.h:78
AV_CPU_FLAG_DOTPROD
#define AV_CPU_FLAG_DOTPROD
Definition: cpu.h:76
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:109
cpu_internal.h
ff_get_cpu_max_align_aarch64
size_t ff_get_cpu_max_align_aarch64(void)
Definition: cpu.c:218
AV_CPU_FLAG_SME2
#define AV_CPU_FLAG_SME2
Definition: cpu.h:82
ff_getauxval
unsigned long ff_getauxval(unsigned long type)
Definition: cpu.c:308
NULL
#define NULL
Definition: coverity.c:32
ff_get_cpu_flags_aarch64
int ff_get_cpu_flags_aarch64(void)
Definition: cpu.c:183
detect_flags
static int detect_flags(void)
Definition: cpu.c:176
AV_CPU_FLAG_I8MM
#define AV_CPU_FLAG_I8MM
Definition: cpu.h:77
cpu.h
AV_CPU_FLAG_ARM_CRC
#define AV_CPU_FLAG_ARM_CRC
Definition: cpu.h:81
AV_CPU_FLAG_NEON
#define AV_CPU_FLAG_NEON
Definition: cpu.h:73
size
int size
Definition: twinvq_data.h:10344
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AV_CPU_FLAG_SME_I16I64
#define AV_CPU_FLAG_SME_I16I64
Definition: cpu.h:83
AV_CPU_FLAG_ARMV8
#define AV_CPU_FLAG_ARMV8
Definition: cpu.h:74
len
int len
Definition: vorbis_enc_data.h:426
AV_CPU_FLAG_SME
#define AV_CPU_FLAG_SME
Definition: cpu.h:80
AT_HWCAP2
#define AT_HWCAP2
Definition: cpu.c:53