arm_cpudetect.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #error "CONFIG_RUNTIME_CPU_DETECT should be enabled!"
  37. #elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */
  38. /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
  39. #define WIN32_LEAN_AND_MEAN
  40. #define WIN32_EXTRA_LEAN
  41. #include <windows.h>
  42. int arm_cpu_caps(void) {
  43. int flags;
  44. int mask;
  45. if (!arm_cpu_env_flags(&flags)) {
  46. return flags;
  47. }
  48. mask = arm_cpu_env_mask();
  49. /* MSVC has no inline __asm support for ARM, but it does let you __emit
  50. * instructions via their assembled hex code.
  51. * All of these instructions should be essentially nops.
  52. */
  53. #if HAVE_MEDIA
  54. if (mask & HAS_MEDIA) {
  55. __try {
  56. /*SHADD8 r3,r3,r3*/
  57. __emit(0xE6333F93);
  58. flags |= HAS_MEDIA;
  59. } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
  60. /*Ignore exception.*/
  61. }
  62. }
  63. #endif /* HAVE_MEDIA */
  64. #if HAVE_NEON || HAVE_NEON_ASM
  65. if (mask &HAS_NEON) {
  66. __try {
  67. /*VORR q0,q0,q0*/
  68. __emit(0xF2200150);
  69. flags |= HAS_NEON;
  70. } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
  71. /*Ignore exception.*/
  72. }
  73. }
  74. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  75. return flags & mask;
  76. }
  77. #elif defined(__ANDROID__) /* end _MSC_VER */
  78. #include <cpu-features.h>
  79. int arm_cpu_caps(void) {
  80. int flags;
  81. int mask;
  82. uint64_t features;
  83. if (!arm_cpu_env_flags(&flags)) {
  84. return flags;
  85. }
  86. mask = arm_cpu_env_mask();
  87. features = android_getCpuFeatures();
  88. #if HAVE_MEDIA
  89. flags |= HAS_MEDIA;
  90. #endif /* HAVE_MEDIA */
  91. #if HAVE_NEON || HAVE_NEON_ASM
  92. if (features & ANDROID_CPU_ARM_FEATURE_NEON)
  93. flags |= HAS_NEON;
  94. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  95. return flags & mask;
  96. }
  97. #elif defined(__linux__) /* end __ANDROID__ */
  98. #include <stdio.h>
  99. int arm_cpu_caps(void) {
  100. FILE *fin;
  101. int flags;
  102. int mask;
  103. if (!arm_cpu_env_flags(&flags)) {
  104. return flags;
  105. }
  106. mask = arm_cpu_env_mask();
  107. /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
  108. * on Android.
  109. * This also means that detection will fail in Scratchbox.
  110. */
  111. fin = fopen("/proc/cpuinfo", "r");
  112. if (fin != NULL) {
  113. /* 512 should be enough for anybody (it's even enough for all the flags
  114. * that x86 has accumulated... so far).
  115. */
  116. char buf[512];
  117. while (fgets(buf, 511, fin) != NULL) {
  118. #if HAVE_NEON || HAVE_NEON_ASM
  119. if (memcmp(buf, "Features", 8) == 0) {
  120. char *p;
  121. p = strstr(buf, " neon");
  122. if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
  123. flags |= HAS_NEON;
  124. }
  125. }
  126. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  127. #if HAVE_MEDIA
  128. if (memcmp(buf, "CPU architecture:", 17) == 0) {
  129. int version;
  130. version = atoi(buf + 17);
  131. if (version >= 6) {
  132. flags |= HAS_MEDIA;
  133. }
  134. }
  135. #endif /* HAVE_MEDIA */
  136. }
  137. fclose(fin);
  138. }
  139. return flags & mask;
  140. }
  141. #else /* end __linux__ */
  142. int arm_cpu_caps(void) {
  143. int flags;
  144. int mask;
  145. if (!arm_cpu_env_flags(&flags)) {
  146. return flags;
  147. }
  148. mask = arm_cpu_env_mask();
  149. #if HAVE_MEDIA
  150. flags |= HAS_MEDIA;
  151. #endif /* HAVE_MEDIA */
  152. #if HAVE_NEON || HAVE_NEON_ASM
  153. flags |= HAS_NEON;
  154. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  155. return flags & mask;
  156. }
  157. #warning "ARM run-time CPU detection is disabled for this platform..."
  158. #endif