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.
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"
14 using base::TimeTicks
;
19 static const int kBenchmarkIterations
= 200000;
20 static const float kScale
= 0.5;
21 static const int kVectorSize
= 8192;
23 class VectorMathPerfTest
: public testing::Test
{
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
);
35 void RunBenchmark(void (*fn
)(const float[], float, int, float[]),
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(),
43 kVectorSize
- (aligned
? 0 : 1),
44 output_vector_
.get());
46 double total_time_seconds
= (TimeTicks::HighResNow() - start
).InSecondsF();
47 perf_test::PrintResult(test_name
,
50 kBenchmarkIterations
/ total_time_seconds
,
56 scoped_ptr_malloc
<float, base::ScopedPtrAlignedFree
> input_vector_
;
57 scoped_ptr_malloc
<float, base::ScopedPtrAlignedFree
> output_vector_
;
59 DISALLOW_COPY_AND_ASSIGN(VectorMathPerfTest
);
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
69 // Benchmark for each optimized vector_math::FMAC() method.
70 TEST_F(VectorMathPerfTest
, FMAC
) {
71 // Benchmark FMAC_C().
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());
78 // Benchmark FMAC_FUNC() with unaligned size.
79 ASSERT_NE((kVectorSize
- 1) % (vector_math::kRequiredAlignment
/
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)),
87 vector_math::FMAC_FUNC
, true, "vector_math_fmac", "optimized_aligned");
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
100 // Benchmark for each optimized vector_math::FMUL() method.
101 TEST_F(VectorMathPerfTest
, FMUL
) {
102 // Benchmark FMUL_C().
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());
109 // Benchmark FMUL_FUNC() with unaligned size.
110 ASSERT_NE((kVectorSize
- 1) % (vector_math::kRequiredAlignment
/
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)),
118 vector_math::FMUL_FUNC
, true, "vector_math_fmac", "optimized_aligned");