Home | History | Annotate | Download | only in base
      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 "base/cpu.h"
      9 #include "base/logging.h"
     10 #include "build/build_config.h"
     11 
     12 #if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
     13 #include <arm_neon.h>
     14 #endif
     15 
     16 namespace media {
     17 namespace vector_math {
     18 
     19 // If we know the minimum architecture at compile time, avoid CPU detection.
     20 // Force NaCl code to use C routines since (at present) nothing there uses these
     21 // methods and plumbing the -msse built library is non-trivial.  iOS lies about
     22 // its architecture, so we also need to exclude it here.
     23 #if defined(ARCH_CPU_X86_FAMILY) && !defined(OS_NACL) && !defined(OS_IOS)
     24 #if defined(__SSE__)
     25 #define FMAC_FUNC FMAC_SSE
     26 #define FMUL_FUNC FMUL_SSE
     27 void Initialize() {}
     28 #else
     29 // X86 CPU detection required.  Functions will be set by Initialize().
     30 // TODO(dalecurtis): Once Chrome moves to an SSE baseline this can be removed.
     31 #define FMAC_FUNC g_fmac_proc_
     32 #define FMUL_FUNC g_fmul_proc_
     33 
     34 typedef void (*MathProc)(const float src[], float scale, int len, float dest[]);
     35 static MathProc g_fmac_proc_ = NULL;
     36 static MathProc g_fmul_proc_ = NULL;
     37 
     38 void Initialize() {
     39   CHECK(!g_fmac_proc_);
     40   CHECK(!g_fmul_proc_);
     41   const bool kUseSSE = base::CPU().has_sse();
     42   g_fmac_proc_ = kUseSSE ? FMAC_SSE : FMAC_C;
     43   g_fmul_proc_ = kUseSSE ? FMUL_SSE : FMUL_C;
     44 }
     45 #endif
     46 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
     47 #define FMAC_FUNC FMAC_NEON
     48 #define FMUL_FUNC FMUL_NEON
     49 void Initialize() {}
     50 #else
     51 // Unknown architecture.
     52 #define FMAC_FUNC FMAC_C
     53 #define FMUL_FUNC FMUL_C
     54 void Initialize() {}
     55 #endif
     56 
     57 void FMAC(const float src[], float scale, int len, float dest[]) {
     58   // Ensure |src| and |dest| are 16-byte aligned.
     59   DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & (kRequiredAlignment - 1));
     60   DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & (kRequiredAlignment - 1));
     61   return FMAC_FUNC(src, scale, len, dest);
     62 }
     63 
     64 void FMAC_C(const float src[], float scale, int len, float dest[]) {
     65   for (int i = 0; i < len; ++i)
     66     dest[i] += src[i] * scale;
     67 }
     68 
     69 void FMUL(const float src[], float scale, int len, float dest[]) {
     70   // Ensure |src| and |dest| are 16-byte aligned.
     71   DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & (kRequiredAlignment - 1));
     72   DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & (kRequiredAlignment - 1));
     73   return FMUL_FUNC(src, scale, len, dest);
     74 }
     75 
     76 void FMUL_C(const float src[], float scale, int len, float dest[]) {
     77   for (int i = 0; i < len; ++i)
     78     dest[i] = src[i] * scale;
     79 }
     80 
     81 #if defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON)
     82 void FMAC_NEON(const float src[], float scale, int len, float dest[]) {
     83   const int rem = len % 4;
     84   const int last_index = len - rem;
     85   float32x4_t m_scale = vmovq_n_f32(scale);
     86   for (int i = 0; i < last_index; i += 4) {
     87     vst1q_f32(dest + i, vmlaq_f32(
     88         vld1q_f32(dest + i), vld1q_f32(src + i), m_scale));
     89   }
     90 
     91   // Handle any remaining values that wouldn't fit in an NEON pass.
     92   for (int i = last_index; i < len; ++i)
     93     dest[i] += src[i] * scale;
     94 }
     95 
     96 void FMUL_NEON(const float src[], float scale, int len, float dest[]) {
     97   const int rem = len % 4;
     98   const int last_index = len - rem;
     99   float32x4_t m_scale = vmovq_n_f32(scale);
    100   for (int i = 0; i < last_index; i += 4)
    101     vst1q_f32(dest + i, vmulq_f32(vld1q_f32(src + i), m_scale));
    102 
    103   // Handle any remaining values that wouldn't fit in an NEON pass.
    104   for (int i = last_index; i < len; ++i)
    105     dest[i] = src[i] * scale;
    106 }
    107 #endif
    108 
    109 }  // namespace vector_math
    110 }  // namespace media
    111