Update contrib.
2 * LIBOIL - Library of Optimized Inner Loops
3 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
31 #include <liboil/liboilfunction.h>
32 #include <liboil/liboildebug.h>
33 #include <liboil/liboilcpu.h>
34 #include <liboil/liboilfault.h>
35 #include <liboil/liboilutils.h>
37 #if defined(__linux__)
38 #include <linux/auxvec.h>
39 #include <sys/types.h>
45 #ifndef PPC_FEATURE_HAS_ALTIVEC
46 /* From linux-2.6/include/asm-powerpc/cputable.h */
47 #define PPC_FEATURE_HAS_ALTIVEC 0x10000000
52 #if defined(__APPLE__)
53 #include <sys/types.h>
54 #include <sys/sysctl.h>
57 #if defined(__FreeBSD__)
58 #include <sys/types.h>
59 #include <sys/sysctl.h>
66 oil_profile_stamp_tb(void)
69 __asm__ __volatile__("mftb %0\n" : "=r" (ts));
73 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__)
75 test_altivec (void * ignored)
77 asm volatile ("vor v0, v0, v0\n");
81 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
83 oil_check_altivec_sysctl_freebsd (void)
89 ret = sysctlbyname("hw.altivec", &av, &len, NULL, 0);
91 oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
96 #if defined(__APPLE__)
98 oil_check_altivec_sysctl_darwin (void)
104 ret = sysctlbyname("hw.vectorunit", &vu, &len, NULL, 0);
106 oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
111 #if defined(__linux__)
113 oil_check_altivec_proc_auxv (void)
115 static int available = -1;
117 unsigned long buf[64];
121 /* Flags already set */
122 if (available != -1) {
126 fd = open("/proc/self/auxv", O_RDONLY);
132 count = read(fd, buf, sizeof(buf));
137 for (i=0; i < (count / sizeof(unsigned long)); i += 2) {
138 if (buf[i] == AT_HWCAP) {
139 new_avail = !!(buf[i+1] & PPC_FEATURE_HAS_ALTIVEC);
141 } else if (buf[i] == AT_NULL) {
146 if (count == sizeof(buf)) {
154 available = new_avail;
156 oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
161 #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__)
163 oil_check_altivec_fault (void)
165 oil_fault_check_enable ();
166 if (oil_fault_check_try(test_altivec, NULL)) {
167 OIL_DEBUG ("cpu flag altivec");
168 oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
170 oil_fault_check_disable ();
175 oil_cpu_detect_arch(void)
177 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
178 oil_check_altivec_sysctl_freebsd();
179 #elif defined(__APPLE__)
180 oil_check_altivec_sysctl_darwin();
181 #elif defined(__linux__)
182 oil_check_altivec_proc_auxv();
184 oil_check_altivec_fault();
187 _oil_profile_stamp = oil_profile_stamp_tb;