1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <stdlib.h>
12 #include <string.h>
13 #include "vpx_ports/arm.h"
14 #include "./vpx_config.h"
15 
16 #ifdef WINAPI_FAMILY
17 #include <winapifamily.h>
18 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
19 #define getenv(x) NULL
20 #endif
21 #endif
22 
arm_cpu_env_flags(int * flags)23 static int arm_cpu_env_flags(int *flags) {
24   char *env;
25   env = getenv("VPX_SIMD_CAPS");
26   if (env && *env) {
27     *flags = (int)strtol(env, NULL, 0);
28     return 0;
29   }
30   *flags = 0;
31   return -1;
32 }
33 
arm_cpu_env_mask(void)34 static int arm_cpu_env_mask(void) {
35   char *env;
36   env = getenv("VPX_SIMD_CAPS_MASK");
37   return env && *env ? (int)strtol(env, NULL, 0) : ~0;
38 }
39 
40 #if !CONFIG_RUNTIME_CPU_DETECT
41 
arm_cpu_caps(void)42 int arm_cpu_caps(void) {
43   /* This function should actually be a no-op. There is no way to adjust any of
44    * these because the RTCD tables do not exist: the functions are called
45    * statically */
46   int flags;
47   int mask;
48   if (!arm_cpu_env_flags(&flags)) {
49     return flags;
50   }
51   mask = arm_cpu_env_mask();
52 #if HAVE_MEDIA
53   flags |= HAS_MEDIA;
54 #endif /* HAVE_MEDIA */
55 #if HAVE_NEON || HAVE_NEON_ASM
56   flags |= HAS_NEON;
57 #endif /* HAVE_NEON  || HAVE_NEON_ASM */
58   return flags & mask;
59 }
60 
61 #elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */
62 /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
63 #define WIN32_LEAN_AND_MEAN
64 #define WIN32_EXTRA_LEAN
65 #include <windows.h>
66 
arm_cpu_caps(void)67 int arm_cpu_caps(void) {
68   int flags;
69   int mask;
70   if (!arm_cpu_env_flags(&flags)) {
71     return flags;
72   }
73   mask = arm_cpu_env_mask();
74   /* MSVC has no inline __asm support for ARM, but it does let you __emit
75    *  instructions via their assembled hex code.
76    * All of these instructions should be essentially nops.
77    */
78 #if HAVE_MEDIA
79   if (mask & HAS_MEDIA)
80     __try {
81       /*SHADD8 r3,r3,r3*/
82       __emit(0xE6333F93);
83       flags |= HAS_MEDIA;
84     } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
85     /*Ignore exception.*/
86   }
87 }
88 #endif /* HAVE_MEDIA */
89 #if HAVE_NEON || HAVE_NEON_ASM
90 if (mask &HAS_NEON) {
91   __try {
92     /*VORR q0,q0,q0*/
93     __emit(0xF2200150);
94     flags |= HAS_NEON;
GetExceptionCode()95   } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
96     /*Ignore exception.*/
97   }
98 }
99 #endif /* HAVE_NEON || HAVE_NEON_ASM */
100 return flags & mask;
101 }
102 
103 #elif defined(__ANDROID__) /* end _MSC_VER */
104 #include <cpu-features.h>
105 
106 int arm_cpu_caps(void) {
107   int flags;
108   int mask;
109   uint64_t features;
110   if (!arm_cpu_env_flags(&flags)) {
111     return flags;
112   }
113   mask = arm_cpu_env_mask();
114   features = android_getCpuFeatures();
115 
116 #if HAVE_MEDIA
117   flags |= HAS_MEDIA;
118 #endif /* HAVE_MEDIA */
119 #if HAVE_NEON || HAVE_NEON_ASM
120   if (features & ANDROID_CPU_ARM_FEATURE_NEON)
121     flags |= HAS_NEON;
122 #endif /* HAVE_NEON || HAVE_NEON_ASM */
123   return flags & mask;
124 }
125 
126 #elif defined(__linux__) /* end __ANDROID__ */
127 
128 #include <stdio.h>
129 
130 int arm_cpu_caps(void) {
131   FILE *fin;
132   int flags;
133   int mask;
134   if (!arm_cpu_env_flags(&flags)) {
135     return flags;
136   }
137   mask = arm_cpu_env_mask();
138   /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
139    *  on Android.
140    * This also means that detection will fail in Scratchbox.
141    */
142   fin = fopen("/proc/cpuinfo", "r");
143   if (fin != NULL) {
144     /* 512 should be enough for anybody (it's even enough for all the flags
145      * that x86 has accumulated... so far).
146      */
147     char buf[512];
148     while (fgets(buf, 511, fin) != NULL) {
149 #if HAVE_NEON || HAVE_NEON_ASM
150       if (memcmp(buf, "Features", 8) == 0) {
151         char *p;
152         p = strstr(buf, " neon");
153         if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
154           flags |= HAS_NEON;
155         }
156       }
157 #endif /* HAVE_NEON || HAVE_NEON_ASM */
158 #if HAVE_MEDIA
159       if (memcmp(buf, "CPU architecture:", 17) == 0) {
160         int version;
161         version = atoi(buf + 17);
162         if (version >= 6) {
163           flags |= HAS_MEDIA;
164         }
165       }
166 #endif /* HAVE_MEDIA */
167     }
168     fclose(fin);
169   }
170   return flags & mask;
171 }
172 #else /* end __linux__ */
173 #error "--enable-runtime-cpu-detect selected, but no CPU detection method " \
174 "available for your platform. Reconfigure with --disable-runtime-cpu-detect."
175 #endif
176