Libav
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 "config.h"
20 
21 #if HAVE_UNISTD_H
22 #include <unistd.h>
23 #elif !HAVE_GETOPT
24 #include "compat/getopt.c"
25 #endif
26 
27 #include <stdint.h>
28 #include <stdio.h>
29 
30 #include "libavutil/avstring.h"
31 #include "libavutil/common.h"
32 #include "libavutil/cpu.h"
33 
34 static const struct {
35  int flag;
36  const char *name;
37 } cpu_flag_tab[] = {
38 #if ARCH_AARCH64
39  { AV_CPU_FLAG_ARMV8, "armv8" },
40  { AV_CPU_FLAG_NEON, "neon" },
41  { AV_CPU_FLAG_VFP, "vfp" },
42 #elif ARCH_ARM
43  { AV_CPU_FLAG_ARMV5TE, "armv5te" },
44  { AV_CPU_FLAG_ARMV6, "armv6" },
45  { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
46  { AV_CPU_FLAG_VFP, "vfp" },
47  { AV_CPU_FLAG_VFP_VM, "vfp_vm" },
48  { AV_CPU_FLAG_VFPV3, "vfpv3" },
49  { AV_CPU_FLAG_NEON, "neon" },
50 #elif ARCH_PPC
51  { AV_CPU_FLAG_ALTIVEC, "altivec" },
52 #elif ARCH_X86
53  { AV_CPU_FLAG_MMX, "mmx" },
54  { AV_CPU_FLAG_MMXEXT, "mmxext" },
55  { AV_CPU_FLAG_SSE, "sse" },
56  { AV_CPU_FLAG_SSE2, "sse2" },
57  { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
58  { AV_CPU_FLAG_SSE3, "sse3" },
59  { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
60  { AV_CPU_FLAG_SSSE3, "ssse3" },
61  { AV_CPU_FLAG_ATOM, "atom" },
62  { AV_CPU_FLAG_SSE4, "sse4.1" },
63  { AV_CPU_FLAG_SSE42, "sse4.2" },
64  { AV_CPU_FLAG_AVX, "avx" },
65  { AV_CPU_FLAG_AVXSLOW, "avxslow" },
66  { AV_CPU_FLAG_XOP, "xop" },
67  { AV_CPU_FLAG_FMA3, "fma3" },
68  { AV_CPU_FLAG_FMA4, "fma4" },
69  { AV_CPU_FLAG_3DNOW, "3dnow" },
70  { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
71  { AV_CPU_FLAG_CMOV, "cmov" },
72  { AV_CPU_FLAG_AVX2, "avx2" },
73  { AV_CPU_FLAG_BMI1, "bmi1" },
74  { AV_CPU_FLAG_BMI2, "bmi2" },
75 #endif
76  { 0 }
77 };
78 
79 static void print_cpu_flags(int cpu_flags, const char *type)
80 {
81  int i;
82 
83  fprintf(stderr, "cpu_flags(%s) = 0x%08X\n", type, cpu_flags);
84  fprintf(stderr, "cpu_flags_str(%s) =", type);
85  for (i = 0; cpu_flag_tab[i].flag; i++)
86  if (cpu_flags & cpu_flag_tab[i].flag)
87  fprintf(stderr, " %s", cpu_flag_tab[i].name);
88  fprintf(stderr, "\n");
89 }
90 
91 
92 int main(int argc, char **argv)
93 {
94  int cpu_flags_raw = av_get_cpu_flags();
95  int cpu_flags_eff;
96  int cpu_count = av_cpu_count();
97  char threads[5] = "auto";
98 
99  if (cpu_flags_raw < 0)
100  return 1;
101 
102  for (;;) {
103  int c = getopt(argc, argv, "c:t:");
104  if (c == -1)
105  break;
106  switch (c) {
107  case 'c':
108  {
109  int cpuflags = av_parse_cpu_flags(optarg);
110  if (cpuflags < 0)
111  return 2;
112  av_set_cpu_flags_mask(cpuflags);
113  break;
114  }
115  case 't':
116  {
117  int len = av_strlcpy(threads, optarg, sizeof(threads));
118  if (len >= sizeof(threads)) {
119  fprintf(stderr, "Invalid thread count '%s'\n", optarg);
120  return 2;
121  }
122  }
123  }
124  }
125 
126  cpu_flags_eff = av_get_cpu_flags();
127 
128  if (cpu_flags_eff < 0)
129  return 3;
130 
131  print_cpu_flags(cpu_flags_raw, "raw");
132  print_cpu_flags(cpu_flags_eff, "effective");
133  fprintf(stderr, "threads = %s (cpu_count = %d)\n", threads, cpu_count);
134 
135  return 0;
136 }
#define AV_CPU_FLAG_AVX
AVX functions: requires OS support even if YMM registers aren&#39;t used.
Definition: cpu.h:45
#define AV_CPU_FLAG_ALTIVEC
standard
Definition: cpu.h:55
int av_cpu_count(void)
Definition: cpu.c:153
#define AV_CPU_FLAG_SSE
SSE functions.
Definition: cpu.h:32
int flag
Definition: cpu.c:35
void av_set_cpu_flags_mask(int mask)
Set a mask on flags returned by av_get_cpu_flags().
Definition: cpu.c:69
#define AV_CPU_FLAG_CMOV
i686 cmov
Definition: cpu.h:49
#define AV_CPU_FLAG_VFP
Definition: cpu.h:62
#define AV_CPU_FLAG_FMA3
Haswell FMA3 functions.
Definition: cpu.h:51
#define AV_CPU_FLAG_NEON
Definition: cpu.h:64
#define AV_CPU_FLAG_MMXEXT
SSE integer functions or AMD MMX ext.
Definition: cpu.h:30
const char * name
Definition: cpu.c:36
int av_parse_cpu_flags(const char *s)
Parse CPU flags from a string.
Definition: cpu.c:75
#define AV_CPU_FLAG_ATOM
Atom processor, some SSSE3 instructions are slower.
Definition: cpu.h:42
#define AV_CPU_FLAG_AVX2
AVX2 functions: requires OS support even if YMM registers aren&#39;t used.
Definition: cpu.h:50
#define AV_CPU_FLAG_SSE2SLOW
SSE2 supported, but usually not faster.
Definition: cpu.h:34
#define AV_CPU_FLAG_XOP
Bulldozer XOP functions.
Definition: cpu.h:47
#define AV_CPU_FLAG_SSE42
Nehalem SSE4.2 functions.
Definition: cpu.h:44
#define AV_CPU_FLAG_SSSE3
Conroe SSSE3 functions.
Definition: cpu.h:40
#define AV_CPU_FLAG_ARMV6T2
Definition: cpu.h:61
#define AV_CPU_FLAG_VFP_VM
VFPv2 vector mode, deprecated in ARMv7-A and unavailable in various CPUs implementations.
Definition: cpu.h:66
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
#define AV_CPU_FLAG_ARMV5TE
Definition: cpu.h:59
#define AV_CPU_FLAG_SSE3
Prescott SSE3 functions.
Definition: cpu.h:37
#define AV_CPU_FLAG_BMI2
Bit Manipulation Instruction Set 2.
Definition: cpu.h:53
#define AV_CPU_FLAG_VFPV3
Definition: cpu.h:63
#define AV_CPU_FLAG_3DNOW
AMD 3DNOW.
Definition: cpu.h:31
#define AV_CPU_FLAG_ARMV6
Definition: cpu.h:60
#define AV_CPU_FLAG_SSE3SLOW
SSE3 supported, but usually not faster.
Definition: cpu.h:38
#define AV_CPU_FLAG_AVXSLOW
AVX supported, but slow when using YMM registers (e.g. Bulldozer)
Definition: cpu.h:46
#define AV_CPU_FLAG_BMI1
Bit Manipulation Instruction Set 1.
Definition: cpu.h:52
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:29
static int getopt(int argc, char *argv[], char *opts)
Definition: getopt.c:41
#define AV_CPU_FLAG_FMA4
Bulldozer FMA4 functions.
Definition: cpu.h:48
#define AV_CPU_FLAG_SSE4
Penryn SSE4.1 functions.
Definition: cpu.h:43
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:47
#define AV_CPU_FLAG_3DNOWEXT
AMD 3DNowExt.
Definition: cpu.h:36
static const struct @171 cpu_flag_tab[]
common internal and external API header
#define AV_CPU_FLAG_ARMV8
Definition: cpu.h:65
static char * optarg
Definition: getopt.c:39
int main(int argc, char **argv)
Definition: cpu.c:92
int len
#define AV_CPU_FLAG_SSE2
PIV SSE2 functions.
Definition: cpu.h:33
static void print_cpu_flags(int cpu_flags, const char *type)
Definition: cpu.c:79