This source file includes following definitions.
- Initialize
- Initialize
- Initialize
- Initialize
- FMAC
- FMAC_C
- FMUL
- FMUL_C
- Crossfade
- EWMAAndMaxPower
- EWMAAndMaxPower_C
- FMAC_NEON
- FMUL_NEON
- EWMAAndMaxPower_NEON
#include "media/base/vector_math.h"
#include "media/base/vector_math_testing.h"
#include <algorithm>
#include "base/cpu.h"
#include "base/logging.h"
#include "build/build_config.h"
#if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
#include <arm_neon.h>
#endif
namespace media {
namespace vector_math {
#if defined(ARCH_CPU_X86_FAMILY) && !defined(OS_NACL)
#if defined(__SSE__)
#define FMAC_FUNC FMAC_SSE
#define FMUL_FUNC FMUL_SSE
#define EWMAAndMaxPower_FUNC EWMAAndMaxPower_SSE
void Initialize() {}
#else
#define FMAC_FUNC g_fmac_proc_
#define FMUL_FUNC g_fmul_proc_
#define EWMAAndMaxPower_FUNC g_ewma_power_proc_
typedef void (*MathProc)(const float src[], float scale, int len, float dest[]);
static MathProc g_fmac_proc_ = NULL;
static MathProc g_fmul_proc_ = NULL;
typedef std::pair<float, float> (*EWMAAndMaxPowerProc)(
float initial_value, const float src[], int len, float smoothing_factor);
static EWMAAndMaxPowerProc g_ewma_power_proc_ = NULL;
void Initialize() {
CHECK(!g_fmac_proc_);
CHECK(!g_fmul_proc_);
CHECK(!g_ewma_power_proc_);
const bool kUseSSE = base::CPU().has_sse();
g_fmac_proc_ = kUseSSE ? FMAC_SSE : FMAC_C;
g_fmul_proc_ = kUseSSE ? FMUL_SSE : FMUL_C;
g_ewma_power_proc_ = kUseSSE ? EWMAAndMaxPower_SSE : EWMAAndMaxPower_C;
}
#endif
#elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
#define FMAC_FUNC FMAC_NEON
#define FMUL_FUNC FMUL_NEON
#define EWMAAndMaxPower_FUNC EWMAAndMaxPower_NEON
void Initialize() {}
#else
#define FMAC_FUNC FMAC_C
#define FMUL_FUNC FMUL_C
#define EWMAAndMaxPower_FUNC EWMAAndMaxPower_C
void Initialize() {}
#endif
void FMAC(const float src[], float scale, int len, float dest[]) {
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & (kRequiredAlignment - 1));
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & (kRequiredAlignment - 1));
return FMAC_FUNC(src, scale, len, dest);
}
void FMAC_C(const float src[], float scale, int len, float dest[]) {
for (int i = 0; i < len; ++i)
dest[i] += src[i] * scale;
}
void FMUL(const float src[], float scale, int len, float dest[]) {
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & (kRequiredAlignment - 1));
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & (kRequiredAlignment - 1));
return FMUL_FUNC(src, scale, len, dest);
}
void FMUL_C(const float src[], float scale, int len, float dest[]) {
for (int i = 0; i < len; ++i)
dest[i] = src[i] * scale;
}
void Crossfade(const float src[], int len, float dest[]) {
float cf_ratio = 0;
const float cf_increment = 1.0f / len;
for (int i = 0; i < len; ++i, cf_ratio += cf_increment)
dest[i] = (1.0f - cf_ratio) * src[i] + cf_ratio * dest[i];
}
std::pair<float, float> EWMAAndMaxPower(
float initial_value, const float src[], int len, float smoothing_factor) {
DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & (kRequiredAlignment - 1));
return EWMAAndMaxPower_FUNC(initial_value, src, len, smoothing_factor);
}
std::pair<float, float> EWMAAndMaxPower_C(
float initial_value, const float src[], int len, float smoothing_factor) {
std::pair<float, float> result(initial_value, 0.0f);
const float weight_prev = 1.0f - smoothing_factor;
for (int i = 0; i < len; ++i) {
result.first *= weight_prev;
const float sample = src[i];
const float sample_squared = sample * sample;
result.first += sample_squared * smoothing_factor;
result.second = std::max(result.second, sample_squared);
}
return result;
}
#if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
void FMAC_NEON(const float src[], float scale, int len, float dest[]) {
const int rem = len % 4;
const int last_index = len - rem;
float32x4_t m_scale = vmovq_n_f32(scale);
for (int i = 0; i < last_index; i += 4) {
vst1q_f32(dest + i, vmlaq_f32(
vld1q_f32(dest + i), vld1q_f32(src + i), m_scale));
}
for (int i = last_index; i < len; ++i)
dest[i] += src[i] * scale;
}
void FMUL_NEON(const float src[], float scale, int len, float dest[]) {
const int rem = len % 4;
const int last_index = len - rem;
float32x4_t m_scale = vmovq_n_f32(scale);
for (int i = 0; i < last_index; i += 4)
vst1q_f32(dest + i, vmulq_f32(vld1q_f32(src + i), m_scale));
for (int i = last_index; i < len; ++i)
dest[i] = src[i] * scale;
}
std::pair<float, float> EWMAAndMaxPower_NEON(
float initial_value, const float src[], int len, float smoothing_factor) {
const int rem = len % 4;
const int last_index = len - rem;
const float32x4_t smoothing_factor_x4 = vdupq_n_f32(smoothing_factor);
const float weight_prev = 1.0f - smoothing_factor;
const float32x4_t weight_prev_x4 = vdupq_n_f32(weight_prev);
const float32x4_t weight_prev_squared_x4 =
vmulq_f32(weight_prev_x4, weight_prev_x4);
const float32x4_t weight_prev_4th_x4 =
vmulq_f32(weight_prev_squared_x4, weight_prev_squared_x4);
float32x4_t max_x4 = vdupq_n_f32(0.0f);
float32x4_t ewma_x4 = vsetq_lane_f32(initial_value, vdupq_n_f32(0.0f), 3);
int i;
for (i = 0; i < last_index; i += 4) {
ewma_x4 = vmulq_f32(ewma_x4, weight_prev_4th_x4);
const float32x4_t sample_x4 = vld1q_f32(src + i);
const float32x4_t sample_squared_x4 = vmulq_f32(sample_x4, sample_x4);
max_x4 = vmaxq_f32(max_x4, sample_squared_x4);
ewma_x4 = vmlaq_f32(ewma_x4, sample_squared_x4, smoothing_factor_x4);
}
float ewma = vgetq_lane_f32(ewma_x4, 3);
ewma_x4 = vmulq_f32(ewma_x4, weight_prev_x4);
ewma += vgetq_lane_f32(ewma_x4, 2);
ewma_x4 = vmulq_f32(ewma_x4, weight_prev_x4);
ewma += vgetq_lane_f32(ewma_x4, 1);
ewma_x4 = vmulq_f32(ewma_x4, weight_prev_x4);
ewma += vgetq_lane_f32(ewma_x4, 0);
float32x2_t max_x2 = vpmax_f32(vget_low_f32(max_x4), vget_high_f32(max_x4));
max_x2 = vpmax_f32(max_x2, max_x2);
std::pair<float, float> result(ewma, vget_lane_f32(max_x2, 0));
for (; i < len; ++i) {
result.first *= weight_prev;
const float sample = src[i];
const float sample_squared = sample * sample;
result.first += sample_squared * smoothing_factor;
result.second = std::max(result.second, sample_squared);
}
return result;
}
#endif
}
}