Merge mozilla-central to autoland.
[gecko.git] / mozglue / build / arm.cpp
blob20eb9b5821a2e253fe9bef596349a876c97d2546
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(__linux__) || defined(ANDROID)
17 # include <stdio.h>
18 # include <stdlib.h>
19 # include <string.h>
21 enum {
22 MOZILLA_HAS_EDSP_FLAG = 1,
23 MOZILLA_HAS_ARMV6_FLAG = 2,
24 MOZILLA_HAS_ARMV7_FLAG = 4,
25 MOZILLA_HAS_NEON_FLAG = 8,
26 MOZILLA_HAS_AES_FLAG = 16
29 static unsigned get_arm_cpu_flags(void) {
30 unsigned flags;
31 FILE* fin;
32 bool armv6_processor = false;
33 flags = 0;
34 /*Reading /proc/self/auxv would be easier, but that doesn't work reliably on
35 Android. This also means that detection will fail in Scratchbox, which is
36 desirable, as NEON does not work in the qemu shipped with the Maemo 5 SDK.
37 I don't know if /proc/self/auxv would do any better in that case, anyway,
38 or if it would return random flags from the host CPU.*/
39 fin = fopen("/proc/cpuinfo", "r");
40 if (fin != nullptr) {
41 /*512 should be enough for anybody (it's even enough for all the flags that
42 x86 has accumulated... so far).*/
43 char buf[512];
44 while (fgets(buf, 511, fin) != nullptr) {
45 if (memcmp(buf, "Features", 8) == 0) {
46 char* p;
47 p = strstr(buf, " edsp");
48 if (p != nullptr && (p[5] == ' ' || p[5] == '\n'))
49 flags |= MOZILLA_HAS_EDSP_FLAG;
50 p = strstr(buf, " neon");
51 if (p != nullptr && (p[5] == ' ' || p[5] == '\n'))
52 flags |= MOZILLA_HAS_NEON_FLAG;
53 p = strstr(buf, " aes");
54 if (p != nullptr && (p[4] == ' ' || p[4] == '\n')) {
55 flags |= MOZILLA_HAS_AES_FLAG;
58 if (memcmp(buf, "CPU architecture:", 17) == 0) {
59 int version;
60 version = atoi(buf + 17);
61 if (version >= 6) flags |= MOZILLA_HAS_ARMV6_FLAG;
62 if (version >= 7) flags |= MOZILLA_HAS_ARMV7_FLAG;
64 /* media/webrtc/trunk/src/system_wrappers/source/cpu_features_arm.c
65 * Unfortunately, it seems that certain ARMv6-based CPUs
66 * report an incorrect architecture number of 7!
68 * We try to correct this by looking at the 'elf_format'
69 * field reported by the 'Processor' field, which is of the
70 * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
71 * an ARMv6-one.
73 if (memcmp(buf, "Processor\t:", 11) == 0) {
74 if (strstr(buf, "(v6l)") != 0) {
75 armv6_processor = true;
79 fclose(fin);
81 if (armv6_processor) {
82 // ARMv6 pretending to be ARMv7? clear flag
83 if (flags & MOZILLA_HAS_ARMV7_FLAG) {
84 flags &= ~MOZILLA_HAS_ARMV7_FLAG;
87 return flags;
90 // Cache a local copy so we only have to read /proc/cpuinfo once.
91 static unsigned arm_cpu_flags = get_arm_cpu_flags();
93 # if !defined(MOZILLA_PRESUME_EDSP)
94 static bool check_edsp(void) {
95 return (arm_cpu_flags & MOZILLA_HAS_EDSP_FLAG) != 0;
97 # endif
99 # if !defined(MOZILLA_PRESUME_ARMV6)
100 static bool check_armv6(void) {
101 return (arm_cpu_flags & MOZILLA_HAS_ARMV6_FLAG) != 0;
103 # endif
105 # if !defined(MOZILLA_PRESUME_ARMV7)
106 static bool check_armv7(void) {
107 return (arm_cpu_flags & MOZILLA_HAS_ARMV7_FLAG) != 0;
109 # endif
111 # if !defined(MOZILLA_PRESUME_NEON)
112 static bool check_neon(void) {
113 return (arm_cpu_flags & MOZILLA_HAS_NEON_FLAG) != 0;
115 # endif
117 # if !defined(MOZILLA_PRESUME_ARM_AES)
118 static bool check_aes(void) {
119 return (arm_cpu_flags & MOZILLA_HAS_AES_FLAG) != 0;
121 # endif
123 # endif // defined(__linux__) || defined(ANDROID)
125 namespace mozilla {
126 namespace arm_private {
127 # if !defined(MOZILLA_PRESUME_EDSP)
128 bool edsp_enabled = check_edsp();
129 # endif
130 # if !defined(MOZILLA_PRESUME_ARMV6)
131 bool armv6_enabled = check_armv6();
132 # endif
133 # if !defined(MOZILLA_PRESUME_ARMV7)
134 bool armv7_enabled = check_armv7();
135 # endif
136 # if !defined(MOZILLA_PRESUME_NEON)
137 bool neon_enabled = check_neon();
138 # endif
139 # if !defined(MOZILLA_PRESUME_ARM_AES)
140 bool aes_enabled = check_aes();
141 # endif
142 } // namespace arm_private
143 } // namespace mozilla
145 #endif // MOZILLA_ARM_HAVE_CPUID_DETECTION