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 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
* Simd Library (http://ermig1979.github.io/Simd).
*
* Copyright (c) 2011-2022 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/SimdLoadBlock.h"
#include "Simd/SimdStore.h"
#include "Simd/SimdUnpack.h"
namespace Simd
{
#ifdef SIMD_SSE41_ENABLE
namespace Sse41
{
namespace
{
struct Buffer
{
Buffer(size_t width)
{
_p = Allocate(sizeof(uint16_t) * 3 * width);
src0 = (uint16_t*)_p;
src1 = src0 + width;
src2 = src1 + width;
}
~Buffer()
{
Free(_p);
}
uint16_t * src0;
uint16_t * src1;
uint16_t * src2;
private:
void * _p;
};
}
const __m128i K8_01_02 = SIMD_MM_SET2_EPI8(0x01, 0x02);
template<int part> SIMD_INLINE __m128i BinomialSumUnpackedU8(__m128i a[3])
{
return _mm_add_epi16(_mm_maddubs_epi16(UnpackU8<part>(a[0], a[1]), K8_01_02), UnpackU8<part>(a[2]));
}
template<bool align> SIMD_INLINE void BlurCol(__m128i a[3], uint16_t * b)
{
Store<align>((__m128i*)b + 0, BinomialSumUnpackedU8<0>(a));
Store<align>((__m128i*)b + 1, BinomialSumUnpackedU8<1>(a));
}
template<bool align> SIMD_INLINE __m128i BlurRow16(const Buffer & buffer, size_t offset)
{
return DivideBy16(BinomialSum16(
Load<align>((__m128i*)(buffer.src0 + offset)),
Load<align>((__m128i*)(buffer.src1 + offset)),
Load<align>((__m128i*)(buffer.src2 + offset))));
}
template<bool align> SIMD_INLINE __m128i BlurRow(const Buffer & buffer, size_t offset)
{
return _mm_packus_epi16(BlurRow16<align>(buffer, offset), BlurRow16<align>(buffer, offset + HA));
}
template <bool align, size_t step> void GaussianBlur3x3(
const uint8_t * src, size_t srcStride, size_t width, size_t height, uint8_t * dst, size_t dstStride)
{
assert(step*width >= A);
if (align)
assert(Aligned(src) && Aligned(srcStride) && Aligned(step*width) && Aligned(dst) && Aligned(dstStride));
__m128i a[3];
size_t size = step*width;
size_t bodySize = Simd::AlignHi(size, A) - A;
Buffer buffer(Simd::AlignHi(size, A));
LoadNose3<align, step>(src + 0, a);
BlurCol<true>(a, buffer.src0 + 0);
for (size_t col = A; col < bodySize; col += A)
{
LoadBody3<align, step>(src + col, a);
BlurCol<true>(a, buffer.src0 + col);
}
LoadTail3<align, step>(src + size - A, a);
BlurCol<align>(a, buffer.src0 + size - A);
memcpy(buffer.src1, buffer.src0, sizeof(uint16_t)*size);
for (size_t row = 0; row < height; ++row, dst += dstStride)
{
const uint8_t *src2 = src + srcStride*(row + 1);
if (row >= height - 2)
src2 = src + srcStride*(height - 1);
LoadNose3<align, step>(src2 + 0, a);
BlurCol<true>(a, buffer.src2 + 0);
for (size_t col = A; col < bodySize; col += A)
{
LoadBody3<align, step>(src2 + col, a);
BlurCol<true>(a, buffer.src2 + col);
}
LoadTail3<align, step>(src2 + size - A, a);
BlurCol<align>(a, buffer.src2 + size - A);
for (size_t col = 0; col < bodySize; col += A)
Store<align>((__m128i*)(dst + col), BlurRow<true>(buffer, col));
Store<align>((__m128i*)(dst + size - A), BlurRow<align>(buffer, size - A));
Swap(buffer.src0, buffer.src2);
Swap(buffer.src0, buffer.src1);
}
}
template <bool align> void GaussianBlur3x3(const uint8_t * src, size_t srcStride, size_t width, size_t height,
size_t channelCount, uint8_t * dst, size_t dstStride)
{
assert(channelCount > 0 && channelCount <= 4);
switch (channelCount)
{
case 1: GaussianBlur3x3<align, 1>(src, srcStride, width, height, dst, dstStride); break;
case 2: GaussianBlur3x3<align, 2>(src, srcStride, width, height, dst, dstStride); break;
case 3: GaussianBlur3x3<align, 3>(src, srcStride, width, height, dst, dstStride); break;
case 4: GaussianBlur3x3<align, 4>(src, srcStride, width, height, dst, dstStride); break;
}
}
void GaussianBlur3x3(const uint8_t * src, size_t srcStride, size_t width, size_t height,
size_t channelCount, uint8_t * dst, size_t dstStride)
{
if (Aligned(src) && Aligned(srcStride) && Aligned(channelCount*width) && Aligned(dst) && Aligned(dstStride))
GaussianBlur3x3<true>(src, srcStride, width, height, channelCount, dst, dstStride);
else
GaussianBlur3x3<false>(src, srcStride, width, height, channelCount, dst, dstStride);
}
}
#endif
}
|