972e4eb79f3808c5fffb81d238c5a6dd9c6583f5
[libav.git] / libavutil / cpu.c
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 <stdint.h>
20
21 #include "cpu.h"
22 #include "cpu_internal.h"
23 #include "config.h"
24 #include "opt.h"
25 #include "common.h"
26
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
45 static int cpuflags_mask = -1, checked;
46
47 int av_get_cpu_flags(void)
48 {
49 static int flags;
50
51 if (checked)
52 return flags;
53
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();
62
63 flags &= cpuflags_mask;
64 checked = 1;
65
66 return flags;
67 }
68
69 void av_set_cpu_flags_mask(int mask)
70 {
71 cpuflags_mask = mask;
72 checked = 0;
73 }
74
75 int av_parse_cpu_flags(const char *s)
76 {
77 #define CPUFLAG_MMXEXT (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT | AV_CPU_FLAG_CMOV)
78 #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
79 #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
80 #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMXEXT)
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)
90 #define CPUFLAG_FMA3 (AV_CPU_FLAG_FMA3 | CPUFLAG_AVX)
91 #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
92 #define CPUFLAG_AVX2 (AV_CPU_FLAG_AVX2 | CPUFLAG_AVX)
93 static const AVOption cpuflags_opts[] = {
94 { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
95 #if ARCH_PPC
96 { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
97 #elif ARCH_X86
98 { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
99 { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_MMXEXT }, .unit = "flags" },
100 { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE }, .unit = "flags" },
101 { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2 }, .unit = "flags" },
102 { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2SLOW }, .unit = "flags" },
103 { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3 }, .unit = "flags" },
104 { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3SLOW }, .unit = "flags" },
105 { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSSE3 }, .unit = "flags" },
106 { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
107 { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE4 }, .unit = "flags" },
108 { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE42 }, .unit = "flags" },
109 { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX }, .unit = "flags" },
110 { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_XOP }, .unit = "flags" },
111 { "fma3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA3 }, .unit = "flags" },
112 { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA4 }, .unit = "flags" },
113 { "avx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX2 }, .unit = "flags" },
114 { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOW }, .unit = "flags" },
115 { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOWEXT }, .unit = "flags" },
116 { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
117 #elif ARCH_ARM
118 { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
119 { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
120 { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
121 { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
122 { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
123 { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
124 #elif ARCH_AARCH64
125 { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
126 { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
127 #endif
128 { NULL },
129 };
130 static const AVClass class = {
131 .class_name = "cpuflags",
132 .item_name = av_default_item_name,
133 .option = cpuflags_opts,
134 .version = LIBAVUTIL_VERSION_INT,
135 };
136
137 int flags = 0, ret;
138 const AVClass *pclass = &class;
139
140 if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
141 return ret;
142
143 return flags & INT_MAX;
144 }
145
146 int av_cpu_count(void)
147 {
148 int nb_cpus = 1;
149 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
150 cpu_set_t cpuset;
151
152 CPU_ZERO(&cpuset);
153
154 if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
155 nb_cpus = CPU_COUNT(&cpuset);
156 #elif HAVE_GETPROCESSAFFINITYMASK
157 DWORD_PTR proc_aff, sys_aff;
158 if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
159 nb_cpus = av_popcount64(proc_aff);
160 #elif HAVE_SYSCTL && defined(HW_NCPU)
161 int mib[2] = { CTL_HW, HW_NCPU };
162 size_t len = sizeof(nb_cpus);
163
164 if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
165 nb_cpus = 0;
166 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
167 nb_cpus = sysconf(_SC_NPROC_ONLN);
168 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
169 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
170 #endif
171
172 return nb_cpus;
173 }
174
175 #ifdef TEST
176
177 #include <stdio.h>
178
179 static const struct {
180 int flag;
181 const char *name;
182 } cpu_flag_tab[] = {
183 #if ARCH_AARCH64
184 { AV_CPU_FLAG_NEON, "neon" },
185 { AV_CPU_FLAG_VFP, "vfp" },
186 #elif ARCH_ARM
187 { AV_CPU_FLAG_ARMV5TE, "armv5te" },
188 { AV_CPU_FLAG_ARMV6, "armv6" },
189 { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
190 { AV_CPU_FLAG_VFP, "vfp" },
191 { AV_CPU_FLAG_VFPV3, "vfpv3" },
192 { AV_CPU_FLAG_NEON, "neon" },
193 #elif ARCH_PPC
194 { AV_CPU_FLAG_ALTIVEC, "altivec" },
195 #elif ARCH_X86
196 { AV_CPU_FLAG_MMX, "mmx" },
197 { AV_CPU_FLAG_MMXEXT, "mmxext" },
198 { AV_CPU_FLAG_SSE, "sse" },
199 { AV_CPU_FLAG_SSE2, "sse2" },
200 { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
201 { AV_CPU_FLAG_SSE3, "sse3" },
202 { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
203 { AV_CPU_FLAG_SSSE3, "ssse3" },
204 { AV_CPU_FLAG_ATOM, "atom" },
205 { AV_CPU_FLAG_SSE4, "sse4.1" },
206 { AV_CPU_FLAG_SSE42, "sse4.2" },
207 { AV_CPU_FLAG_AVX, "avx" },
208 { AV_CPU_FLAG_XOP, "xop" },
209 { AV_CPU_FLAG_FMA3, "fma3" },
210 { AV_CPU_FLAG_FMA4, "fma4" },
211 { AV_CPU_FLAG_3DNOW, "3dnow" },
212 { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
213 { AV_CPU_FLAG_CMOV, "cmov" },
214 { AV_CPU_FLAG_AVX2, "avx2" },
215 #endif
216 { 0 }
217 };
218
219 int main(void)
220 {
221 int cpu_flags = av_get_cpu_flags();
222 int i;
223
224 printf("cpu_flags = 0x%08X\n", cpu_flags);
225 printf("cpu_flags =");
226 for (i = 0; cpu_flag_tab[i].flag; i++)
227 if (cpu_flags & cpu_flag_tab[i].flag)
228 printf(" %s", cpu_flag_tab[i].name);
229 printf("\n");
230
231 return 0;
232 }
233
234 #endif