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 
34 static int detect_flags(void)
35 {
36  int flags = 0;
37 
38  unsigned long hwcap = ff_getauxval(AT_HWCAP);
39  unsigned long hwcap2 = ff_getauxval(AT_HWCAP2);
40 
41  if (hwcap & HWCAP_AARCH64_CRC32)
43  if (hwcap & HWCAP_AARCH64_ASIMDDP)
45  if (hwcap & HWCAP_AARCH64_SVE)
47  if (hwcap2 & HWCAP2_AARCH64_SVE2)
49  if (hwcap2 & HWCAP2_AARCH64_I8MM)
51  if (hwcap2 & HWCAP2_AARCH64_SME)
53 
54  return flags;
55 }
56 
57 #elif defined(__APPLE__) && HAVE_SYSCTLBYNAME
58 #include <sys/sysctl.h>
59 
60 static int have_feature(const char *feature) {
61  uint32_t value = 0;
62  size_t size = sizeof(value);
63  if (!sysctlbyname(feature, &value, &size, NULL, 0))
64  return value;
65  return 0;
66 }
67 
68 static int detect_flags(void)
69 {
70  int flags = 0;
71 
72  if (have_feature("hw.optional.arm.FEAT_DotProd"))
74  if (have_feature("hw.optional.arm.FEAT_I8MM"))
76  if (have_feature("hw.optional.arm.FEAT_SME"))
78  if (have_feature("hw.optional.armv8_crc32"))
80 
81  return flags;
82 }
83 
84 #elif defined(__OpenBSD__)
85 #include <machine/armreg.h>
86 #include <machine/cpu.h>
87 #include <sys/types.h>
88 #include <sys/sysctl.h>
89 
90 static int detect_flags(void)
91 {
92  int flags = 0;
93 
94 #ifdef CPU_ID_AA64ISAR0
95  int mib[2];
96  uint64_t isar0;
97  uint64_t isar1;
98  size_t len;
99 
100  mib[0] = CTL_MACHDEP;
101  mib[1] = CPU_ID_AA64ISAR0;
102  len = sizeof(isar0);
103  if (sysctl(mib, 2, &isar0, &len, NULL, 0) != -1) {
104  if (ID_AA64ISAR0_DP(isar0) >= ID_AA64ISAR0_DP_IMPL)
106  }
107 
108  mib[0] = CTL_MACHDEP;
109  mib[1] = CPU_ID_AA64ISAR1;
110  len = sizeof(isar1);
111  if (sysctl(mib, 2, &isar1, &len, NULL, 0) != -1) {
112 #ifdef ID_AA64ISAR1_I8MM_IMPL
113  if (ID_AA64ISAR1_I8MM(isar1) >= ID_AA64ISAR1_I8MM_IMPL)
115 #endif
116  }
117 #endif
118 
119  return flags;
120 }
121 
122 #elif defined(_WIN32)
123 #include <windows.h>
124 
125 static int detect_flags(void)
126 {
127  int flags = 0;
128 #ifdef PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE
129  if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
131 #endif
132 #ifdef PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE
133  if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
135 #endif
136 #ifdef PF_ARM_SVE_I8MM_INSTRUCTIONS_AVAILABLE
137  /* There's no PF_* flag that indicates whether plain I8MM is available
138  * or not. But if SVE_I8MM is available, that also implies that
139  * regular I8MM is available. */
140  if (IsProcessorFeaturePresent(PF_ARM_SVE_I8MM_INSTRUCTIONS_AVAILABLE))
142 #endif
143 #ifdef PF_ARM_SVE_INSTRUCTIONS_AVAILABLE
144  if (IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE))
146 #endif
147 #ifdef PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE
148  if (IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE))
150 #endif
151 #ifdef PF_ARM_SME_INSTRUCTIONS_AVAILABLE
152  if (IsProcessorFeaturePresent(PF_ARM_SME_INSTRUCTIONS_AVAILABLE))
154 #endif
155  return flags;
156 }
157 #else
158 
159 static int detect_flags(void)
160 {
161  return 0;
162 }
163 
164 #endif
165 
167 {
168  int flags = AV_CPU_FLAG_ARMV8 * HAVE_ARMV8 |
169  AV_CPU_FLAG_NEON * HAVE_NEON;
170 
171 #ifdef __ARM_FEATURE_DOTPROD
173 #endif
174 #ifdef __ARM_FEATURE_MATMUL_INT8
176 #endif
177 #ifdef __ARM_FEATURE_SVE
179 #endif
180 #ifdef __ARM_FEATURE_SVE2
182 #endif
183 #ifdef __ARM_FEATURE_SME
185 #endif
186 
187  flags |= detect_flags();
188 
189  return flags;
190 }
191 
193 {
194  int flags = av_get_cpu_flags();
195 
196  if (flags & AV_CPU_FLAG_NEON)
197  return 16;
198 
199  return 8;
200 }
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:192
ff_getauxval
unsigned long ff_getauxval(unsigned long type)
Definition: cpu.c:305
NULL
#define NULL
Definition: coverity.c:32
ff_get_cpu_flags_aarch64
int ff_get_cpu_flags_aarch64(void)
Definition: cpu.c:166
detect_flags
static int detect_flags(void)
Definition: cpu.c:159
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_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