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 "cpu.h"
20 #include "config.h"
21 #include "opt.h"
22 
23 static int flags, checked;
24 
26  flags = arg;
27  checked = arg != -1;
28 }
29 
31 {
32  if (checked)
33  return flags;
34 
38 
39  checked = 1;
40  return flags;
41 }
42 
44 {
45  checked = 0;
47  checked = 1;
48 }
49 
50 int av_parse_cpu_flags(const char *s)
51 {
52 #define CPUFLAG_MMXEXT (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT | AV_CPU_FLAG_CMOV)
53 #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
54 #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
55 #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMXEXT)
56 #define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
57 #define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
58 #define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
59 #define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
60 #define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
61 #define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
62 #define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
63 #define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
64 #define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
65 #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
66  static const AVOption cpuflags_opts[] = {
67  { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
68 #if ARCH_PPC
69  { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
70 #elif ARCH_X86
71  { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
72  { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_MMXEXT }, .unit = "flags" },
73  { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE }, .unit = "flags" },
74  { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2 }, .unit = "flags" },
75  { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2SLOW }, .unit = "flags" },
76  { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3 }, .unit = "flags" },
77  { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3SLOW }, .unit = "flags" },
78  { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSSE3 }, .unit = "flags" },
79  { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
80  { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE4 }, .unit = "flags" },
81  { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE42 }, .unit = "flags" },
82  { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX }, .unit = "flags" },
83  { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_XOP }, .unit = "flags" },
84  { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA4 }, .unit = "flags" },
85  { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOW }, .unit = "flags" },
86  { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOWEXT }, .unit = "flags" },
87  { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
88 #elif ARCH_ARM
89  { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
90  { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
91  { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
92  { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
93  { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
94  { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
95 #endif
96  { NULL },
97  };
98  static const AVClass class = {
99  .class_name = "cpuflags",
100  .item_name = av_default_item_name,
101  .option = cpuflags_opts,
102  .version = LIBAVUTIL_VERSION_INT,
103  };
104 
105  int flags = 0, ret;
106  const AVClass *pclass = &class;
107 
108  if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
109  return ret;
110 
111  return flags & INT_MAX;
112 }
113 
114 int av_parse_cpu_caps(unsigned *flags, const char *s)
115 {
116  static const AVOption cpuflags_opts[] = {
117  { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
118 #if ARCH_PPC
119  { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
120 #elif ARCH_X86
121  { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
122  { "mmx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX2 }, .unit = "flags" },
123  { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX2 }, .unit = "flags" },
124  { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE }, .unit = "flags" },
125  { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE2 }, .unit = "flags" },
126  { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE2SLOW }, .unit = "flags" },
127  { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE3 }, .unit = "flags" },
128  { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE3SLOW }, .unit = "flags" },
129  { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSSE3 }, .unit = "flags" },
130  { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
131  { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE4 }, .unit = "flags" },
132  { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE42 }, .unit = "flags" },
133  { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX }, .unit = "flags" },
134  { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_XOP }, .unit = "flags" },
135  { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_FMA4 }, .unit = "flags" },
136  { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_3DNOW }, .unit = "flags" },
137  { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_3DNOWEXT }, .unit = "flags" },
138  { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
139 
140 #define CPU_FLAG_P2 AV_CPU_FLAG_CMOV | AV_CPU_FLAG_MMX
141 #define CPU_FLAG_P3 CPU_FLAG_P2 | AV_CPU_FLAG_MMX2 | AV_CPU_FLAG_SSE
142 #define CPU_FLAG_P4 CPU_FLAG_P3| AV_CPU_FLAG_SSE2
143  { "pentium2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P2 }, .unit = "flags" },
144  { "pentium3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P3 }, .unit = "flags" },
145  { "pentium4", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P4 }, .unit = "flags" },
146 
147 #define CPU_FLAG_K62 AV_CPU_FLAG_MMX | AV_CPU_FLAG_3DNOW
148 #define CPU_FLAG_ATHLON CPU_FLAG_K62 | AV_CPU_FLAG_CMOV | AV_CPU_FLAG_3DNOWEXT | AV_CPU_FLAG_MMX2
149 #define CPU_FLAG_ATHLONXP CPU_FLAG_ATHLON | AV_CPU_FLAG_SSE
150 #define CPU_FLAG_K8 CPU_FLAG_ATHLONXP | AV_CPU_FLAG_SSE2
151  { "k6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
152  { "k62", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_K62 }, .unit = "flags" },
153  { "athlon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLON }, .unit = "flags" },
154  { "athlonxp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLONXP }, .unit = "flags" },
155  { "k8", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_K8 }, .unit = "flags" },
156 #elif ARCH_ARM
157  { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
158  { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
159  { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
160  { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
161  { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
162  { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
163 #endif
164  { NULL },
165  };
166  static const AVClass class = {
167  .class_name = "cpuflags",
168  .item_name = av_default_item_name,
169  .option = cpuflags_opts,
170  .version = LIBAVUTIL_VERSION_INT,
171  };
172  const AVClass *pclass = &class;
173 
174  return av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, flags);
175 }
176 #ifdef TEST
177 
178 #include <stdio.h>
179 
180 static const struct {
181  int flag;
182  const char *name;
183 } cpu_flag_tab[] = {
184 #if ARCH_ARM
185  { AV_CPU_FLAG_ARMV5TE, "armv5te" },
186  { AV_CPU_FLAG_ARMV6, "armv6" },
187  { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
188  { AV_CPU_FLAG_VFP, "vfp" },
189  { AV_CPU_FLAG_VFPV3, "vfpv3" },
190  { AV_CPU_FLAG_NEON, "neon" },
191 #elif ARCH_PPC
192  { AV_CPU_FLAG_ALTIVEC, "altivec" },
193 #elif ARCH_X86
194  { AV_CPU_FLAG_MMX, "mmx" },
195  { AV_CPU_FLAG_MMXEXT, "mmxext" },
196  { AV_CPU_FLAG_SSE, "sse" },
197  { AV_CPU_FLAG_SSE2, "sse2" },
198  { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
199  { AV_CPU_FLAG_SSE3, "sse3" },
200  { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
201  { AV_CPU_FLAG_SSSE3, "ssse3" },
202  { AV_CPU_FLAG_ATOM, "atom" },
203  { AV_CPU_FLAG_SSE4, "sse4.1" },
204  { AV_CPU_FLAG_SSE42, "sse4.2" },
205  { AV_CPU_FLAG_AVX, "avx" },
206  { AV_CPU_FLAG_XOP, "xop" },
207  { AV_CPU_FLAG_FMA4, "fma4" },
208  { AV_CPU_FLAG_3DNOW, "3dnow" },
209  { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
210  { AV_CPU_FLAG_CMOV, "cmov" },
211 #endif
212  { 0 }
213 };
214 
215 int main(void)
216 {
217  int cpu_flags = av_get_cpu_flags();
218  int i;
219 
220  printf("cpu_flags = 0x%08X\n", cpu_flags);
221  printf("cpu_flags =");
222  for (i = 0; cpu_flag_tab[i].flag; i++)
223  if (cpu_flags & cpu_flag_tab[i].flag)
224  printf(" %s", cpu_flag_tab[i].name);
225  printf("\n");
226 
227  return 0;
228 }
229 
230 #endif
const char * name
Definition: avisynth_c.h:675
#define CPUFLAG_3DNOW
#define AV_CPU_FLAG_AVX
AVX functions: requires OS support even if YMM registers aren&#39;t used.
Definition: cpu.h:43
#define AV_CPU_FLAG_ALTIVEC
standard
Definition: cpu.h:52
const char * s
Definition: avisynth_c.h:668
AVOption.
Definition: opt.h:251
#define ARCH_PPC
Definition: config.h:26
av_default_item_name
#define AV_CPU_FLAG_SSE
SSE functions.
Definition: cpu.h:33
#define CPUFLAG_SSE3
void av_set_cpu_flags_mask(int mask)
Set a mask on flags returned by av_get_cpu_flags().
Definition: cpu.c:43
#define AV_CPU_FLAG_MMX2
SSE integer functions or AMD MMX ext.
Definition: cpu.h:31
#define CPUFLAG_SSE4
#define AV_CPU_FLAG_CMOV
supports cmov instruction
Definition: cpu.h:47
#define AV_CPU_FLAG_VFP
Definition: cpu.h:57
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
static int checked
Definition: cpu.c:23
AVOptions.
#define CPUFLAG_SSE3SLOW
#define AV_CPU_FLAG_NEON
Definition: cpu.h:59
#define AV_CPU_FLAG_MMXEXT
SSE integer functions or AMD MMX ext.
Definition: cpu.h:30
int av_parse_cpu_flags(const char *s)
Parse CPU flags from a string.
Definition: cpu.c:50
#define AV_CPU_FLAG_ATOM
Atom processor, some SSSE3 instructions are slower.
Definition: cpu.h:40
#define AV_CPU_FLAG_SSE2SLOW
SSE2 supported, but usually not faster.
Definition: cpu.h:35
#define AV_CPU_FLAG_XOP
Bulldozer XOP functions.
Definition: cpu.h:44
#define AV_CPU_FLAG_SSE42
Nehalem SSE4.2 functions.
Definition: cpu.h:42
#define AV_CPU_FLAG_SSSE3
Conroe SSSE3 functions.
Definition: cpu.h:39
static const uint16_t mask[17]
Definition: lzw.c:37
#define ARCH_X86
Definition: config.h:35
int av_opt_eval_flags(void *obj, const AVOption *o, const char *val, int *flags_out)
const char * arg
#define AV_CPU_FLAG_ARMV6T2
Definition: cpu.h:56
#define CPUFLAG_SSE
int av_parse_cpu_caps(unsigned *flags, const char *s)
Parse CPU caps from a string and update the given AV_CPU_* flags based on that.
Definition: cpu.c:114
#define AV_CPU_FLAG_ARMV5TE
Definition: cpu.h:54
#define CPUFLAG_SSE2
#define ARCH_ARM
Definition: config.h:16
#define AV_CPU_FLAG_SSE3
Prescott SSE3 functions.
Definition: cpu.h:37
ret
Definition: avfilter.c:821
#define AV_CPU_FLAG_VFPV3
Definition: cpu.h:58
static int cpu_flags
Definition: dct-test.c:77
int ff_get_cpu_flags_x86(void)
Definition: x86/cpu.c:89
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define AV_CPU_FLAG_3DNOW
AMD 3DNOW.
Definition: cpu.h:32
NULL
Definition: eval.c:55
#define AV_CPU_FLAG_ARMV6
Definition: cpu.h:55
#define AV_CPU_FLAG_SSE3SLOW
SSE3 supported, but usually not faster.
Definition: cpu.h:38
#define CPUFLAG_FMA4
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:29
Describe the class of an AVClass context structure.
Definition: log.h:50
synthesis window for stochastic i
#define AV_CPU_FLAG_FMA4
Bulldozer FMA4 functions.
Definition: cpu.h:45
#define AV_CPU_FLAG_SSE4
Penryn SSE4.1 functions.
Definition: cpu.h:41
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:30
#define CPUFLAG_SSE2SLOW
#define CPUFLAG_XOP
static int flags
Definition: cpu.c:23
#define AV_CPU_FLAG_3DNOWEXT
AMD 3DNowExt.
Definition: cpu.h:36
printf("static const uint8_t my_array[100] = {\n")
int ff_get_cpu_flags_ppc(void)
This function MAY rely on signal() or fork() in order to make sure AltiVec is present.
Definition: ppc/cpu.c:38
int ff_get_cpu_flags_arm(void)
Definition: arm/cpu.c:136
#define AV_CPU_FLAG_SSE2
PIV SSE2 functions.
Definition: cpu.h:34
#define CPUFLAG_3DNOWEXT
void av_force_cpu_flags(int arg)
Disables cpu detection and forces the specified flags.
Definition: cpu.c:25
int main(int argc, char **argv)
Definition: main.c:22
#define CPUFLAG_MMXEXT
#define CPUFLAG_AVX
#define CPUFLAG_SSE42
#define CPUFLAG_SSSE3