cpu_test.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "libyuv/basic_types.h"
  13. #include "libyuv/cpu_id.h"
  14. #include "libyuv/version.h"
  15. #include "../unit_test/unit_test.h"
  16. namespace libyuv {
  17. TEST_F(LibYUVBaseTest, TestCpuHas) {
  18. int cpu_flags = TestCpuFlag(-1);
  19. printf("Cpu Flags %x\n", cpu_flags);
  20. int has_arm = TestCpuFlag(kCpuHasARM);
  21. printf("Has ARM %x\n", has_arm);
  22. int has_neon = TestCpuFlag(kCpuHasNEON);
  23. printf("Has NEON %x\n", has_neon);
  24. int has_x86 = TestCpuFlag(kCpuHasX86);
  25. printf("Has X86 %x\n", has_x86);
  26. int has_sse2 = TestCpuFlag(kCpuHasSSE2);
  27. printf("Has SSE2 %x\n", has_sse2);
  28. int has_ssse3 = TestCpuFlag(kCpuHasSSSE3);
  29. printf("Has SSSE3 %x\n", has_ssse3);
  30. int has_sse41 = TestCpuFlag(kCpuHasSSE41);
  31. printf("Has SSE4.1 %x\n", has_sse41);
  32. int has_sse42 = TestCpuFlag(kCpuHasSSE42);
  33. printf("Has SSE4.2 %x\n", has_sse42);
  34. int has_avx = TestCpuFlag(kCpuHasAVX);
  35. printf("Has AVX %x\n", has_avx);
  36. int has_avx2 = TestCpuFlag(kCpuHasAVX2);
  37. printf("Has AVX2 %x\n", has_avx2);
  38. int has_erms = TestCpuFlag(kCpuHasERMS);
  39. printf("Has ERMS %x\n", has_erms);
  40. int has_fma3 = TestCpuFlag(kCpuHasFMA3);
  41. printf("Has FMA3 %x\n", has_fma3);
  42. int has_avx3 = TestCpuFlag(kCpuHasAVX3);
  43. printf("Has AVX3 %x\n", has_avx3);
  44. int has_mips = TestCpuFlag(kCpuHasMIPS);
  45. printf("Has MIPS %x\n", has_mips);
  46. int has_dspr2 = TestCpuFlag(kCpuHasDSPR2);
  47. printf("Has DSPR2 %x\n", has_dspr2);
  48. }
  49. TEST_F(LibYUVBaseTest, TestCpuCompilerEnabled) {
  50. #if defined(__aarch64__)
  51. printf("Arm64 build\n");
  52. #endif
  53. #if defined(__aarch64__) || defined(__ARM_NEON__) || defined(LIBYUV_NEON)
  54. printf("Neon build enabled\n");
  55. #endif
  56. #if defined(__x86_64__) || defined(_M_X64)
  57. printf("x64 build\n");
  58. #endif
  59. #ifdef _MSC_VER
  60. printf("_MSC_VER %d\n", _MSC_VER);
  61. #endif
  62. #if !defined(LIBYUV_DISABLE_X86) && (defined(GCC_HAS_AVX2) || \
  63. defined(CLANG_HAS_AVX2) || defined(VISUALC_HAS_AVX2))
  64. printf("Has AVX2 1\n");
  65. #else
  66. printf("Has AVX2 0\n");
  67. // If compiler does not support AVX2, the following function not expected:
  68. #endif
  69. }
  70. #if defined(__i386__) || defined(__x86_64__) || \
  71. defined(_M_IX86) || defined(_M_X64)
  72. TEST_F(LibYUVBaseTest, TestCpuId) {
  73. int has_x86 = TestCpuFlag(kCpuHasX86);
  74. if (has_x86) {
  75. uint32 cpu_info[4];
  76. // Vendor ID:
  77. // AuthenticAMD AMD processor
  78. // CentaurHauls Centaur processor
  79. // CyrixInstead Cyrix processor
  80. // GenuineIntel Intel processor
  81. // GenuineTMx86 Transmeta processor
  82. // Geode by NSC National Semiconductor processor
  83. // NexGenDriven NexGen processor
  84. // RiseRiseRise Rise Technology processor
  85. // SiS SiS SiS SiS processor
  86. // UMC UMC UMC UMC processor
  87. CpuId(0, 0, cpu_info);
  88. cpu_info[0] = cpu_info[1]; // Reorder output
  89. cpu_info[1] = cpu_info[3];
  90. cpu_info[3] = 0;
  91. printf("Cpu Vendor: %s %x %x %x\n", reinterpret_cast<char*>(&cpu_info[0]),
  92. cpu_info[0], cpu_info[1], cpu_info[2]);
  93. EXPECT_EQ(12, strlen(reinterpret_cast<char*>(&cpu_info[0])));
  94. // CPU Family and Model
  95. // 3:0 - Stepping
  96. // 7:4 - Model
  97. // 11:8 - Family
  98. // 13:12 - Processor Type
  99. // 19:16 - Extended Model
  100. // 27:20 - Extended Family
  101. CpuId(1, 0, cpu_info);
  102. int family = ((cpu_info[0] >> 8) & 0x0f) | ((cpu_info[0] >> 16) & 0xff0);
  103. int model = ((cpu_info[0] >> 4) & 0x0f) | ((cpu_info[0] >> 12) & 0xf0);
  104. printf("Cpu Family %d (0x%x), Model %d (0x%x)\n", family, family,
  105. model, model);
  106. }
  107. }
  108. #endif
  109. static int FileExists(const char* file_name) {
  110. FILE* f = fopen(file_name, "r");
  111. if (!f) {
  112. return 0;
  113. }
  114. fclose(f);
  115. return 1;
  116. }
  117. TEST_F(LibYUVBaseTest, TestLinuxNeon) {
  118. if (FileExists("../../unit_test/testdata/arm_v7.txt")) {
  119. EXPECT_EQ(0, ArmCpuCaps("../../unit_test/testdata/arm_v7.txt"));
  120. EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/tegra3.txt"));
  121. EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/juno.txt"));
  122. } else {
  123. printf("WARNING: unable to load \"../../unit_test/testdata/arm_v7.txt\"\n");
  124. }
  125. #if defined(__linux__) && defined(__ARM_NEON__)
  126. EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("/proc/cpuinfo"));
  127. #endif
  128. }
  129. } // namespace libyuv