]> git.sesse.net Git - ffmpeg/blob - libavutil/cpu-test.c
Merge commit 'def03d14687b9d089950ba8e45083e666de4eb68'
[ffmpeg] / libavutil / cpu-test.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <stdio.h>
20
21 #include "config.h"
22 #include "cpu.h"
23 #include "avstring.h"
24
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #if !HAVE_GETOPT
29 #include "compat/getopt.c"
30 #endif
31
32 static const struct {
33     int flag;
34     const char *name;
35 } cpu_flag_tab[] = {
36 #if   ARCH_AARCH64
37     { AV_CPU_FLAG_ARMV8,     "armv8"      },
38     { AV_CPU_FLAG_NEON,      "neon"       },
39     { AV_CPU_FLAG_VFP,       "vfp"        },
40 #elif ARCH_ARM
41     { AV_CPU_FLAG_ARMV5TE,   "armv5te"    },
42     { AV_CPU_FLAG_ARMV6,     "armv6"      },
43     { AV_CPU_FLAG_ARMV6T2,   "armv6t2"    },
44     { AV_CPU_FLAG_VFP,       "vfp"        },
45     { AV_CPU_FLAG_VFP_VM,    "vfp_vm"     },
46     { AV_CPU_FLAG_VFPV3,     "vfpv3"      },
47     { AV_CPU_FLAG_NEON,      "neon"       },
48     { AV_CPU_FLAG_SETEND,    "setend"     },
49 #elif ARCH_PPC
50     { AV_CPU_FLAG_ALTIVEC,   "altivec"    },
51 #elif ARCH_X86
52     { AV_CPU_FLAG_MMX,       "mmx"        },
53     { AV_CPU_FLAG_MMXEXT,    "mmxext"     },
54     { AV_CPU_FLAG_SSE,       "sse"        },
55     { AV_CPU_FLAG_SSE2,      "sse2"       },
56     { AV_CPU_FLAG_SSE2SLOW,  "sse2slow"   },
57     { AV_CPU_FLAG_SSE3,      "sse3"       },
58     { AV_CPU_FLAG_SSE3SLOW,  "sse3slow"   },
59     { AV_CPU_FLAG_SSSE3,     "ssse3"      },
60     { AV_CPU_FLAG_ATOM,      "atom"       },
61     { AV_CPU_FLAG_SSE4,      "sse4.1"     },
62     { AV_CPU_FLAG_SSE42,     "sse4.2"     },
63     { AV_CPU_FLAG_AVX,       "avx"        },
64     { AV_CPU_FLAG_AVXSLOW,   "avxslow"    },
65     { AV_CPU_FLAG_XOP,       "xop"        },
66     { AV_CPU_FLAG_FMA3,      "fma3"       },
67     { AV_CPU_FLAG_FMA4,      "fma4"       },
68     { AV_CPU_FLAG_3DNOW,     "3dnow"      },
69     { AV_CPU_FLAG_3DNOWEXT,  "3dnowext"   },
70     { AV_CPU_FLAG_CMOV,      "cmov"       },
71     { AV_CPU_FLAG_AVX2,      "avx2"       },
72     { AV_CPU_FLAG_BMI1,      "bmi1"       },
73     { AV_CPU_FLAG_BMI2,      "bmi2"       },
74     { AV_CPU_FLAG_AESNI,     "aesni"      },
75 #endif
76     { 0 }
77 };
78
79 static void print_cpu_flags(int cpu_flags, const char *type)
80 {
81     int i;
82
83     printf("cpu_flags(%s) = 0x%08X\n", type, cpu_flags);
84     printf("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             printf(" %s", cpu_flag_tab[i].name);
88     printf("\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     int i;
99
100     for(i = 0; cpu_flag_tab[i].flag; i++) {
101         unsigned tmp = 0;
102         if (av_parse_cpu_caps(&tmp, cpu_flag_tab[i].name) < 0) {
103             fprintf(stderr, "Table missing %s\n", cpu_flag_tab[i].name);
104             return 4;
105         }
106     }
107
108     if (cpu_flags_raw < 0)
109         return 1;
110
111     for (;;) {
112         int c = getopt(argc, argv, "c:t:");
113         if (c == -1)
114             break;
115         switch (c) {
116         case 'c':
117         {
118             unsigned flags = av_get_cpu_flags();
119             if (av_parse_cpu_caps(&flags, optarg) < 0)
120                 return 2;
121
122             av_force_cpu_flags(flags);
123             break;
124         }
125         case 't':
126         {
127             int len = av_strlcpy(threads, optarg, sizeof(threads));
128             if (len >= sizeof(threads)) {
129                 fprintf(stderr, "Invalid thread count '%s'\n", optarg);
130                 return 2;
131             }
132         }
133         }
134     }
135
136     cpu_flags_eff = av_get_cpu_flags();
137
138     if (cpu_flags_eff < 0)
139         return 3;
140
141     print_cpu_flags(cpu_flags_raw, "raw");
142     print_cpu_flags(cpu_flags_eff, "effective");
143     printf("threads = %s (cpu_count = %d)\n", threads, cpu_count);
144
145     return 0;
146 }