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 */
8 #include "mozilla/Attributes.h"
10 #if defined(MOZILLA_ARM_HAVE_CPUID_DETECTION)
12 // arm.h has parallel #ifs which declare MOZILLA_ARM_HAVE_CPUID_DETECTION.
13 // We don't check it here so that we get compile errors if it's defined, but
14 // we don't compile one of these detection methods. The detection code here is
15 // based on the CPU detection in libtheora.
17 # if defined(__linux__) || defined(ANDROID)
23 MOZILLA_HAS_EDSP_FLAG
= 1,
24 MOZILLA_HAS_ARMV6_FLAG
= 2,
25 MOZILLA_HAS_ARMV7_FLAG
= 4,
26 MOZILLA_HAS_NEON_FLAG
= 8,
27 MOZILLA_HAS_AES_FLAG
= 16
30 static unsigned get_arm_cpu_flags(void) {
33 bool armv6_processor
= false;
35 /*Reading /proc/self/auxv would be easier, but that doesn't work reliably on
36 Android. This also means that detection will fail in Scratchbox, which is
37 desirable, as NEON does not work in the qemu shipped with the Maemo 5 SDK.
38 I don't know if /proc/self/auxv would do any better in that case, anyway,
39 or if it would return random flags from the host CPU.*/
40 fin
= fopen("/proc/cpuinfo", "r");
42 /*512 should be enough for anybody (it's even enough for all the flags that
43 x86 has accumulated... so far).*/
45 while (fgets(buf
, 511, fin
) != nullptr) {
46 if (memcmp(buf
, "Features", 8) == 0) {
48 p
= strstr(buf
, " edsp");
49 if (p
!= nullptr && (p
[5] == ' ' || p
[5] == '\n'))
50 flags
|= MOZILLA_HAS_EDSP_FLAG
;
51 p
= strstr(buf
, " neon");
52 if (p
!= nullptr && (p
[5] == ' ' || p
[5] == '\n'))
53 flags
|= MOZILLA_HAS_NEON_FLAG
;
54 p
= strstr(buf
, " aes");
55 if (p
!= nullptr && (p
[4] == ' ' || p
[4] == '\n')) {
56 flags
|= MOZILLA_HAS_AES_FLAG
;
59 if (memcmp(buf
, "CPU architecture:", 17) == 0) {
61 version
= atoi(buf
+ 17);
62 if (version
>= 6) flags
|= MOZILLA_HAS_ARMV6_FLAG
;
63 if (version
>= 7) flags
|= MOZILLA_HAS_ARMV7_FLAG
;
65 /* media/webrtc/trunk/src/system_wrappers/source/cpu_features_arm.c
66 * Unfortunately, it seems that certain ARMv6-based CPUs
67 * report an incorrect architecture number of 7!
69 * We try to correct this by looking at the 'elf_format'
70 * field reported by the 'Processor' field, which is of the
71 * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
74 if (memcmp(buf
, "Processor\t:", 11) == 0) {
75 if (strstr(buf
, "(v6l)") != 0) {
76 armv6_processor
= true;
82 if (armv6_processor
) {
83 // ARMv6 pretending to be ARMv7? clear flag
84 if (flags
& MOZILLA_HAS_ARMV7_FLAG
) {
85 flags
&= ~MOZILLA_HAS_ARMV7_FLAG
;
91 // Cache a local copy so we only have to read /proc/cpuinfo once.
92 MOZ_RUNINIT
static unsigned arm_cpu_flags
= get_arm_cpu_flags();
94 # if !defined(MOZILLA_PRESUME_EDSP)
95 static bool check_edsp(void) {
96 return (arm_cpu_flags
& MOZILLA_HAS_EDSP_FLAG
) != 0;
100 # if !defined(MOZILLA_PRESUME_ARMV6)
101 static bool check_armv6(void) {
102 return (arm_cpu_flags
& MOZILLA_HAS_ARMV6_FLAG
) != 0;
106 # if !defined(MOZILLA_PRESUME_ARMV7)
107 static bool check_armv7(void) {
108 return (arm_cpu_flags
& MOZILLA_HAS_ARMV7_FLAG
) != 0;
112 # if !defined(MOZILLA_PRESUME_NEON)
113 static bool check_neon(void) {
114 return (arm_cpu_flags
& MOZILLA_HAS_NEON_FLAG
) != 0;
118 # if !defined(MOZILLA_PRESUME_ARM_AES)
119 static bool check_aes(void) {
120 return (arm_cpu_flags
& MOZILLA_HAS_AES_FLAG
) != 0;
124 # endif // defined(__linux__) || defined(ANDROID)
127 namespace arm_private
{
128 # if !defined(MOZILLA_PRESUME_EDSP)
129 MOZ_RUNINIT
bool edsp_enabled
= check_edsp();
131 # if !defined(MOZILLA_PRESUME_ARMV6)
132 MOZ_RUNINIT
bool armv6_enabled
= check_armv6();
134 # if !defined(MOZILLA_PRESUME_ARMV7)
135 MOZ_RUNINIT
bool armv7_enabled
= check_armv7();
137 # if !defined(MOZILLA_PRESUME_NEON)
138 MOZ_RUNINIT
bool neon_enabled
= check_neon();
140 # if !defined(MOZILLA_PRESUME_ARM_AES)
141 MOZ_RUNINIT
bool aes_enabled
= check_aes();
143 } // namespace arm_private
144 } // namespace mozilla
146 #endif // MOZILLA_ARM_HAVE_CPUID_DETECTION