Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vpx_ports / arm_cpudetect.c
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
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
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
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_EDSP
53   flags |= HAS_EDSP;
54 #endif /* HAVE_EDSP */
55 #if HAVE_MEDIA
56   flags |= HAS_MEDIA;
57 #endif /* HAVE_MEDIA */
58 #if HAVE_NEON || HAVE_NEON_ASM
59   flags |= HAS_NEON;
60 #endif /* HAVE_NEON  || HAVE_NEON_ASM */
61   return flags & mask;
62 }
63
64 #elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */
65 /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
66 #define WIN32_LEAN_AND_MEAN
67 #define WIN32_EXTRA_LEAN
68 #include <windows.h>
69
70 int arm_cpu_caps(void) {
71   int flags;
72   int mask;
73   if (!arm_cpu_env_flags(&flags)) {
74     return flags;
75   }
76   mask = arm_cpu_env_mask();
77   /* MSVC has no inline __asm support for ARM, but it does let you __emit
78    *  instructions via their assembled hex code.
79    * All of these instructions should be essentially nops.
80    */
81 #if HAVE_EDSP
82   if (mask & HAS_EDSP) {
83     __try {
84       /*PLD [r13]*/
85       __emit(0xF5DDF000);
86       flags |= HAS_EDSP;
87     } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
88       /*Ignore exception.*/
89     }
90   }
91 #endif /* HAVE_EDSP */
92 #if HAVE_MEDIA
93   if (mask & HAS_MEDIA)
94     __try {
95       /*SHADD8 r3,r3,r3*/
96       __emit(0xE6333F93);
97       flags |= HAS_MEDIA;
98     } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
99     /*Ignore exception.*/
100   }
101 }
102 #endif /* HAVE_MEDIA */
103 #if HAVE_NEON || HAVE_NEON_ASM
104 if (mask &HAS_NEON) {
105   __try {
106     /*VORR q0,q0,q0*/
107     __emit(0xF2200150);
108     flags |= HAS_NEON;
109   } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
110     /*Ignore exception.*/
111   }
112 }
113 #endif /* HAVE_NEON || HAVE_NEON_ASM */
114 return flags & mask;
115 }
116
117 #elif defined(__ANDROID__) /* end _MSC_VER */
118 #include <cpu-features.h>
119
120 int arm_cpu_caps(void) {
121   int flags;
122   int mask;
123   uint64_t features;
124   if (!arm_cpu_env_flags(&flags)) {
125     return flags;
126   }
127   mask = arm_cpu_env_mask();
128   features = android_getCpuFeatures();
129
130 #if HAVE_EDSP
131   flags |= HAS_EDSP;
132 #endif /* HAVE_EDSP */
133 #if HAVE_MEDIA
134   flags |= HAS_MEDIA;
135 #endif /* HAVE_MEDIA */
136 #if HAVE_NEON || HAVE_NEON_ASM
137   if (features & ANDROID_CPU_ARM_FEATURE_NEON)
138     flags |= HAS_NEON;
139 #endif /* HAVE_NEON || HAVE_NEON_ASM */
140   return flags & mask;
141 }
142
143 #elif defined(__linux__) /* end __ANDROID__ */
144
145 #include <stdio.h>
146
147 int arm_cpu_caps(void) {
148   FILE *fin;
149   int flags;
150   int mask;
151   if (!arm_cpu_env_flags(&flags)) {
152     return flags;
153   }
154   mask = arm_cpu_env_mask();
155   /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
156    *  on Android.
157    * This also means that detection will fail in Scratchbox.
158    */
159   fin = fopen("/proc/cpuinfo", "r");
160   if (fin != NULL) {
161     /* 512 should be enough for anybody (it's even enough for all the flags
162      * that x86 has accumulated... so far).
163      */
164     char buf[512];
165     while (fgets(buf, 511, fin) != NULL) {
166 #if HAVE_EDSP || HAVE_NEON || HAVE_NEON_ASM
167       if (memcmp(buf, "Features", 8) == 0) {
168         char *p;
169 #if HAVE_EDSP
170         p = strstr(buf, " edsp");
171         if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
172           flags |= HAS_EDSP;
173         }
174 #endif /* HAVE_EDSP */
175 #if HAVE_NEON || HAVE_NEON_ASM
176         p = strstr(buf, " neon");
177         if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
178           flags |= HAS_NEON;
179         }
180 #endif /* HAVE_NEON || HAVE_NEON_ASM */
181       }
182 #endif /* HAVE_EDSP || HAVE_NEON || HAVE_NEON_ASM */
183 #if HAVE_MEDIA
184       if (memcmp(buf, "CPU architecture:", 17) == 0) {
185         int version;
186         version = atoi(buf + 17);
187         if (version >= 6) {
188           flags |= HAS_MEDIA;
189         }
190       }
191 #endif /* HAVE_MEDIA */
192     }
193     fclose(fin);
194   }
195   return flags & mask;
196 }
197 #else /* end __linux__ */
198 #error "--enable-runtime-cpu-detect selected, but no CPU detection method " \
199 "available for your platform. Reconfigure with --disable-runtime-cpu-detect."
200 #endif