sl@0: /* sl@0: * LIBOIL - Library of Optimized Inner Loops sl@0: * Copyright (c) 2003,2004 David A. Schleef sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR sl@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED sl@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, sl@0: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES sl@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR sl@0: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING sl@0: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE sl@0: * POSSIBILITY OF SUCH DAMAGE. sl@0: */ sl@0: sl@0: #ifdef HAVE_CONFIG_H sl@0: #include "config.h" sl@0: #endif sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #if defined(__linux__) sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifndef PPC_FEATURE_HAS_ALTIVEC sl@0: /* From linux-2.6/include/asm-powerpc/cputable.h */ sl@0: #define PPC_FEATURE_HAS_ALTIVEC 0x10000000 sl@0: #endif sl@0: sl@0: #endif sl@0: sl@0: #if defined(__APPLE__) sl@0: #include sl@0: #include sl@0: #endif sl@0: sl@0: #if defined(__FreeBSD__) sl@0: #include sl@0: #include sl@0: #endif sl@0: sl@0: sl@0: /***** powerpc *****/ sl@0: sl@0: static unsigned long sl@0: oil_profile_stamp_tb(void) sl@0: { sl@0: unsigned long ts; sl@0: __asm__ __volatile__("mftb %0\n" : "=r" (ts)); sl@0: return ts; sl@0: } sl@0: sl@0: #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__) sl@0: static void sl@0: test_altivec (void * ignored) sl@0: { sl@0: asm volatile ("vor v0, v0, v0\n"); sl@0: } sl@0: #endif sl@0: sl@0: #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) sl@0: static void sl@0: oil_check_altivec_sysctl_freebsd (void) sl@0: { sl@0: int ret, av; sl@0: size_t len; sl@0: sl@0: len = sizeof(av); sl@0: ret = sysctlbyname("hw.altivec", &av, &len, NULL, 0); sl@0: if (!ret && av) { sl@0: oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: #if defined(__APPLE__) sl@0: static void sl@0: oil_check_altivec_sysctl_darwin (void) sl@0: { sl@0: int ret, vu; sl@0: size_t len; sl@0: sl@0: len = sizeof(vu); sl@0: ret = sysctlbyname("hw.vectorunit", &vu, &len, NULL, 0); sl@0: if (!ret && vu) { sl@0: oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: #if defined(__linux__) sl@0: static void sl@0: oil_check_altivec_proc_auxv (void) sl@0: { sl@0: static int available = -1; sl@0: int new_avail = 0; sl@0: unsigned long buf[64]; sl@0: ssize_t count; sl@0: int fd, i; sl@0: sl@0: /* Flags already set */ sl@0: if (available != -1) { sl@0: return; sl@0: } sl@0: sl@0: fd = open("/proc/self/auxv", O_RDONLY); sl@0: if (fd < 0) { sl@0: goto out; sl@0: } sl@0: sl@0: more: sl@0: count = read(fd, buf, sizeof(buf)); sl@0: if (count < 0) { sl@0: goto out_close; sl@0: } sl@0: sl@0: for (i=0; i < (count / sizeof(unsigned long)); i += 2) { sl@0: if (buf[i] == AT_HWCAP) { sl@0: new_avail = !!(buf[i+1] & PPC_FEATURE_HAS_ALTIVEC); sl@0: goto out_close; sl@0: } else if (buf[i] == AT_NULL) { sl@0: goto out_close; sl@0: } sl@0: } sl@0: sl@0: if (count == sizeof(buf)) { sl@0: goto more; sl@0: } sl@0: sl@0: out_close: sl@0: close(fd); sl@0: sl@0: out: sl@0: available = new_avail; sl@0: if (available) { sl@0: oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__) sl@0: static void sl@0: oil_check_altivec_fault (void) sl@0: { sl@0: oil_fault_check_enable (); sl@0: if (oil_fault_check_try(test_altivec, NULL)) { sl@0: OIL_DEBUG ("cpu flag altivec"); sl@0: oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC; sl@0: } sl@0: oil_fault_check_disable (); sl@0: } sl@0: #endif sl@0: sl@0: void sl@0: oil_cpu_detect_arch(void) sl@0: { sl@0: #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) sl@0: oil_check_altivec_sysctl_freebsd(); sl@0: #elif defined(__APPLE__) sl@0: oil_check_altivec_sysctl_darwin(); sl@0: #elif defined(__linux__) sl@0: oil_check_altivec_proc_auxv(); sl@0: #else sl@0: oil_check_altivec_fault(); sl@0: #endif sl@0: sl@0: _oil_profile_stamp = oil_profile_stamp_tb; sl@0: } sl@0: sl@0: sl@0: