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.
5 #include "media/base/vector_math.h"
6 #include "media/base/vector_math_testing.h"
11 #include "base/logging.h"
12 #include "build/build_config.h"
14 #if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
19 namespace vector_math
{
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)
26 #define FMAC_FUNC FMAC_SSE
27 #define FMUL_FUNC FMUL_SSE
28 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_SSE
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_
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
;
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
;
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
60 // Unknown architecture.
61 #define FMAC_FUNC FMAC_C
62 #define FMUL_FUNC FMUL_C
63 #define EWMAAndMaxPower_FUNC EWMAAndMaxPower_C
67 void FMAC(const float src
[], float scale
, int len
, float dest
[]) {
68 // Ensure |src| and |dest| are 16-byte aligned.
69 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src
) & (kRequiredAlignment
- 1));
70 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest
) & (kRequiredAlignment
- 1));
71 return FMAC_FUNC(src
, scale
, len
, dest
);
74 void FMAC_C(const float src
[], float scale
, int len
, float dest
[]) {
75 for (int i
= 0; i
< len
; ++i
)
76 dest
[i
] += src
[i
] * scale
;
79 void FMUL(const float src
[], float scale
, int len
, float dest
[]) {
80 // Ensure |src| and |dest| are 16-byte aligned.
81 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src
) & (kRequiredAlignment
- 1));
82 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest
) & (kRequiredAlignment
- 1));
83 return FMUL_FUNC(src
, scale
, len
, dest
);
86 void FMUL_C(const float src
[], float scale
, int len
, float dest
[]) {
87 for (int i
= 0; i
< len
; ++i
)
88 dest
[i
] = src
[i
] * scale
;
91 void Crossfade(const float src
[], int len
, float dest
[]) {
93 const float cf_increment
= 1.0f
/ len
;
94 for (int i
= 0; i
< len
; ++i
, cf_ratio
+= cf_increment
)
95 dest
[i
] = (1.0f
- cf_ratio
) * src
[i
] + cf_ratio
* dest
[i
];
98 std::pair
<float, float> EWMAAndMaxPower(
99 float initial_value
, const float src
[], int len
, float smoothing_factor
) {
100 // Ensure |src| is 16-byte aligned.
101 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src
) & (kRequiredAlignment
- 1));
102 return EWMAAndMaxPower_FUNC(initial_value
, src
, len
, smoothing_factor
);
105 std::pair
<float, float> EWMAAndMaxPower_C(
106 float initial_value
, const float src
[], int len
, float smoothing_factor
) {
107 std::pair
<float, float> result(initial_value
, 0.0f
);
108 const float weight_prev
= 1.0f
- smoothing_factor
;
109 for (int i
= 0; i
< len
; ++i
) {
110 result
.first
*= weight_prev
;
111 const float sample
= src
[i
];
112 const float sample_squared
= sample
* sample
;
113 result
.first
+= sample_squared
* smoothing_factor
;
114 result
.second
= std::max(result
.second
, sample_squared
);
119 #if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
120 void FMAC_NEON(const float src
[], float scale
, int len
, float dest
[]) {
121 const int rem
= len
% 4;
122 const int last_index
= len
- rem
;
123 float32x4_t m_scale
= vmovq_n_f32(scale
);
124 for (int i
= 0; i
< last_index
; i
+= 4) {
125 vst1q_f32(dest
+ i
, vmlaq_f32(
126 vld1q_f32(dest
+ i
), vld1q_f32(src
+ i
), m_scale
));
129 // Handle any remaining values that wouldn't fit in an NEON pass.
130 for (int i
= last_index
; i
< len
; ++i
)
131 dest
[i
] += src
[i
] * scale
;
134 void FMUL_NEON(const float src
[], float scale
, int len
, float dest
[]) {
135 const int rem
= len
% 4;
136 const int last_index
= len
- rem
;
137 float32x4_t m_scale
= vmovq_n_f32(scale
);
138 for (int i
= 0; i
< last_index
; i
+= 4)
139 vst1q_f32(dest
+ i
, vmulq_f32(vld1q_f32(src
+ i
), m_scale
));
141 // Handle any remaining values that wouldn't fit in an NEON pass.
142 for (int i
= last_index
; i
< len
; ++i
)
143 dest
[i
] = src
[i
] * scale
;
146 std::pair
<float, float> EWMAAndMaxPower_NEON(
147 float initial_value
, const float src
[], int len
, float smoothing_factor
) {
148 // When the recurrence is unrolled, we see that we can split it into 4
149 // separate lanes of evaluation:
151 // y[n] = a(S[n]^2) + (1-a)(y[n-1])
152 // = a(S[n]^2) + (1-a)^1(aS[n-1]^2) + (1-a)^2(aS[n-2]^2) + ...
153 // = z[n] + (1-a)^1(z[n-1]) + (1-a)^2(z[n-2]) + (1-a)^3(z[n-3])
155 // where z[n] = a(S[n]^2) + (1-a)^4(z[n-4]) + (1-a)^8(z[n-8]) + ...
157 // Thus, the strategy here is to compute z[n], z[n-1], z[n-2], and z[n-3] in
158 // each of the 4 lanes, and then combine them to give y[n].
160 const int rem
= len
% 4;
161 const int last_index
= len
- rem
;
163 const float32x4_t smoothing_factor_x4
= vdupq_n_f32(smoothing_factor
);
164 const float weight_prev
= 1.0f
- smoothing_factor
;
165 const float32x4_t weight_prev_x4
= vdupq_n_f32(weight_prev
);
166 const float32x4_t weight_prev_squared_x4
=
167 vmulq_f32(weight_prev_x4
, weight_prev_x4
);
168 const float32x4_t weight_prev_4th_x4
=
169 vmulq_f32(weight_prev_squared_x4
, weight_prev_squared_x4
);
171 // Compute z[n], z[n-1], z[n-2], and z[n-3] in parallel in lanes 3, 2, 1 and
173 float32x4_t max_x4
= vdupq_n_f32(0.0f
);
174 float32x4_t ewma_x4
= vsetq_lane_f32(initial_value
, vdupq_n_f32(0.0f
), 3);
176 for (i
= 0; i
< last_index
; i
+= 4) {
177 ewma_x4
= vmulq_f32(ewma_x4
, weight_prev_4th_x4
);
178 const float32x4_t sample_x4
= vld1q_f32(src
+ i
);
179 const float32x4_t sample_squared_x4
= vmulq_f32(sample_x4
, sample_x4
);
180 max_x4
= vmaxq_f32(max_x4
, sample_squared_x4
);
181 ewma_x4
= vmlaq_f32(ewma_x4
, sample_squared_x4
, smoothing_factor_x4
);
184 // y[n] = z[n] + (1-a)^1(z[n-1]) + (1-a)^2(z[n-2]) + (1-a)^3(z[n-3])
185 float ewma
= vgetq_lane_f32(ewma_x4
, 3);
186 ewma_x4
= vmulq_f32(ewma_x4
, weight_prev_x4
);
187 ewma
+= vgetq_lane_f32(ewma_x4
, 2);
188 ewma_x4
= vmulq_f32(ewma_x4
, weight_prev_x4
);
189 ewma
+= vgetq_lane_f32(ewma_x4
, 1);
190 ewma_x4
= vmulq_f32(ewma_x4
, weight_prev_x4
);
191 ewma
+= vgetq_lane_f32(ewma_x4
, 0);
193 // Fold the maximums together to get the overall maximum.
194 float32x2_t max_x2
= vpmax_f32(vget_low_f32(max_x4
), vget_high_f32(max_x4
));
195 max_x2
= vpmax_f32(max_x2
, max_x2
);
197 std::pair
<float, float> result(ewma
, vget_lane_f32(max_x2
, 0));
199 // Handle remaining values at the end of |src|.
200 for (; i
< len
; ++i
) {
201 result
.first
*= weight_prev
;
202 const float sample
= src
[i
];
203 const float sample_squared
= sample
* sample
;
204 result
.first
+= sample_squared
* smoothing_factor
;
205 result
.second
= std::max(result
.second
, sample_squared
);
212 } // namespace vector_math