os/ossrv/genericopenlibs/liboil/src/liboilcpu-powerpc.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/liboil/src/liboilcpu-powerpc.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,191 @@
     1.4 +/*
     1.5 + * LIBOIL - Library of Optimized Inner Loops
     1.6 + * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
     1.7 + * All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions
    1.11 + * are met:
    1.12 + * 1. Redistributions of source code must retain the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer.
    1.14 + * 2. Redistributions in binary form must reproduce the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer in the
    1.16 + *    documentation and/or other materials provided with the distribution.
    1.17 + * 
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    1.20 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.21 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    1.22 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.23 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.24 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.25 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.26 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    1.27 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.28 + * POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +
    1.31 +#ifdef HAVE_CONFIG_H
    1.32 +#include "config.h"
    1.33 +#endif
    1.34 +#include <liboil/liboilfunction.h>
    1.35 +#include <liboil/liboildebug.h>
    1.36 +#include <liboil/liboilcpu.h>
    1.37 +#include <liboil/liboilfault.h>
    1.38 +#include <liboil/liboilutils.h>
    1.39 +
    1.40 +#if defined(__linux__)
    1.41 +#include <linux/auxvec.h>
    1.42 +#include <sys/types.h>
    1.43 +#include <sys/stat.h>
    1.44 +#include <fcntl.h>
    1.45 +#include <unistd.h>
    1.46 +#include <stdio.h>
    1.47 +
    1.48 +#ifndef PPC_FEATURE_HAS_ALTIVEC
    1.49 +/* From linux-2.6/include/asm-powerpc/cputable.h */
    1.50 +#define PPC_FEATURE_HAS_ALTIVEC 0x10000000
    1.51 +#endif
    1.52 +
    1.53 +#endif
    1.54 +
    1.55 +#if defined(__APPLE__)
    1.56 +#include <sys/types.h>
    1.57 +#include <sys/sysctl.h>
    1.58 +#endif
    1.59 +
    1.60 +#if defined(__FreeBSD__)
    1.61 +#include <sys/types.h>
    1.62 +#include <sys/sysctl.h>
    1.63 +#endif
    1.64 +
    1.65 +
    1.66 +/***** powerpc *****/
    1.67 +
    1.68 +static unsigned long
    1.69 +oil_profile_stamp_tb(void)
    1.70 +{
    1.71 +  unsigned long ts;
    1.72 +  __asm__ __volatile__("mftb %0\n" : "=r" (ts));
    1.73 +  return ts;
    1.74 +}
    1.75 +
    1.76 +#if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__)
    1.77 +static void
    1.78 +test_altivec (void * ignored)
    1.79 +{
    1.80 +  asm volatile ("vor v0, v0, v0\n");
    1.81 +}
    1.82 +#endif
    1.83 +
    1.84 +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
    1.85 +static void
    1.86 +oil_check_altivec_sysctl_freebsd (void)
    1.87 +{
    1.88 +  int ret, av;
    1.89 +  size_t len;
    1.90 +
    1.91 +  len = sizeof(av);
    1.92 +  ret = sysctlbyname("hw.altivec", &av, &len, NULL, 0);
    1.93 +  if (!ret && av) {
    1.94 +    oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
    1.95 +  }
    1.96 +}
    1.97 +#endif
    1.98 +
    1.99 +#if defined(__APPLE__)
   1.100 +static void
   1.101 +oil_check_altivec_sysctl_darwin (void)
   1.102 +{
   1.103 +  int ret, vu;
   1.104 +  size_t len;
   1.105 +
   1.106 +  len = sizeof(vu);
   1.107 +  ret = sysctlbyname("hw.vectorunit", &vu, &len, NULL, 0);
   1.108 +  if (!ret && vu) {
   1.109 +    oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
   1.110 +  }
   1.111 +}
   1.112 +#endif
   1.113 +
   1.114 +#if defined(__linux__)
   1.115 +static void
   1.116 +oil_check_altivec_proc_auxv (void)
   1.117 +{
   1.118 +  static int available = -1;
   1.119 +  int new_avail = 0;
   1.120 +  unsigned long buf[64];
   1.121 +  ssize_t count;
   1.122 +  int fd, i;
   1.123 +
   1.124 +  /* Flags already set */
   1.125 +  if (available != -1) {
   1.126 +    return;
   1.127 +  }
   1.128 +
   1.129 +  fd = open("/proc/self/auxv", O_RDONLY);
   1.130 +  if (fd < 0) {
   1.131 +    goto out;
   1.132 +  }
   1.133 +
   1.134 +more:
   1.135 +  count = read(fd, buf, sizeof(buf));
   1.136 +  if (count < 0) {
   1.137 +    goto out_close;
   1.138 +  }
   1.139 +
   1.140 +  for (i=0; i < (count / sizeof(unsigned long)); i += 2) {
   1.141 +    if (buf[i] == AT_HWCAP) {
   1.142 +      new_avail = !!(buf[i+1] & PPC_FEATURE_HAS_ALTIVEC);
   1.143 +      goto out_close;
   1.144 +    } else if (buf[i] == AT_NULL) {
   1.145 +      goto out_close;
   1.146 +    }
   1.147 +  }
   1.148 +
   1.149 +  if (count == sizeof(buf)) {
   1.150 +    goto more;
   1.151 +  }
   1.152 +
   1.153 +out_close:
   1.154 +  close(fd);
   1.155 +
   1.156 +out:
   1.157 +  available = new_avail;
   1.158 +  if (available) {
   1.159 +    oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
   1.160 +  }
   1.161 +}
   1.162 +#endif
   1.163 +
   1.164 +#if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__)
   1.165 +static void
   1.166 +oil_check_altivec_fault (void)
   1.167 +{
   1.168 +  oil_fault_check_enable ();
   1.169 +  if (oil_fault_check_try(test_altivec, NULL)) {
   1.170 +    OIL_DEBUG ("cpu flag altivec");
   1.171 +    oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
   1.172 +  }
   1.173 +  oil_fault_check_disable ();
   1.174 +}
   1.175 +#endif
   1.176 +
   1.177 +void
   1.178 +oil_cpu_detect_arch(void)
   1.179 +{
   1.180 +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
   1.181 +  oil_check_altivec_sysctl_freebsd();
   1.182 +#elif defined(__APPLE__)
   1.183 +  oil_check_altivec_sysctl_darwin();
   1.184 +#elif defined(__linux__)
   1.185 +  oil_check_altivec_proc_auxv();
   1.186 +#else
   1.187 +  oil_check_altivec_fault();
   1.188 +#endif
   1.189 +
   1.190 +  _oil_profile_stamp = oil_profile_stamp_tb;
   1.191 +}
   1.192 +
   1.193 +
   1.194 +