cpu: split flag checks per arch in av_cpu_max_align()
[libav.git] / libavutil / ppc / cpu.c
CommitLineData
89523bee 1/*
2912e87a 2 * This file is part of Libav.
89523bee 3 *
2912e87a 4 * Libav is free software; you can redistribute it and/or
89523bee
LB
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,
89523bee
LB
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
89523bee
LB
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
89523bee
LB
19#ifdef __APPLE__
20#include <sys/sysctl.h>
baa94563
LB
21#elif defined(__linux__)
22#include <asm/cputable.h>
23#include <linux/auxvec.h>
24#include <fcntl.h>
25#include <unistd.h>
67a7e4db 26#elif defined(__OpenBSD__)
acce61a7
B
27#include <sys/param.h>
28#include <sys/sysctl.h>
29#include <machine/cpu.h>
67a7e4db 30#elif defined(__AMIGAOS4__)
89523bee
LB
31#include <exec/exec.h>
32#include <interfaces/exec.h>
33#include <proto/exec.h>
89523bee
LB
34#endif /* __APPLE__ */
35
9275438a 36#include "libavutil/cpu.h"
b78b10c4 37#include "libavutil/cpu_internal.h"
b8178661 38#include "config.h"
b8178661 39
89523bee 40/**
8556ea03
DB
41 * This function MAY rely on signal() or fork() in order to make sure AltiVec
42 * is present.
89523bee 43 */
9275438a 44int ff_get_cpu_flags_ppc(void)
89523bee 45{
ccf22d3e 46#if HAVE_ALTIVEC
89523bee
LB
47#ifdef __AMIGAOS4__
48 ULONG result = 0;
49 extern struct ExecIFace *IExec;
50
51 IExec->GetCPUInfoTags(GCIT_VectorUnit, &result, TAG_DONE);
ccf22d3e
SS
52 if (result == VECTORTYPE_ALTIVEC)
53 return AV_CPU_FLAG_ALTIVEC;
89523bee 54 return 0;
acce61a7
B
55#elif defined(__APPLE__) || defined(__OpenBSD__)
56#ifdef __OpenBSD__
57 int sels[2] = {CTL_MACHDEP, CPU_ALTIVEC};
58#else
89523bee 59 int sels[2] = {CTL_HW, HW_VECTORUNIT};
acce61a7 60#endif
89523bee
LB
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
ccf22d3e
SS
67 if (err == 0)
68 return has_vu ? AV_CPU_FLAG_ALTIVEC : 0;
89523bee 69 return 0;
baa94563
LB
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;
7d07ee5a
LB
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
baa94563
LB
96 goto out;
97 }
98 }
99 }
100
101out:
102 close(fd);
103 return ret;
e90f5b5a 104#elif CONFIG_RUNTIME_CPUDETECT
3058872c
LB
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
7d07ee5a 114 int ret = 0;
b8659d94 115 int proc_ver;
8556ea03 116 // Support of mfspr PVR emulation added in Linux 2.6.17.
be449fca 117 __asm__ volatile("mfspr %0, 287" : "=r" (proc_ver));
b8659d94
RD
118 proc_ver >>= 16;
119 if (proc_ver & 0x8000 ||
3058872c
LB
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)
7d07ee5a
LB
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;
89523bee 137#else
8556ea03 138 // Since we were compiled for AltiVec, just assume we have it
d9a5dab2 139 // until someone comes up with a proper way (not involving signal hacks).
ccf22d3e 140 return AV_CPU_FLAG_ALTIVEC;
89523bee 141#endif /* __AMIGAOS4__ */
ccf22d3e
SS
142#endif /* HAVE_ALTIVEC */
143 return 0;
89523bee 144}
3d828c9f
JA
145
146size_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}