| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 6 #define _USE_MATH_DEFINES |
| 7 #include <cmath> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/memory/aligned_memory.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/string_number_conversions.h" |
| 13 #include "base/time.h" |
| 14 #include "media/base/vector_math.h" |
| 15 #include "media/base/vector_math_testing.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using base::TimeTicks; |
| 19 using std::fill; |
| 20 |
| 21 // Command line switch for runtime adjustment of benchmark iterations. |
| 22 static const char kBenchmarkIterations[] = "vector-math-iterations"; |
| 23 static const int kDefaultIterations = 10; |
| 24 |
| 25 // Default test values. |
| 26 static const float kScale = 0.5; |
| 27 static const float kInputFillValue = 1.0; |
| 28 static const float kOutputFillValue = 3.0; |
| 29 |
| 30 namespace media { |
| 31 |
| 32 class VectorMathTest : public testing::Test { |
| 33 public: |
| 34 static const int kVectorSize = 8192; |
| 35 |
| 36 VectorMathTest() { |
| 37 // Initialize input and output vectors. |
| 38 input_vector.reset(static_cast<float*>(base::AlignedAlloc( |
| 39 sizeof(float) * kVectorSize, vector_math::kRequiredAlignment))); |
| 40 output_vector.reset(static_cast<float*>(base::AlignedAlloc( |
| 41 sizeof(float) * kVectorSize, vector_math::kRequiredAlignment))); |
| 42 } |
| 43 |
| 44 void FillTestVectors(float input, float output) { |
| 45 // Setup input and output vectors. |
| 46 fill(input_vector.get(), input_vector.get() + kVectorSize, input); |
| 47 fill(output_vector.get(), output_vector.get() + kVectorSize, output); |
| 48 } |
| 49 |
| 50 void VerifyOutput(float value) { |
| 51 for (int i = 0; i < kVectorSize; ++i) |
| 52 ASSERT_FLOAT_EQ(output_vector.get()[i], value); |
| 53 } |
| 54 |
| 55 int BenchmarkIterations() { |
| 56 int vector_math_iterations = kDefaultIterations; |
| 57 std::string iterations( |
| 58 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 59 kBenchmarkIterations)); |
| 60 if (!iterations.empty()) |
| 61 base::StringToInt(iterations, &vector_math_iterations); |
| 62 return vector_math_iterations; |
| 63 } |
| 64 |
| 65 protected: |
| 66 int benchmark_iterations; |
| 67 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> input_vector; |
| 68 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> output_vector; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(VectorMathTest); |
| 71 }; |
| 72 |
| 73 // Ensure each optimized vector_math::FMAC() method returns the same value. |
| 74 TEST_F(VectorMathTest, FMAC) { |
| 75 static const float kResult = kInputFillValue * kScale + kOutputFillValue; |
| 76 |
| 77 { |
| 78 SCOPED_TRACE("FMAC"); |
| 79 FillTestVectors(kInputFillValue, kOutputFillValue); |
| 80 vector_math::FMAC( |
| 81 input_vector.get(), kScale, kVectorSize, output_vector.get()); |
| 82 VerifyOutput(kResult); |
| 83 } |
| 84 |
| 85 { |
| 86 SCOPED_TRACE("FMAC_C"); |
| 87 FillTestVectors(kInputFillValue, kOutputFillValue); |
| 88 vector_math::FMAC_C( |
| 89 input_vector.get(), kScale, kVectorSize, output_vector.get()); |
| 90 VerifyOutput(kResult); |
| 91 } |
| 92 |
| 93 #if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__) |
| 94 { |
| 95 SCOPED_TRACE("FMAC_SSE"); |
| 96 FillTestVectors(kInputFillValue, kOutputFillValue); |
| 97 vector_math::FMAC_SSE( |
| 98 input_vector.get(), kScale, kVectorSize, output_vector.get()); |
| 99 VerifyOutput(kResult); |
| 100 } |
| 101 #endif |
| 102 } |
| 103 |
| 104 // Benchmark for each optimized vector_math::FMAC() method. Original benchmarks |
| 105 // were run with --vector-fmac-iterations=200000. |
| 106 TEST_F(VectorMathTest, FMACBenchmark) { |
| 107 static const int kBenchmarkIterations = BenchmarkIterations(); |
| 108 |
| 109 printf("Benchmarking %d iterations:\n", kBenchmarkIterations); |
| 110 |
| 111 // Benchmark FMAC_C(). |
| 112 FillTestVectors(kInputFillValue, kOutputFillValue); |
| 113 TimeTicks start = TimeTicks::HighResNow(); |
| 114 for (int i = 0; i < kBenchmarkIterations; ++i) { |
| 115 vector_math::FMAC_C( |
| 116 input_vector.get(), kScale, kVectorSize, output_vector.get()); |
| 117 } |
| 118 double total_time_c_ms = (TimeTicks::HighResNow() - start).InMillisecondsF(); |
| 119 printf("FMAC_C took %.2fms.\n", total_time_c_ms); |
| 120 |
| 121 #if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__) |
| 122 // Benchmark FMAC_SSE() with unaligned size. |
| 123 ASSERT_NE((kVectorSize - 1) % (vector_math::kRequiredAlignment / |
| 124 sizeof(float)), 0U); |
| 125 FillTestVectors(kInputFillValue, kOutputFillValue); |
| 126 start = TimeTicks::HighResNow(); |
| 127 for (int j = 0; j < kBenchmarkIterations; ++j) { |
| 128 vector_math::FMAC_SSE( |
| 129 input_vector.get(), kScale, kVectorSize - 1, output_vector.get()); |
| 130 } |
| 131 double total_time_sse_unaligned_ms = |
| 132 (TimeTicks::HighResNow() - start).InMillisecondsF(); |
| 133 printf("FMAC_SSE (unaligned size) took %.2fms; which is %.2fx faster than" |
| 134 " FMAC_C.\n", total_time_sse_unaligned_ms, |
| 135 total_time_c_ms / total_time_sse_unaligned_ms); |
| 136 |
| 137 // Benchmark FMAC_SSE() with aligned size. |
| 138 ASSERT_EQ(kVectorSize % (vector_math::kRequiredAlignment / sizeof(float)), |
| 139 0U); |
| 140 FillTestVectors(kInputFillValue, kOutputFillValue); |
| 141 start = TimeTicks::HighResNow(); |
| 142 for (int j = 0; j < kBenchmarkIterations; ++j) { |
| 143 vector_math::FMAC_SSE( |
| 144 input_vector.get(), kScale, kVectorSize, output_vector.get()); |
| 145 } |
| 146 double total_time_sse_aligned_ms = |
| 147 (TimeTicks::HighResNow() - start).InMillisecondsF(); |
| 148 printf("FMAC_SSE (aligned size) took %.2fms; which is %.2fx faster than" |
| 149 " FMAC_C and %.2fx faster than FMAC_SSE (unaligned size).\n", |
| 150 total_time_sse_aligned_ms, total_time_c_ms / total_time_sse_aligned_ms, |
| 151 total_time_sse_unaligned_ms / total_time_sse_aligned_ms); |
| 152 #endif |
| 153 } |
| 154 |
| 155 } // namespace media |
| OLD | NEW |