arm.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/. */
  4. /* compile-time and runtime tests for whether to use various ARM extensions */
  5. #include "arm.h"
  6. #if defined(MOZILLA_ARM_HAVE_CPUID_DETECTION)
  7. // arm.h has parallel #ifs which declare MOZILLA_ARM_HAVE_CPUID_DETECTION.
  8. // We don't check it here so that we get compile errors if it's defined, but
  9. // we don't compile one of these detection methods. The detection code here is
  10. // based on the CPU detection in libtheora.
  11. # if defined(__linux__)
  12. # include <stdio.h>
  13. # include <stdlib.h>
  14. # include <string.h>
  15. enum{
  16. MOZILLA_HAS_EDSP_FLAG=1,
  17. MOZILLA_HAS_ARMV6_FLAG=2,
  18. MOZILLA_HAS_ARMV7_FLAG=4,
  19. MOZILLA_HAS_NEON_FLAG=8
  20. };
  21. static unsigned
  22. get_arm_cpu_flags(void)
  23. {
  24. unsigned flags;
  25. FILE *fin;
  26. bool armv6_processor = false;
  27. flags = 0;
  28. /*Reading /proc/self/auxv would be easier, but that doesn't work reliably on
  29. Android. This also means that detection will fail in Scratchbox, which is
  30. desirable, as NEON does not work in the qemu shipped with the Maemo 5 SDK.
  31. I don't know if /proc/self/auxv would do any better in that case, anyway,
  32. or if it would return random flags from the host CPU.*/
  33. fin = fopen ("/proc/cpuinfo","r");
  34. if (fin != nullptr)
  35. {
  36. /*512 should be enough for anybody (it's even enough for all the flags that
  37. x86 has accumulated... so far).*/
  38. char buf[512];
  39. while (fgets(buf, 511, fin) != nullptr)
  40. {
  41. if (memcmp(buf, "Features", 8) == 0)
  42. {
  43. char *p;
  44. p = strstr(buf, " edsp");
  45. if (p != nullptr && (p[5] == ' ' || p[5] == '\n'))
  46. flags |= MOZILLA_HAS_EDSP_FLAG;
  47. p = strstr(buf, " neon");
  48. if( p != nullptr && (p[5] == ' ' || p[5] == '\n'))
  49. flags |= MOZILLA_HAS_NEON_FLAG;
  50. }
  51. if (memcmp(buf, "CPU architecture:", 17) == 0)
  52. {
  53. int version;
  54. version = atoi(buf + 17);
  55. if (version >= 6)
  56. flags |= MOZILLA_HAS_ARMV6_FLAG;
  57. if (version >= 7)
  58. flags |= MOZILLA_HAS_ARMV7_FLAG;
  59. }
  60. /* media/webrtc/trunk/src/system_wrappers/source/cpu_features_arm.c
  61. * Unfortunately, it seems that certain ARMv6-based CPUs
  62. * report an incorrect architecture number of 7!
  63. *
  64. * We try to correct this by looking at the 'elf_format'
  65. * field reported by the 'Processor' field, which is of the
  66. * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
  67. * an ARMv6-one.
  68. */
  69. if (memcmp(buf, "Processor\t:", 11) == 0) {
  70. if (strstr(buf, "(v6l)") != 0) {
  71. armv6_processor = true;
  72. }
  73. }
  74. }
  75. fclose(fin);
  76. }
  77. if (armv6_processor) {
  78. // ARMv6 pretending to be ARMv7? clear flag
  79. if (flags & MOZILLA_HAS_ARMV7_FLAG) {
  80. flags &= ~MOZILLA_HAS_ARMV7_FLAG;
  81. }
  82. }
  83. return flags;
  84. }
  85. // Cache a local copy so we only have to read /proc/cpuinfo once.
  86. static unsigned arm_cpu_flags = get_arm_cpu_flags();
  87. # if !defined(MOZILLA_PRESUME_EDSP)
  88. static bool
  89. check_edsp(void)
  90. {
  91. return (arm_cpu_flags & MOZILLA_HAS_EDSP_FLAG) != 0;
  92. }
  93. # endif
  94. # if !defined(MOZILLA_PRESUME_ARMV6)
  95. static bool
  96. check_armv6(void)
  97. {
  98. return (arm_cpu_flags & MOZILLA_HAS_ARMV6_FLAG) != 0;
  99. }
  100. # endif
  101. # if !defined(MOZILLA_PRESUME_ARMV7)
  102. static bool
  103. check_armv7(void)
  104. {
  105. return (arm_cpu_flags & MOZILLA_HAS_ARMV7_FLAG) != 0;
  106. }
  107. # endif
  108. # if !defined(MOZILLA_PRESUME_NEON)
  109. static bool
  110. check_neon(void)
  111. {
  112. return (arm_cpu_flags & MOZILLA_HAS_NEON_FLAG) != 0;
  113. }
  114. # endif
  115. # endif // defined(__linux__)
  116. namespace mozilla {
  117. namespace arm_private {
  118. # if !defined(MOZILLA_PRESUME_EDSP)
  119. bool edsp_enabled = check_edsp();
  120. # endif
  121. # if !defined(MOZILLA_PRESUME_ARMV6)
  122. bool armv6_enabled = check_armv6();
  123. # endif
  124. # if !defined(MOZILLA_PRESUME_ARMV7)
  125. bool armv7_enabled = check_armv7();
  126. # endif
  127. # if !defined(MOZILLA_PRESUME_NEON)
  128. bool neon_enabled = check_neon();
  129. # endif
  130. } // namespace arm_private
  131. } // namespace mozilla
  132. #endif // MOZILLA_ARM_HAVE_CPUID_DETECTION