Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / base / vector_math_perftest.cc
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 int kEWMABenchmarkIterations = 50000;
21 static const float kScale = 0.5;
22 static const int kVectorSize = 8192;
23
24 class VectorMathPerfTest : public testing::Test {
25  public:
26   VectorMathPerfTest() {
27     // Initialize input and output vectors.
28     input_vector_.reset(static_cast<float*>(base::AlignedAlloc(
29         sizeof(float) * kVectorSize, vector_math::kRequiredAlignment)));
30     output_vector_.reset(static_cast<float*>(base::AlignedAlloc(
31         sizeof(float) * kVectorSize, vector_math::kRequiredAlignment)));
32     fill(input_vector_.get(), input_vector_.get() + kVectorSize, 1.0f);
33     fill(output_vector_.get(), output_vector_.get() + kVectorSize, 0.0f);
34   }
35
36   void RunBenchmark(void (*fn)(const float[], float, int, float[]),
37                     bool aligned,
38                     const std::string& test_name,
39                     const std::string& trace_name) {
40     TimeTicks start = TimeTicks::HighResNow();
41     for (int i = 0; i < kBenchmarkIterations; ++i) {
42       fn(input_vector_.get(),
43          kScale,
44          kVectorSize - (aligned ? 0 : 1),
45          output_vector_.get());
46     }
47     double total_time_milliseconds =
48         (TimeTicks::HighResNow() - start).InMillisecondsF();
49     perf_test::PrintResult(test_name,
50                            "",
51                            trace_name,
52                            kBenchmarkIterations / total_time_milliseconds,
53                            "runs/ms",
54                            true);
55   }
56
57   void RunBenchmark(
58       std::pair<float, float> (*fn)(float, const float[], int, float),
59       int len,
60       const std::string& test_name,
61       const std::string& trace_name) {
62     TimeTicks start = TimeTicks::HighResNow();
63     for (int i = 0; i < kEWMABenchmarkIterations; ++i) {
64       fn(0.5f, input_vector_.get(), len, 0.1f);
65     }
66     double total_time_milliseconds =
67         (TimeTicks::HighResNow() - start).InMillisecondsF();
68     perf_test::PrintResult(test_name,
69                            "",
70                            trace_name,
71                            kEWMABenchmarkIterations / total_time_milliseconds,
72                            "runs/ms",
73                            true);
74   }
75
76  protected:
77   scoped_ptr<float, base::AlignedFreeDeleter> input_vector_;
78   scoped_ptr<float, base::AlignedFreeDeleter> output_vector_;
79
80   DISALLOW_COPY_AND_ASSIGN(VectorMathPerfTest);
81 };
82
83 // Define platform independent function name for FMAC* perf tests.
84 #if defined(ARCH_CPU_X86_FAMILY)
85 #define FMAC_FUNC FMAC_SSE
86 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
87 #define FMAC_FUNC FMAC_NEON
88 #endif
89
90 // Benchmark for each optimized vector_math::FMAC() method.
91 TEST_F(VectorMathPerfTest, FMAC) {
92   // Benchmark FMAC_C().
93   RunBenchmark(
94       vector_math::FMAC_C, true, "vector_math_fmac", "unoptimized");
95 #if defined(FMAC_FUNC)
96 #if defined(ARCH_CPU_X86_FAMILY)
97   ASSERT_TRUE(base::CPU().has_sse());
98 #endif
99   // Benchmark FMAC_FUNC() with unaligned size.
100   ASSERT_NE((kVectorSize - 1) % (vector_math::kRequiredAlignment /
101                                  sizeof(float)), 0U);
102   RunBenchmark(
103       vector_math::FMAC_FUNC, false, "vector_math_fmac", "optimized_unaligned");
104   // Benchmark FMAC_FUNC() with aligned size.
105   ASSERT_EQ(kVectorSize % (vector_math::kRequiredAlignment / sizeof(float)),
106             0U);
107   RunBenchmark(
108       vector_math::FMAC_FUNC, true, "vector_math_fmac", "optimized_aligned");
109 #endif
110 }
111
112 #undef FMAC_FUNC
113
114 // Define platform independent function name for FMULBenchmark* tests.
115 #if defined(ARCH_CPU_X86_FAMILY)
116 #define FMUL_FUNC FMUL_SSE
117 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
118 #define FMUL_FUNC FMUL_NEON
119 #endif
120
121 // Benchmark for each optimized vector_math::FMUL() method.
122 TEST_F(VectorMathPerfTest, FMUL) {
123   // Benchmark FMUL_C().
124   RunBenchmark(
125       vector_math::FMUL_C, true, "vector_math_fmul", "unoptimized");
126 #if defined(FMUL_FUNC)
127 #if defined(ARCH_CPU_X86_FAMILY)
128   ASSERT_TRUE(base::CPU().has_sse());
129 #endif
130   // Benchmark FMUL_FUNC() with unaligned size.
131   ASSERT_NE((kVectorSize - 1) % (vector_math::kRequiredAlignment /
132                                  sizeof(float)), 0U);
133   RunBenchmark(
134       vector_math::FMUL_FUNC, false, "vector_math_fmul", "optimized_unaligned");
135   // Benchmark FMUL_FUNC() with aligned size.
136   ASSERT_EQ(kVectorSize % (vector_math::kRequiredAlignment / sizeof(float)),
137             0U);
138   RunBenchmark(
139       vector_math::FMUL_FUNC, true, "vector_math_fmul", "optimized_aligned");
140 #endif
141 }
142
143 #undef FMUL_FUNC
144
145 #if defined(ARCH_CPU_X86_FAMILY)
146 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_SSE
147 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
148 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_NEON
149 #endif
150
151 // Benchmark for each optimized vector_math::EWMAAndMaxPower() method.
152 TEST_F(VectorMathPerfTest, EWMAAndMaxPower) {
153   // Benchmark EWMAAndMaxPower_C().
154   RunBenchmark(vector_math::EWMAAndMaxPower_C,
155                kVectorSize,
156                "vector_math_ewma_and_max_power",
157                "unoptimized");
158 #if defined(EWMAAndMaxPower_FUNC)
159 #if defined(ARCH_CPU_X86_FAMILY)
160   ASSERT_TRUE(base::CPU().has_sse());
161 #endif
162   // Benchmark EWMAAndMaxPower_FUNC() with unaligned size.
163   ASSERT_NE((kVectorSize - 1) % (vector_math::kRequiredAlignment /
164                                  sizeof(float)), 0U);
165   RunBenchmark(vector_math::EWMAAndMaxPower_FUNC,
166                kVectorSize - 1,
167                "vector_math_ewma_and_max_power",
168                "optimized_unaligned");
169   // Benchmark EWMAAndMaxPower_FUNC() with aligned size.
170   ASSERT_EQ(kVectorSize % (vector_math::kRequiredAlignment / sizeof(float)),
171             0U);
172   RunBenchmark(vector_math::EWMAAndMaxPower_FUNC,
173                kVectorSize,
174                "vector_math_ewma_and_max_power",
175                "optimized_aligned");
176 #endif
177 }
178
179 #undef EWMAAndMaxPower_FUNC
180
181 } // namespace media