2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/cpu.h"
20 #include "libavutil/cpu_internal.h"
22 #if defined __linux__ || defined __ANDROID__
28 #include "libavutil/avstring.h"
31 #if defined __linux__ || defined __ANDROID__
33 #define HWCAP_LOONGSON_CPUCFG (1 << 14)
35 static int cpucfg_available(void)
37 return getauxval(AT_HWCAP) & HWCAP_LOONGSON_CPUCFG;
40 /* Most toolchains have no CPUCFG support yet */
41 static uint32_t read_cpucfg(uint32_t reg)
46 "parse_r __res,%0\n\t"
49 ".word (0xc8080118 | (reg << 21) | (__res << 11))\n\t"
57 #define LOONGSON_CFG1 0x1
59 #define LOONGSON_CFG1_MMI (1 << 4)
60 #define LOONGSON_CFG1_MSA1 (1 << 5)
62 static int cpu_flags_cpucfg(void)
65 uint32_t cfg1 = read_cpucfg(LOONGSON_CFG1);
67 if (cfg1 & LOONGSON_CFG1_MMI)
68 flags |= AV_CPU_FLAG_MMI;
70 if (cfg1 & LOONGSON_CFG1_MSA1)
71 flags |= AV_CPU_FLAG_MSA;
76 static int cpu_flags_cpuinfo(void)
78 FILE *f = fopen("/proc/cpuinfo", "r");
85 while (fgets(buf, sizeof(buf), f)) {
86 /* Legacy kernel may not export MMI in ASEs implemented */
87 if (av_strstart(buf, "cpu model", NULL)) {
88 if (strstr(buf, "Loongson-3 "))
89 flags |= AV_CPU_FLAG_MMI;
92 if (av_strstart(buf, "ASEs implemented", NULL)) {
93 if (strstr(buf, " loongson-mmi"))
94 flags |= AV_CPU_FLAG_MMI;
95 if (strstr(buf, " msa"))
96 flags |= AV_CPU_FLAG_MSA;
106 int ff_get_cpu_flags_mips(void)
108 #if defined __linux__ || defined __ANDROID__
109 if (cpucfg_available())
110 return cpu_flags_cpucfg();
112 return cpu_flags_cpuinfo();
114 /* Assume no SIMD ASE supported */
119 size_t ff_get_cpu_max_align_mips(void)
121 int flags = av_get_cpu_flags();
123 if (flags & AV_CPU_FLAG_MSA)
127 * MMI itself is 64-bit but quad word load & store
128 * needs 128-bit align.
130 if (flags & AV_CPU_FLAG_MMI)