d651eb2089d77820ff04e00bf5b7cadedf96fc0a
[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 #define CPUFLAG_BMI1 (AV_CPU_FLAG_BMI1)
94 #define CPUFLAG_BMI2 (AV_CPU_FLAG_BMI2 | CPUFLAG_BMI1)
95 static const AVOption cpuflags_opts[] = {
96 { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
97 #if ARCH_PPC
98 { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
99 #elif ARCH_X86
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" },
113 { "fma3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA3 }, .unit = "flags" },
114 { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA4 }, .unit = "flags" },
115 { "avx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX2 }, .unit = "flags" },
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" },
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" },
121 #elif ARCH_ARM
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" },
128 #elif ARCH_AARCH64
129 { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
130 { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
131 #endif
132 { NULL },
133 };
134 static const AVClass class = {
135 .class_name = "cpuflags",
136 .item_name = av_default_item_name,
137 .option = cpuflags_opts,
138 .version = LIBAVUTIL_VERSION_INT,
139 };
140
141 int flags = 0, ret;
142 const AVClass *pclass = &class;
143
144 if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
145 return ret;
146
147 return flags & INT_MAX;
148 }
149
150 int av_cpu_count(void)
151 {
152 int nb_cpus = 1;
153 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
154 cpu_set_t cpuset;
155
156 CPU_ZERO(&cpuset);
157
158 if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
159 nb_cpus = CPU_COUNT(&cpuset);
160 #elif HAVE_GETPROCESSAFFINITYMASK
161 DWORD_PTR proc_aff, sys_aff;
162 if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
163 nb_cpus = av_popcount64(proc_aff);
164 #elif HAVE_SYSCTL && defined(HW_NCPU)
165 int mib[2] = { CTL_HW, HW_NCPU };
166 size_t len = sizeof(nb_cpus);
167
168 if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
169 nb_cpus = 0;
170 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
171 nb_cpus = sysconf(_SC_NPROC_ONLN);
172 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
173 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
174 #endif
175
176 return nb_cpus;
177 }
178
179 #ifdef TEST
180
181 #include <stdio.h>
182
183 static const struct {
184 int flag;
185 const char *name;
186 } cpu_flag_tab[] = {
187 #if ARCH_AARCH64
188 { AV_CPU_FLAG_NEON, "neon" },
189 { AV_CPU_FLAG_VFP, "vfp" },
190 #elif ARCH_ARM
191 { AV_CPU_FLAG_ARMV5TE, "armv5te" },
192 { AV_CPU_FLAG_ARMV6, "armv6" },
193 { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
194 { AV_CPU_FLAG_VFP, "vfp" },
195 { AV_CPU_FLAG_VFPV3, "vfpv3" },
196 { AV_CPU_FLAG_NEON, "neon" },
197 #elif ARCH_PPC
198 { AV_CPU_FLAG_ALTIVEC, "altivec" },
199 #elif ARCH_X86
200 { AV_CPU_FLAG_MMX, "mmx" },
201 { AV_CPU_FLAG_MMXEXT, "mmxext" },
202 { AV_CPU_FLAG_SSE, "sse" },
203 { AV_CPU_FLAG_SSE2, "sse2" },
204 { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
205 { AV_CPU_FLAG_SSE3, "sse3" },
206 { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
207 { AV_CPU_FLAG_SSSE3, "ssse3" },
208 { AV_CPU_FLAG_ATOM, "atom" },
209 { AV_CPU_FLAG_SSE4, "sse4.1" },
210 { AV_CPU_FLAG_SSE42, "sse4.2" },
211 { AV_CPU_FLAG_AVX, "avx" },
212 { AV_CPU_FLAG_XOP, "xop" },
213 { AV_CPU_FLAG_FMA3, "fma3" },
214 { AV_CPU_FLAG_FMA4, "fma4" },
215 { AV_CPU_FLAG_3DNOW, "3dnow" },
216 { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
217 { AV_CPU_FLAG_CMOV, "cmov" },
218 { AV_CPU_FLAG_AVX2, "avx2" },
219 { AV_CPU_FLAG_BMI1, "bmi1" },
220 { AV_CPU_FLAG_BMI2, "bmi2" },
221 #endif
222 { 0 }
223 };
224
225 int main(void)
226 {
227 int cpu_flags = av_get_cpu_flags();
228 int i;
229
230 printf("cpu_flags = 0x%08X\n", cpu_flags);
231 printf("cpu_flags =");
232 for (i = 0; cpu_flag_tab[i].flag; i++)
233 if (cpu_flags & cpu_flag_tab[i].flag)
234 printf(" %s", cpu_flag_tab[i].name);
235 printf("\n");
236
237 return 0;
238 }
239
240 #endif