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