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 */
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
22 # if !defined(MOZILLA_PRESUME_EDSP)
26 # if defined(MOZILLA_MAY_SUPPORT_EDSP)
33 __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION
)
40 # endif // !MOZILLA_PRESUME_EDSP
42 # if !defined(MOZILLA_PRESUME_ARMV6)
46 # if defined(MOZILLA_MAY_SUPPORT_ARMV6)
53 __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION
)
60 # endif // !MOZILLA_PRESUME_ARMV6
62 # if !defined(MOZILLA_PRESUME_ARMV7)
66 # if defined(MOZILLA_MAY_SUPPORT_ARMV7)
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
76 __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION
)
83 # endif // !MOZILLA_PRESUME_ARMV7
85 # if !defined(MOZILLA_PRESUME_NEON)
89 # if defined(MOZILLA_MAY_SUPPORT_NEON)
96 __except(GetExceptionCode()==EXCEPTION_ILLEGAL_INSTRUCTION
)
103 # endif // !MOZILLA_PRESUME_NEON
105 # elif defined(__linux__) || defined(ANDROID)
111 MOZILLA_HAS_EDSP_FLAG
=1,
112 MOZILLA_HAS_ARMV6_FLAG
=2,
113 MOZILLA_HAS_ARMV7_FLAG
=4,
114 MOZILLA_HAS_NEON_FLAG
=8
118 get_arm_cpu_flags(void)
122 bool armv6_processor
= false;
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");
132 /*512 should be enough for anybody (it's even enough for all the flags that
133 x86 has accumulated... so far).*/
135 while (fgets(buf
, 511, fin
) != nullptr)
137 if (memcmp(buf
, "Features", 8) == 0)
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)
150 version
= atoi(buf
+ 17);
152 flags
|= MOZILLA_HAS_ARMV6_FLAG
;
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
165 if (memcmp(buf
, "Processor\t:", 11) == 0) {
166 if (strstr(buf
, "(v6l)") != 0) {
167 armv6_processor
= true;
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
;
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)
189 return (arm_cpu_flags
& MOZILLA_HAS_EDSP_FLAG
) != 0;
193 # if !defined(MOZILLA_PRESUME_ARMV6)
197 return (arm_cpu_flags
& MOZILLA_HAS_ARMV6_FLAG
) != 0;
201 # if !defined(MOZILLA_PRESUME_ARMV7)
205 return (arm_cpu_flags
& MOZILLA_HAS_ARMV7_FLAG
) != 0;
209 # if !defined(MOZILLA_PRESUME_NEON)
213 return (arm_cpu_flags
& MOZILLA_HAS_NEON_FLAG
) != 0;
217 # endif // defined(__linux__) || defined(ANDROID)
220 namespace arm_private
{
221 # if !defined(MOZILLA_PRESUME_EDSP)
222 bool edsp_enabled
= check_edsp();
224 # if !defined(MOZILLA_PRESUME_ARMV6)
225 bool armv6_enabled
= check_armv6();
227 # if !defined(MOZILLA_PRESUME_ARMV7)
228 bool armv7_enabled
= check_armv7();
230 # if !defined(MOZILLA_PRESUME_NEON)
231 bool neon_enabled
= check_neon();
233 } // namespace arm_private
234 } // namespace mozilla
236 #endif // MOZILLA_ARM_HAVE_CPUID_DETECTION