1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/*
* Simd Library (http://ermig1979.github.io/Simd).
*
* Copyright (c) 2011-2017 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:SimdOperationBinary8u
*
* 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"
namespace Simd
{
#ifdef SIMD_AVX2_ENABLE
namespace Avx2
{
template <SimdOperationBinary8uType type> SIMD_INLINE __m256i OperationBinary8u(const __m256i & a, const __m256i & b);
template <> SIMD_INLINE __m256i OperationBinary8u<SimdOperationBinary8uAverage>(const __m256i & a, const __m256i & b)
{
return _mm256_avg_epu8(a, b);
}
template <> SIMD_INLINE __m256i OperationBinary8u<SimdOperationBinary8uAnd>(const __m256i & a, const __m256i & b)
{
return _mm256_and_si256(a, b);
}
template <> SIMD_INLINE __m256i OperationBinary8u<SimdOperationBinary8uOr>(const __m256i & a, const __m256i & b)
{
return _mm256_or_si256(a, b);
}
template <> SIMD_INLINE __m256i OperationBinary8u<SimdOperationBinary8uMaximum>(const __m256i & a, const __m256i & b)
{
return _mm256_max_epu8(a, b);
}
template <> SIMD_INLINE __m256i OperationBinary8u<SimdOperationBinary8uMinimum>(const __m256i & a, const __m256i & b)
{
return _mm256_min_epu8(a, b);
}
template <> SIMD_INLINE __m256i OperationBinary8u<SimdOperationBinary8uSaturatedSubtraction>(const __m256i & a, const __m256i & b)
{
return _mm256_subs_epu8(a, b);
}
template <> SIMD_INLINE __m256i OperationBinary8u<SimdOperationBinary8uSaturatedAddition>(const __m256i & a, const __m256i & b)
{
return _mm256_adds_epu8(a, b);
}
template <> SIMD_INLINE __m256i OperationBinary8u<SimdOperationBinary8uSubtraction>(const __m256i & a, const __m256i & b)
{
return _mm256_sub_epi8(a, b);
}
template <> SIMD_INLINE __m256i OperationBinary8u<SimdOperationBinary8uAddition>(const __m256i & a, const __m256i & b)
{
return _mm256_add_epi8(a, b);
}
template <bool align, SimdOperationBinary8uType type> void OperationBinary8u(const uint8_t * a, size_t aStride, const uint8_t * b, size_t bStride,
size_t width, size_t height, size_t channelCount, uint8_t * dst, size_t dstStride)
{
assert(width*channelCount >= A);
if (align)
assert(Aligned(a) && Aligned(aStride) && Aligned(b) && Aligned(bStride) && Aligned(dst) && Aligned(dstStride));
size_t size = channelCount*width;
size_t alignedSize = Simd::AlignLo(size, A);
for (size_t row = 0; row < height; ++row)
{
for (size_t offset = 0; offset < alignedSize; offset += A)
{
const __m256i a_ = Load<align>((__m256i*)(a + offset));
const __m256i b_ = Load<align>((__m256i*)(b + offset));
Store<align>((__m256i*)(dst + offset), OperationBinary8u<type>(a_, b_));
}
if (alignedSize != size)
{
const __m256i a_ = Load<false>((__m256i*)(a + size - A));
const __m256i b_ = Load<false>((__m256i*)(b + size - A));
Store<false>((__m256i*)(dst + size - A), OperationBinary8u<type>(a_, b_));
}
a += aStride;
b += bStride;
dst += dstStride;
}
}
template <bool align> void OperationBinary8u(const uint8_t * a, size_t aStride, const uint8_t * b, size_t bStride,
size_t width, size_t height, size_t channelCount, uint8_t * dst, size_t dstStride, SimdOperationBinary8uType type)
{
switch (type)
{
case SimdOperationBinary8uAverage:
return OperationBinary8u<align, SimdOperationBinary8uAverage>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride);
case SimdOperationBinary8uAnd:
return OperationBinary8u<align, SimdOperationBinary8uAnd>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride);
case SimdOperationBinary8uOr:
return OperationBinary8u<align, SimdOperationBinary8uOr>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride);
case SimdOperationBinary8uMaximum:
return OperationBinary8u<align, SimdOperationBinary8uMaximum>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride);
case SimdOperationBinary8uMinimum:
return OperationBinary8u<align, SimdOperationBinary8uMinimum>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride);
case SimdOperationBinary8uSaturatedSubtraction:
return OperationBinary8u<align, SimdOperationBinary8uSaturatedSubtraction>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride);
case SimdOperationBinary8uSaturatedAddition:
return OperationBinary8u<align, SimdOperationBinary8uSaturatedAddition>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride);
case SimdOperationBinary8uSubtraction:
return OperationBinary8u<align, SimdOperationBinary8uSubtraction>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride);
case SimdOperationBinary8uAddition:
return OperationBinary8u<align, SimdOperationBinary8uAddition>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride);
default:
assert(0);
}
}
void OperationBinary8u(const uint8_t * a, size_t aStride, const uint8_t * b, size_t bStride,
size_t width, size_t height, size_t channelCount, uint8_t * dst, size_t dstStride, SimdOperationBinary8uType type)
{
if (Aligned(a) && Aligned(aStride) && Aligned(b) && Aligned(bStride) && Aligned(dst) && Aligned(dstStride))
OperationBinary8u<true>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride, type);
else
OperationBinary8u<false>(a, aStride, b, bStride, width, height, channelCount, dst, dstStride, type);
}
}
#endif// SIMD_AVX2_ENABLE
}
|