Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(471)

Side by Side Diff: media/base/vector_math.cc

Issue 308003004: Remove runtime CPU detection for SSE optimized media/ methods. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove vector math. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/base/vector_math.h" 5 #include "media/base/vector_math.h"
6 #include "media/base/vector_math_testing.h" 6 #include "media/base/vector_math_testing.h"
7 7
8 #include <algorithm> 8 #include <algorithm>
9 9
10 #include "base/cpu.h"
11 #include "base/logging.h" 10 #include "base/logging.h"
12 #include "build/build_config.h" 11 #include "build/build_config.h"
13 12
14 #if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) 13 #if defined(ARCH_CPU_X86_FAMILY)
14 #include <xmmintrin.h>
15 #define FMAC_FUNC FMAC_SSE
16 #define FMUL_FUNC FMUL_SSE
17 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_SSE
18 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
15 #include <arm_neon.h> 19 #include <arm_neon.h>
20 #define FMAC_FUNC FMAC_NEON
21 #define FMUL_FUNC FMUL_NEON
22 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_NEON
23 #else
24 #define FMAC_FUNC FMAC_C
25 #define FMUL_FUNC FMUL_C
26 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_C
16 #endif 27 #endif
17 28
18 namespace media { 29 namespace media {
19 namespace vector_math { 30 namespace vector_math {
20 31
21 // If we know the minimum architecture at compile time, avoid CPU detection.
22 // Force NaCl code to use C routines since (at present) nothing there uses these
23 // methods and plumbing the -msse built library is non-trivial.
24 #if defined(ARCH_CPU_X86_FAMILY) && !defined(OS_NACL)
25 #if defined(__SSE__)
26 #define FMAC_FUNC FMAC_SSE
27 #define FMUL_FUNC FMUL_SSE
28 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_SSE
29 void Initialize() {}
30 #else
31 // X86 CPU detection required. Functions will be set by Initialize().
32 // TODO(dalecurtis): Once Chrome moves to an SSE baseline this can be removed.
33 #define FMAC_FUNC g_fmac_proc_
34 #define FMUL_FUNC g_fmul_proc_
35 #define EWMAAndMaxPower_FUNC g_ewma_power_proc_
36
37 typedef void (*MathProc)(const float src[], float scale, int len, float dest[]);
38 static MathProc g_fmac_proc_ = NULL;
39 static MathProc g_fmul_proc_ = NULL;
40 typedef std::pair<float, float> (*EWMAAndMaxPowerProc)(
41 float initial_value, const float src[], int len, float smoothing_factor);
42 static EWMAAndMaxPowerProc g_ewma_power_proc_ = NULL;
43
44 void Initialize() {
45 CHECK(!g_fmac_proc_);
46 CHECK(!g_fmul_proc_);
47 CHECK(!g_ewma_power_proc_);
48 const bool kUseSSE = base::CPU().has_sse();
49 g_fmac_proc_ = kUseSSE ? FMAC_SSE : FMAC_C;
50 g_fmul_proc_ = kUseSSE ? FMUL_SSE : FMUL_C;
51 g_ewma_power_proc_ = kUseSSE ? EWMAAndMaxPower_SSE : EWMAAndMaxPower_C;
52 }
53 #endif
54 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
55 #define FMAC_FUNC FMAC_NEON
56 #define FMUL_FUNC FMUL_NEON
57 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_NEON
58 void Initialize() {}
59 #else
60 // Unknown architecture.
61 #define FMAC_FUNC FMAC_C
62 #define FMUL_FUNC FMUL_C
63 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_C
64 void Initialize() {}
65 #endif
66
67 void FMAC(const float src[], float scale, int len, float dest[]) { 32 void FMAC(const float src[], float scale, int len, float dest[]) {
68 // Ensure |src| and |dest| are 16-byte aligned. 33 // Ensure |src| and |dest| are 16-byte aligned.
69 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & (kRequiredAlignment - 1)); 34 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & (kRequiredAlignment - 1));
70 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & (kRequiredAlignment - 1)); 35 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & (kRequiredAlignment - 1));
71 return FMAC_FUNC(src, scale, len, dest); 36 return FMAC_FUNC(src, scale, len, dest);
72 } 37 }
73 38
74 void FMAC_C(const float src[], float scale, int len, float dest[]) { 39 void FMAC_C(const float src[], float scale, int len, float dest[]) {
75 for (int i = 0; i < len; ++i) 40 for (int i = 0; i < len; ++i)
76 dest[i] += src[i] * scale; 41 dest[i] += src[i] * scale;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 for (int i = 0; i < len; ++i) { 74 for (int i = 0; i < len; ++i) {
110 result.first *= weight_prev; 75 result.first *= weight_prev;
111 const float sample = src[i]; 76 const float sample = src[i];
112 const float sample_squared = sample * sample; 77 const float sample_squared = sample * sample;
113 result.first += sample_squared * smoothing_factor; 78 result.first += sample_squared * smoothing_factor;
114 result.second = std::max(result.second, sample_squared); 79 result.second = std::max(result.second, sample_squared);
115 } 80 }
116 return result; 81 return result;
117 } 82 }
118 83
84 #if defined(ARCH_CPU_X86_FAMILY)
85 void FMUL_SSE(const float src[], float scale, int len, float dest[]) {
86 const int rem = len % 4;
87 const int last_index = len - rem;
88 __m128 m_scale = _mm_set_ps1(scale);
89 for (int i = 0; i < last_index; i += 4)
90 _mm_store_ps(dest + i, _mm_mul_ps(_mm_load_ps(src + i), m_scale));
91
92 // Handle any remaining values that wouldn't fit in an SSE pass.
93 for (int i = last_index; i < len; ++i)
94 dest[i] = src[i] * scale;
95 }
96
97 void FMAC_SSE(const float src[], float scale, int len, float dest[]) {
98 const int rem = len % 4;
99 const int last_index = len - rem;
100 __m128 m_scale = _mm_set_ps1(scale);
101 for (int i = 0; i < last_index; i += 4) {
102 _mm_store_ps(dest + i, _mm_add_ps(_mm_load_ps(dest + i),
103 _mm_mul_ps(_mm_load_ps(src + i), m_scale)));
104 }
105
106 // Handle any remaining values that wouldn't fit in an SSE pass.
107 for (int i = last_index; i < len; ++i)
108 dest[i] += src[i] * scale;
109 }
110
111 // Convenience macro to extract float 0 through 3 from the vector |a|. This is
112 // needed because compilers other than clang don't support access via
113 // operator[]().
114 #define EXTRACT_FLOAT(a, i) \
115 (i == 0 ? \
116 _mm_cvtss_f32(a) : \
117 _mm_cvtss_f32(_mm_shuffle_ps(a, a, i)))
118
119 std::pair<float, float> EWMAAndMaxPower_SSE(
120 float initial_value, const float src[], int len, float smoothing_factor) {
121 // When the recurrence is unrolled, we see that we can split it into 4
122 // separate lanes of evaluation:
123 //
124 // y[n] = a(S[n]^2) + (1-a)(y[n-1])
125 // = a(S[n]^2) + (1-a)^1(aS[n-1]^2) + (1-a)^2(aS[n-2]^2) + ...
126 // = z[n] + (1-a)^1(z[n-1]) + (1-a)^2(z[n-2]) + (1-a)^3(z[n-3])
127 //
128 // where z[n] = a(S[n]^2) + (1-a)^4(z[n-4]) + (1-a)^8(z[n-8]) + ...
129 //
130 // Thus, the strategy here is to compute z[n], z[n-1], z[n-2], and z[n-3] in
131 // each of the 4 lanes, and then combine them to give y[n].
132
133 const int rem = len % 4;
134 const int last_index = len - rem;
135
136 const __m128 smoothing_factor_x4 = _mm_set_ps1(smoothing_factor);
137 const float weight_prev = 1.0f - smoothing_factor;
138 const __m128 weight_prev_x4 = _mm_set_ps1(weight_prev);
139 const __m128 weight_prev_squared_x4 =
140 _mm_mul_ps(weight_prev_x4, weight_prev_x4);
141 const __m128 weight_prev_4th_x4 =
142 _mm_mul_ps(weight_prev_squared_x4, weight_prev_squared_x4);
143
144 // Compute z[n], z[n-1], z[n-2], and z[n-3] in parallel in lanes 3, 2, 1 and
145 // 0, respectively.
146 __m128 max_x4 = _mm_setzero_ps();
147 __m128 ewma_x4 = _mm_setr_ps(0.0f, 0.0f, 0.0f, initial_value);
148 int i;
149 for (i = 0; i < last_index; i += 4) {
150 ewma_x4 = _mm_mul_ps(ewma_x4, weight_prev_4th_x4);
151 const __m128 sample_x4 = _mm_load_ps(src + i);
152 const __m128 sample_squared_x4 = _mm_mul_ps(sample_x4, sample_x4);
153 max_x4 = _mm_max_ps(max_x4, sample_squared_x4);
154 // Note: The compiler optimizes this to a single multiply-and-accumulate
155 // instruction:
156 ewma_x4 = _mm_add_ps(ewma_x4,
157 _mm_mul_ps(sample_squared_x4, smoothing_factor_x4));
158 }
159
160 // y[n] = z[n] + (1-a)^1(z[n-1]) + (1-a)^2(z[n-2]) + (1-a)^3(z[n-3])
161 float ewma = EXTRACT_FLOAT(ewma_x4, 3);
162 ewma_x4 = _mm_mul_ps(ewma_x4, weight_prev_x4);
163 ewma += EXTRACT_FLOAT(ewma_x4, 2);
164 ewma_x4 = _mm_mul_ps(ewma_x4, weight_prev_x4);
165 ewma += EXTRACT_FLOAT(ewma_x4, 1);
166 ewma_x4 = _mm_mul_ss(ewma_x4, weight_prev_x4);
167 ewma += EXTRACT_FLOAT(ewma_x4, 0);
168
169 // Fold the maximums together to get the overall maximum.
170 max_x4 = _mm_max_ps(max_x4,
171 _mm_shuffle_ps(max_x4, max_x4, _MM_SHUFFLE(3, 3, 1, 1)));
172 max_x4 = _mm_max_ss(max_x4, _mm_shuffle_ps(max_x4, max_x4, 2));
173
174 std::pair<float, float> result(ewma, EXTRACT_FLOAT(max_x4, 0));
175
176 // Handle remaining values at the end of |src|.
177 for (; i < len; ++i) {
178 result.first *= weight_prev;
179 const float sample = src[i];
180 const float sample_squared = sample * sample;
181 result.first += sample_squared * smoothing_factor;
182 result.second = std::max(result.second, sample_squared);
183 }
184
185 return result;
186 }
187 #endif
188
119 #if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) 189 #if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
120 void FMAC_NEON(const float src[], float scale, int len, float dest[]) { 190 void FMAC_NEON(const float src[], float scale, int len, float dest[]) {
121 const int rem = len % 4; 191 const int rem = len % 4;
122 const int last_index = len - rem; 192 const int last_index = len - rem;
123 float32x4_t m_scale = vmovq_n_f32(scale); 193 float32x4_t m_scale = vmovq_n_f32(scale);
124 for (int i = 0; i < last_index; i += 4) { 194 for (int i = 0; i < last_index; i += 4) {
125 vst1q_f32(dest + i, vmlaq_f32( 195 vst1q_f32(dest + i, vmlaq_f32(
126 vld1q_f32(dest + i), vld1q_f32(src + i), m_scale)); 196 vld1q_f32(dest + i), vld1q_f32(src + i), m_scale));
127 } 197 }
128 198
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 result.first += sample_squared * smoothing_factor; 274 result.first += sample_squared * smoothing_factor;
205 result.second = std::max(result.second, sample_squared); 275 result.second = std::max(result.second, sample_squared);
206 } 276 }
207 277
208 return result; 278 return result;
209 } 279 }
210 #endif 280 #endif
211 281
212 } // namespace vector_math 282 } // namespace vector_math
213 } // namespace media 283 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698