cpu.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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 cpuflags_mask = -1, checked;
24 
26 {
27  static int flags;
28 
29  if (checked)
30  return flags;
31 
32  if (ARCH_ARM) flags = ff_get_cpu_flags_arm();
33  if (ARCH_PPC) flags = ff_get_cpu_flags_ppc();
34  if (ARCH_X86) flags = ff_get_cpu_flags_x86();
35 
36  flags &= cpuflags_mask;
37  checked = 1;
38 
39  return flags;
40 }
41 
43 {
45  checked = 0;
46 }
47 
48 int av_parse_cpu_flags(const char *s)
49 {
50 #define CPUFLAG_MMXEXT (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT | AV_CPU_FLAG_CMOV)
51 #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
52 #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
53 #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMXEXT)
54 #define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
55 #define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
56 #define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
57 #define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
58 #define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
59 #define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
60 #define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
61 #define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
62 #define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
63 #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
64  static const AVOption cpuflags_opts[] = {
65  { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
66 #if ARCH_PPC
67  { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
68 #elif ARCH_X86
69  { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
70  { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_MMXEXT }, .unit = "flags" },
71  { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE }, .unit = "flags" },
72  { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2 }, .unit = "flags" },
73  { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2SLOW }, .unit = "flags" },
74  { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3 }, .unit = "flags" },
75  { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3SLOW }, .unit = "flags" },
76  { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSSE3 }, .unit = "flags" },
77  { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
78  { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE4 }, .unit = "flags" },
79  { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE42 }, .unit = "flags" },
80  { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX }, .unit = "flags" },
81  { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_XOP }, .unit = "flags" },
82  { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA4 }, .unit = "flags" },
83  { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOW }, .unit = "flags" },
84  { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOWEXT }, .unit = "flags" },
85  { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
86 #elif ARCH_ARM
87  { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
88  { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
89  { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
90  { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
91  { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
92  { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
93 #endif
94  { NULL },
95  };
96  static const AVClass class = {
97  .class_name = "cpuflags",
98  .item_name = av_default_item_name,
99  .option = cpuflags_opts,
100  .version = LIBAVUTIL_VERSION_INT,
101  };
102 
103  int flags = 0, ret;
104  const AVClass *pclass = &class;
105 
106  if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
107  return ret;
108 
109  return flags & INT_MAX;
110 }
111 
112 #ifdef TEST
113 
114 #include <stdio.h>
115 
116 static const struct {
117  int flag;
118  const char *name;
119 } cpu_flag_tab[] = {
120 #if ARCH_ARM
121  { AV_CPU_FLAG_ARMV5TE, "armv5te" },
122  { AV_CPU_FLAG_ARMV6, "armv6" },
123  { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
124  { AV_CPU_FLAG_VFP, "vfp" },
125  { AV_CPU_FLAG_VFPV3, "vfpv3" },
126  { AV_CPU_FLAG_NEON, "neon" },
127 #elif ARCH_PPC
128  { AV_CPU_FLAG_ALTIVEC, "altivec" },
129 #elif ARCH_X86
130  { AV_CPU_FLAG_MMX, "mmx" },
131  { AV_CPU_FLAG_MMXEXT, "mmxext" },
132  { AV_CPU_FLAG_SSE, "sse" },
133  { AV_CPU_FLAG_SSE2, "sse2" },
134  { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
135  { AV_CPU_FLAG_SSE3, "sse3" },
136  { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
137  { AV_CPU_FLAG_SSSE3, "ssse3" },
138  { AV_CPU_FLAG_ATOM, "atom" },
139  { AV_CPU_FLAG_SSE4, "sse4.1" },
140  { AV_CPU_FLAG_SSE42, "sse4.2" },
141  { AV_CPU_FLAG_AVX, "avx" },
142  { AV_CPU_FLAG_XOP, "xop" },
143  { AV_CPU_FLAG_FMA4, "fma4" },
144  { AV_CPU_FLAG_3DNOW, "3dnow" },
145  { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
146  { AV_CPU_FLAG_CMOV, "cmov" },
147 #endif
148  { 0 }
149 };
150 
151 int main(void)
152 {
153  int cpu_flags = av_get_cpu_flags();
154  int i;
155 
156  printf("cpu_flags = 0x%08X\n", cpu_flags);
157  printf("cpu_flags =");
158  for (i = 0; cpu_flag_tab[i].flag; i++)
159  if (cpu_flags & cpu_flag_tab[i].flag)
160  printf(" %s", cpu_flag_tab[i].name);
161  printf("\n");
162 
163  return 0;
164 }
165 
166 #endif