cpu: split flag checks per arch in av_cpu_max_align()
[libav.git] / libavutil / ppc / 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 #ifdef __APPLE__
20 #include <sys/sysctl.h>
21 #elif defined(__linux__)
22 #include <asm/cputable.h>
23 #include <linux/auxvec.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #elif defined(__OpenBSD__)
27 #include <sys/param.h>
28 #include <sys/sysctl.h>
29 #include <machine/cpu.h>
30 #elif defined(__AMIGAOS4__)
31 #include <exec/exec.h>
32 #include <interfaces/exec.h>
33 #include <proto/exec.h>
34 #endif /* __APPLE__ */
35
36 #include "libavutil/cpu.h"
37 #include "libavutil/cpu_internal.h"
38 #include "config.h"
39
40 /**
41 * This function MAY rely on signal() or fork() in order to make sure AltiVec
42 * is present.
43 */
44 int ff_get_cpu_flags_ppc(void)
45 {
46 #if HAVE_ALTIVEC
47 #ifdef __AMIGAOS4__
48 ULONG result = 0;
49 extern struct ExecIFace *IExec;
50
51 IExec->GetCPUInfoTags(GCIT_VectorUnit, &result, TAG_DONE);
52 if (result == VECTORTYPE_ALTIVEC)
53 return AV_CPU_FLAG_ALTIVEC;
54 return 0;
55 #elif defined(__APPLE__) || defined(__OpenBSD__)
56 #ifdef __OpenBSD__
57 int sels[2] = {CTL_MACHDEP, CPU_ALTIVEC};
58 #else
59 int sels[2] = {CTL_HW, HW_VECTORUNIT};
60 #endif
61 int has_vu = 0;
62 size_t len = sizeof(has_vu);
63 int err;
64
65 err = sysctl(sels, 2, &has_vu, &len, NULL, 0);
66
67 if (err == 0)
68 return has_vu ? AV_CPU_FLAG_ALTIVEC : 0;
69 return 0;
70 #elif defined(__linux__)
71 // The linux kernel could have the altivec support disabled
72 // even if the cpu has it.
73 int i, ret = 0;
74 int fd = open("/proc/self/auxv", O_RDONLY);
75 unsigned long buf[64] = { 0 };
76 ssize_t count;
77
78 if (fd < 0)
79 return 0;
80
81 while ((count = read(fd, buf, sizeof(buf))) > 0) {
82 for (i = 0; i < count / sizeof(*buf); i += 2) {
83 if (buf[i] == AT_NULL)
84 goto out;
85 if (buf[i] == AT_HWCAP) {
86 if (buf[i + 1] & PPC_FEATURE_HAS_ALTIVEC)
87 ret = AV_CPU_FLAG_ALTIVEC;
88 #ifdef PPC_FEATURE_HAS_VSX
89 if (buf[i + 1] & PPC_FEATURE_HAS_VSX)
90 ret |= AV_CPU_FLAG_VSX;
91 #endif
92 #ifdef PPC_FEATURE_ARCH_2_07
93 if (buf[i + 1] & PPC_FEATURE_HAS_POWER8)
94 ret |= AV_CPU_FLAG_POWER8;
95 #endif
96 goto out;
97 }
98 }
99 }
100
101 out:
102 close(fd);
103 return ret;
104 #elif CONFIG_RUNTIME_CPUDETECT
105 #define PVR_G4_7400 0x000C
106 #define PVR_G5_970 0x0039
107 #define PVR_G5_970FX 0x003C
108 #define PVR_G5_970MP 0x0044
109 #define PVR_G5_970GX 0x0045
110 #define PVR_POWER6 0x003E
111 #define PVR_POWER7 0x003F
112 #define PVR_POWER8 0x004B
113 #define PVR_CELL_PPU 0x0070
114 int ret = 0;
115 int proc_ver;
116 // Support of mfspr PVR emulation added in Linux 2.6.17.
117 __asm__ volatile("mfspr %0, 287" : "=r" (proc_ver));
118 proc_ver >>= 16;
119 if (proc_ver & 0x8000 ||
120 proc_ver == PVR_G4_7400 ||
121 proc_ver == PVR_G5_970 ||
122 proc_ver == PVR_G5_970FX ||
123 proc_ver == PVR_G5_970MP ||
124 proc_ver == PVR_G5_970GX ||
125 proc_ver == PVR_POWER6 ||
126 proc_ver == PVR_POWER7 ||
127 proc_ver == PVR_POWER8 ||
128 proc_ver == PVR_CELL_PPU)
129 ret = AV_CPU_FLAG_ALTIVEC;
130 if (proc_ver == PVR_POWER7 ||
131 proc_ver == PVR_POWER8)
132 ret |= AV_CPU_FLAG_VSX;
133 if (proc_ver == PVR_POWER8)
134 ret |= AV_CPU_FLAG_POWER8;
135
136 return ret;
137 #else
138 // Since we were compiled for AltiVec, just assume we have it
139 // until someone comes up with a proper way (not involving signal hacks).
140 return AV_CPU_FLAG_ALTIVEC;
141 #endif /* __AMIGAOS4__ */
142 #endif /* HAVE_ALTIVEC */
143 return 0;
144 }
145
146 size_t ff_get_cpu_max_align_ppc(void)
147 {
148 int flags = av_get_cpu_flags();
149
150 if (flags & AV_CPU_FLAG_ALTIVEC)
151 return 16;
152 return 8;
153 }