http: Refactor http_open_cnx
[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>
c6c98d08 183
c31bc537
MR
184static const struct {
185 int flag;
186 const char *name;
187} cpu_flag_tab[] = {
b7b17ed6 188#if ARCH_AARCH64
8675bcb0 189 { AV_CPU_FLAG_ARMV8, "armv8" },
b7b17ed6
JG
190 { AV_CPU_FLAG_NEON, "neon" },
191 { AV_CPU_FLAG_VFP, "vfp" },
192#elif ARCH_ARM
d526c533
MR
193 { AV_CPU_FLAG_ARMV5TE, "armv5te" },
194 { AV_CPU_FLAG_ARMV6, "armv6" },
195 { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
196 { AV_CPU_FLAG_VFP, "vfp" },
197 { AV_CPU_FLAG_VFPV3, "vfpv3" },
198 { AV_CPU_FLAG_NEON, "neon" },
199#elif ARCH_PPC
c31bc537
MR
200 { AV_CPU_FLAG_ALTIVEC, "altivec" },
201#elif ARCH_X86
202 { AV_CPU_FLAG_MMX, "mmx" },
239fdf1b 203 { AV_CPU_FLAG_MMXEXT, "mmxext" },
c31bc537
MR
204 { AV_CPU_FLAG_SSE, "sse" },
205 { AV_CPU_FLAG_SSE2, "sse2" },
206 { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
207 { AV_CPU_FLAG_SSE3, "sse3" },
208 { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
209 { AV_CPU_FLAG_SSSE3, "ssse3" },
210 { AV_CPU_FLAG_ATOM, "atom" },
211 { AV_CPU_FLAG_SSE4, "sse4.1" },
212 { AV_CPU_FLAG_SSE42, "sse4.2" },
213 { AV_CPU_FLAG_AVX, "avx" },
96a59cf3 214 { AV_CPU_FLAG_XOP, "xop" },
1b932eb1 215 { AV_CPU_FLAG_FMA3, "fma3" },
96a59cf3 216 { AV_CPU_FLAG_FMA4, "fma4" },
c31bc537
MR
217 { AV_CPU_FLAG_3DNOW, "3dnow" },
218 { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
65345a5a 219 { AV_CPU_FLAG_CMOV, "cmov" },
4d6ee072 220 { AV_CPU_FLAG_AVX2, "avx2" },
d59fcdaf
JA
221 { AV_CPU_FLAG_BMI1, "bmi1" },
222 { AV_CPU_FLAG_BMI2, "bmi2" },
c31bc537
MR
223#endif
224 { 0 }
225};
226
c6c98d08
SS
227int main(void)
228{
229 int cpu_flags = av_get_cpu_flags();
c31bc537 230 int i;
c6c98d08
SS
231
232 printf("cpu_flags = 0x%08X\n", cpu_flags);
c31bc537
MR
233 printf("cpu_flags =");
234 for (i = 0; cpu_flag_tab[i].flag; i++)
235 if (cpu_flags & cpu_flag_tab[i].flag)
236 printf(" %s", cpu_flag_tab[i].name);
237 printf("\n");
238
c6c98d08
SS
239 return 0;
240}
241
242#endif