• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "media/base/vector_math.h"
6 #include "media/base/vector_math_testing.h"
7 
8 #include <algorithm>
9 
10 #include "base/cpu.h"
11 #include "base/logging.h"
12 #include "build/build_config.h"
13 
14 #if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
15 #include <arm_neon.h>
16 #endif
17 
18 namespace media {
19 namespace vector_math {
20 
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
Initialize()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 
FMAC(const float src[],float scale,int len,float dest[])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);
72 }
73 
FMAC_C(const float src[],float scale,int len,float 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;
77 }
78 
FMUL(const float src[],float scale,int len,float dest[])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);
84 }
85 
FMUL_C(const float src[],float scale,int len,float 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;
89 }
90 
EWMAAndMaxPower(float initial_value,const float src[],int len,float smoothing_factor)91 std::pair<float, float> EWMAAndMaxPower(
92     float initial_value, const float src[], int len, float smoothing_factor) {
93   // Ensure |src| is 16-byte aligned.
94   DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & (kRequiredAlignment - 1));
95   return EWMAAndMaxPower_FUNC(initial_value, src, len, smoothing_factor);
96 }
97 
EWMAAndMaxPower_C(float initial_value,const float src[],int len,float smoothing_factor)98 std::pair<float, float> EWMAAndMaxPower_C(
99     float initial_value, const float src[], int len, float smoothing_factor) {
100   std::pair<float, float> result(initial_value, 0.0f);
101   const float weight_prev = 1.0f - smoothing_factor;
102   for (int i = 0; i < len; ++i) {
103     result.first *= weight_prev;
104     const float sample = src[i];
105     const float sample_squared = sample * sample;
106     result.first += sample_squared * smoothing_factor;
107     result.second = std::max(result.second, sample_squared);
108   }
109   return result;
110 }
111 
112 #if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
FMAC_NEON(const float src[],float scale,int len,float dest[])113 void FMAC_NEON(const float src[], float scale, int len, float dest[]) {
114   const int rem = len % 4;
115   const int last_index = len - rem;
116   float32x4_t m_scale = vmovq_n_f32(scale);
117   for (int i = 0; i < last_index; i += 4) {
118     vst1q_f32(dest + i, vmlaq_f32(
119         vld1q_f32(dest + i), vld1q_f32(src + i), m_scale));
120   }
121 
122   // Handle any remaining values that wouldn't fit in an NEON pass.
123   for (int i = last_index; i < len; ++i)
124     dest[i] += src[i] * scale;
125 }
126 
FMUL_NEON(const float src[],float scale,int len,float dest[])127 void FMUL_NEON(const float src[], float scale, int len, float dest[]) {
128   const int rem = len % 4;
129   const int last_index = len - rem;
130   float32x4_t m_scale = vmovq_n_f32(scale);
131   for (int i = 0; i < last_index; i += 4)
132     vst1q_f32(dest + i, vmulq_f32(vld1q_f32(src + i), m_scale));
133 
134   // Handle any remaining values that wouldn't fit in an NEON pass.
135   for (int i = last_index; i < len; ++i)
136     dest[i] = src[i] * scale;
137 }
138 
EWMAAndMaxPower_NEON(float initial_value,const float src[],int len,float smoothing_factor)139 std::pair<float, float> EWMAAndMaxPower_NEON(
140     float initial_value, const float src[], int len, float smoothing_factor) {
141   // When the recurrence is unrolled, we see that we can split it into 4
142   // separate lanes of evaluation:
143   //
144   // y[n] = a(S[n]^2) + (1-a)(y[n-1])
145   //      = a(S[n]^2) + (1-a)^1(aS[n-1]^2) + (1-a)^2(aS[n-2]^2) + ...
146   //      = z[n] + (1-a)^1(z[n-1]) + (1-a)^2(z[n-2]) + (1-a)^3(z[n-3])
147   //
148   // where z[n] = a(S[n]^2) + (1-a)^4(z[n-4]) + (1-a)^8(z[n-8]) + ...
149   //
150   // Thus, the strategy here is to compute z[n], z[n-1], z[n-2], and z[n-3] in
151   // each of the 4 lanes, and then combine them to give y[n].
152 
153   const int rem = len % 4;
154   const int last_index = len - rem;
155 
156   const float32x4_t smoothing_factor_x4 = vdupq_n_f32(smoothing_factor);
157   const float weight_prev = 1.0f - smoothing_factor;
158   const float32x4_t weight_prev_x4 = vdupq_n_f32(weight_prev);
159   const float32x4_t weight_prev_squared_x4 =
160       vmulq_f32(weight_prev_x4, weight_prev_x4);
161   const float32x4_t weight_prev_4th_x4 =
162       vmulq_f32(weight_prev_squared_x4, weight_prev_squared_x4);
163 
164   // Compute z[n], z[n-1], z[n-2], and z[n-3] in parallel in lanes 3, 2, 1 and
165   // 0, respectively.
166   float32x4_t max_x4 = vdupq_n_f32(0.0f);
167   float32x4_t ewma_x4 = vsetq_lane_f32(initial_value, vdupq_n_f32(0.0f), 3);
168   int i;
169   for (i = 0; i < last_index; i += 4) {
170     ewma_x4 = vmulq_f32(ewma_x4, weight_prev_4th_x4);
171     const float32x4_t sample_x4 = vld1q_f32(src + i);
172     const float32x4_t sample_squared_x4 = vmulq_f32(sample_x4, sample_x4);
173     max_x4 = vmaxq_f32(max_x4, sample_squared_x4);
174     ewma_x4 = vmlaq_f32(ewma_x4, sample_squared_x4, smoothing_factor_x4);
175   }
176 
177   // y[n] = z[n] + (1-a)^1(z[n-1]) + (1-a)^2(z[n-2]) + (1-a)^3(z[n-3])
178   float ewma = vgetq_lane_f32(ewma_x4, 3);
179   ewma_x4 = vmulq_f32(ewma_x4, weight_prev_x4);
180   ewma += vgetq_lane_f32(ewma_x4, 2);
181   ewma_x4 = vmulq_f32(ewma_x4, weight_prev_x4);
182   ewma += vgetq_lane_f32(ewma_x4, 1);
183   ewma_x4 = vmulq_f32(ewma_x4, weight_prev_x4);
184   ewma += vgetq_lane_f32(ewma_x4, 0);
185 
186   // Fold the maximums together to get the overall maximum.
187   float32x2_t max_x2 = vpmax_f32(vget_low_f32(max_x4), vget_high_f32(max_x4));
188   max_x2 = vpmax_f32(max_x2, max_x2);
189 
190   std::pair<float, float> result(ewma, vget_lane_f32(max_x2, 0));
191 
192   // Handle remaining values at the end of |src|.
193   for (; i < len; ++i) {
194     result.first *= weight_prev;
195     const float sample = src[i];
196     const float sample_squared = sample * sample;
197     result.first += sample_squared * smoothing_factor;
198     result.second = std::max(result.second, sample_squared);
199   }
200 
201   return result;
202 }
203 #endif
204 
205 }  // namespace vector_math
206 }  // namespace media
207