os/ossrv/genericopenlibs/liboil/src/liboilcpu-powerpc.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
 * LIBOIL - Library of Optimized Inner Loops
sl@0
     3
 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
sl@0
     4
 * All rights reserved.
sl@0
     5
 *
sl@0
     6
 * Redistribution and use in source and binary forms, with or without
sl@0
     7
 * modification, are permitted provided that the following conditions
sl@0
     8
 * are met:
sl@0
     9
 * 1. Redistributions of source code must retain the above copyright
sl@0
    10
 *    notice, this list of conditions and the following disclaimer.
sl@0
    11
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    12
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    13
 *    documentation and/or other materials provided with the distribution.
sl@0
    14
 * 
sl@0
    15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
sl@0
    16
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
sl@0
    17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    18
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
sl@0
    19
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
sl@0
    20
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
sl@0
    21
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    23
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
sl@0
    24
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
sl@0
    25
 * POSSIBILITY OF SUCH DAMAGE.
sl@0
    26
 */
sl@0
    27
sl@0
    28
#ifdef HAVE_CONFIG_H
sl@0
    29
#include "config.h"
sl@0
    30
#endif
sl@0
    31
#include <liboil/liboilfunction.h>
sl@0
    32
#include <liboil/liboildebug.h>
sl@0
    33
#include <liboil/liboilcpu.h>
sl@0
    34
#include <liboil/liboilfault.h>
sl@0
    35
#include <liboil/liboilutils.h>
sl@0
    36
sl@0
    37
#if defined(__linux__)
sl@0
    38
#include <linux/auxvec.h>
sl@0
    39
#include <sys/types.h>
sl@0
    40
#include <sys/stat.h>
sl@0
    41
#include <fcntl.h>
sl@0
    42
#include <unistd.h>
sl@0
    43
#include <stdio.h>
sl@0
    44
sl@0
    45
#ifndef PPC_FEATURE_HAS_ALTIVEC
sl@0
    46
/* From linux-2.6/include/asm-powerpc/cputable.h */
sl@0
    47
#define PPC_FEATURE_HAS_ALTIVEC 0x10000000
sl@0
    48
#endif
sl@0
    49
sl@0
    50
#endif
sl@0
    51
sl@0
    52
#if defined(__APPLE__)
sl@0
    53
#include <sys/types.h>
sl@0
    54
#include <sys/sysctl.h>
sl@0
    55
#endif
sl@0
    56
sl@0
    57
#if defined(__FreeBSD__)
sl@0
    58
#include <sys/types.h>
sl@0
    59
#include <sys/sysctl.h>
sl@0
    60
#endif
sl@0
    61
sl@0
    62
sl@0
    63
/***** powerpc *****/
sl@0
    64
sl@0
    65
static unsigned long
sl@0
    66
oil_profile_stamp_tb(void)
sl@0
    67
{
sl@0
    68
  unsigned long ts;
sl@0
    69
  __asm__ __volatile__("mftb %0\n" : "=r" (ts));
sl@0
    70
  return ts;
sl@0
    71
}
sl@0
    72
sl@0
    73
#if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__)
sl@0
    74
static void
sl@0
    75
test_altivec (void * ignored)
sl@0
    76
{
sl@0
    77
  asm volatile ("vor v0, v0, v0\n");
sl@0
    78
}
sl@0
    79
#endif
sl@0
    80
sl@0
    81
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
sl@0
    82
static void
sl@0
    83
oil_check_altivec_sysctl_freebsd (void)
sl@0
    84
{
sl@0
    85
  int ret, av;
sl@0
    86
  size_t len;
sl@0
    87
sl@0
    88
  len = sizeof(av);
sl@0
    89
  ret = sysctlbyname("hw.altivec", &av, &len, NULL, 0);
sl@0
    90
  if (!ret && av) {
sl@0
    91
    oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
sl@0
    92
  }
sl@0
    93
}
sl@0
    94
#endif
sl@0
    95
sl@0
    96
#if defined(__APPLE__)
sl@0
    97
static void
sl@0
    98
oil_check_altivec_sysctl_darwin (void)
sl@0
    99
{
sl@0
   100
  int ret, vu;
sl@0
   101
  size_t len;
sl@0
   102
sl@0
   103
  len = sizeof(vu);
sl@0
   104
  ret = sysctlbyname("hw.vectorunit", &vu, &len, NULL, 0);
sl@0
   105
  if (!ret && vu) {
sl@0
   106
    oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
sl@0
   107
  }
sl@0
   108
}
sl@0
   109
#endif
sl@0
   110
sl@0
   111
#if defined(__linux__)
sl@0
   112
static void
sl@0
   113
oil_check_altivec_proc_auxv (void)
sl@0
   114
{
sl@0
   115
  static int available = -1;
sl@0
   116
  int new_avail = 0;
sl@0
   117
  unsigned long buf[64];
sl@0
   118
  ssize_t count;
sl@0
   119
  int fd, i;
sl@0
   120
sl@0
   121
  /* Flags already set */
sl@0
   122
  if (available != -1) {
sl@0
   123
    return;
sl@0
   124
  }
sl@0
   125
sl@0
   126
  fd = open("/proc/self/auxv", O_RDONLY);
sl@0
   127
  if (fd < 0) {
sl@0
   128
    goto out;
sl@0
   129
  }
sl@0
   130
sl@0
   131
more:
sl@0
   132
  count = read(fd, buf, sizeof(buf));
sl@0
   133
  if (count < 0) {
sl@0
   134
    goto out_close;
sl@0
   135
  }
sl@0
   136
sl@0
   137
  for (i=0; i < (count / sizeof(unsigned long)); i += 2) {
sl@0
   138
    if (buf[i] == AT_HWCAP) {
sl@0
   139
      new_avail = !!(buf[i+1] & PPC_FEATURE_HAS_ALTIVEC);
sl@0
   140
      goto out_close;
sl@0
   141
    } else if (buf[i] == AT_NULL) {
sl@0
   142
      goto out_close;
sl@0
   143
    }
sl@0
   144
  }
sl@0
   145
sl@0
   146
  if (count == sizeof(buf)) {
sl@0
   147
    goto more;
sl@0
   148
  }
sl@0
   149
sl@0
   150
out_close:
sl@0
   151
  close(fd);
sl@0
   152
sl@0
   153
out:
sl@0
   154
  available = new_avail;
sl@0
   155
  if (available) {
sl@0
   156
    oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
sl@0
   157
  }
sl@0
   158
}
sl@0
   159
#endif
sl@0
   160
sl@0
   161
#if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__APPLE__) && !defined(__linux__)
sl@0
   162
static void
sl@0
   163
oil_check_altivec_fault (void)
sl@0
   164
{
sl@0
   165
  oil_fault_check_enable ();
sl@0
   166
  if (oil_fault_check_try(test_altivec, NULL)) {
sl@0
   167
    OIL_DEBUG ("cpu flag altivec");
sl@0
   168
    oil_cpu_flags |= OIL_IMPL_FLAG_ALTIVEC;
sl@0
   169
  }
sl@0
   170
  oil_fault_check_disable ();
sl@0
   171
}
sl@0
   172
#endif
sl@0
   173
sl@0
   174
void
sl@0
   175
oil_cpu_detect_arch(void)
sl@0
   176
{
sl@0
   177
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
sl@0
   178
  oil_check_altivec_sysctl_freebsd();
sl@0
   179
#elif defined(__APPLE__)
sl@0
   180
  oil_check_altivec_sysctl_darwin();
sl@0
   181
#elif defined(__linux__)
sl@0
   182
  oil_check_altivec_proc_auxv();
sl@0
   183
#else
sl@0
   184
  oil_check_altivec_fault();
sl@0
   185
#endif
sl@0
   186
sl@0
   187
  _oil_profile_stamp = oil_profile_stamp_tb;
sl@0
   188
}
sl@0
   189
sl@0
   190
sl@0
   191