cpu_test.cc 4.57 KB
Newer Older
1
/*
2
 *  Copyright 2012 The LibYuv Project Authors. All rights reserved.
3 4 5 6
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS. All contributing project authors may
8 9 10 11 12 13 14 15
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <stdlib.h>
#include <string.h>

#include "libyuv/basic_types.h"
#include "libyuv/cpu_id.h"
16
#include "libyuv/row.h"  // For HAS_ARGBSHUFFLEROW_AVX2.
17
#include "libyuv/version.h"
18
#include "../unit_test/unit_test.h"
19 20 21

namespace libyuv {

22
TEST_F(libyuvTest, TestCpuHas) {
23
  int cpu_flags = TestCpuFlag(-1);
24 25
  printf("Cpu Flags %x\n", cpu_flags);
  int has_arm = TestCpuFlag(kCpuHasARM);
26
  printf("Has ARM %x\n", has_arm);
27
  int has_neon = TestCpuFlag(kCpuHasNEON);
28
  printf("Has NEON %x\n", has_neon);
29
  int has_x86 = TestCpuFlag(kCpuHasX86);
30
  printf("Has X86 %x\n", has_x86);
31
  int has_sse2 = TestCpuFlag(kCpuHasSSE2);
32
  printf("Has SSE2 %x\n", has_sse2);
33
  int has_ssse3 = TestCpuFlag(kCpuHasSSSE3);
34
  printf("Has SSSE3 %x\n", has_ssse3);
35
  int has_sse41 = TestCpuFlag(kCpuHasSSE41);
36
  printf("Has SSE4.1 %x\n", has_sse41);
37
  int has_sse42 = TestCpuFlag(kCpuHasSSE42);
38
  printf("Has SSE4.2 %x\n", has_sse42);
39
  int has_avx = TestCpuFlag(kCpuHasAVX);
40
  printf("Has AVX %x\n", has_avx);
41 42
  int has_avx2 = TestCpuFlag(kCpuHasAVX2);
  printf("Has AVX2 %x\n", has_avx2);
fbarchard@google.com's avatar
fbarchard@google.com committed
43 44
  int has_erms = TestCpuFlag(kCpuHasERMS);
  printf("Has ERMS %x\n", has_erms);
45 46
  int has_fma3 = TestCpuFlag(kCpuHasFMA3);
  printf("Has FMA3 %x\n", has_fma3);
47 48 49 50 51 52
  int has_mips = TestCpuFlag(kCpuHasMIPS);
  printf("Has MIPS %x\n", has_mips);
  int has_mips_dsp = TestCpuFlag(kCpuHasMIPS_DSP);
  printf("Has MIPS DSP %x\n", has_mips_dsp);
  int has_mips_dspr2 = TestCpuFlag(kCpuHasMIPS_DSPR2);
  printf("Has MIPS DSPR2 %x\n", has_mips_dspr2);
53 54
}

55
TEST_F(libyuvTest, TestCompilerHasAVX2) {
56 57 58 59 60
#ifdef _MSC_VER
printf("_MSC_VER %d\n", _MSC_VER);
#endif
#if !defined(LIBYUV_DISABLE_X86) && (defined(GCC_HAS_AVX2) || \
    defined(CLANG_HAS_AVX2) || defined(VISUALC_HAS_AVX2))
61
  printf("Has AVX2 1\n");
62 63 64 65
  // If compiler supports AVX2, the following function is expected to exist:
#if !defined(HAS_ARGBSHUFFLEROW_AVX2)
  EXPECT_TRUE(0);  // HAS_ARGBSHUFFLEROW_AVX2 was expected.
#endif
66 67
#else
  printf("Has AVX2 0\n");
68 69 70 71
  // If compiler does not support AVX2, the following function not expected:
#if defined(HAS_ARGBSHUFFLEROW_AVX2)
  EXPECT_TRUE(0);  // HAS_ARGBSHUFFLEROW_AVX2 was not expected.
#endif
72 73 74
#endif
}

75 76 77 78 79
#if defined(__i386__) || defined(__x86_64__) || \
    defined(_M_IX86) || defined(_M_X64)
TEST_F(libyuvTest, TestCpuId) {
  int has_x86 = TestCpuFlag(kCpuHasX86);
  if (has_x86) {
80
    uint32 cpu_info[4];
81 82 83 84 85 86 87 88 89 90 91
    // Vendor ID:
    // AuthenticAMD AMD processor
    // CentaurHauls Centaur processor
    // CyrixInstead Cyrix processor
    // GenuineIntel Intel processor
    // GenuineTMx86 Transmeta processor
    // Geode by NSC National Semiconductor processor
    // NexGenDriven NexGen processor
    // RiseRiseRise Rise Technology processor
    // SiS SiS SiS  SiS processor
    // UMC UMC UMC  UMC processor
92
    CpuId(0, 0, cpu_info);
93 94 95
    cpu_info[0] = cpu_info[1];  // Reorder output
    cpu_info[1] = cpu_info[3];
    cpu_info[3] = 0;
96 97
    printf("Cpu Vendor: %s %x %x %x\n", reinterpret_cast<char*>(&cpu_info[0]),
           cpu_info[0], cpu_info[1], cpu_info[2]);
98
    EXPECT_EQ(12, strlen(reinterpret_cast<char*>(&cpu_info[0])));
99 100 101 102 103 104 105 106

    // CPU Family and Model
    // 3:0 - Stepping
    // 7:4 - Model
    // 11:8 - Family
    // 13:12 - Processor Type
    // 19:16 - Extended Model
    // 27:20 - Extended Family
107
    CpuId(1, 0, cpu_info);
108 109
    int family = ((cpu_info[0] >> 8) & 0x0f) | ((cpu_info[0] >> 16) & 0xff0);
    int model = ((cpu_info[0] >> 4) & 0x0f) | ((cpu_info[0] >> 12) & 0xf0);
110 111
    printf("Cpu Family %d (0x%x), Model %d (0x%x)\n", family, family,
           model, model);
112 113 114 115
  }
}
#endif

116 117 118 119 120 121 122 123 124
static int FileExists(const char* file_name) {
  FILE* f = fopen(file_name, "r");
  if (!f) {
    return 0;
  }
  fclose(f);
  return 1;
}

125
TEST_F(libyuvTest, TestLinuxNeon) {
126 127 128
  if (FileExists("../../unit_test/testdata/arm_v7.txt")) {
    EXPECT_EQ(0, ArmCpuCaps("../../unit_test/testdata/arm_v7.txt"));
    EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/tegra3.txt"));
129
    EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/juno.txt"));
130
  } else {
131
    printf("WARNING: unable to load \"../../unit_test/testdata/arm_v7.txt\"\n");
132 133
  }
#if defined(__linux__) && defined(__ARM_NEON__)
134
  EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("/proc/cpuinfo"));
135
#endif
136 137 138
}

}  // namespace libyuv