arm_cpudetect.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "vpx_ports/arm.h"
  13. #include "./vpx_config.h"
  14. #ifdef WINAPI_FAMILY
  15. #include <winapifamily.h>
  16. #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
  17. #define getenv(x) NULL
  18. #endif
  19. #endif
  20. static int arm_cpu_env_flags(int *flags) {
  21. char *env;
  22. env = getenv("VPX_SIMD_CAPS");
  23. if (env && *env) {
  24. *flags = (int)strtol(env, NULL, 0);
  25. return 0;
  26. }
  27. *flags = 0;
  28. return -1;
  29. }
  30. static int arm_cpu_env_mask(void) {
  31. char *env;
  32. env = getenv("VPX_SIMD_CAPS_MASK");
  33. return env && *env ? (int)strtol(env, NULL, 0) : ~0;
  34. }
  35. #if !CONFIG_RUNTIME_CPU_DETECT
  36. int arm_cpu_caps(void) {
  37. /* This function should actually be a no-op. There is no way to adjust any of
  38. * these because the RTCD tables do not exist: the functions are called
  39. * statically */
  40. int flags;
  41. int mask;
  42. if (!arm_cpu_env_flags(&flags)) {
  43. return flags;
  44. }
  45. mask = arm_cpu_env_mask();
  46. #if HAVE_MEDIA
  47. flags |= HAS_MEDIA;
  48. #endif /* HAVE_MEDIA */
  49. #if HAVE_NEON || HAVE_NEON_ASM
  50. flags |= HAS_NEON;
  51. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  52. return flags & mask;
  53. }
  54. #elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */
  55. /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
  56. #define WIN32_LEAN_AND_MEAN
  57. #define WIN32_EXTRA_LEAN
  58. #include <windows.h>
  59. int arm_cpu_caps(void) {
  60. int flags;
  61. int mask;
  62. if (!arm_cpu_env_flags(&flags)) {
  63. return flags;
  64. }
  65. mask = arm_cpu_env_mask();
  66. /* MSVC has no inline __asm support for ARM, but it does let you __emit
  67. * instructions via their assembled hex code.
  68. * All of these instructions should be essentially nops.
  69. */
  70. #if HAVE_MEDIA
  71. if (mask & HAS_MEDIA)
  72. __try {
  73. /*SHADD8 r3,r3,r3*/
  74. __emit(0xE6333F93);
  75. flags |= HAS_MEDIA;
  76. } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
  77. /*Ignore exception.*/
  78. }
  79. }
  80. #endif /* HAVE_MEDIA */
  81. #if HAVE_NEON || HAVE_NEON_ASM
  82. if (mask &HAS_NEON) {
  83. __try {
  84. /*VORR q0,q0,q0*/
  85. __emit(0xF2200150);
  86. flags |= HAS_NEON;
  87. } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
  88. /*Ignore exception.*/
  89. }
  90. }
  91. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  92. return flags & mask;
  93. }
  94. #elif defined(__ANDROID__) /* end _MSC_VER */
  95. #include <cpu-features.h>
  96. int arm_cpu_caps(void) {
  97. int flags;
  98. int mask;
  99. uint64_t features;
  100. if (!arm_cpu_env_flags(&flags)) {
  101. return flags;
  102. }
  103. mask = arm_cpu_env_mask();
  104. features = android_getCpuFeatures();
  105. #if HAVE_MEDIA
  106. flags |= HAS_MEDIA;
  107. #endif /* HAVE_MEDIA */
  108. #if HAVE_NEON || HAVE_NEON_ASM
  109. if (features & ANDROID_CPU_ARM_FEATURE_NEON)
  110. flags |= HAS_NEON;
  111. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  112. return flags & mask;
  113. }
  114. #elif defined(__linux__) /* end __ANDROID__ */
  115. #include <stdio.h>
  116. int arm_cpu_caps(void) {
  117. FILE *fin;
  118. int flags;
  119. int mask;
  120. if (!arm_cpu_env_flags(&flags)) {
  121. return flags;
  122. }
  123. mask = arm_cpu_env_mask();
  124. /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
  125. * on Android.
  126. * This also means that detection will fail in Scratchbox.
  127. */
  128. fin = fopen("/proc/cpuinfo", "r");
  129. if (fin != NULL) {
  130. /* 512 should be enough for anybody (it's even enough for all the flags
  131. * that x86 has accumulated... so far).
  132. */
  133. char buf[512];
  134. while (fgets(buf, 511, fin) != NULL) {
  135. #if HAVE_NEON || HAVE_NEON_ASM
  136. if (memcmp(buf, "Features", 8) == 0) {
  137. char *p;
  138. p = strstr(buf, " neon");
  139. if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
  140. flags |= HAS_NEON;
  141. }
  142. }
  143. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  144. #if HAVE_MEDIA
  145. if (memcmp(buf, "CPU architecture:", 17) == 0) {
  146. int version;
  147. version = atoi(buf + 17);
  148. if (version >= 6) {
  149. flags |= HAS_MEDIA;
  150. }
  151. }
  152. #endif /* HAVE_MEDIA */
  153. }
  154. fclose(fin);
  155. }
  156. return flags & mask;
  157. }
  158. #else /* end __linux__ */
  159. #error "--enable-runtime-cpu-detect selected, but no CPU detection method " \
  160. "available for your platform. Reconfigure with --disable-runtime-cpu-detect."
  161. #endif