media/libvpx/vpx_ports/arm_cpudetect.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vpx_ports/arm_cpudetect.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,192 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +#include <stdlib.h>
    1.15 +#include <string.h>
    1.16 +#include "arm.h"
    1.17 +
    1.18 +static int arm_cpu_env_flags(int *flags) {
    1.19 +  char *env;
    1.20 +  env = getenv("VPX_SIMD_CAPS");
    1.21 +  if (env && *env) {
    1.22 +    *flags = (int)strtol(env, NULL, 0);
    1.23 +    return 0;
    1.24 +  }
    1.25 +  *flags = 0;
    1.26 +  return -1;
    1.27 +}
    1.28 +
    1.29 +static int arm_cpu_env_mask(void) {
    1.30 +  char *env;
    1.31 +  env = getenv("VPX_SIMD_CAPS_MASK");
    1.32 +  return env && *env ? (int)strtol(env, NULL, 0) : ~0;
    1.33 +}
    1.34 +
    1.35 +#if !CONFIG_RUNTIME_CPU_DETECT
    1.36 +
    1.37 +int arm_cpu_caps(void) {
    1.38 +  /* This function should actually be a no-op. There is no way to adjust any of
    1.39 +   * these because the RTCD tables do not exist: the functions are called
    1.40 +   * statically */
    1.41 +  int flags;
    1.42 +  int mask;
    1.43 +  if (!arm_cpu_env_flags(&flags)) {
    1.44 +    return flags;
    1.45 +  }
    1.46 +  mask = arm_cpu_env_mask();
    1.47 +#if HAVE_EDSP
    1.48 +  flags |= HAS_EDSP;
    1.49 +#endif /* HAVE_EDSP */
    1.50 +#if HAVE_MEDIA
    1.51 +  flags |= HAS_MEDIA;
    1.52 +#endif /* HAVE_MEDIA */
    1.53 +#if HAVE_NEON
    1.54 +  flags |= HAS_NEON;
    1.55 +#endif /* HAVE_NEON */
    1.56 +  return flags & mask;
    1.57 +}
    1.58 +
    1.59 +#elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */
    1.60 +/*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
    1.61 +#define WIN32_LEAN_AND_MEAN
    1.62 +#define WIN32_EXTRA_LEAN
    1.63 +#include <windows.h>
    1.64 +
    1.65 +int arm_cpu_caps(void) {
    1.66 +  int flags;
    1.67 +  int mask;
    1.68 +  if (!arm_cpu_env_flags(&flags)) {
    1.69 +    return flags;
    1.70 +  }
    1.71 +  mask = arm_cpu_env_mask();
    1.72 +  /* MSVC has no inline __asm support for ARM, but it does let you __emit
    1.73 +   *  instructions via their assembled hex code.
    1.74 +   * All of these instructions should be essentially nops.
    1.75 +   */
    1.76 +#if HAVE_EDSP
    1.77 +  if (mask & HAS_EDSP) {
    1.78 +    __try {
    1.79 +      /*PLD [r13]*/
    1.80 +      __emit(0xF5DDF000);
    1.81 +      flags |= HAS_EDSP;
    1.82 +    } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
    1.83 +      /*Ignore exception.*/
    1.84 +    }
    1.85 +  }
    1.86 +#if HAVE_MEDIA
    1.87 +  if (mask & HAS_MEDIA)
    1.88 +    __try {
    1.89 +      /*SHADD8 r3,r3,r3*/
    1.90 +      __emit(0xE6333F93);
    1.91 +      flags |= HAS_MEDIA;
    1.92 +    } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
    1.93 +    /*Ignore exception.*/
    1.94 +  }
    1.95 +}
    1.96 +#if HAVE_NEON
    1.97 +if (mask &HAS_NEON) {
    1.98 +  __try {
    1.99 +    /*VORR q0,q0,q0*/
   1.100 +    __emit(0xF2200150);
   1.101 +    flags |= HAS_NEON;
   1.102 +  } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
   1.103 +    /*Ignore exception.*/
   1.104 +  }
   1.105 +}
   1.106 +#endif /* HAVE_NEON */
   1.107 +#endif /* HAVE_MEDIA */
   1.108 +#endif /* HAVE_EDSP */
   1.109 +return flags & mask;
   1.110 +}
   1.111 +
   1.112 +#elif defined(__ANDROID__) /* end _MSC_VER */
   1.113 +#include <cpu-features.h>
   1.114 +
   1.115 +int arm_cpu_caps(void) {
   1.116 +  int flags;
   1.117 +  int mask;
   1.118 +  uint64_t features;
   1.119 +  if (!arm_cpu_env_flags(&flags)) {
   1.120 +    return flags;
   1.121 +  }
   1.122 +  mask = arm_cpu_env_mask();
   1.123 +  features = android_getCpuFeatures();
   1.124 +
   1.125 +#if HAVE_EDSP
   1.126 +  flags |= HAS_EDSP;
   1.127 +#endif /* HAVE_EDSP */
   1.128 +#if HAVE_MEDIA
   1.129 +  flags |= HAS_MEDIA;
   1.130 +#endif /* HAVE_MEDIA */
   1.131 +#if HAVE_NEON
   1.132 +  if (features & ANDROID_CPU_ARM_FEATURE_NEON)
   1.133 +    flags |= HAS_NEON;
   1.134 +#endif /* HAVE_NEON */
   1.135 +  return flags & mask;
   1.136 +}
   1.137 +
   1.138 +#elif defined(__linux__) /* end __ANDROID__ */
   1.139 +
   1.140 +#include <stdio.h>
   1.141 +
   1.142 +int arm_cpu_caps(void) {
   1.143 +  FILE *fin;
   1.144 +  int flags;
   1.145 +  int mask;
   1.146 +  if (!arm_cpu_env_flags(&flags)) {
   1.147 +    return flags;
   1.148 +  }
   1.149 +  mask = arm_cpu_env_mask();
   1.150 +  /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
   1.151 +   *  on Android.
   1.152 +   * This also means that detection will fail in Scratchbox.
   1.153 +   */
   1.154 +  fin = fopen("/proc/cpuinfo", "r");
   1.155 +  if (fin != NULL) {
   1.156 +    /* 512 should be enough for anybody (it's even enough for all the flags
   1.157 +     * that x86 has accumulated... so far).
   1.158 +     */
   1.159 +    char buf[512];
   1.160 +    while (fgets(buf, 511, fin) != NULL) {
   1.161 +#if HAVE_EDSP || HAVE_NEON
   1.162 +      if (memcmp(buf, "Features", 8) == 0) {
   1.163 +        char *p;
   1.164 +#if HAVE_EDSP
   1.165 +        p = strstr(buf, " edsp");
   1.166 +        if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
   1.167 +          flags |= HAS_EDSP;
   1.168 +        }
   1.169 +#if HAVE_NEON
   1.170 +        p = strstr(buf, " neon");
   1.171 +        if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
   1.172 +          flags |= HAS_NEON;
   1.173 +        }
   1.174 +#endif /* HAVE_NEON */
   1.175 +#endif /* HAVE_EDSP */
   1.176 +      }
   1.177 +#endif /* HAVE_EDSP || HAVE_NEON */
   1.178 +#if HAVE_MEDIA
   1.179 +      if (memcmp(buf, "CPU architecture:", 17) == 0) {
   1.180 +        int version;
   1.181 +        version = atoi(buf + 17);
   1.182 +        if (version >= 6) {
   1.183 +          flags |= HAS_MEDIA;
   1.184 +        }
   1.185 +      }
   1.186 +#endif /* HAVE_MEDIA */
   1.187 +    }
   1.188 +    fclose(fin);
   1.189 +  }
   1.190 +  return flags & mask;
   1.191 +}
   1.192 +#else /* end __linux__ */
   1.193 +#error "--enable-runtime-cpu-detect selected, but no CPU detection method " \
   1.194 +"available for your platform. Reconfigure with --disable-runtime-cpu-detect."
   1.195 +#endif

mercurial