merge mozilla-aurora to b2g44 a=merge
[gecko.git] / mozglue / build / arm.cpp
blobdff466a16e8ceb8436223f4a413f5b28f5a85733
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /* compile-time and runtime tests for whether to use various ARM extensions */
7 #include "arm.h"
9 #if defined(MOZILLA_ARM_HAVE_CPUID_DETECTION)
11 // arm.h has parallel #ifs which declare MOZILLA_ARM_HAVE_CPUID_DETECTION.
12 // We don't check it here so that we get compile errors if it's defined, but
13 // we don't compile one of these detection methods. The detection code here is
14 // based on the CPU detection in libtheora.
16 # if defined(_MSC_VER)
17 //For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.
18 # define WIN32_LEAN_AND_MEAN
19 # define WIN32_EXTRA_LEAN
20 # include <windows.h>
22 # if !defined(MOZILLA_PRESUME_EDSP)
23 static bool
24 check_edsp(void)
26 # if defined(MOZILLA_MAY_SUPPORT_EDSP)
27 __try
29 //PLD [r13]
30 __emit(0xF5DDF000);
31 return true;
33 __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION)
35 //Ignore exception.
37 # endif
38 return false;
40 # endif // !MOZILLA_PRESUME_EDSP
42 # if !defined(MOZILLA_PRESUME_ARMV6)
43 static bool
44 check_armv6(void)
46 # if defined(MOZILLA_MAY_SUPPORT_ARMV6)
47 __try
49 //SHADD8 r3,r3,r3
50 __emit(0xE6333F93);
51 return true;
53 __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION)
55 //Ignore exception.
57 # endif
58 return false;
60 # endif // !MOZILLA_PRESUME_ARMV6
62 # if !defined(MOZILLA_PRESUME_ARMV7)
63 static bool
64 check_armv7(void)
66 # if defined(MOZILLA_MAY_SUPPORT_ARMV7)
67 __try
69 // ARMv7 DMB (Data Memory Barrier) for stores (DMB.ST)
70 // The Data Memory Barrier existed before ARMv7 as a
71 // cp15 operation, but ARMv7 introduced a dedicated
72 // instruction, DMB.
73 emit(0xF57FF05E);
74 return true;
76 __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION)
78 //Ignore exception.
80 # endif
81 return false;
83 # endif // !MOZILLA_PRESUME_ARMV7
85 # if !defined(MOZILLA_PRESUME_NEON)
86 static bool
87 check_neon(void)
89 # if defined(MOZILLA_MAY_SUPPORT_NEON)
90 __try
92 //VORR q0,q0,q0
93 __emit(0xF2200150);
94 return true;
96 __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION)
98 //Ignore exception.
100 # endif
101 return false;
103 # endif // !MOZILLA_PRESUME_NEON
105 # elif defined(__linux__) || defined(ANDROID)
106 # include <stdio.h>
107 # include <stdlib.h>
108 # include <string.h>
110 enum{
111 MOZILLA_HAS_EDSP_FLAG=1,
112 MOZILLA_HAS_ARMV6_FLAG=2,
113 MOZILLA_HAS_ARMV7_FLAG=4,
114 MOZILLA_HAS_NEON_FLAG=8
117 static unsigned
118 get_arm_cpu_flags(void)
120 unsigned flags;
121 FILE *fin;
122 bool armv6_processor = false;
123 flags = 0;
124 /*Reading /proc/self/auxv would be easier, but that doesn't work reliably on
125 Android. This also means that detection will fail in Scratchbox, which is
126 desirable, as NEON does not work in the qemu shipped with the Maemo 5 SDK.
127 I don't know if /proc/self/auxv would do any better in that case, anyway,
128 or if it would return random flags from the host CPU.*/
129 fin = fopen ("/proc/cpuinfo","r");
130 if (fin != nullptr)
132 /*512 should be enough for anybody (it's even enough for all the flags that
133 x86 has accumulated... so far).*/
134 char buf[512];
135 while (fgets(buf, 511, fin) != nullptr)
137 if (memcmp(buf, "Features", 8) == 0)
139 char *p;
140 p = strstr(buf, " edsp");
141 if (p != nullptr && (p[5] == ' ' || p[5] == '\n'))
142 flags |= MOZILLA_HAS_EDSP_FLAG;
143 p = strstr(buf, " neon");
144 if( p != nullptr && (p[5] == ' ' || p[5] == '\n'))
145 flags |= MOZILLA_HAS_NEON_FLAG;
147 if (memcmp(buf, "CPU architecture:", 17) == 0)
149 int version;
150 version = atoi(buf + 17);
151 if (version >= 6)
152 flags |= MOZILLA_HAS_ARMV6_FLAG;
153 if (version >= 7)
154 flags |= MOZILLA_HAS_ARMV7_FLAG;
156 /* media/webrtc/trunk/src/system_wrappers/source/cpu_features_arm.c
157 * Unfortunately, it seems that certain ARMv6-based CPUs
158 * report an incorrect architecture number of 7!
160 * We try to correct this by looking at the 'elf_format'
161 * field reported by the 'Processor' field, which is of the
162 * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
163 * an ARMv6-one.
165 if (memcmp(buf, "Processor\t:", 11) == 0) {
166 if (strstr(buf, "(v6l)") != 0) {
167 armv6_processor = true;
171 fclose(fin);
173 if (armv6_processor) {
174 // ARMv6 pretending to be ARMv7? clear flag
175 if (flags & MOZILLA_HAS_ARMV7_FLAG) {
176 flags &= ~MOZILLA_HAS_ARMV7_FLAG;
179 return flags;
182 // Cache a local copy so we only have to read /proc/cpuinfo once.
183 static unsigned arm_cpu_flags = get_arm_cpu_flags();
185 # if !defined(MOZILLA_PRESUME_EDSP)
186 static bool
187 check_edsp(void)
189 return (arm_cpu_flags & MOZILLA_HAS_EDSP_FLAG) != 0;
191 # endif
193 # if !defined(MOZILLA_PRESUME_ARMV6)
194 static bool
195 check_armv6(void)
197 return (arm_cpu_flags & MOZILLA_HAS_ARMV6_FLAG) != 0;
199 # endif
201 # if !defined(MOZILLA_PRESUME_ARMV7)
202 static bool
203 check_armv7(void)
205 return (arm_cpu_flags & MOZILLA_HAS_ARMV7_FLAG) != 0;
207 # endif
209 # if !defined(MOZILLA_PRESUME_NEON)
210 static bool
211 check_neon(void)
213 return (arm_cpu_flags & MOZILLA_HAS_NEON_FLAG) != 0;
215 # endif
217 # endif // defined(__linux__) || defined(ANDROID)
219 namespace mozilla {
220 namespace arm_private {
221 # if !defined(MOZILLA_PRESUME_EDSP)
222 bool edsp_enabled = check_edsp();
223 # endif
224 # if !defined(MOZILLA_PRESUME_ARMV6)
225 bool armv6_enabled = check_armv6();
226 # endif
227 # if !defined(MOZILLA_PRESUME_ARMV7)
228 bool armv7_enabled = check_armv7();
229 # endif
230 # if !defined(MOZILLA_PRESUME_NEON)
231 bool neon_enabled = check_neon();
232 # endif
233 } // namespace arm_private
234 } // namespace mozilla
236 #endif // MOZILLA_ARM_HAVE_CPUID_DETECTION