cpu.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // CPU detection
  11. //
  12. // Author: Christian Duvivier ([email protected])
  13. #include "src/dsp/cpu.h"
  14. #if defined(WEBP_HAVE_NEON_RTCD)
  15. #include <stdio.h>
  16. #include <string.h>
  17. #endif
  18. #if defined(WEBP_ANDROID_NEON)
  19. #include <cpu-features.h>
  20. #endif
  21. //------------------------------------------------------------------------------
  22. // SSE2 detection.
  23. //
  24. // apple/darwin gcc-4.0.1 defines __PIC__, but not __pic__ with -fPIC.
  25. #if (defined(__pic__) || defined(__PIC__)) && defined(__i386__)
  26. static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) {
  27. __asm__ volatile (
  28. "mov %%ebx, %%edi\n"
  29. "cpuid\n"
  30. "xchg %%edi, %%ebx\n"
  31. : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
  32. : "a"(info_type), "c"(0));
  33. }
  34. #elif defined(__i386__) || defined(__x86_64__)
  35. static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) {
  36. __asm__ volatile (
  37. "cpuid\n"
  38. : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
  39. : "a"(info_type), "c"(0));
  40. }
  41. #elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
  42. #if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 150030729 // >= VS2008 SP1
  43. #include <intrin.h>
  44. #define GetCPUInfo(info, type) __cpuidex(info, type, 0) // set ecx=0
  45. #define WEBP_HAVE_MSC_CPUID
  46. #elif _MSC_VER > 1310
  47. #include <intrin.h>
  48. #define GetCPUInfo __cpuid
  49. #define WEBP_HAVE_MSC_CPUID
  50. #endif
  51. #endif
  52. // NaCl has no support for xgetbv or the raw opcode.
  53. #if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__))
  54. static WEBP_INLINE uint64_t xgetbv(void) {
  55. const uint32_t ecx = 0;
  56. uint32_t eax, edx;
  57. // Use the raw opcode for xgetbv for compatibility with older toolchains.
  58. __asm__ volatile (
  59. ".byte 0x0f, 0x01, 0xd0\n"
  60. : "=a"(eax), "=d"(edx) : "c" (ecx));
  61. return ((uint64_t)edx << 32) | eax;
  62. }
  63. #elif (defined(_M_X64) || defined(_M_IX86)) && \
  64. defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 160040219 // >= VS2010 SP1
  65. #include <immintrin.h>
  66. #define xgetbv() _xgetbv(0)
  67. #elif defined(_MSC_VER) && defined(_M_IX86)
  68. static WEBP_INLINE uint64_t xgetbv(void) {
  69. uint32_t eax_, edx_;
  70. __asm {
  71. xor ecx, ecx // ecx = 0
  72. // Use the raw opcode for xgetbv for compatibility with older toolchains.
  73. __asm _emit 0x0f __asm _emit 0x01 __asm _emit 0xd0
  74. mov eax_, eax
  75. mov edx_, edx
  76. }
  77. return ((uint64_t)edx_ << 32) | eax_;
  78. }
  79. #else
  80. #define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains.
  81. #endif
  82. #if defined(__i386__) || defined(__x86_64__) || defined(WEBP_HAVE_MSC_CPUID)
  83. // helper function for run-time detection of slow SSSE3 platforms
  84. static int CheckSlowModel(int info) {
  85. // Table listing display models with longer latencies for the bsr instruction
  86. // (ie 2 cycles vs 10/16 cycles) and some SSSE3 instructions like pshufb.
  87. // Refer to Intel 64 and IA-32 Architectures Optimization Reference Manual.
  88. static const uint8_t kSlowModels[] = {
  89. 0x37, 0x4a, 0x4d, // Silvermont Microarchitecture
  90. 0x1c, 0x26, 0x27 // Atom Microarchitecture
  91. };
  92. const uint32_t model = ((info & 0xf0000) >> 12) | ((info >> 4) & 0xf);
  93. const uint32_t family = (info >> 8) & 0xf;
  94. if (family == 0x06) {
  95. size_t i;
  96. for (i = 0; i < sizeof(kSlowModels) / sizeof(kSlowModels[0]); ++i) {
  97. if (model == kSlowModels[i]) return 1;
  98. }
  99. }
  100. return 0;
  101. }
  102. static int x86CPUInfo(CPUFeature feature) {
  103. int max_cpuid_value;
  104. int cpu_info[4];
  105. int is_intel = 0;
  106. // get the highest feature value cpuid supports
  107. GetCPUInfo(cpu_info, 0);
  108. max_cpuid_value = cpu_info[0];
  109. if (max_cpuid_value < 1) {
  110. return 0;
  111. } else {
  112. const int VENDOR_ID_INTEL_EBX = 0x756e6547; // uneG
  113. const int VENDOR_ID_INTEL_EDX = 0x49656e69; // Ieni
  114. const int VENDOR_ID_INTEL_ECX = 0x6c65746e; // letn
  115. is_intel = (cpu_info[1] == VENDOR_ID_INTEL_EBX &&
  116. cpu_info[2] == VENDOR_ID_INTEL_ECX &&
  117. cpu_info[3] == VENDOR_ID_INTEL_EDX); // genuine Intel?
  118. }
  119. GetCPUInfo(cpu_info, 1);
  120. if (feature == kSSE2) {
  121. return !!(cpu_info[3] & (1 << 26));
  122. }
  123. if (feature == kSSE3) {
  124. return !!(cpu_info[2] & (1 << 0));
  125. }
  126. if (feature == kSlowSSSE3) {
  127. if (is_intel && (cpu_info[2] & (1 << 9))) { // SSSE3?
  128. return CheckSlowModel(cpu_info[0]);
  129. }
  130. return 0;
  131. }
  132. if (feature == kSSE4_1) {
  133. return !!(cpu_info[2] & (1 << 19));
  134. }
  135. if (feature == kAVX) {
  136. // bits 27 (OSXSAVE) & 28 (256-bit AVX)
  137. if ((cpu_info[2] & 0x18000000) == 0x18000000) {
  138. // XMM state and YMM state enabled by the OS.
  139. return (xgetbv() & 0x6) == 0x6;
  140. }
  141. }
  142. if (feature == kAVX2) {
  143. if (x86CPUInfo(kAVX) && max_cpuid_value >= 7) {
  144. GetCPUInfo(cpu_info, 7);
  145. return !!(cpu_info[1] & (1 << 5));
  146. }
  147. }
  148. return 0;
  149. }
  150. WEBP_EXTERN VP8CPUInfo VP8GetCPUInfo;
  151. VP8CPUInfo VP8GetCPUInfo = x86CPUInfo;
  152. #elif defined(WEBP_ANDROID_NEON) // NB: needs to be before generic NEON test.
  153. static int AndroidCPUInfo(CPUFeature feature) {
  154. const AndroidCpuFamily cpu_family = android_getCpuFamily();
  155. const uint64_t cpu_features = android_getCpuFeatures();
  156. if (feature == kNEON) {
  157. return cpu_family == ANDROID_CPU_FAMILY_ARM &&
  158. (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
  159. }
  160. return 0;
  161. }
  162. WEBP_EXTERN VP8CPUInfo VP8GetCPUInfo;
  163. VP8CPUInfo VP8GetCPUInfo = AndroidCPUInfo;
  164. #elif defined(EMSCRIPTEN) // also needs to be before generic NEON test
  165. // Use compile flags as an indicator of SIMD support instead of a runtime check.
  166. static int wasmCPUInfo(CPUFeature feature) {
  167. switch (feature) {
  168. #ifdef WEBP_HAVE_SSE2
  169. case kSSE2:
  170. return 1;
  171. #endif
  172. #ifdef WEBP_HAVE_SSE41
  173. case kSSE3:
  174. case kSlowSSSE3:
  175. case kSSE4_1:
  176. return 1;
  177. #endif
  178. #ifdef WEBP_HAVE_NEON
  179. case kNEON:
  180. return 1;
  181. #endif
  182. default:
  183. break;
  184. }
  185. return 0;
  186. }
  187. WEBP_EXTERN VP8CPUInfo VP8GetCPUInfo;
  188. VP8CPUInfo VP8GetCPUInfo = wasmCPUInfo;
  189. #elif defined(WEBP_HAVE_NEON)
  190. // In most cases this function doesn't check for NEON support (it's assumed by
  191. // the configuration), but enables turning off NEON at runtime, for testing
  192. // purposes, by setting VP8GetCPUInfo = NULL.
  193. static int armCPUInfo(CPUFeature feature) {
  194. if (feature != kNEON) return 0;
  195. #if defined(__linux__) && defined(WEBP_HAVE_NEON_RTCD)
  196. {
  197. int has_neon = 0;
  198. char line[200];
  199. FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
  200. if (cpuinfo == NULL) return 0;
  201. while (fgets(line, sizeof(line), cpuinfo)) {
  202. if (!strncmp(line, "Features", 8)) {
  203. if (strstr(line, " neon ") != NULL) {
  204. has_neon = 1;
  205. break;
  206. }
  207. }
  208. }
  209. fclose(cpuinfo);
  210. return has_neon;
  211. }
  212. #else
  213. return 1;
  214. #endif
  215. }
  216. WEBP_EXTERN VP8CPUInfo VP8GetCPUInfo;
  217. VP8CPUInfo VP8GetCPUInfo = armCPUInfo;
  218. #elif defined(WEBP_USE_MIPS32) || defined(WEBP_USE_MIPS_DSP_R2) || \
  219. defined(WEBP_USE_MSA)
  220. static int mipsCPUInfo(CPUFeature feature) {
  221. if ((feature == kMIPS32) || (feature == kMIPSdspR2) || (feature == kMSA)) {
  222. return 1;
  223. } else {
  224. return 0;
  225. }
  226. }
  227. WEBP_EXTERN VP8CPUInfo VP8GetCPUInfo;
  228. VP8CPUInfo VP8GetCPUInfo = mipsCPUInfo;
  229. #else
  230. WEBP_EXTERN VP8CPUInfo VP8GetCPUInfo;
  231. VP8CPUInfo VP8GetCPUInfo = NULL;
  232. #endif