Skip to content

Commit

Permalink
*updated Simd Library.
Browse files Browse the repository at this point in the history
  • Loading branch information
ermig1979 committed Apr 28, 2018
1 parent 5a3d102 commit fd89312
Show file tree
Hide file tree
Showing 81 changed files with 9,526 additions and 1,533 deletions.
7 changes: 4 additions & 3 deletions src/3rd/Simd/SimdArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define __SimdArray_h__

#include "Simd/SimdMemory.h"
#include "Simd/SimdEnable.h"

namespace Simd
{
Expand All @@ -33,7 +34,7 @@ namespace Simd
T * const data;
size_t const size;

SIMD_INLINE Array(size_t size_ = 0, bool clear = false)
SIMD_INLINE Array(size_t size_ = 0, bool clear = false, size_t align = SIMD_ALIGN)
: data(0)
, size(0)
{
Expand All @@ -46,15 +47,15 @@ namespace Simd
Simd::Free(data);
}

SIMD_INLINE void Resize(size_t size_, bool clear = false)
SIMD_INLINE void Resize(size_t size_, bool clear = false, size_t align = SIMD_ALIGN)
{
if (size_ != size)
{
if (data)
Simd::Free(data);
*(size_t*)&size = size_;
if (size_)
*(T**)&data = (T*)Simd::Allocate(size * sizeof(T));
*(T**)&data = (T*)Simd::Allocate(size * sizeof(T), align);
}
if (clear)
Clear();
Expand Down
14 changes: 12 additions & 2 deletions src/3rd/Simd/SimdAvx1.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Simd Library (http://ermig1979.github.io/Simd).
*
* Copyright (c) 2011-2017 Yermalayeu Ihar.
* Copyright (c) 2011-2018 Yermalayeu Ihar.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,7 +31,11 @@ namespace Simd
#ifdef SIMD_AVX_ENABLE
namespace Avx
{
void HogLiteFilterFeatures(const float * src, size_t srcStride, size_t srcWidth, size_t srcHeight, size_t featureSize, const float * filter, size_t filterSize, const uint32_t * mask, size_t maskStride, float * dst, size_t dstStride);
void CosineDistance32f(const float * a, const float * b, size_t size, float * distance);

void Gemm32fNN(size_t M, size_t N, size_t K, const float * alpha, const float * A, size_t lda, const float * B, size_t ldb, const float * beta, float * C, size_t ldc);

void HogLiteFilterFeatures(const float * src, size_t srcStride, size_t srcWidth, size_t srcHeight, size_t featureSize, const float * filter, size_t filterWidth, size_t filterHeight, const uint32_t * mask, size_t maskStride, float * dst, size_t dstStride);

void HogLiteResizeFeatures(const float * src, size_t srcStride, size_t srcWidth, size_t srcHeight, size_t featureSize, float * dst, size_t dstStride, size_t dstWidth, size_t dstHeight);

Expand Down Expand Up @@ -100,6 +104,12 @@ namespace Simd
void SquaredDifferenceKahanSum32f(const float * a, const float * b, size_t size, float * sum);

void SvmSumLinear(const float * x, const float * svs, const float * weights, size_t length, size_t count, float * sum);

void SynetAddBias(const float * bias, size_t count, size_t size, float * dst);

void SynetEltwiseLayerForward(float const * const * src, const float * weight, size_t count, size_t size, SimdSynetEltwiseOperationType type, float * dst);

void SynetScaleLayerForward(const float * src, const float * scale, const float * bias, size_t count, size_t size, float * dst);
}
#endif// SIMD_AVX_ENABLE
}
Expand Down
92 changes: 92 additions & 0 deletions src/3rd/Simd/SimdAvx1Float32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Simd Library (http://ermig1979.github.io/Simd).
*
* Copyright (c) 2011-2018 Yermalayeu Ihar.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Simd/SimdMemory.h"
#include "Simd/SimdStore.h"
#include "Simd/SimdExtract.h"

namespace Simd
{
#ifdef SIMD_AVX_ENABLE
namespace Avx
{
template<bool align> void CosineDistance32f(const float * a, const float * b, size_t size, float * distance)
{
if (align)
assert(Aligned(a) && Aligned(b));

size_t partialAlignedSize = AlignLo(size, F);
size_t fullAlignedSize = AlignLo(size, DF);
size_t i = 0;
__m256 _aa[2] = { _mm256_setzero_ps(), _mm256_setzero_ps() };
__m256 _ab[2] = { _mm256_setzero_ps(), _mm256_setzero_ps() };
__m256 _bb[2] = { _mm256_setzero_ps(), _mm256_setzero_ps() };
if (fullAlignedSize)
{
for (; i < fullAlignedSize; i += DF)
{
__m256 a0 = Load<align>(a + i + 0 * F);
__m256 b0 = Load<align>(b + i + 0 * F);
_aa[0] = _mm256_add_ps(_aa[0], _mm256_mul_ps(a0, a0));
_ab[0] = _mm256_add_ps(_ab[0], _mm256_mul_ps(a0, b0));
_bb[0] = _mm256_add_ps(_bb[0], _mm256_mul_ps(b0, b0));
__m256 a1 = Load<align>(a + i + 1 * F);
__m256 b1 = Load<align>(b + i + 1 * F);
_aa[1] = _mm256_add_ps(_aa[1], _mm256_mul_ps(a1, a1));
_ab[1] = _mm256_add_ps(_ab[1], _mm256_mul_ps(a1, b1));
_bb[1] = _mm256_add_ps(_bb[1], _mm256_mul_ps(b1, b1));
}
_aa[0] = _mm256_add_ps(_aa[0], _aa[1]);
_ab[0] = _mm256_add_ps(_ab[0], _ab[1]);
_bb[0] = _mm256_add_ps(_bb[0], _bb[1]);
}
for (; i < partialAlignedSize; i += F)
{
__m256 a0 = Load<align>(a + i);
__m256 b0 = Load<align>(b + i);
_aa[0] = _mm256_add_ps(_aa[0], _mm256_mul_ps(a0, a0));
_ab[0] = _mm256_add_ps(_ab[0], _mm256_mul_ps(a0, b0));
_bb[0] = _mm256_add_ps(_bb[0], _mm256_mul_ps(b0, b0));
}
float aa = ExtractSum(_aa[0]), ab = ExtractSum(_ab[0]), bb = ExtractSum(_bb[0]);
for (; i < size; ++i)
{
float _a = a[i];
float _b = b[i];
aa += _a * _a;
ab += _a * _b;
bb += _b * _b;
}
*distance = 1.0f - ab / ::sqrt(aa*bb);
}

void CosineDistance32f(const float * a, const float * b, size_t size, float * distance)
{
if (Aligned(a) && Aligned(b))
CosineDistance32f<true>(a, b, size, distance);
else
CosineDistance32f<false>(a, b, size, distance);
}
}
#endif// SIMD_AVX_ENABLE
}
Loading

0 comments on commit fd89312

Please sign in to comment.