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