cpu-test: Add unistd.h #include for getopt()
[libav.git] / libavutil / cpu.c
CommitLineData
c6c98d08 1/*
2912e87a 2 * This file is part of Libav.
c6c98d08 3 *
2912e87a 4 * Libav is free software; you can redistribute it and/or
c6c98d08
SS
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 *
2912e87a 9 * Libav is distributed in the hope that it will be useful,
c6c98d08
SS
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
2912e87a 15 * License along with Libav; if not, write to the Free Software
c6c98d08
SS
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
8f8bc923
DB
19#include <stdint.h>
20
c6c98d08 21#include "cpu.h"
b78b10c4 22#include "cpu_internal.h"
c6c98d08 23#include "config.h"
3527a739 24#include "opt.h"
597208fd 25#include "common.h"
c6c98d08 26
2a6eaeaa
AK
27#if HAVE_SCHED_GETAFFINITY
28#define _GNU_SOURCE
29#include <sched.h>
30#endif
31#if HAVE_GETPROCESSAFFINITYMASK
32#include <windows.h>
33#endif
34#if HAVE_SYSCTL
35#if HAVE_SYS_PARAM_H
36#include <sys/param.h>
37#endif
38#include <sys/types.h>
39#include <sys/sysctl.h>
40#endif
41#if HAVE_SYSCONF
42#include <unistd.h>
43#endif
44
a9c5b6f6 45static int cpuflags_mask = -1, checked;
4d851f8d 46
c6c98d08
SS
47int av_get_cpu_flags(void)
48{
4d851f8d 49 static int flags;
a3f0d2b9
MR
50
51 if (checked)
52 return flags;
53
b7b17ed6
JG
54 if (ARCH_AARCH64)
55 flags = ff_get_cpu_flags_aarch64();
56 if (ARCH_ARM)
57 flags = ff_get_cpu_flags_arm();
58 if (ARCH_PPC)
59 flags = ff_get_cpu_flags_ppc();
60 if (ARCH_X86)
61 flags = ff_get_cpu_flags_x86();
a3f0d2b9 62
4d851f8d 63 flags &= cpuflags_mask;
a3f0d2b9 64 checked = 1;
4d851f8d 65
a3f0d2b9 66 return flags;
c6c98d08 67}
c6c98d08 68
4d851f8d
AK
69void av_set_cpu_flags_mask(int mask)
70{
71 cpuflags_mask = mask;
72 checked = 0;
73}
74
3527a739
MR
75int av_parse_cpu_flags(const char *s)
76{
239fdf1b 77#define CPUFLAG_MMXEXT (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT | AV_CPU_FLAG_CMOV)
3527a739
MR
78#define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
79#define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
239fdf1b 80#define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMXEXT)
3527a739
MR
81#define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
82#define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
83#define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
84#define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
85#define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
86#define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
87#define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
88#define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
89#define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
1b932eb1 90#define CPUFLAG_FMA3 (AV_CPU_FLAG_FMA3 | CPUFLAG_AVX)
3527a739 91#define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
4d6ee072 92#define CPUFLAG_AVX2 (AV_CPU_FLAG_AVX2 | CPUFLAG_AVX)
d59fcdaf
JA
93#define CPUFLAG_BMI1 (AV_CPU_FLAG_BMI1)
94#define CPUFLAG_BMI2 (AV_CPU_FLAG_BMI2 | CPUFLAG_BMI1)
3527a739 95 static const AVOption cpuflags_opts[] = {
d58dd4b5 96 { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
6eca84ba 97#if ARCH_PPC
124134e4 98 { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
6eca84ba 99#elif ARCH_X86
124134e4
MS
100 { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
101 { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_MMXEXT }, .unit = "flags" },
102 { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE }, .unit = "flags" },
103 { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2 }, .unit = "flags" },
104 { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2SLOW }, .unit = "flags" },
105 { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3 }, .unit = "flags" },
106 { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3SLOW }, .unit = "flags" },
107 { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSSE3 }, .unit = "flags" },
108 { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
109 { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE4 }, .unit = "flags" },
110 { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE42 }, .unit = "flags" },
111 { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX }, .unit = "flags" },
112 { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_XOP }, .unit = "flags" },
1b932eb1 113 { "fma3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA3 }, .unit = "flags" },
124134e4 114 { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA4 }, .unit = "flags" },
4d6ee072 115 { "avx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX2 }, .unit = "flags" },
d59fcdaf
JA
116 { "bmi1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_BMI1 }, .unit = "flags" },
117 { "bmi2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_BMI2 }, .unit = "flags" },
124134e4
MS
118 { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOW }, .unit = "flags" },
119 { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOWEXT }, .unit = "flags" },
120 { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
6eca84ba 121#elif ARCH_ARM
124134e4
MS
122 { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
123 { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
124 { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
125 { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
126 { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
127 { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
b7b17ed6 128#elif ARCH_AARCH64
8675bcb0 129 { "armv8", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV8 }, .unit = "flags" },
b7b17ed6
JG
130 { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
131 { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
6eca84ba 132#endif
3527a739
MR
133 { NULL },
134 };
135 static const AVClass class = {
136 .class_name = "cpuflags",
137 .item_name = av_default_item_name,
138 .option = cpuflags_opts,
139 .version = LIBAVUTIL_VERSION_INT,
140 };
141
142 int flags = 0, ret;
143 const AVClass *pclass = &class;
144
145 if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
146 return ret;
147
148 return flags & INT_MAX;
149}
150
2a6eaeaa
AK
151int av_cpu_count(void)
152{
f0ce1d99 153 int nb_cpus = 1;
2a6eaeaa
AK
154#if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
155 cpu_set_t cpuset;
156
157 CPU_ZERO(&cpuset);
158
f0ce1d99 159 if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
2a6eaeaa 160 nb_cpus = CPU_COUNT(&cpuset);
2a6eaeaa
AK
161#elif HAVE_GETPROCESSAFFINITYMASK
162 DWORD_PTR proc_aff, sys_aff;
f0ce1d99 163 if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
2a6eaeaa
AK
164 nb_cpus = av_popcount64(proc_aff);
165#elif HAVE_SYSCTL && defined(HW_NCPU)
166 int mib[2] = { CTL_HW, HW_NCPU };
167 size_t len = sizeof(nb_cpus);
168
f0ce1d99 169 if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
2a6eaeaa
AK
170 nb_cpus = 0;
171#elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
172 nb_cpus = sysconf(_SC_NPROC_ONLN);
173#elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
174 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
175#endif
176
177 return nb_cpus;
178}
179
c6c98d08
SS
180#ifdef TEST
181
ec5f5719 182#include <stdio.h>
35324054
DB
183#if HAVE_UNISTD_H
184#include <unistd.h>
185#endif
186
caf5ef85 187#include "avstring.h"
c6c98d08 188
07d8fa58
JG
189#if !HAVE_GETOPT
190#include "compat/getopt.c"
191#endif
192
c31bc537
MR
193static const struct {
194 int flag;
195 const char *name;
196} cpu_flag_tab[] = {
b7b17ed6 197#if ARCH_AARCH64
8675bcb0 198 { AV_CPU_FLAG_ARMV8, "armv8" },
b7b17ed6
JG
199 { AV_CPU_FLAG_NEON, "neon" },
200 { AV_CPU_FLAG_VFP, "vfp" },
201#elif ARCH_ARM
d526c533
MR
202 { AV_CPU_FLAG_ARMV5TE, "armv5te" },
203 { AV_CPU_FLAG_ARMV6, "armv6" },
204 { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
205 { AV_CPU_FLAG_VFP, "vfp" },
206 { AV_CPU_FLAG_VFPV3, "vfpv3" },
207 { AV_CPU_FLAG_NEON, "neon" },
208#elif ARCH_PPC
c31bc537
MR
209 { AV_CPU_FLAG_ALTIVEC, "altivec" },
210#elif ARCH_X86
211 { AV_CPU_FLAG_MMX, "mmx" },
239fdf1b 212 { AV_CPU_FLAG_MMXEXT, "mmxext" },
c31bc537
MR
213 { AV_CPU_FLAG_SSE, "sse" },
214 { AV_CPU_FLAG_SSE2, "sse2" },
215 { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
216 { AV_CPU_FLAG_SSE3, "sse3" },
217 { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
218 { AV_CPU_FLAG_SSSE3, "ssse3" },
219 { AV_CPU_FLAG_ATOM, "atom" },
220 { AV_CPU_FLAG_SSE4, "sse4.1" },
221 { AV_CPU_FLAG_SSE42, "sse4.2" },
222 { AV_CPU_FLAG_AVX, "avx" },
96a59cf3 223 { AV_CPU_FLAG_XOP, "xop" },
1b932eb1 224 { AV_CPU_FLAG_FMA3, "fma3" },
96a59cf3 225 { AV_CPU_FLAG_FMA4, "fma4" },
c31bc537
MR
226 { AV_CPU_FLAG_3DNOW, "3dnow" },
227 { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
65345a5a 228 { AV_CPU_FLAG_CMOV, "cmov" },
4d6ee072 229 { AV_CPU_FLAG_AVX2, "avx2" },
d59fcdaf
JA
230 { AV_CPU_FLAG_BMI1, "bmi1" },
231 { AV_CPU_FLAG_BMI2, "bmi2" },
c31bc537
MR
232#endif
233 { 0 }
234};
235
07d8fa58 236static void print_cpu_flags(int cpu_flags, const char *type)
c6c98d08 237{
c31bc537 238 int i;
c6c98d08 239
07d8fa58
JG
240 fprintf(stderr, "cpu_flags(%s) = 0x%08X\n", type, cpu_flags);
241 fprintf(stderr, "cpu_flags_str(%s) =", type);
c31bc537
MR
242 for (i = 0; cpu_flag_tab[i].flag; i++)
243 if (cpu_flags & cpu_flag_tab[i].flag)
07d8fa58
JG
244 fprintf(stderr, " %s", cpu_flag_tab[i].name);
245 fprintf(stderr, "\n");
246}
247
248
249int main(int argc, char **argv)
250{
251 int cpu_flags_raw = av_get_cpu_flags();
252 int cpu_flags_eff;
caf5ef85
JG
253 int cpu_count = av_cpu_count();
254 char threads[5] = "auto";
07d8fa58
JG
255
256 if (cpu_flags_raw < 0)
257 return 1;
258
259 for (;;) {
caf5ef85 260 int c = getopt(argc, argv, "c:t:");
07d8fa58
JG
261 if (c == -1)
262 break;
263 switch (c) {
264 case 'c':
265 {
266 int cpuflags = av_parse_cpu_flags(optarg);
267 if (cpuflags < 0)
268 return 2;
269 av_set_cpu_flags_mask(cpuflags);
270 break;
271 }
caf5ef85
JG
272 case 't':
273 {
274 int len = av_strlcpy(threads, optarg, sizeof(threads));
275 if (len >= sizeof(threads)) {
276 fprintf(stderr, "Invalid thread count '%s'\n", optarg);
277 return 2;
278 }
279 }
07d8fa58
JG
280 }
281 }
282
283 cpu_flags_eff = av_get_cpu_flags();
284
285 if (cpu_flags_eff < 0)
286 return 3;
287
288 print_cpu_flags(cpu_flags_raw, "raw");
289 print_cpu_flags(cpu_flags_eff, "effective");
caf5ef85 290 fprintf(stderr, "threads = %s (cpu_count = %d)\n", threads, cpu_count);
c31bc537 291
c6c98d08
SS
292 return 0;
293}
294
295#endif