Chromium Code Reviews| Index: media/base/vector_math.cc |
| diff --git a/media/base/vector_math.cc b/media/base/vector_math.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..663d25761701106914e709caec25adc09fb1b9bf |
| --- /dev/null |
| +++ b/media/base/vector_math.cc |
| @@ -0,0 +1,58 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/base/vector_math.h" |
| + |
| +#include "base/cpu.h" |
| +#include "base/logging.h" |
| +#include "build/build_config.h" |
| + |
| +#if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__) |
| +#include <xmmintrin.h> |
| +#endif |
| + |
| +namespace media { |
| + |
| +void VectorMath::FMAC(const float src[], float scale, int len, float dest[]) { |
| + // Rely on function level static initialization to keep VectorFMACProc |
| + // selection thread safe. |
| + typedef void (*VectorFMACProc)(const float src[], float scale, int len, |
| + float dest[]); |
| +#if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__) |
| + static const VectorFMACProc kVectorFMACProc = |
| + base::CPU().has_sse() ? FMAC_SSE : FMAC_C; |
| +#else |
| + static const VectorFMACProc kVectorFMACProc = FMAC_C; |
| +#endif |
| + |
| + return kVectorFMACProc(src, scale, len, dest); |
| +} |
| + |
| +void VectorMath::FMAC_C(const float src[], float scale, int len, |
| + float dest[]) { |
| + for (int i = 0; i < len; ++i) |
| + dest[i] += src[i] * scale; |
| +} |
| + |
| +#if defined(ARCH_CPU_X86_FAMILY) && defined(__SSE__) |
| +void VectorMath::FMAC_SSE(const float src[], float scale, int len, |
| + float dest[]) { |
| + // Ensure |src| and |dest| are 16-byte aligned. |
| + DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & 0x0F); |
| + DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & 0x0F); |
|
Ami GONE FROM CHROMIUM
2012/08/23 17:28:12
Do these checks belong in FMAC() instead of here?
DaleCurtis
2012/08/23 19:59:54
Technically no, but since CPU selection is done at
|
| + |
| + __m128 m_scale = _mm_set_ps1(scale); |
| + int rem = len % 4; |
| + for (int i = 0; i < len - rem; i += 4) { |
| + _mm_store_ps(dest + i, _mm_add_ps(_mm_load_ps(dest + i), |
| + _mm_mul_ps(_mm_load_ps(src + i), m_scale))); |
| + } |
| + |
| + // Handle any remaining values that wouldn't fit in an SSE pass. |
| + if (rem) |
| + FMAC_C(src + len - rem, scale, rem, dest + len - rem); |
| +} |
| +#endif |
| + |
| +} // namespace media |