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