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(__linux__) || defined(ANDROID)
22 MOZILLA_HAS_EDSP_FLAG
= 1,
23 MOZILLA_HAS_ARMV6_FLAG
= 2,
24 MOZILLA_HAS_ARMV7_FLAG
= 4,
25 MOZILLA_HAS_NEON_FLAG
= 8
28 static unsigned get_arm_cpu_flags(void) {
31 bool armv6_processor
= false;
33 /*Reading /proc/self/auxv would be easier, but that doesn't work reliably on
34 Android. This also means that detection will fail in Scratchbox, which is
35 desirable, as NEON does not work in the qemu shipped with the Maemo 5 SDK.
36 I don't know if /proc/self/auxv would do any better in that case, anyway,
37 or if it would return random flags from the host CPU.*/
38 fin
= fopen("/proc/cpuinfo", "r");
40 /*512 should be enough for anybody (it's even enough for all the flags that
41 x86 has accumulated... so far).*/
43 while (fgets(buf
, 511, fin
) != nullptr) {
44 if (memcmp(buf
, "Features", 8) == 0) {
46 p
= strstr(buf
, " edsp");
47 if (p
!= nullptr && (p
[5] == ' ' || p
[5] == '\n'))
48 flags
|= MOZILLA_HAS_EDSP_FLAG
;
49 p
= strstr(buf
, " neon");
50 if (p
!= nullptr && (p
[5] == ' ' || p
[5] == '\n'))
51 flags
|= MOZILLA_HAS_NEON_FLAG
;
53 if (memcmp(buf
, "CPU architecture:", 17) == 0) {
55 version
= atoi(buf
+ 17);
56 if (version
>= 6) flags
|= MOZILLA_HAS_ARMV6_FLAG
;
57 if (version
>= 7) flags
|= MOZILLA_HAS_ARMV7_FLAG
;
59 /* media/webrtc/trunk/src/system_wrappers/source/cpu_features_arm.c
60 * Unfortunately, it seems that certain ARMv6-based CPUs
61 * report an incorrect architecture number of 7!
63 * We try to correct this by looking at the 'elf_format'
64 * field reported by the 'Processor' field, which is of the
65 * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
68 if (memcmp(buf
, "Processor\t:", 11) == 0) {
69 if (strstr(buf
, "(v6l)") != 0) {
70 armv6_processor
= true;
76 if (armv6_processor
) {
77 // ARMv6 pretending to be ARMv7? clear flag
78 if (flags
& MOZILLA_HAS_ARMV7_FLAG
) {
79 flags
&= ~MOZILLA_HAS_ARMV7_FLAG
;
85 // Cache a local copy so we only have to read /proc/cpuinfo once.
86 static unsigned arm_cpu_flags
= get_arm_cpu_flags();
88 # if !defined(MOZILLA_PRESUME_EDSP)
89 static bool check_edsp(void) {
90 return (arm_cpu_flags
& MOZILLA_HAS_EDSP_FLAG
) != 0;
94 # if !defined(MOZILLA_PRESUME_ARMV6)
95 static bool check_armv6(void) {
96 return (arm_cpu_flags
& MOZILLA_HAS_ARMV6_FLAG
) != 0;
100 # if !defined(MOZILLA_PRESUME_ARMV7)
101 static bool check_armv7(void) {
102 return (arm_cpu_flags
& MOZILLA_HAS_ARMV7_FLAG
) != 0;
106 # if !defined(MOZILLA_PRESUME_NEON)
107 static bool check_neon(void) {
108 return (arm_cpu_flags
& MOZILLA_HAS_NEON_FLAG
) != 0;
112 # endif // defined(__linux__) || defined(ANDROID)
115 namespace arm_private
{
116 # if !defined(MOZILLA_PRESUME_EDSP)
117 bool edsp_enabled
= check_edsp();
119 # if !defined(MOZILLA_PRESUME_ARMV6)
120 bool armv6_enabled
= check_armv6();
122 # if !defined(MOZILLA_PRESUME_ARMV7)
123 bool armv7_enabled
= check_armv7();
125 # if !defined(MOZILLA_PRESUME_NEON)
126 bool neon_enabled
= check_neon();
128 } // namespace arm_private
129 } // namespace mozilla
131 #endif // MOZILLA_ARM_HAVE_CPUID_DETECTION