GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/dye/dyepalette_replaceaoglcolor.cpp Lines: 86 103 83.5 %
Date: 2021-03-17 Branches: 27 38 71.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2007-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#ifdef USE_OPENGL
25
26
#include "resources/dye/dyepalette.h"
27
28
PRAGMA48(GCC diagnostic push)
29
PRAGMA48(GCC diagnostic ignored "-Wshadow")
30
#ifndef SDL_BIG_ENDIAN
31
#include <SDL_endian.h>
32
#endif  // SDL_BYTEORDER
33
PRAGMA48(GCC diagnostic pop)
34
35
#ifdef SIMD_SUPPORTED
36
// avx2
37
#include <immintrin.h>
38
#endif  // SIMD_SUPPORTED
39
40
#include "debug.h"
41
42
6
void DyePalette::replaceAOGLColorDefault(uint32_t *restrict pixels,
43
                                         const int bufSize) const restrict2
44
{
45
12
    STD_VECTOR<DyeColor>::const_iterator it_end = mColors.end();
46
12
    const size_t sz = mColors.size();
47
6
    if (sz == 0U || pixels == nullptr)
48
        return;
49
6
    if ((sz % 2) != 0U)
50
        -- it_end;
51
52
23
    for (const uint32_t *const p_end = pixels + CAST_SIZE(bufSize);
53
23
         pixels != p_end;
54
         ++pixels)
55
    {
56
17
        uint8_t *const p = reinterpret_cast<uint8_t *>(pixels);
57
17
        const unsigned int data = *pixels;
58
59
34
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
60
35
        while (it != it_end)
61
        {
62
31
            const DyeColor &col = *it;
63
31
            ++ it;
64
31
            const DyeColor &col2 = *it;
65
66
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
67
            const unsigned int coldata = (col.value[0] << 24U)
68
                | (col.value[1] << 16U)
69
                | (col.value[2] << 8U)
70
                | col.value[3];
71
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
72
73
31
            const unsigned int coldata = (col.value[0])
74
31
                | (col.value[1] << 8U)
75
31
                | (col.value[2] << 16U)
76
31
                | (col.value[3] << 24U);
77
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
78
79
31
            if (data == coldata)
80
            {
81
13
                p[0] = col2.value[0];
82
13
                p[1] = col2.value[1];
83
13
                p[2] = col2.value[2];
84
13
                p[3] = col2.value[3];
85
13
                break;
86
            }
87
88
            ++ it;
89
        }
90
    }
91
}
92
93
#ifdef SIMD_SUPPORTED
94
/*
95
static void print256(const char *const text, const __m256i &val);
96
static void print256(const char *const text, const __m256i &val)
97
{
98
    printf("%s 0x%016llx%016llx%016llx%016llx\n", text, val[0], val[1], val[2], val[3]);
99
}
100
*/
101
102
__attribute__ ((target ("sse2")))
103
1
void DyePalette::replaceAOGLColorSse2(uint32_t *restrict pixels,
104
                                      const int bufSize) const restrict2
105
{
106
2
    STD_VECTOR<DyeColor>::const_iterator it_end = mColors.end();
107
2
    const size_t sz = mColors.size();
108
1
    if (sz == 0U || pixels == nullptr)
109
        return;
110
1
    if ((sz % 2) != 0U)
111
        -- it_end;
112
113
1
    const int mod = bufSize % 4;
114
1
    const int bufEnd = bufSize - mod;
115
116
3
    for (int ptr = 0; ptr < bufEnd; ptr += 4)
117
    {
118
//        __m128i base = _mm_load_si128(reinterpret_cast<__m128i*>(
119
//            &pixels[ptr]));
120
        __m128i base = _mm_loadu_si128(reinterpret_cast<__m128i*>(
121
4
            &pixels[ptr]));
122
123
4
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
124
6
        while (it != it_end)
125
        {
126
4
            const DyeColor &col = *it;
127
4
            ++ it;
128
4
            const DyeColor &col2 = *it;
129
130
8
            __m128i newMask = _mm_set1_epi32(col2.valueAOgl);
131
8
            __m128i cmpMask = _mm_set1_epi32(col.valueAOgl);
132
4
            __m128i cmpRes = _mm_cmpeq_epi32(base, cmpMask);
133
4
            __m128i srcAnd = _mm_andnot_si128(cmpRes, base);
134
4
            __m128i dstAnd = _mm_and_si128(cmpRes, newMask);
135
4
            base = _mm_or_si128(srcAnd, dstAnd);
136
137
            ++ it;
138
        }
139
//        _mm_store_si128(reinterpret_cast<__m128i*>(&pixels[ptr]), base);
140
4
        _mm_storeu_si128(reinterpret_cast<__m128i*>(&pixels[ptr]), base);
141
    }
142
143
1
    for (int ptr = bufSize - mod; ptr < bufSize; ptr ++)
144
    {
145
        uint8_t *const p = reinterpret_cast<uint8_t *>(&pixels[ptr]);
146
        const unsigned int data = pixels[ptr];
147
148
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
149
        while (it != it_end)
150
        {
151
            const DyeColor &col = *it;
152
            ++ it;
153
            const DyeColor &col2 = *it;
154
155
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
156
            const unsigned int coldata = (col.value[0] << 24U)
157
                | (col.value[1] << 16U)
158
                | (col.value[2] << 8U)
159
                | col.value[3];
160
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
161
162
            const unsigned int coldata = (col.value[0])
163
                | (col.value[1] << 8U)
164
                | (col.value[2] << 16U)
165
                | (col.value[3] << 24U);
166
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
167
168
            if (data == coldata)
169
            {
170
                p[0] = col2.value[0];
171
                p[1] = col2.value[1];
172
                p[2] = col2.value[2];
173
                p[3] = col2.value[3];
174
                break;
175
            }
176
177
            ++ it;
178
        }
179
    }
180
}
181
182
__attribute__ ((target ("avx2")))
183
7
void DyePalette::replaceAOGLColorAvx2(uint32_t *restrict pixels,
184
                                      const int bufSize) const restrict2
185
{
186
14
    STD_VECTOR<DyeColor>::const_iterator it_end = mColors.end();
187
14
    const size_t sz = mColors.size();
188
7
    if (sz == 0U || pixels == nullptr)
189
        return;
190
7
    if ((sz % 2) != 0U)
191
        -- it_end;
192
193
7
    const int mod = bufSize % 8;
194
7
    const int bufEnd = bufSize - mod;
195
196
9
    for (int ptr = 0; ptr < bufEnd; ptr += 8)
197
    {
198
//        __m256i base = _mm256_load_si256(reinterpret_cast<__m256i*>(
199
//            &pixels[ptr]));
200
        __m256i base = _mm256_loadu_si256(reinterpret_cast<__m256i*>(
201
4
            &pixels[ptr]));
202
203
4
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
204
6
        while (it != it_end)
205
        {
206
4
            const DyeColor &col = *it;
207
4
            ++ it;
208
4
            const DyeColor &col2 = *it;
209
210
8
            __m256i newMask = _mm256_set1_epi32(col2.valueAOgl);
211
8
            __m256i cmpMask = _mm256_set1_epi32(col.valueAOgl);
212
4
            __m256i cmpRes = _mm256_cmpeq_epi32(base, cmpMask);
213
4
            __m256i srcAnd = _mm256_andnot_si256(cmpRes, base);
214
4
            __m256i dstAnd = _mm256_and_si256(cmpRes, newMask);
215
4
            base = _mm256_or_si256(srcAnd, dstAnd);
216
217
            ++ it;
218
        }
219
//        _mm256_store_si256(reinterpret_cast<__m256i*>(&pixels[ptr]),
220
//            base);
221
2
        _mm256_storeu_si256(reinterpret_cast<__m256i*>(&pixels[ptr]),
222
2
            base);
223
    }
224
225
25
    for (int ptr = bufSize - mod; ptr < bufSize; ptr ++)
226
    {
227
9
        uint8_t *const p = reinterpret_cast<uint8_t *>(&pixels[ptr]);
228
9
        const unsigned int data = pixels[ptr];
229
230
18
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
231
18
        while (it != it_end)
232
        {
233
16
            const DyeColor &col = *it;
234
16
            ++ it;
235
16
            const DyeColor &col2 = *it;
236
237
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
238
            const unsigned int coldata = (col.value[0] << 24U)
239
                | (col.value[1] << 16U)
240
                | (col.value[2] << 8U)
241
                | col.value[3];
242
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
243
244
16
            const unsigned int coldata = (col.value[0])
245
16
                | (col.value[1] << 8U)
246
16
                | (col.value[2] << 16U)
247
16
                | (col.value[3] << 24U);
248
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
249
250
16
            if (data == coldata)
251
            {
252
7
                p[0] = col2.value[0];
253
7
                p[1] = col2.value[1];
254
7
                p[2] = col2.value[2];
255
7
                p[3] = col2.value[3];
256
7
                break;
257
            }
258
            ++ it;
259
        }
260
    }
261
}
262
263
#endif   // SIMD_SUPPORTED
264
#endif  // USE_OPENGL