cpu_test.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2012 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 <stdlib.h>
  11. #include <string.h>
  12. #include "../unit_test/unit_test.h"
  13. #include "libyuv/basic_types.h"
  14. #include "libyuv/cpu_id.h"
  15. #include "libyuv/version.h"
  16. namespace libyuv {
  17. TEST_F(LibYUVBaseTest, TestCpuHas) {
  18. int cpu_flags = TestCpuFlag(-1);
  19. printf("Cpu Flags %d\n", cpu_flags);
  20. #if defined(__arm__) || defined(__aarch64__)
  21. int has_arm = TestCpuFlag(kCpuHasARM);
  22. printf("Has ARM %d\n", has_arm);
  23. int has_neon = TestCpuFlag(kCpuHasNEON);
  24. printf("Has NEON %d\n", has_neon);
  25. #endif
  26. int has_x86 = TestCpuFlag(kCpuHasX86);
  27. int has_sse2 = TestCpuFlag(kCpuHasSSE2);
  28. int has_ssse3 = TestCpuFlag(kCpuHasSSSE3);
  29. int has_sse41 = TestCpuFlag(kCpuHasSSE41);
  30. int has_sse42 = TestCpuFlag(kCpuHasSSE42);
  31. int has_avx = TestCpuFlag(kCpuHasAVX);
  32. int has_avx2 = TestCpuFlag(kCpuHasAVX2);
  33. int has_erms = TestCpuFlag(kCpuHasERMS);
  34. int has_fma3 = TestCpuFlag(kCpuHasFMA3);
  35. int has_f16c = TestCpuFlag(kCpuHasF16C);
  36. int has_gfni = TestCpuFlag(kCpuHasGFNI);
  37. int has_avx512bw = TestCpuFlag(kCpuHasAVX512BW);
  38. int has_avx512vl = TestCpuFlag(kCpuHasAVX512VL);
  39. int has_avx512vbmi = TestCpuFlag(kCpuHasAVX512VBMI);
  40. int has_avx512vbmi2 = TestCpuFlag(kCpuHasAVX512VBMI2);
  41. int has_avx512vbitalg = TestCpuFlag(kCpuHasAVX512VBITALG);
  42. int has_avx512vpopcntdq = TestCpuFlag(kCpuHasAVX512VPOPCNTDQ);
  43. printf("Has X86 %d\n", has_x86);
  44. printf("Has SSE2 %d\n", has_sse2);
  45. printf("Has SSSE3 %d\n", has_ssse3);
  46. printf("Has SSE41 %d\n", has_sse41);
  47. printf("Has SSE42 %d\n", has_sse42);
  48. printf("Has AVX %d\n", has_avx);
  49. printf("Has AVX2 %d\n", has_avx2);
  50. printf("Has ERMS %d\n", has_erms);
  51. printf("Has FMA3 %d\n", has_fma3);
  52. printf("Has F16C %d\n", has_f16c);
  53. printf("Has GFNI %d\n", has_gfni);
  54. printf("Has AVX512BW %d\n", has_avx512bw);
  55. printf("Has AVX512VL %d\n", has_avx512vl);
  56. printf("Has AVX512VBMI %d\n", has_avx512vbmi);
  57. printf("Has AVX512VBMI2 %d\n", has_avx512vbmi2);
  58. printf("Has AVX512VBITALG %d\n", has_avx512vbitalg);
  59. printf("Has AVX512VPOPCNTDQ %d\n", has_avx512vpopcntdq);
  60. #if defined(__mips__)
  61. int has_mips = TestCpuFlag(kCpuHasMIPS);
  62. printf("Has MIPS %d\n", has_mips);
  63. int has_msa = TestCpuFlag(kCpuHasMSA);
  64. printf("Has MSA %d\n", has_msa);
  65. int has_mmi = TestCpuFlag(kCpuHasMMI);
  66. printf("Has MMI %d\n", has_mmi);
  67. #endif
  68. }
  69. TEST_F(LibYUVBaseTest, TestCpuCompilerEnabled) {
  70. #if defined(__aarch64__)
  71. printf("Arm64 build\n");
  72. #endif
  73. #if defined(__aarch64__) || defined(__ARM_NEON__) || defined(LIBYUV_NEON)
  74. printf("Neon build enabled\n");
  75. #endif
  76. #if defined(__x86_64__) || defined(_M_X64)
  77. printf("x64 build\n");
  78. #endif
  79. #ifdef _MSC_VER
  80. printf("_MSC_VER %d\n", _MSC_VER);
  81. #endif
  82. #if !defined(LIBYUV_DISABLE_X86) && \
  83. (defined(GCC_HAS_AVX2) || defined(CLANG_HAS_AVX2) || \
  84. defined(VISUALC_HAS_AVX2))
  85. printf("Has AVX2 1\n");
  86. #else
  87. printf("Has AVX2 0\n");
  88. // If compiler does not support AVX2, the following function not expected:
  89. #endif
  90. }
  91. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
  92. defined(_M_X64)
  93. TEST_F(LibYUVBaseTest, TestCpuId) {
  94. int has_x86 = TestCpuFlag(kCpuHasX86);
  95. if (has_x86) {
  96. int cpu_info[4];
  97. // Vendor ID:
  98. // AuthenticAMD AMD processor
  99. // CentaurHauls Centaur processor
  100. // CyrixInstead Cyrix processor
  101. // GenuineIntel Intel processor
  102. // GenuineTMx86 Transmeta processor
  103. // Geode by NSC National Semiconductor processor
  104. // NexGenDriven NexGen processor
  105. // RiseRiseRise Rise Technology processor
  106. // SiS SiS SiS SiS processor
  107. // UMC UMC UMC UMC processor
  108. CpuId(0, 0, cpu_info);
  109. cpu_info[0] = cpu_info[1]; // Reorder output
  110. cpu_info[1] = cpu_info[3];
  111. cpu_info[3] = 0;
  112. printf("Cpu Vendor: %s %x %x %x\n", reinterpret_cast<char*>(&cpu_info[0]),
  113. cpu_info[0], cpu_info[1], cpu_info[2]);
  114. EXPECT_EQ(12u, strlen(reinterpret_cast<char*>(&cpu_info[0])));
  115. // CPU Family and Model
  116. // 3:0 - Stepping
  117. // 7:4 - Model
  118. // 11:8 - Family
  119. // 13:12 - Processor Type
  120. // 19:16 - Extended Model
  121. // 27:20 - Extended Family
  122. CpuId(1, 0, cpu_info);
  123. int family = ((cpu_info[0] >> 8) & 0x0f) | ((cpu_info[0] >> 16) & 0xff0);
  124. int model = ((cpu_info[0] >> 4) & 0x0f) | ((cpu_info[0] >> 12) & 0xf0);
  125. printf("Cpu Family %d (0x%x), Model %d (0x%x)\n", family, family, model,
  126. model);
  127. }
  128. }
  129. #endif
  130. static int FileExists(const char* file_name) {
  131. FILE* f = fopen(file_name, "r");
  132. if (!f) {
  133. return 0;
  134. }
  135. fclose(f);
  136. return 1;
  137. }
  138. TEST_F(LibYUVBaseTest, TestLinuxNeon) {
  139. if (FileExists("../../unit_test/testdata/arm_v7.txt")) {
  140. printf("Note: testing to load \"../../unit_test/testdata/arm_v7.txt\"\n");
  141. EXPECT_EQ(0, ArmCpuCaps("../../unit_test/testdata/arm_v7.txt"));
  142. EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/tegra3.txt"));
  143. EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/juno.txt"));
  144. } else {
  145. printf("WARNING: unable to load \"../../unit_test/testdata/arm_v7.txt\"\n");
  146. }
  147. #if defined(__linux__) && defined(__ARM_NEON__)
  148. EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("/proc/cpuinfo"));
  149. #endif
  150. }
  151. TEST_F(LibYUVBaseTest, TestSetCpuFlags) {
  152. // Reset any masked flags that may have been set so auto init is enabled.
  153. MaskCpuFlags(0);
  154. int original_cpu_flags = TestCpuFlag(-1);
  155. // Test setting different CPU configurations.
  156. int cpu_flags = kCpuHasARM | kCpuHasNEON | kCpuInitialized;
  157. SetCpuFlags(cpu_flags);
  158. EXPECT_EQ(cpu_flags, TestCpuFlag(-1));
  159. cpu_flags = kCpuHasX86 | kCpuInitialized;
  160. SetCpuFlags(cpu_flags);
  161. EXPECT_EQ(cpu_flags, TestCpuFlag(-1));
  162. // Test that setting 0 turns auto-init back on.
  163. SetCpuFlags(0);
  164. EXPECT_EQ(original_cpu_flags, TestCpuFlag(-1));
  165. // Restore the CPU flag mask.
  166. MaskCpuFlags(benchmark_cpu_info_);
  167. }
  168. } // namespace libyuv