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_ASIMDDP (1 << 20)
28 #define HWCAP_AARCH64_SVE (1 << 22)
29 #define HWCAP2_AARCH64_SVE2 (1 << 1)
30 #define HWCAP2_AARCH64_I8MM (1 << 13)
31 #define HWCAP2_AARCH64_SME (1 << 23)
32 
33 static int detect_flags(void)
34 {
35  int flags = 0;
36 
37  unsigned long hwcap = ff_getauxval(AT_HWCAP);
38  unsigned long hwcap2 = ff_getauxval(AT_HWCAP2);
39 
40  if (hwcap & HWCAP_AARCH64_ASIMDDP)
42  if (hwcap & HWCAP_AARCH64_SVE)
44  if (hwcap2 & HWCAP2_AARCH64_SVE2)
46  if (hwcap2 & HWCAP2_AARCH64_I8MM)
48  if (hwcap2 & HWCAP2_AARCH64_SME)
50 
51  return flags;
52 }
53 
54 #elif defined(__APPLE__) && HAVE_SYSCTLBYNAME
55 #include <sys/sysctl.h>
56 
57 static int have_feature(const char *feature) {
58  uint32_t value = 0;
59  size_t size = sizeof(value);
60  if (!sysctlbyname(feature, &value, &size, NULL, 0))
61  return value;
62  return 0;
63 }
64 
65 static int detect_flags(void)
66 {
67  int flags = 0;
68 
69  if (have_feature("hw.optional.arm.FEAT_DotProd"))
71  if (have_feature("hw.optional.arm.FEAT_I8MM"))
73  if (have_feature("hw.optional.arm.FEAT_SME"))
75 
76  return flags;
77 }
78 
79 #elif defined(__OpenBSD__)
80 #include <machine/armreg.h>
81 #include <machine/cpu.h>
82 #include <sys/types.h>
83 #include <sys/sysctl.h>
84 
85 static int detect_flags(void)
86 {
87  int flags = 0;
88 
89 #ifdef CPU_ID_AA64ISAR0
90  int mib[2];
91  uint64_t isar0;
92  uint64_t isar1;
93  size_t len;
94 
95  mib[0] = CTL_MACHDEP;
96  mib[1] = CPU_ID_AA64ISAR0;
97  len = sizeof(isar0);
98  if (sysctl(mib, 2, &isar0, &len, NULL, 0) != -1) {
99  if (ID_AA64ISAR0_DP(isar0) >= ID_AA64ISAR0_DP_IMPL)
101  }
102 
103  mib[0] = CTL_MACHDEP;
104  mib[1] = CPU_ID_AA64ISAR1;
105  len = sizeof(isar1);
106  if (sysctl(mib, 2, &isar1, &len, NULL, 0) != -1) {
107 #ifdef ID_AA64ISAR1_I8MM_IMPL
108  if (ID_AA64ISAR1_I8MM(isar1) >= ID_AA64ISAR1_I8MM_IMPL)
110 #endif
111  }
112 #endif
113 
114  return flags;
115 }
116 
117 #elif defined(_WIN32)
118 #include <windows.h>
119 
120 static int detect_flags(void)
121 {
122  int flags = 0;
123 #ifdef PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE
124  if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
126 #endif
127 #ifdef PF_ARM_SVE_I8MM_INSTRUCTIONS_AVAILABLE
128  /* There's no PF_* flag that indicates whether plain I8MM is available
129  * or not. But if SVE_I8MM is available, that also implies that
130  * regular I8MM is available. */
131  if (IsProcessorFeaturePresent(PF_ARM_SVE_I8MM_INSTRUCTIONS_AVAILABLE))
133 #endif
134 #ifdef PF_ARM_SVE_INSTRUCTIONS_AVAILABLE
135  if (IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE))
137 #endif
138 #ifdef PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE
139  if (IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE))
141 #endif
142 #ifdef PF_ARM_SME_INSTRUCTIONS_AVAILABLE
143  if (IsProcessorFeaturePresent(PF_ARM_SME_INSTRUCTIONS_AVAILABLE))
145 #endif
146  return flags;
147 }
148 #else
149 
150 static int detect_flags(void)
151 {
152  return 0;
153 }
154 
155 #endif
156 
158 {
159  int flags = AV_CPU_FLAG_ARMV8 * HAVE_ARMV8 |
160  AV_CPU_FLAG_NEON * HAVE_NEON;
161 
162 #ifdef __ARM_FEATURE_DOTPROD
164 #endif
165 #ifdef __ARM_FEATURE_MATMUL_INT8
167 #endif
168 #ifdef __ARM_FEATURE_SVE
170 #endif
171 #ifdef __ARM_FEATURE_SVE2
173 #endif
174 #ifdef __ARM_FEATURE_SME
176 #endif
177 
178  flags |= detect_flags();
179 
180  return flags;
181 }
182 
184 {
185  int flags = av_get_cpu_flags();
186 
187  if (flags & AV_CPU_FLAG_NEON)
188  return 16;
189 
190  return 8;
191 }
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:78
AV_CPU_FLAG_SVE
#define AV_CPU_FLAG_SVE
Definition: cpu.h:77
AV_CPU_FLAG_DOTPROD
#define AV_CPU_FLAG_DOTPROD
Definition: cpu.h:75
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:183
ff_getauxval
unsigned long ff_getauxval(unsigned long type)
Definition: cpu.c:303
NULL
#define NULL
Definition: coverity.c:32
ff_get_cpu_flags_aarch64
int ff_get_cpu_flags_aarch64(void)
Definition: cpu.c:157
detect_flags
static int detect_flags(void)
Definition: cpu.c:150
AV_CPU_FLAG_I8MM
#define AV_CPU_FLAG_I8MM
Definition: cpu.h:76
cpu.h
AV_CPU_FLAG_NEON
#define AV_CPU_FLAG_NEON
Definition: cpu.h:72
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:73
len
int len
Definition: vorbis_enc_data.h:426
AV_CPU_FLAG_SME
#define AV_CPU_FLAG_SME
Definition: cpu.h:79
AT_HWCAP2
#define AT_HWCAP2
Definition: cpu.c:53