FFmpeg
Loading...
Searching...
No Matches
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"
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_AES (1 << 3)
28#define HWCAP_AARCH64_PMULL (1 << 4)
29#define HWCAP_AARCH64_CRC32 (1 << 7)
30#define HWCAP_AARCH64_SHA3 (1 << 17)
31#define HWCAP_AARCH64_ASIMDDP (1 << 20)
32#define HWCAP_AARCH64_SVE (1 << 22)
33#define HWCAP2_AARCH64_SVE2 (1 << 1)
34#define HWCAP2_AARCH64_I8MM (1 << 13)
35#define HWCAP2_AARCH64_SME (1 << 23)
36#define HWCAP2_AARCH64_SME_I16I64 (1 << 24)
37#define HWCAP2_AARCH64_SME2 (1ULL << 37)
38
39static int detect_flags(void)
40{
41 int flags = 0;
42
43 unsigned long hwcap = ff_getauxval(AT_HWCAP);
44 unsigned long hwcap2 = ff_getauxval(AT_HWCAP2);
45
46 if (hwcap & HWCAP_AARCH64_AES)
48 if (hwcap & HWCAP_AARCH64_PMULL)
50 if (hwcap & HWCAP_AARCH64_SHA3)
52 if (hwcap & HWCAP_AARCH64_CRC32)
54 if (hwcap & HWCAP_AARCH64_ASIMDDP)
56 if (hwcap & HWCAP_AARCH64_SVE)
58 if (hwcap2 & HWCAP2_AARCH64_SVE2)
60 if (hwcap2 & HWCAP2_AARCH64_I8MM)
62 if (hwcap2 & HWCAP2_AARCH64_SME)
64 if (hwcap2 & HWCAP2_AARCH64_SME_I16I64)
66 if (hwcap2 & HWCAP2_AARCH64_SME2)
68
69 return flags;
70}
71
72#elif defined(__APPLE__) && HAVE_SYSCTLBYNAME
73#include <sys/sysctl.h>
74
75static int have_feature(const char *feature) {
76 uint32_t value = 0;
77 size_t size = sizeof(value);
78 if (!sysctlbyname(feature, &value, &size, NULL, 0))
79 return value;
80 return 0;
81}
82
83static int detect_flags(void)
84{
85 int flags = 0;
86
87 if (have_feature("hw.optional.arm.FEAT_DotProd"))
89 if (have_feature("hw.optional.arm.FEAT_I8MM"))
91 if (have_feature("hw.optional.arm.FEAT_SME"))
93 if (have_feature("hw.optional.arm.FEAT_SME_I16I64"))
95 if (have_feature("hw.optional.armv8_crc32"))
97 if (have_feature("hw.optional.arm.FEAT_AES"))
99 if (have_feature("hw.optional.arm.FEAT_PMULL"))
101 if (have_feature("hw.optional.armv8_2_sha3"))
103 if (have_feature("hw.optional.arm.FEAT_SME2"))
105
106 return flags;
107}
108
109#elif defined(__OpenBSD__)
110#include <machine/armreg.h>
111#include <machine/cpu.h>
112#include <sys/types.h>
113#include <sys/sysctl.h>
114
115static int detect_flags(void)
116{
117 int flags = 0;
118
119#ifdef CPU_ID_AA64ISAR0
120 int mib[2];
121 uint64_t isar0;
122 uint64_t isar1;
123 size_t len;
124
125 mib[0] = CTL_MACHDEP;
126 mib[1] = CPU_ID_AA64ISAR0;
127 len = sizeof(isar0);
128 if (sysctl(mib, 2, &isar0, &len, NULL, 0) != -1) {
129 if (ID_AA64ISAR0_DP(isar0) >= ID_AA64ISAR0_DP_IMPL)
131 if (ID_AA64ISAR0_CRC32(isar0) >= ID_AA64ISAR0_CRC32_BASE)
133 if (ID_AA64ISAR0_AES(isar0) >= ID_AA64ISAR0_AES_BASE)
135 if (ID_AA64ISAR0_AES(isar0) >= ID_AA64ISAR0_AES_PMULL)
137 if (ID_AA64ISAR0_SHA3(isar0) >= ID_AA64ISAR0_SHA3_IMPL)
139 }
140
141 mib[0] = CTL_MACHDEP;
142 mib[1] = CPU_ID_AA64ISAR1;
143 len = sizeof(isar1);
144 if (sysctl(mib, 2, &isar1, &len, NULL, 0) != -1) {
145#ifdef ID_AA64ISAR1_I8MM_IMPL
146 if (ID_AA64ISAR1_I8MM(isar1) >= ID_AA64ISAR1_I8MM_IMPL)
148#endif
149 }
150#endif
151
152 return flags;
153}
154
155#elif defined(_WIN32)
156#include <windows.h>
157
158static int detect_flags(void)
159{
160 int flags = 0;
161#ifdef PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE
162 if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
164#endif
165#ifdef PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE
166 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE)) {
169 }
170#endif
171#ifdef PF_ARM_SHA3_INSTRUCTIONS_AVAILABLE
172 if (IsProcessorFeaturePresent(PF_ARM_SHA3_INSTRUCTIONS_AVAILABLE))
174#endif
175#ifdef PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE
176 if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
178#endif
179#ifdef PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE
180 if (IsProcessorFeaturePresent(PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE))
182#endif
183#ifdef PF_ARM_SVE_INSTRUCTIONS_AVAILABLE
184 if (IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE))
186#endif
187#ifdef PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE
188 if (IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE))
190#endif
191#ifdef PF_ARM_SME_INSTRUCTIONS_AVAILABLE
192 if (IsProcessorFeaturePresent(PF_ARM_SME_INSTRUCTIONS_AVAILABLE))
194#endif
195#ifdef PF_ARM_SME_I16I64_INSTRUCTIONS_AVAILABLE
196 if (IsProcessorFeaturePresent(PF_ARM_SME_I16I64_INSTRUCTIONS_AVAILABLE))
198#endif
199#ifdef PF_ARM_SME2_INSTRUCTIONS_AVAILABLE
200 if (IsProcessorFeaturePresent(PF_ARM_SME2_INSTRUCTIONS_AVAILABLE))
202#endif
203 return flags;
204}
205#else
206
207static int detect_flags(void)
208{
209 return 0;
210}
211
212#endif
213
215{
218
219#ifdef __ARM_FEATURE_DOTPROD
221#endif
222#ifdef __ARM_FEATURE_MATMUL_INT8
224#endif
225#ifdef __ARM_FEATURE_SVE
227#endif
228#ifdef __ARM_FEATURE_SVE2
230#endif
231#ifdef __ARM_FEATURE_SME
233#endif
234#ifdef __ARM_FEATURE_CRC32
236#endif
237#ifdef __ARM_FEATURE_AES
240#endif
241#ifdef __ARM_FEATURE_SHA3
243#endif
244#ifdef __ARM_FEATURE_SME_I16I64
246#endif
247#ifdef __ARM_FEATURE_SME2
249#endif
250
251 flags |= detect_flags();
252
253 return flags;
254}
255
257{
258 int flags = av_get_cpu_flags();
259
261 return 16;
262
263 return 8;
264}
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define HAVE_ARMV8
Definition config.h:41
#define HAVE_NEON
Definition config.h:48
#define NULL
Definition coverity.c:32
double value
Definition eval.c:102
int ff_get_cpu_flags_aarch64(void)
Definition cpu.c:214
size_t ff_get_cpu_max_align_aarch64(void)
Definition cpu.c:256
static int detect_flags(void)
Definition cpu.c:207
unsigned long ff_getauxval(unsigned long type)
Definition cpu.c:310
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition cpu.c:109
#define AV_CPU_FLAG_DOTPROD
Definition cpu.h:76
#define AV_CPU_FLAG_SVE
Definition cpu.h:78
#define AV_CPU_FLAG_ARM_AES
Definition cpu.h:87
#define AV_CPU_FLAG_NEON
Definition cpu.h:73
#define AV_CPU_FLAG_I8MM
Definition cpu.h:77
#define AV_CPU_FLAG_SVE2
Definition cpu.h:79
#define AV_CPU_FLAG_SME_I16I64
Definition cpu.h:83
#define AV_CPU_FLAG_EOR3
Definition cpu.h:86
#define AV_CPU_FLAG_SME2
Definition cpu.h:82
#define AV_CPU_FLAG_ARM_CRC
Definition cpu.h:81
#define AV_CPU_FLAG_ARMV8
Definition cpu.h:74
#define AV_CPU_FLAG_PMULL
Definition cpu.h:85
#define AV_CPU_FLAG_SME
Definition cpu.h:80
#define AT_HWCAP2
Definition cpu.c:53
#define AT_HWCAP
Definition cpu.c:50
int size
int len