cpu_id.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright 2011 The LibYuv 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 "libyuv/cpu_id.h"
  11. #if defined(_MSC_VER)
  12. #include <intrin.h> // For __cpuidex()
  13. #endif
  14. #if !defined(__pnacl__) && !defined(__CLR_VER) && \
  15. !defined(__native_client__) && (defined(_M_IX86) || defined(_M_X64)) && \
  16. defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219)
  17. #include <immintrin.h> // For _xgetbv()
  18. #endif
  19. #if !defined(__native_client__)
  20. #include <stdlib.h> // For getenv()
  21. #endif
  22. // For ArmCpuCaps() but unittested on all platforms
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "libyuv/basic_types.h" // For CPU_X86
  26. #ifdef __cplusplus
  27. namespace libyuv {
  28. extern "C" {
  29. #endif
  30. // For functions that use the stack and have runtime checks for overflow,
  31. // use SAFEBUFFERS to avoid additional check.
  32. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219) && \
  33. !defined(__clang__)
  34. #define SAFEBUFFERS __declspec(safebuffers)
  35. #else
  36. #define SAFEBUFFERS
  37. #endif
  38. // Low level cpuid for X86.
  39. #if (defined(_M_IX86) || defined(_M_X64) || \
  40. defined(__i386__) || defined(__x86_64__)) && \
  41. !defined(__pnacl__) && !defined(__CLR_VER)
  42. LIBYUV_API
  43. void CpuId(uint32 info_eax, uint32 info_ecx, uint32* cpu_info) {
  44. #if defined(_MSC_VER)
  45. // Visual C version uses intrinsic or inline x86 assembly.
  46. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219)
  47. __cpuidex((int*)(cpu_info), info_eax, info_ecx);
  48. #elif defined(_M_IX86)
  49. __asm {
  50. mov eax, info_eax
  51. mov ecx, info_ecx
  52. mov edi, cpu_info
  53. cpuid
  54. mov [edi], eax
  55. mov [edi + 4], ebx
  56. mov [edi + 8], ecx
  57. mov [edi + 12], edx
  58. }
  59. #else // Visual C but not x86
  60. if (info_ecx == 0) {
  61. __cpuid((int*)(cpu_info), info_eax);
  62. } else {
  63. cpu_info[3] = cpu_info[2] = cpu_info[1] = cpu_info[0] = 0;
  64. }
  65. #endif
  66. // GCC version uses inline x86 assembly.
  67. #else // defined(_MSC_VER)
  68. uint32 info_ebx, info_edx;
  69. asm volatile (
  70. #if defined( __i386__) && defined(__PIC__)
  71. // Preserve ebx for fpic 32 bit.
  72. "mov %%ebx, %%edi \n"
  73. "cpuid \n"
  74. "xchg %%edi, %%ebx \n"
  75. : "=D" (info_ebx),
  76. #else
  77. "cpuid \n"
  78. : "=b" (info_ebx),
  79. #endif // defined( __i386__) && defined(__PIC__)
  80. "+a" (info_eax), "+c" (info_ecx), "=d" (info_edx));
  81. cpu_info[0] = info_eax;
  82. cpu_info[1] = info_ebx;
  83. cpu_info[2] = info_ecx;
  84. cpu_info[3] = info_edx;
  85. #endif // defined(_MSC_VER)
  86. }
  87. #else // (defined(_M_IX86) || defined(_M_X64) ...
  88. LIBYUV_API
  89. void CpuId(uint32 eax, uint32 ecx, uint32* cpu_info) {
  90. cpu_info[0] = cpu_info[1] = cpu_info[2] = cpu_info[3] = 0;
  91. }
  92. #endif
  93. // For VS2010 and earlier emit can be used:
  94. // _asm _emit 0x0f _asm _emit 0x01 _asm _emit 0xd0 // For VS2010 and earlier.
  95. // __asm {
  96. // xor ecx, ecx // xcr 0
  97. // xgetbv
  98. // mov xcr0, eax
  99. // }
  100. // For VS2013 and earlier 32 bit, the _xgetbv(0) optimizer produces bad code.
  101. // https://code.google.com/p/libyuv/issues/detail?id=529
  102. #if defined(_M_IX86) && (_MSC_VER < 1900)
  103. #pragma optimize("g", off)
  104. #endif
  105. #if (defined(_M_IX86) || defined(_M_X64) || \
  106. defined(__i386__) || defined(__x86_64__)) && \
  107. !defined(__pnacl__) && !defined(__CLR_VER) && !defined(__native_client__)
  108. #define HAS_XGETBV
  109. // X86 CPUs have xgetbv to detect OS saves high parts of ymm registers.
  110. int GetXCR0() {
  111. uint32 xcr0 = 0u;
  112. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219)
  113. xcr0 = (uint32)(_xgetbv(0)); // VS2010 SP1 required.
  114. #elif defined(__i386__) || defined(__x86_64__)
  115. asm(".byte 0x0f, 0x01, 0xd0" : "=a" (xcr0) : "c" (0) : "%edx");
  116. #endif // defined(__i386__) || defined(__x86_64__)
  117. return xcr0;
  118. }
  119. #endif // defined(_M_IX86) || defined(_M_X64) ..
  120. // Return optimization to previous setting.
  121. #if defined(_M_IX86) && (_MSC_VER < 1900)
  122. #pragma optimize("g", on)
  123. #endif
  124. // based on libvpx arm_cpudetect.c
  125. // For Arm, but public to allow testing on any CPU
  126. LIBYUV_API SAFEBUFFERS
  127. int ArmCpuCaps(const char* cpuinfo_name) {
  128. char cpuinfo_line[512];
  129. FILE* f = fopen(cpuinfo_name, "r");
  130. if (!f) {
  131. // Assume Neon if /proc/cpuinfo is unavailable.
  132. // This will occur for Chrome sandbox for Pepper or Render process.
  133. return kCpuHasNEON;
  134. }
  135. while (fgets(cpuinfo_line, sizeof(cpuinfo_line) - 1, f)) {
  136. if (memcmp(cpuinfo_line, "Features", 8) == 0) {
  137. char* p = strstr(cpuinfo_line, " neon");
  138. if (p && (p[5] == ' ' || p[5] == '\n')) {
  139. fclose(f);
  140. return kCpuHasNEON;
  141. }
  142. // aarch64 uses asimd for Neon.
  143. p = strstr(cpuinfo_line, " asimd");
  144. if (p && (p[6] == ' ' || p[6] == '\n')) {
  145. fclose(f);
  146. return kCpuHasNEON;
  147. }
  148. }
  149. }
  150. fclose(f);
  151. return 0;
  152. }
  153. // CPU detect function for SIMD instruction sets.
  154. LIBYUV_API
  155. int cpu_info_ = 0; // cpu_info is not initialized yet.
  156. // Test environment variable for disabling CPU features. Any non-zero value
  157. // to disable. Zero ignored to make it easy to set the variable on/off.
  158. #if !defined(__native_client__) && !defined(_M_ARM)
  159. static LIBYUV_BOOL TestEnv(const char* name) {
  160. const char* var = getenv(name);
  161. if (var) {
  162. if (var[0] != '0') {
  163. return LIBYUV_TRUE;
  164. }
  165. }
  166. return LIBYUV_FALSE;
  167. }
  168. #else // nacl does not support getenv().
  169. static LIBYUV_BOOL TestEnv(const char*) {
  170. return LIBYUV_FALSE;
  171. }
  172. #endif
  173. LIBYUV_API SAFEBUFFERS
  174. int InitCpuFlags(void) {
  175. // TODO(fbarchard): swap kCpuInit logic so 0 means uninitialized.
  176. int cpu_info = 0;
  177. #if !defined(__pnacl__) && !defined(__CLR_VER) && defined(CPU_X86)
  178. uint32 cpu_info0[4] = { 0, 0, 0, 0 };
  179. uint32 cpu_info1[4] = { 0, 0, 0, 0 };
  180. uint32 cpu_info7[4] = { 0, 0, 0, 0 };
  181. CpuId(0, 0, cpu_info0);
  182. CpuId(1, 0, cpu_info1);
  183. if (cpu_info0[0] >= 7) {
  184. CpuId(7, 0, cpu_info7);
  185. }
  186. cpu_info = ((cpu_info1[3] & 0x04000000) ? kCpuHasSSE2 : 0) |
  187. ((cpu_info1[2] & 0x00000200) ? kCpuHasSSSE3 : 0) |
  188. ((cpu_info1[2] & 0x00080000) ? kCpuHasSSE41 : 0) |
  189. ((cpu_info1[2] & 0x00100000) ? kCpuHasSSE42 : 0) |
  190. ((cpu_info7[1] & 0x00000200) ? kCpuHasERMS : 0) |
  191. ((cpu_info1[2] & 0x00001000) ? kCpuHasFMA3 : 0) |
  192. kCpuHasX86;
  193. #ifdef HAS_XGETBV
  194. // AVX requires CPU has AVX, XSAVE and OSXSave for xgetbv
  195. if (((cpu_info1[2] & 0x1c000000) == 0x1c000000) && // AVX and OSXSave
  196. ((GetXCR0() & 6) == 6)) { // Test OS saves YMM registers
  197. cpu_info |= ((cpu_info7[1] & 0x00000020) ? kCpuHasAVX2 : 0) | kCpuHasAVX;
  198. // Detect AVX512bw
  199. if ((GetXCR0() & 0xe0) == 0xe0) {
  200. cpu_info |= (cpu_info7[1] & 0x40000000) ? kCpuHasAVX3 : 0;
  201. }
  202. }
  203. #endif
  204. // Environment variable overrides for testing.
  205. if (TestEnv("LIBYUV_DISABLE_X86")) {
  206. cpu_info &= ~kCpuHasX86;
  207. }
  208. if (TestEnv("LIBYUV_DISABLE_SSE2")) {
  209. cpu_info &= ~kCpuHasSSE2;
  210. }
  211. if (TestEnv("LIBYUV_DISABLE_SSSE3")) {
  212. cpu_info &= ~kCpuHasSSSE3;
  213. }
  214. if (TestEnv("LIBYUV_DISABLE_SSE41")) {
  215. cpu_info &= ~kCpuHasSSE41;
  216. }
  217. if (TestEnv("LIBYUV_DISABLE_SSE42")) {
  218. cpu_info &= ~kCpuHasSSE42;
  219. }
  220. if (TestEnv("LIBYUV_DISABLE_AVX")) {
  221. cpu_info &= ~kCpuHasAVX;
  222. }
  223. if (TestEnv("LIBYUV_DISABLE_AVX2")) {
  224. cpu_info &= ~kCpuHasAVX2;
  225. }
  226. if (TestEnv("LIBYUV_DISABLE_ERMS")) {
  227. cpu_info &= ~kCpuHasERMS;
  228. }
  229. if (TestEnv("LIBYUV_DISABLE_FMA3")) {
  230. cpu_info &= ~kCpuHasFMA3;
  231. }
  232. if (TestEnv("LIBYUV_DISABLE_AVX3")) {
  233. cpu_info &= ~kCpuHasAVX3;
  234. }
  235. #endif
  236. #if defined(__mips__) && defined(__linux__)
  237. #if defined(__mips_dspr2)
  238. cpu_info |= kCpuHasDSPR2;
  239. #endif
  240. cpu_info |= kCpuHasMIPS;
  241. if (getenv("LIBYUV_DISABLE_DSPR2")) {
  242. cpu_info &= ~kCpuHasDSPR2;
  243. }
  244. #endif
  245. #if defined(__arm__) || defined(__aarch64__)
  246. // gcc -mfpu=neon defines __ARM_NEON__
  247. // __ARM_NEON__ generates code that requires Neon. NaCL also requires Neon.
  248. // For Linux, /proc/cpuinfo can be tested but without that assume Neon.
  249. #if defined(__ARM_NEON__) || defined(__native_client__) || !defined(__linux__)
  250. cpu_info = kCpuHasNEON;
  251. // For aarch64(arm64), /proc/cpuinfo's feature is not complete, e.g. no neon
  252. // flag in it.
  253. // So for aarch64, neon enabling is hard coded here.
  254. #endif
  255. #if defined(__aarch64__)
  256. cpu_info = kCpuHasNEON;
  257. #else
  258. // Linux arm parse text file for neon detect.
  259. cpu_info = ArmCpuCaps("/proc/cpuinfo");
  260. #endif
  261. cpu_info |= kCpuHasARM;
  262. if (TestEnv("LIBYUV_DISABLE_NEON")) {
  263. cpu_info &= ~kCpuHasNEON;
  264. }
  265. #endif // __arm__
  266. if (TestEnv("LIBYUV_DISABLE_ASM")) {
  267. cpu_info = 0;
  268. }
  269. cpu_info |= kCpuInitialized;
  270. cpu_info_ = cpu_info;
  271. return cpu_info;
  272. }
  273. // Note that use of this function is not thread safe.
  274. LIBYUV_API
  275. void MaskCpuFlags(int enable_flags) {
  276. cpu_info_ = InitCpuFlags() & enable_flags;
  277. }
  278. #ifdef __cplusplus
  279. } // extern "C"
  280. } // namespace libyuv
  281. #endif