hadamard: Add 4x4 test.
[aom.git] / aom_ports / arm_cpudetect.c
blob23d3aa503aa6de90109ddcb00df49d0e1ed66ae4
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
12 #include <stdlib.h>
13 #include <string.h>
14 #include "aom_ports/arm.h"
15 #include "config/aom_config.h"
17 #ifdef WINAPI_FAMILY
18 #include <winapifamily.h>
19 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
20 #define getenv(x) NULL
21 #endif
22 #endif
24 static int arm_cpu_env_flags(int *flags) {
25 char *env;
26 env = getenv("AOM_SIMD_CAPS");
27 if (env && *env) {
28 *flags = (int)strtol(env, NULL, 0);
29 return 0;
31 *flags = 0;
32 return -1;
35 static int arm_cpu_env_mask(void) {
36 char *env;
37 env = getenv("AOM_SIMD_CAPS_MASK");
38 return env && *env ? (int)strtol(env, NULL, 0) : ~0;
41 #if !CONFIG_RUNTIME_CPU_DETECT || defined(__APPLE__)
43 int aom_arm_cpu_caps(void) {
44 /* This function should actually be a no-op. There is no way to adjust any of
45 * these because the RTCD tables do not exist: the functions are called
46 * statically */
47 int flags;
48 int mask;
49 if (!arm_cpu_env_flags(&flags)) {
50 return flags;
52 mask = arm_cpu_env_mask();
53 #if HAVE_NEON
54 flags |= HAS_NEON;
55 #endif /* HAVE_NEON */
56 return flags & mask;
59 #elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT || __APPLE__ */
60 /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
61 #define WIN32_LEAN_AND_MEAN
62 #define WIN32_EXTRA_LEAN
63 #include <windows.h>
65 int aom_arm_cpu_caps(void) {
66 int flags;
67 int mask;
68 if (!arm_cpu_env_flags(&flags)) {
69 return flags;
71 mask = arm_cpu_env_mask();
72 /* MSVC has no inline __asm support for ARM, but it does let you __emit
73 * instructions via their assembled hex code.
74 * All of these instructions should be essentially nops.
76 #if HAVE_NEON
77 if (mask & HAS_NEON) {
78 __try {
79 /*VORR q0,q0,q0*/
80 __emit(0xF2200150);
81 flags |= HAS_NEON;
82 } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
83 /*Ignore exception.*/
86 #endif /* HAVE_NEON */
87 return flags & mask;
90 #elif defined(__ANDROID__) /* end _MSC_VER */
91 #include <cpu-features.h>
93 int aom_arm_cpu_caps(void) {
94 int flags;
95 int mask;
96 uint64_t features;
97 if (!arm_cpu_env_flags(&flags)) {
98 return flags;
100 mask = arm_cpu_env_mask();
101 features = android_getCpuFeatures();
103 #if HAVE_NEON
104 if (features & ANDROID_CPU_ARM_FEATURE_NEON) flags |= HAS_NEON;
105 #endif /* HAVE_NEON */
106 return flags & mask;
109 #elif defined(__linux__) /* end __ANDROID__ */
111 #include <stdio.h>
113 int aom_arm_cpu_caps(void) {
114 FILE *fin;
115 int flags;
116 int mask;
117 if (!arm_cpu_env_flags(&flags)) {
118 return flags;
120 mask = arm_cpu_env_mask();
121 /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
122 * on Android.
123 * This also means that detection will fail in Scratchbox.
125 fin = fopen("/proc/cpuinfo", "r");
126 if (fin != NULL) {
127 /* 512 should be enough for anybody (it's even enough for all the flags
128 * that x86 has accumulated... so far).
130 char buf[512];
131 while (fgets(buf, 511, fin) != NULL) {
132 #if HAVE_NEON
133 if (memcmp(buf, "Features", 8) == 0) {
134 char *p;
135 p = strstr(buf, " neon");
136 if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
137 flags |= HAS_NEON;
140 #endif /* HAVE_NEON */
142 fclose(fin);
144 return flags & mask;
146 #else /* end __linux__ */
147 #error \
148 "Runtime CPU detection selected, but no CPU detection method " \
149 "available for your platform. Rerun cmake with -DCONFIG_RUNTIME_CPU_DETECT=0."
150 #endif