GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/dye/dyepalette_replaceacolor.cpp Lines: 102 102 100.0 %
Date: 2018-06-18 21:15:20 Branches: 32 38 84.2 %

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-2018  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
#include "resources/dye/dyepalette.h"
24
25
PRAGMA48(GCC diagnostic push)
26
PRAGMA48(GCC diagnostic ignored "-Wshadow")
27
#ifndef SDL_BIG_ENDIAN
28
#include <SDL_endian.h>
29
#endif  // SDL_BYTEORDER
30
PRAGMA48(GCC diagnostic pop)
31
32
#ifdef SIMD_SUPPORTED
33
// avx2
34
#include <immintrin.h>
35
#endif  // SIMD_SUPPORTED
36
37
#include "debug.h"
38
39
22
void DyePalette::replaceAColorDefault(uint32_t *restrict pixels,
40
                                      const int bufSize) const restrict2
41
{
42
44
    STD_VECTOR<DyeColor>::const_iterator it_end = mColors.end();
43
44
    const size_t sz = mColors.size();
44
22
    if ((sz == 0u) || (pixels == nullptr))
45
        return;
46
22
    if ((sz % 2) != 0u)
47
        -- it_end;
48
49
124
    for (const uint32_t *const p_end = pixels + CAST_SIZE(bufSize);
50
124
         pixels != p_end;
51
         ++pixels)
52
    {
53
102
        uint8_t *const p = reinterpret_cast<uint8_t *>(pixels);
54
102
        const unsigned int data = *pixels;
55
56
204
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
57
204
        while (it != it_end)
58
        {
59
166
            const DyeColor &col = *it;
60
166
            ++ it;
61
166
            const DyeColor &col2 = *it;
62
63
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
64
            const unsigned int coldata = (col.value[3] << 24U)
65
                | (col.value[2] << 16U)
66
                | (col.value[1] << 8U)
67
                | (col.value[0]);
68
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
69
332
            const unsigned int coldata = (col.value[3])
70
166
                | (col.value[2] << 8U)
71
332
                | (col.value[1] << 16U) |
72
166
                (col.value[0] << 24U);
73
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
74
75
166
            if (data == coldata)
76
            {
77
64
                p[3] = col2.value[0];
78
64
                p[2] = col2.value[1];
79
64
                p[1] = col2.value[2];
80
64
                p[0] = col2.value[3];
81
64
                break;
82
            }
83
84
            ++ it;
85
        }
86
    }
87
}
88
89
#ifdef SIMD_SUPPORTED
90
/*
91
static void print256(const char *const text, const __m256i &val);
92
static void print256(const char *const text, const __m256i &val)
93
{
94
    printf("%s 0x%016llx%016llx%016llx%016llx\n", text, val[0], val[1], val[2], val[3]);
95
}
96
*/
97
98
__attribute__ ((target ("sse2")))
99
22
void DyePalette::replaceAColorSse2(uint32_t *restrict pixels,
100
                                   const int bufSize) const restrict2
101
{
102
44
    STD_VECTOR<DyeColor>::const_iterator it_end = mColors.end();
103
44
    const size_t sz = mColors.size();
104
22
    if ((sz == 0u) || (pixels == nullptr))
105
        return;
106
22
    if ((sz % 2) != 0u)
107
        -- it_end;
108
22
    const int mod = bufSize % 4;
109
22
    const int bufEnd = bufSize - mod;
110
111
40
    for (int ptr = 0; ptr < bufEnd; ptr += 4)
112
    {
113
//        __m128i base = _mm_load_si128(reinterpret_cast<__m128i*>(pixels));
114
        __m128i base = _mm_loadu_si128(reinterpret_cast<__m128i*>(
115
36
            &pixels[ptr]));
116
117
36
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
118
54
        while (it != it_end)
119
        {
120
36
            const DyeColor &col = *it;
121
36
            ++ it;
122
36
            const DyeColor &col2 = *it;
123
124
72
            __m128i newMask = _mm_set1_epi32(col2.valueA);
125
72
            __m128i cmpMask = _mm_set1_epi32(col.valueA);
126
36
            __m128i cmpRes = _mm_cmpeq_epi32(base, cmpMask);
127
36
            __m128i srcAnd = _mm_andnot_si128(cmpRes, base);
128
36
            __m128i dstAnd = _mm_and_si128(cmpRes, newMask);
129
36
            base = _mm_or_si128(srcAnd, dstAnd);
130
131
            ++ it;
132
        }
133
//        _mm_store_si128(reinterpret_cast<__m128i*>(pixels), base);
134
36
        _mm_storeu_si128(reinterpret_cast<__m128i*>(&pixels[ptr]), base);
135
    }
136
137
    // complete end without simd
138
82
    for (int ptr = bufSize - mod; ptr < bufSize; ptr ++)
139
    {
140
30
        uint8_t *const p = reinterpret_cast<uint8_t *>(&pixels[ptr]);
141
30
        const unsigned int data = pixels[ptr];
142
143
60
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
144
54
        while (it != it_end)
145
        {
146
46
            const DyeColor &col = *it;
147
46
            ++ it;
148
46
            const DyeColor &col2 = *it;
149
150
138
            const unsigned int coldata = (col.value[3]) |
151
92
                (col.value[2] << 8U) |
152
92
                (col.value[1] << 16U) |
153
46
                (col.value[0] << 24U);
154
155
46
            if (data == coldata)
156
            {
157
22
                p[3] = col2.value[0];
158
22
                p[2] = col2.value[1];
159
22
                p[1] = col2.value[2];
160
22
                p[0] = col2.value[3];
161
22
                break;
162
            }
163
164
            ++ it;
165
        }
166
    }
167
}
168
169
__attribute__ ((target ("avx2")))
170
46
void DyePalette::replaceAColorAvx2(uint32_t *restrict pixels,
171
                                   const int bufSize) const restrict2
172
{
173
92
    STD_VECTOR<DyeColor>::const_iterator it_end = mColors.end();
174
92
    const size_t sz = mColors.size();
175
46
    if ((sz == 0u) || (pixels == nullptr))
176
        return;
177
46
    if ((sz % 2) != 0u)
178
        -- it_end;
179
46
    const int mod = bufSize % 8;
180
46
    const int bufEnd = bufSize - mod;
181
182
314
    for (int ptr = 0; ptr < bufEnd; ptr += 8)
183
    {
184
//        __m256i base = _mm256_load_si256(reinterpret_cast<__m256i*>(pixels));
185
        __m256i base = _mm256_loadu_si256(reinterpret_cast<__m256i*>(
186
536
            &pixels[ptr]));
187
188
536
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
189
548
        while (it != it_end)
190
        {
191
280
            const DyeColor &col = *it;
192
280
            ++ it;
193
280
            const DyeColor &col2 = *it;
194
195
560
            __m256i newMask = _mm256_set1_epi32(col2.valueA);
196
560
            __m256i cmpMask = _mm256_set1_epi32(col.valueA);
197
280
            __m256i cmpRes = _mm256_cmpeq_epi32(base, cmpMask);
198
280
            __m256i srcAnd = _mm256_andnot_si256(cmpRes, base);
199
280
            __m256i dstAnd = _mm256_and_si256(cmpRes, newMask);
200
280
            base = _mm256_or_si256(srcAnd, dstAnd);
201
202
            ++ it;
203
        }
204
//        _mm256_store_si256(reinterpret_cast<__m256i*>(pixels), base);
205
536
        _mm256_storeu_si256(reinterpret_cast<__m256i*>(&pixels[ptr]), base);
206
    }
207
208
    // complete end without simd
209
262
    for (int ptr = bufSize - mod; ptr < bufSize; ptr ++)
210
    {
211
108
        uint8_t *const p = reinterpret_cast<uint8_t *>(&pixels[ptr]);
212
108
        const unsigned int data = pixels[ptr];
213
214
216
        STD_VECTOR<DyeColor>::const_iterator it = mColors.begin();
215
200
        while (it != it_end)
216
        {
217
168
            const DyeColor &col = *it;
218
168
            ++ it;
219
168
            const DyeColor &col2 = *it;
220
221
504
            const unsigned int coldata = (col.value[3]) |
222
336
                (col.value[2] << 8U) |
223
336
                (col.value[1] << 16U) |
224
168
                (col.value[0] << 24U);
225
226
168
            if (data == coldata)
227
            {
228
76
                p[3] = col2.value[0];
229
76
                p[2] = col2.value[1];
230
76
                p[1] = col2.value[2];
231
76
                p[0] = col2.value[3];
232
76
                break;
233
            }
234
235
            ++ it;
236
        }
237
    }
238
}
239
240
#endif  // SIMD_SUPPORTED