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