FFmpeg
Loading...
Searching...
No Matches
cpu.c
Go to the documentation of this file.
1/*
2 * CPU detection code, extracted from mmx.h
3 * (c)1997-99 by H. Dietz and R. Fisher
4 * Converted to C and improved by Fabrice Bellard.
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <stdlib.h>
24#include <string.h>
25
26#include "libavutil/x86/asm.h"
27#include "libavutil/x86/cpu.h"
28#include "libavutil/cpu.h"
30
31#if HAVE_X86ASM
32
33#define cpuid_count(index, subleaf, eax, ebx, ecx, edx) \
34 ff_cpu_cpuid(index, subleaf, &eax, &ebx, &ecx, &edx)
35
36#define xgetbv(index, eax, edx) \
37 ff_cpu_xgetbv(index, &eax, &edx)
38
39#elif HAVE_INLINE_ASM
40
41/* ebx saving is necessary for PIC. gcc seems unable to see it alone */
42#define cpuid_count(index, subleaf, eax, ebx, ecx, edx) \
43 __asm__ volatile ( \
44 "mov %%"FF_REG_b", %%"FF_REG_S" \n\t" \
45 "cpuid \n\t" \
46 "xchg %%"FF_REG_b", %%"FF_REG_S \
47 : "=a" (eax), "=S" (ebx), "=c" (ecx), "=d" (edx) \
48 : "0" (index), "2" (subleaf))
49
50#define xgetbv(index, eax, edx) \
51 __asm__ (".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c" (index))
52
53#define get_eflags(x) \
54 __asm__ volatile ("pushfl \n" \
55 "pop %0 \n" \
56 : "=r"(x))
57
58#define set_eflags(x) \
59 __asm__ volatile ("push %0 \n" \
60 "popfl \n" \
61 :: "r"(x))
62
63#endif /* HAVE_INLINE_ASM */
64
65#ifdef cpuid_count
66#define cpuid(index, eax, ebx, ecx, edx) \
67 cpuid_count(index, 0, eax, ebx, ecx, edx)
68#endif
69
70#if ARCH_X86_64
71
72#define cpuid_test() 1
73
74#elif HAVE_X86ASM
75
76#define cpuid_test ff_cpu_cpuid_test
77
78#elif HAVE_INLINE_ASM
79
80static int cpuid_test(void)
81{
82 x86_reg a, c;
83
84 /* Check if CPUID is supported by attempting to toggle the ID bit in
85 * the EFLAGS register. */
86 get_eflags(a);
87 set_eflags(a ^ 0x200000);
88 get_eflags(c);
89
90 return a != c;
91}
92#endif
93
94/* Function to test if multimedia instructions are supported... */
96{
97 int rval = 0;
98
99#ifdef cpuid
100
101 int eax, ebx, ecx, edx;
102 int max_std_level, max_ext_level, std_caps = 0, ext_caps = 0;
103 int family = 0, model = 0;
104 union { int i[3]; char c[12]; } vendor;
105 int xcr0_lo = 0, xcr0_hi = 0;
106
107 if (!cpuid_test())
108 return 0; /* CPUID not supported */
109
110 cpuid(0, max_std_level, vendor.i[0], vendor.i[2], vendor.i[1]);
111
112 if (max_std_level >= 1) {
113 cpuid(1, eax, ebx, ecx, std_caps);
114 family = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);
115 model = ((eax >> 4) & 0xf) + ((eax >> 12) & 0xf0);
116 if (std_caps & (1 << 15))
117 rval |= AV_CPU_FLAG_CMOV;
118 if (std_caps & (1 << 23))
119 rval |= AV_CPU_FLAG_MMX;
120 if (std_caps & (1 << 25))
121 rval |= AV_CPU_FLAG_MMXEXT;
122#if HAVE_SSE
123 if (std_caps & (1 << 25))
124 rval |= AV_CPU_FLAG_SSE;
125 if (std_caps & (1 << 26))
126 rval |= AV_CPU_FLAG_SSE2;
127 if (ecx & 1)
128 rval |= AV_CPU_FLAG_SSE3;
129 if (ecx & 0x2)
130 rval |= AV_CPU_FLAG_CLMUL;
131 if (ecx & 0x00000200 )
132 rval |= AV_CPU_FLAG_SSSE3;
133 if (ecx & 0x00080000 )
134 rval |= AV_CPU_FLAG_SSE4;
135 if (ecx & 0x00100000 )
136 rval |= AV_CPU_FLAG_SSE42;
137 if (ecx & 0x02000000 )
138 rval |= AV_CPU_FLAG_AESNI;
139#if HAVE_AVX
140 /* Check OXSAVE and AVX bits */
141 if ((ecx & 0x18000000) == 0x18000000) {
142 /* Check for OS support */
143 xgetbv(0, xcr0_lo, xcr0_hi);
144 if ((xcr0_lo & 0x6) == 0x6) {
145 rval |= AV_CPU_FLAG_AVX;
146 if (ecx & 0x00001000)
147 rval |= AV_CPU_FLAG_FMA3;
148 }
149 }
150#endif /* HAVE_AVX */
151#endif /* HAVE_SSE */
152 }
153 if (max_std_level >= 7) {
154 cpuid(7, eax, ebx, ecx, edx);
155#if HAVE_AVX2
156 if ((rval & AV_CPU_FLAG_AVX) && (ebx & 0x00000020))
157 rval |= AV_CPU_FLAG_AVX2;
158#if HAVE_AVX512 /* F, CD, BW, DQ, VL */
159 if ((xcr0_lo & 0xe0) == 0xe0) { /* OPMASK/ZMM state */
160 if ((rval & AV_CPU_FLAG_AVX2) && (ebx & 0xd0030000) == 0xd0030000) {
161 rval |= AV_CPU_FLAG_AVX512;
162#if HAVE_AVX512ICL
163 if ((ebx & 0xd0200000) == 0xd0200000 && (ecx & 0x5f42) == 0x5f42)
164 rval |= AV_CPU_FLAG_AVX512ICL;
165#endif /* HAVE_AVX512ICL */
166 }
167 }
168#endif /* HAVE_AVX512 */
169#endif /* HAVE_AVX2 */
170 /* BMI1/2 don't need OS support */
171 if (ebx & 0x00000008) {
172 rval |= AV_CPU_FLAG_BMI1;
173 if (ebx & 0x00000100)
174 rval |= AV_CPU_FLAG_BMI2;
175 }
176#if HAVE_AVXVNNI
177 /* AVX-VNNI is VEX coded, so it needs the OS YMM state, like AVX2.
178 * eax of subleaf 0 is the highest subleaf. */
179 if ((rval & AV_CPU_FLAG_AVX) && eax >= 1) {
180 cpuid_count(7, 1, eax, ebx, ecx, edx);
181 if (eax & 0x00000010)
182 rval |= AV_CPU_FLAG_AVXVNNI;
183 }
184#endif /* HAVE_AVXVNNI */
185 }
186
187 cpuid(0x80000000, max_ext_level, ebx, ecx, edx);
188
189 if (max_ext_level >= 0x80000001) {
190 cpuid(0x80000001, eax, ebx, ecx, ext_caps);
191 if (ext_caps & (1U << 31))
192 rval |= AV_CPU_FLAG_3DNOW;
193 if (ext_caps & (1 << 30))
194 rval |= AV_CPU_FLAG_3DNOWEXT;
195 if (ext_caps & (1 << 23))
196 rval |= AV_CPU_FLAG_MMX;
197 if (ext_caps & (1 << 22))
198 rval |= AV_CPU_FLAG_MMXEXT;
199
200 if (!strncmp(vendor.c, "AuthenticAMD", 12)) {
201 /* Allow for selectively disabling SSE2 functions on AMD processors
202 with SSE2 support but not SSE4a. This includes Athlon64, some
203 Opteron, and some Sempron processors. MMX, SSE, or 3DNow! are faster
204 than SSE2 often enough to utilize this special-case flag.
205 AV_CPU_FLAG_SSE2 and AV_CPU_FLAG_SSE2SLOW are both set in this case
206 so that SSE2 is used unless explicitly disabled by checking
207 AV_CPU_FLAG_SSE2SLOW. */
208 if (rval & AV_CPU_FLAG_SSE2 && !(ecx & 0x00000040))
209 rval |= AV_CPU_FLAG_SSE2SLOW;
210
211 /* Similar to the above but for AVX functions on AMD processors.
212 This is necessary only for functions using YMM registers on Bulldozer
213 and Jaguar based CPUs as they lack 256-bit execution units. SSE/AVX
214 functions using XMM registers are always faster on them.
215 AV_CPU_FLAG_AVX and AV_CPU_FLAG_AVXSLOW are both set so that AVX is
216 used unless explicitly disabled by checking AV_CPU_FLAG_AVXSLOW. */
217 if ((family == 0x15 || family == 0x16) && (rval & AV_CPU_FLAG_AVX))
218 rval |= AV_CPU_FLAG_AVXSLOW;
219
220 /* Zen 3 and earlier have slow gather */
221 if ((family <= 0x19) && (rval & AV_CPU_FLAG_AVX2))
223 }
224
225 /* XOP and FMA4 use the AVX instruction coding scheme, so they can't be
226 * used unless the OS has AVX support. */
227 if (rval & AV_CPU_FLAG_AVX) {
228 if (ecx & 0x00000800)
229 rval |= AV_CPU_FLAG_XOP;
230 if (ecx & 0x00010000)
231 rval |= AV_CPU_FLAG_FMA4;
232 }
233 }
234
235 if (!strncmp(vendor.c, "GenuineIntel", 12)) {
236 if (family == 6 && (model == 9 || model == 13 || model == 14)) {
237 /* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and
238 * 6/14 (core1 "yonah") theoretically support sse2, but it's
239 * usually slower than mmx, so let's just pretend they don't.
240 * AV_CPU_FLAG_SSE2 is disabled and AV_CPU_FLAG_SSE2SLOW is
241 * enabled so that SSE2 is not used unless explicitly enabled
242 * by checking AV_CPU_FLAG_SSE2SLOW. The same situation
243 * applies for AV_CPU_FLAG_SSE3 and AV_CPU_FLAG_SSE3SLOW. */
244 if (rval & AV_CPU_FLAG_SSE2)
246 if (rval & AV_CPU_FLAG_SSE3)
248 }
249 /* The Atom processor has SSSE3 support, which is useful in many cases,
250 * but sometimes the SSSE3 version is slower than the SSE2 equivalent
251 * on the Atom, but is generally faster on other processors supporting
252 * SSSE3. This flag allows for selectively disabling certain SSSE3
253 * functions on the Atom. */
254 if (family == 6 && model == 28)
255 rval |= AV_CPU_FLAG_ATOM;
256
257 /* Conroe has a slow shuffle unit. Check the model number to ensure not
258 * to include crippled low-end Penryns and Nehalems that lack SSE4. */
259 if ((rval & AV_CPU_FLAG_SSSE3) && !(rval & AV_CPU_FLAG_SSE4) &&
260 family == 6 && model < 23)
261 rval |= AV_CPU_FLAG_SSSE3SLOW;
262
263 /* Ice Lake and below have slow gather due to Gather Data Sampling
264 * mitigation. */
265 if ((rval & AV_CPU_FLAG_AVX2) && family == 6 && model < 143)
267 }
268
269#endif /* cpuid */
270
271 return rval;
272}
273
275{
276 int flags = av_get_cpu_flags();
277
279 return 64;
280 if (flags & (AV_CPU_FLAG_AVX2 |
286 return 32;
287 if (flags & (AV_CPU_FLAG_AESNI |
298 return 16;
299
300 return 8;
301}
int x86_reg
Definition asm.h:71
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
int ff_get_cpu_flags_x86(void)
Definition cpu.c:95
size_t ff_get_cpu_max_align_x86(void)
Definition cpu.c:274
int a
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition cpu.c:109
#define AV_CPU_FLAG_FMA3
Haswell FMA3 functions.
Definition cpu.h:57
#define AV_CPU_FLAG_SSE2
PIV SSE2 functions.
Definition cpu.h:37
#define AV_CPU_FLAG_BMI1
Bit Manipulation Instruction Set 1.
Definition cpu.h:58
#define AV_CPU_FLAG_AVXVNNI
AVX-VNNI: VEX-coded VNNI, requires OS support for YMM registers.
Definition cpu.h:62
#define AV_CPU_FLAG_SSE3SLOW
SSE3 supported, but usually not faster than regular MMX/SSE (e.g.
Definition cpu.h:42
#define AV_CPU_FLAG_AVX512
AVX-512 functions: requires OS support even if YMM/ZMM registers aren't used.
Definition cpu.h:60
#define AV_CPU_FLAG_SSE42
Nehalem SSE4.2 functions.
Definition cpu.h:48
#define AV_CPU_FLAG_CMOV
supports cmov instruction
Definition cpu.h:55
#define AV_CPU_FLAG_SSE
SSE functions.
Definition cpu.h:36
#define AV_CPU_FLAG_SSE4
Penryn SSE4.1 functions.
Definition cpu.h:47
#define AV_CPU_FLAG_FMA4
Bulldozer FMA4 functions.
Definition cpu.h:54
#define AV_CPU_FLAG_BMI2
Bit Manipulation Instruction Set 2.
Definition cpu.h:59
#define AV_CPU_FLAG_SSE2SLOW
SSE2 supported, but usually not faster than regular MMX/SSE (e.g.
Definition cpu.h:38
#define AV_CPU_FLAG_MMXEXT
SSE integer functions or AMD MMX ext.
Definition cpu.h:33
#define AV_CPU_FLAG_SSE3
Prescott SSE3 functions.
Definition cpu.h:41
#define AV_CPU_FLAG_AESNI
Advanced Encryption Standard functions.
Definition cpu.h:49
#define AV_CPU_FLAG_3DNOWEXT
AMD 3DNowExt.
Definition cpu.h:40
#define AV_CPU_FLAG_XOP
Bulldozer XOP functions.
Definition cpu.h:53
#define AV_CPU_FLAG_ATOM
Atom processor, some SSSE3 instructions are slower.
Definition cpu.h:46
#define AV_CPU_FLAG_AVX2
AVX2 functions: requires OS support even if YMM registers aren't used.
Definition cpu.h:56
#define AV_CPU_FLAG_SSSE3
Conroe SSSE3 functions.
Definition cpu.h:44
#define AV_CPU_FLAG_AVX512ICL
F/CD/BW/DQ/VL/VNNI/IFMA/VBMI/VBMI2/VPOPCNTDQ/BITALG/GFNI/VAES/VPCLMULQDQ.
Definition cpu.h:61
#define AV_CPU_FLAG_CLMUL
Carry-less Multiplication instruction.
Definition cpu.h:50
#define AV_CPU_FLAG_SSSE3SLOW
SSSE3 supported, but usually not faster.
Definition cpu.h:45
#define AV_CPU_FLAG_SLOW_GATHER
CPU has slow gathers.
Definition cpu.h:63
#define AV_CPU_FLAG_AVXSLOW
AVX supported, but slow when using YMM registers (e.g. Bulldozer)
Definition cpu.h:52
#define AV_CPU_FLAG_MMX
standard MMX
Definition cpu.h:32
#define AV_CPU_FLAG_3DNOW
AMD 3DNOW.
Definition cpu.h:35
#define AV_CPU_FLAG_AVX
AVX functions: requires OS support even if YMM registers aren't used.
Definition cpu.h:51
static double c[64]