Coverage Report

Created: 2026-10-31 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Simd/src/Simd/SimdSse41ResizerNearest.cpp
Line
Count
Source
1
/*
2
* Simd Library (http://ermig1979.github.io/Simd).
3
*
4
* Copyright (c) 2011-2026 Yermalayeu Ihar.
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
* of this software and associated documentation files (the "Software"), to deal
8
* in the Software without restriction, including without limitation the rights
9
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
* copies of the Software, and to permit persons to whom the Software is
11
* furnished to do so, subject to the following conditions:
12
*
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
* SOFTWARE.
23
*/
24
#include "Simd/SimdMemory.h"
25
#include "Simd/SimdStore.h"
26
#include "Simd/SimdResizer.h"
27
#include "Simd/SimdResizerCommon.h"
28
#include "Simd/SimdCopy.h"
29
#include "Simd/SimdParallel.hpp"
30
31
namespace Simd
32
{
33
#ifdef SIMD_SSE41_ENABLE
34
    namespace Sse41
35
    {
36
        ResizerNearest::ResizerNearest(const ResParam& param)
37
72
            : Base::ResizerNearest(param)
38
72
            , _blocks(0)
39
72
        {
40
72
        }
41
42
        size_t ResizerNearest::BlockCountMax(size_t align)
43
23
        {
44
23
            return (size_t)::ceil(float(Simd::Max(_param.srcW, _param.dstW) * _param.PixelSize()) / (align - _param.PixelSize()));
45
23
        }
46
47
        void ResizerNearest::EstimateParams()
48
56
        {
49
56
            if (_blocks)
50
23
                return;
51
33
            Base::ResizerNearest::EstimateParams();
52
33
            const size_t pixelSize = _param.PixelSize();
53
33
            if (pixelSize *_param.dstW < A || pixelSize * _param.srcW < A)
54
8
                return;
55
25
            if (pixelSize < 4 && _param.srcW < 4 * _param.dstW)
56
23
                _blocks = BlockCountMax(A);
57
25
            float scale = (float)_param.srcW / _param.dstW;
58
25
            if (_blocks)
59
23
            {
60
23
                _tails = 0;
61
23
                _ix16x1.Resize(_blocks);
62
23
                _tail16x1.Resize((size_t)::ceil(A * scale / pixelSize));
63
23
                size_t dstRowSize = _param.dstW * pixelSize;
64
23
                int block = 0;
65
23
                _ix16x1[0].src = 0;
66
23
                _ix16x1[0].dst = 0;
67
2.20k
                for (int dstIndex = 0; dstIndex < (int)_param.dstW; ++dstIndex)
68
2.17k
                {
69
2.17k
                    int srcIndex = _ix[dstIndex] / (int)pixelSize;
70
2.17k
                    int dst = dstIndex * (int)pixelSize - _ix16x1[block].dst;
71
2.17k
                    int src = srcIndex * (int)pixelSize - _ix16x1[block].src;
72
2.17k
                    if (src >= int(A - pixelSize) || dst >= int(A - pixelSize))
73
326
                    {
74
326
                        block++;
75
326
                        _ix16x1[block].src = srcIndex * (int)pixelSize;
76
326
                        _ix16x1[block].dst = dstIndex * (int)pixelSize;
77
326
                        if (_ix16x1[block].dst > int(dstRowSize - A))
78
36
                        {
79
36
                            _tail16x1[_tails] = LeftNotZero8i(dstRowSize - _ix16x1[block].dst);
80
36
                            _tails++;
81
36
                        }
82
326
                        dst = 0;
83
326
                        src = srcIndex * (int)pixelSize - _ix16x1[block].src;
84
326
                    }
85
6.70k
                    for(size_t i = 0; i < pixelSize; ++i)
86
4.52k
                        _ix16x1[block].shuffle[dst + i] = uint8_t(src + i);
87
2.17k
                }
88
23
                _blocks = block + 1;
89
23
            }
90
25
        }
91
92
        void ResizerNearest::Shuffle16x1(const uint8_t* src, size_t srcStride, size_t dyBeg, size_t dyEnd, uint8_t* dst, size_t dstStride)
93
46
        {
94
46
            size_t body = _blocks - _tails;
95
3.81k
            for (size_t dy = dyBeg; dy < dyEnd; dy++)
96
3.77k
            {
97
3.77k
                const uint8_t* srcRow = src + _iy[dy] * srcStride;
98
3.77k
                size_t i = 0, t = 0;
99
72.6k
                for (; i < body; ++i)
100
68.8k
                {
101
68.8k
                   const IndexShuffle16x1& index = _ix16x1[i];
102
68.8k
                    __m128i _src = _mm_loadu_si128((__m128i*)(srcRow + index.src));
103
68.8k
                    __m128i _shuffle = _mm_loadu_si128((__m128i*) & index.shuffle);
104
68.8k
                    _mm_storeu_si128((__m128i*)(dst + index.dst), _mm_shuffle_epi8(_src, _shuffle));
105
68.8k
                }
106
9.47k
                for (; i < _blocks; ++i, t++)
107
5.70k
                {
108
5.70k
                    const IndexShuffle16x1& index = _ix16x1[i];
109
5.70k
                    __m128i _src = _mm_loadu_si128((__m128i*)(srcRow + index.src));
110
5.70k
                    __m128i _shuffle = _mm_loadu_si128((__m128i*) & index.shuffle);
111
5.70k
                    StoreMasked<false>((__m128i*)(dst + index.dst), _mm_shuffle_epi8(_src, _shuffle), _tail16x1[t]);
112
5.70k
                }
113
3.77k
                dst += dstStride;
114
3.77k
            }
115
46
        }
116
117
        SIMD_INLINE void CopyPixel12(const uint8_t* src, uint8_t* dst)
118
0
        {
119
0
            __m128i val = _mm_loadu_si128((__m128i*)src);
120
0
            _mm_storeu_si128((__m128i*)dst, val);
121
0
        }
122
        
123
        void ResizerNearest::Resize12(const uint8_t* src, size_t srcStride, size_t dyBeg, size_t dyEnd, uint8_t* dst, size_t dstStride)
124
0
        {
125
0
            size_t body = _param.dstW - 1;
126
0
            for (size_t dy = dyBeg; dy < dyEnd; dy++)
127
0
            {
128
0
                const uint8_t* srcRow = src + _iy[dy] * srcStride;
129
0
                size_t dx = 0, offset = 0;
130
0
                for (; dx < body; dx++, offset += 12)
131
0
                    CopyPixel12(srcRow + _ix[dx], dst + offset);
132
0
                Base::CopyPixel<12>(srcRow + _ix[dx], dst + offset);
133
0
                dst += dstStride;
134
0
            }
135
0
        }
136
137
        void ResizerNearest::Run(const uint8_t* src, size_t srcStride, uint8_t* dst, size_t dstStride)
138
56
        {
139
56
            EstimateParams();
140
56
            if (_blocks)
141
46
            {
142
46
                Simd::Parallel(0, _param.dstH, [&](size_t thread, size_t dstBeg, size_t dstEnd)
143
46
                {
144
46
                    this->Shuffle16x1(src, srcStride, dstBeg, dstEnd, dst + dstBeg * dstStride, dstStride);
145
46
                }, _threads, 1);
146
46
            }
147
10
            else if (_pixelSize == 12)
148
0
            {
149
0
                Simd::Parallel(0, _param.dstH, [&](size_t thread, size_t dstBeg, size_t dstEnd)
150
0
                {
151
0
                    this->Resize12(src, srcStride, dstBeg, dstEnd, dst + dstBeg * dstStride, dstStride);
152
0
                }, _threads, 1);
153
0
            }
154
10
            else
155
10
                Base::ResizerNearest::Run(src, srcStride, dst, dstStride);
156
56
        }
157
    }
158
#endif
159
}
160