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  if (ID_AA64ISAR0_CRC32(isar0) >= ID_AA64ISAR0_CRC32_BASE)
108  }
109 
110  mib[0] = CTL_MACHDEP;
111  mib[1] = CPU_ID_AA64ISAR1;
112  len = sizeof(isar1);
113  if (sysctl(mib, 2, &isar1, &len, NULL, 0) != -1) {
114 #ifdef ID_AA64ISAR1_I8MM_IMPL
115  if (ID_AA64ISAR1_I8MM(isar1) >= ID_AA64ISAR1_I8MM_IMPL)
117 #endif
118  }
119 #endif
120 
121  return flags;
122 }
123 
124 #elif defined(_WIN32)
125 #include <windows.h>
126 
127 static int detect_flags(void)
128 {
129  int flags = 0;
130 #ifdef PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE
131  if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
133 #endif
134 #ifdef PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE
135  if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
137 #endif
138 #ifdef PF_ARM_SVE_I8MM_INSTRUCTIONS_AVAILABLE
139  /* There's no PF_* flag that indicates whether plain I8MM is available
140  * or not. But if SVE_I8MM is available, that also implies that
141  * regular I8MM is available. */
142  if (IsProcessorFeaturePresent(PF_ARM_SVE_I8MM_INSTRUCTIONS_AVAILABLE))
144 #endif
145 #ifdef PF_ARM_SVE_INSTRUCTIONS_AVAILABLE
146  if (IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE))
148 #endif
149 #ifdef PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE
150  if (IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE))
152 #endif
153 #ifdef PF_ARM_SME_INSTRUCTIONS_AVAILABLE
154  if (IsProcessorFeaturePresent(PF_ARM_SME_INSTRUCTIONS_AVAILABLE))
156 #endif
157  return flags;
158 }
159 #else
160 
161 static int detect_flags(void)
162 {
163  return 0;
164 }
165 
166 #endif
167 
169 {
170  int flags = AV_CPU_FLAG_ARMV8 * HAVE_ARMV8 |
171  AV_CPU_FLAG_NEON * HAVE_NEON;
172 
173 #ifdef __ARM_FEATURE_DOTPROD
175 #endif
176 #ifdef __ARM_FEATURE_MATMUL_INT8
178 #endif
179 #ifdef __ARM_FEATURE_SVE
181 #endif
182 #ifdef __ARM_FEATURE_SVE2
184 #endif
185 #ifdef __ARM_FEATURE_SME
187 #endif
188 
189  flags |= detect_flags();
190 
191  return flags;
192 }
193 
195 {
196  int flags = av_get_cpu_flags();
197 
198  if (flags & AV_CPU_FLAG_NEON)
199  return 16;
200 
201  return 8;
202 }
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:194
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:168
detect_flags
static int detect_flags(void)
Definition: cpu.c:161
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