2
0

cpu_id.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. LIBYUV_API SAFEBUFFERS
  154. int MipsCpuCaps(const char* cpuinfo_name, const char ase[]) {
  155. char cpuinfo_line[512];
  156. int len = (int)strlen(ase);
  157. FILE* f = fopen(cpuinfo_name, "r");
  158. if (!f) {
  159. // ase enabled if /proc/cpuinfo is unavailable.
  160. if (strcmp(ase, " msa") == 0) {
  161. return kCpuHasMSA;
  162. }
  163. if (strcmp(ase, " dspr2") == 0) {
  164. return kCpuHasDSPR2;
  165. }
  166. }
  167. while (fgets(cpuinfo_line, sizeof(cpuinfo_line) - 1, f)) {
  168. if (memcmp(cpuinfo_line, "ASEs implemented", 16) == 0) {
  169. char* p = strstr(cpuinfo_line, ase);
  170. if (p && (p[len] == ' ' || p[len] == '\n')) {
  171. fclose(f);
  172. if (strcmp(ase, " msa") == 0) {
  173. return kCpuHasMSA;
  174. }
  175. if (strcmp(ase, " dspr2") == 0) {
  176. return kCpuHasDSPR2;
  177. }
  178. }
  179. }
  180. }
  181. fclose(f);
  182. return 0;
  183. }
  184. // CPU detect function for SIMD instruction sets.
  185. LIBYUV_API
  186. int cpu_info_ = 0; // cpu_info is not initialized yet.
  187. // Test environment variable for disabling CPU features. Any non-zero value
  188. // to disable. Zero ignored to make it easy to set the variable on/off.
  189. #if !defined(__native_client__) && !defined(_M_ARM)
  190. static LIBYUV_BOOL TestEnv(const char* name) {
  191. const char* var = getenv(name);
  192. if (var) {
  193. if (var[0] != '0') {
  194. return LIBYUV_TRUE;
  195. }
  196. }
  197. return LIBYUV_FALSE;
  198. }
  199. #else // nacl does not support getenv().
  200. static LIBYUV_BOOL TestEnv(const char*) {
  201. return LIBYUV_FALSE;
  202. }
  203. #endif
  204. LIBYUV_API SAFEBUFFERS
  205. int InitCpuFlags(void) {
  206. // TODO(fbarchard): swap kCpuInit logic so 0 means uninitialized.
  207. int cpu_info = 0;
  208. #if !defined(__pnacl__) && !defined(__CLR_VER) && defined(CPU_X86)
  209. uint32 cpu_info0[4] = { 0, 0, 0, 0 };
  210. uint32 cpu_info1[4] = { 0, 0, 0, 0 };
  211. uint32 cpu_info7[4] = { 0, 0, 0, 0 };
  212. CpuId(0, 0, cpu_info0);
  213. CpuId(1, 0, cpu_info1);
  214. if (cpu_info0[0] >= 7) {
  215. CpuId(7, 0, cpu_info7);
  216. }
  217. cpu_info = ((cpu_info1[3] & 0x04000000) ? kCpuHasSSE2 : 0) |
  218. ((cpu_info1[2] & 0x00000200) ? kCpuHasSSSE3 : 0) |
  219. ((cpu_info1[2] & 0x00080000) ? kCpuHasSSE41 : 0) |
  220. ((cpu_info1[2] & 0x00100000) ? kCpuHasSSE42 : 0) |
  221. ((cpu_info7[1] & 0x00000200) ? kCpuHasERMS : 0) |
  222. ((cpu_info1[2] & 0x00001000) ? kCpuHasFMA3 : 0) |
  223. kCpuHasX86;
  224. #ifdef HAS_XGETBV
  225. // AVX requires CPU has AVX, XSAVE and OSXSave for xgetbv
  226. if (((cpu_info1[2] & 0x1c000000) == 0x1c000000) && // AVX and OSXSave
  227. ((GetXCR0() & 6) == 6)) { // Test OS saves YMM registers
  228. cpu_info |= ((cpu_info7[1] & 0x00000020) ? kCpuHasAVX2 : 0) | kCpuHasAVX;
  229. // Detect AVX512bw
  230. if ((GetXCR0() & 0xe0) == 0xe0) {
  231. cpu_info |= (cpu_info7[1] & 0x40000000) ? kCpuHasAVX3 : 0;
  232. }
  233. }
  234. #endif
  235. // Environment variable overrides for testing.
  236. if (TestEnv("LIBYUV_DISABLE_X86")) {
  237. cpu_info &= ~kCpuHasX86;
  238. }
  239. if (TestEnv("LIBYUV_DISABLE_SSE2")) {
  240. cpu_info &= ~kCpuHasSSE2;
  241. }
  242. if (TestEnv("LIBYUV_DISABLE_SSSE3")) {
  243. cpu_info &= ~kCpuHasSSSE3;
  244. }
  245. if (TestEnv("LIBYUV_DISABLE_SSE41")) {
  246. cpu_info &= ~kCpuHasSSE41;
  247. }
  248. if (TestEnv("LIBYUV_DISABLE_SSE42")) {
  249. cpu_info &= ~kCpuHasSSE42;
  250. }
  251. if (TestEnv("LIBYUV_DISABLE_AVX")) {
  252. cpu_info &= ~kCpuHasAVX;
  253. }
  254. if (TestEnv("LIBYUV_DISABLE_AVX2")) {
  255. cpu_info &= ~kCpuHasAVX2;
  256. }
  257. if (TestEnv("LIBYUV_DISABLE_ERMS")) {
  258. cpu_info &= ~kCpuHasERMS;
  259. }
  260. if (TestEnv("LIBYUV_DISABLE_FMA3")) {
  261. cpu_info &= ~kCpuHasFMA3;
  262. }
  263. if (TestEnv("LIBYUV_DISABLE_AVX3")) {
  264. cpu_info &= ~kCpuHasAVX3;
  265. }
  266. #endif
  267. #if defined(__mips__) && defined(__linux__)
  268. #if defined(__mips_dspr2)
  269. cpu_info |= kCpuHasDSPR2;
  270. #endif
  271. #if defined(__mips_msa)
  272. cpu_info = MipsCpuCaps("/proc/cpuinfo", " msa");
  273. #endif
  274. cpu_info |= kCpuHasMIPS;
  275. if (getenv("LIBYUV_DISABLE_DSPR2")) {
  276. cpu_info &= ~kCpuHasDSPR2;
  277. }
  278. if (getenv("LIBYUV_DISABLE_MSA")) {
  279. cpu_info &= ~kCpuHasMSA;
  280. }
  281. #endif
  282. #if defined(__arm__) || defined(__aarch64__)
  283. // gcc -mfpu=neon defines __ARM_NEON__
  284. // __ARM_NEON__ generates code that requires Neon. NaCL also requires Neon.
  285. // For Linux, /proc/cpuinfo can be tested but without that assume Neon.
  286. #if defined(__ARM_NEON__) || defined(__native_client__) || !defined(__linux__)
  287. cpu_info = kCpuHasNEON;
  288. // For aarch64(arm64), /proc/cpuinfo's feature is not complete, e.g. no neon
  289. // flag in it.
  290. // So for aarch64, neon enabling is hard coded here.
  291. #endif
  292. #if defined(__aarch64__)
  293. cpu_info = kCpuHasNEON;
  294. #else
  295. // Linux arm parse text file for neon detect.
  296. cpu_info = ArmCpuCaps("/proc/cpuinfo");
  297. #endif
  298. cpu_info |= kCpuHasARM;
  299. if (TestEnv("LIBYUV_DISABLE_NEON")) {
  300. cpu_info &= ~kCpuHasNEON;
  301. }
  302. #endif // __arm__
  303. if (TestEnv("LIBYUV_DISABLE_ASM")) {
  304. cpu_info = 0;
  305. }
  306. cpu_info |= kCpuInitialized;
  307. cpu_info_ = cpu_info;
  308. return cpu_info;
  309. }
  310. // Note that use of this function is not thread safe.
  311. LIBYUV_API
  312. void MaskCpuFlags(int enable_flags) {
  313. cpu_info_ = InitCpuFlags() & enable_flags;
  314. }
  315. #ifdef __cplusplus
  316. } // extern "C"
  317. } // namespace libyuv
  318. #endif