2
0

arm_cpudetect.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_config.h"
  13. #include "vpx_ports/arm.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_NEON || HAVE_NEON_ASM
  47. flags |= HAS_NEON;
  48. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  49. return flags & mask;
  50. }
  51. #elif defined(_MSC_VER) /* end !CONFIG_RUNTIME_CPU_DETECT */
  52. /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
  53. #ifndef WIN32_LEAN_AND_MEAN
  54. #define WIN32_LEAN_AND_MEAN
  55. #endif
  56. #ifndef WIN32_EXTRA_LEAN
  57. #define WIN32_EXTRA_LEAN
  58. #endif
  59. #include <windows.h>
  60. int arm_cpu_caps(void) {
  61. int flags;
  62. int mask;
  63. if (!arm_cpu_env_flags(&flags)) {
  64. return flags;
  65. }
  66. mask = arm_cpu_env_mask();
  67. /* MSVC has no inline __asm support for ARM, but it does let you __emit
  68. * instructions via their assembled hex code.
  69. * All of these instructions should be essentially nops.
  70. */
  71. #if HAVE_NEON || HAVE_NEON_ASM
  72. if (mask & HAS_NEON) {
  73. __try {
  74. /*VORR q0,q0,q0*/
  75. __emit(0xF2200150);
  76. flags |= HAS_NEON;
  77. } __except (GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION) {
  78. /*Ignore exception.*/
  79. }
  80. }
  81. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  82. return flags & mask;
  83. }
  84. #elif defined(__ANDROID__) /* end _MSC_VER */
  85. #include <cpu-features.h>
  86. int arm_cpu_caps(void) {
  87. int flags;
  88. int mask;
  89. uint64_t features;
  90. if (!arm_cpu_env_flags(&flags)) {
  91. return flags;
  92. }
  93. mask = arm_cpu_env_mask();
  94. features = android_getCpuFeatures();
  95. #if HAVE_NEON || HAVE_NEON_ASM
  96. if (features & ANDROID_CPU_ARM_FEATURE_NEON) flags |= HAS_NEON;
  97. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  98. return flags & mask;
  99. }
  100. #elif defined(__linux__) /* end __ANDROID__ */
  101. #include <stdio.h>
  102. int arm_cpu_caps(void) {
  103. FILE *fin;
  104. int flags;
  105. int mask;
  106. if (!arm_cpu_env_flags(&flags)) {
  107. return flags;
  108. }
  109. mask = arm_cpu_env_mask();
  110. /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
  111. * on Android.
  112. * This also means that detection will fail in Scratchbox.
  113. */
  114. fin = fopen("/proc/cpuinfo", "r");
  115. if (fin != NULL) {
  116. /* 512 should be enough for anybody (it's even enough for all the flags
  117. * that x86 has accumulated... so far).
  118. */
  119. char buf[512];
  120. while (fgets(buf, 511, fin) != NULL) {
  121. #if HAVE_NEON || HAVE_NEON_ASM
  122. if (memcmp(buf, "Features", 8) == 0) {
  123. char *p;
  124. p = strstr(buf, " neon");
  125. if (p != NULL && (p[5] == ' ' || p[5] == '\n')) {
  126. flags |= HAS_NEON;
  127. }
  128. }
  129. #endif /* HAVE_NEON || HAVE_NEON_ASM */
  130. }
  131. fclose(fin);
  132. }
  133. return flags & mask;
  134. }
  135. #else /* end __linux__ */
  136. #error \
  137. "--enable-runtime-cpu-detect selected, but no CPU detection method " \
  138. "available for your platform. Reconfigure with --disable-runtime-cpu-detect."
  139. #endif