OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/cpu.h" |
| 6 #include "base/memory/aligned_memory.h" |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/time/time.h" |
| 9 #include "media/base/vector_math.h" |
| 10 #include "media/base/vector_math_testing.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "testing/perf/perf_test.h" |
| 13 |
| 14 using base::TimeTicks; |
| 15 using std::fill; |
| 16 |
| 17 namespace media { |
| 18 |
| 19 static const int kBenchmarkIterations = 200000; |
| 20 static const float kScale = 0.5; |
| 21 static const int kVectorSize = 8192; |
| 22 |
| 23 class VectorMathPerfTest : public testing::Test { |
| 24 public: |
| 25 VectorMathPerfTest() { |
| 26 // Initialize input and output vectors. |
| 27 input_vector_.reset(static_cast<float*>(base::AlignedAlloc( |
| 28 sizeof(float) * kVectorSize, vector_math::kRequiredAlignment))); |
| 29 output_vector_.reset(static_cast<float*>(base::AlignedAlloc( |
| 30 sizeof(float) * kVectorSize, vector_math::kRequiredAlignment))); |
| 31 fill(input_vector_.get(), input_vector_.get() + kVectorSize, 1.0f); |
| 32 fill(output_vector_.get(), output_vector_.get() + kVectorSize, 0.0f); |
| 33 } |
| 34 |
| 35 void RunBenchmark(void (*fn)(const float[], float, int, float[]), |
| 36 bool aligned, |
| 37 const std::string& test_name, |
| 38 const std::string& trace_name) { |
| 39 TimeTicks start = TimeTicks::HighResNow(); |
| 40 for (int i = 0; i < kBenchmarkIterations; ++i) { |
| 41 fn(input_vector_.get(), |
| 42 kScale, |
| 43 kVectorSize - (aligned ? 0 : 1), |
| 44 output_vector_.get()); |
| 45 } |
| 46 double total_time_seconds = (TimeTicks::HighResNow() - start).InSecondsF(); |
| 47 perf_test::PrintResult(test_name, |
| 48 "", |
| 49 trace_name, |
| 50 kBenchmarkIterations / total_time_seconds, |
| 51 "runs/s", |
| 52 true); |
| 53 } |
| 54 |
| 55 protected: |
| 56 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> input_vector_; |
| 57 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> output_vector_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(VectorMathPerfTest); |
| 60 }; |
| 61 |
| 62 // Define platform independent function name for FMAC* perf tests. |
| 63 #if defined(ARCH_CPU_X86_FAMILY) |
| 64 #define FMAC_FUNC FMAC_SSE |
| 65 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) |
| 66 #define FMAC_FUNC FMAC_NEON |
| 67 #endif |
| 68 |
| 69 // Benchmark for each optimized vector_math::FMAC() method. |
| 70 TEST_F(VectorMathPerfTest, FMAC) { |
| 71 // Benchmark FMAC_C(). |
| 72 RunBenchmark( |
| 73 vector_math::FMAC_C, true, "vector_math_fmac", "unoptimized"); |
| 74 #if defined(FMAC_FUNC) |
| 75 #if defined(ARCH_CPU_X86_FAMILY) |
| 76 ASSERT_TRUE(base::CPU().has_sse()); |
| 77 #endif |
| 78 // Benchmark FMAC_FUNC() with unaligned size. |
| 79 ASSERT_NE((kVectorSize - 1) % (vector_math::kRequiredAlignment / |
| 80 sizeof(float)), 0U); |
| 81 RunBenchmark( |
| 82 vector_math::FMAC_FUNC, false, "vector_math_fmac", "optimized_unaligned"); |
| 83 // Benchmark FMAC_FUNC() with aligned size. |
| 84 ASSERT_EQ(kVectorSize % (vector_math::kRequiredAlignment / sizeof(float)), |
| 85 0U); |
| 86 RunBenchmark( |
| 87 vector_math::FMAC_FUNC, true, "vector_math_fmac", "optimized_aligned"); |
| 88 #endif |
| 89 } |
| 90 |
| 91 #undef FMAC_FUNC |
| 92 |
| 93 // Define platform independent function name for FMULBenchmark* tests. |
| 94 #if defined(ARCH_CPU_X86_FAMILY) |
| 95 #define FMUL_FUNC FMUL_SSE |
| 96 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) |
| 97 #define FMUL_FUNC FMUL_NEON |
| 98 #endif |
| 99 |
| 100 // Benchmark for each optimized vector_math::FMUL() method. |
| 101 TEST_F(VectorMathPerfTest, FMUL) { |
| 102 // Benchmark FMUL_C(). |
| 103 RunBenchmark( |
| 104 vector_math::FMUL_C, true, "vector_math_fmul", "unoptimized"); |
| 105 #if defined(FMUL_FUNC) |
| 106 #if defined(ARCH_CPU_X86_FAMILY) |
| 107 ASSERT_TRUE(base::CPU().has_sse()); |
| 108 #endif |
| 109 // Benchmark FMUL_FUNC() with unaligned size. |
| 110 ASSERT_NE((kVectorSize - 1) % (vector_math::kRequiredAlignment / |
| 111 sizeof(float)), 0U); |
| 112 RunBenchmark( |
| 113 vector_math::FMUL_FUNC, false, "vector_math_fmac", "optimized_unaligned"); |
| 114 // Benchmark FMUL_FUNC() with aligned size. |
| 115 ASSERT_EQ(kVectorSize % (vector_math::kRequiredAlignment / sizeof(float)), |
| 116 0U); |
| 117 RunBenchmark( |
| 118 vector_math::FMUL_FUNC, true, "vector_math_fmac", "optimized_aligned"); |
| 119 #endif |
| 120 } |
| 121 |
| 122 #undef FMUL_FUNC |
| 123 |
| 124 } // namespace media |
OLD | NEW |