aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/SkNxTest.cpp
blob: 5509814c07ee1b6ff27037ef6cd65a12a2fb44ee (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Sk4px.h"
#include "SkNx.h"
#include "SkRandom.h"
#include "Test.h"

template <int N>
static void test_Nf(skiatest::Reporter* r) {

    auto assert_nearly_eq = [&](float eps, const SkNx<N, float>& v,
                                float a, float b, float c, float d) {
        auto close = [=](float a, float b) { return fabsf(a-b) <= eps; };
        float vals[4];
        v.store(vals);
        bool ok = close(vals[0], a) && close(vals[1], b)
               && close(   v[0], a) && close(   v[1], b);
        REPORTER_ASSERT(r, ok);
        if (N == 4) {
            ok = close(vals[2], c) && close(vals[3], d)
              && close(   v[2], c) && close(   v[3], d);
            REPORTER_ASSERT(r, ok);
        }
    };
    auto assert_eq = [&](const SkNx<N, float>& v, float a, float b, float c, float d) {
        return assert_nearly_eq(0, v, a,b,c,d);
    };

    float vals[] = {3, 4, 5, 6};
    SkNx<N,float> a = SkNx<N,float>::Load(vals),
                  b(a),
                  c = a;
    SkNx<N,float> d;
    d = a;

    assert_eq(a, 3, 4, 5, 6);
    assert_eq(b, 3, 4, 5, 6);
    assert_eq(c, 3, 4, 5, 6);
    assert_eq(d, 3, 4, 5, 6);

    assert_eq(a+b, 6, 8, 10, 12);
    assert_eq(a*b, 9, 16, 25, 36);
    assert_eq(a*b-b, 6, 12, 20, 30);
    assert_eq((a*b).sqrt(), 3, 4, 5, 6);
    assert_eq(a/b, 1, 1, 1, 1);
    assert_eq(SkNx<N,float>(0)-a, -3, -4, -5, -6);

    SkNx<N,float> fours(4);

    assert_eq(fours.sqrt(), 2,2,2,2);
    assert_nearly_eq(0.001f, fours.rsqrt(), 0.5, 0.5, 0.5, 0.5);

    assert_nearly_eq(0.001f, fours.invert(), 0.25, 0.25, 0.25, 0.25);

    assert_eq(SkNx<N,float>::Min(a, fours), 3, 4, 4, 4);
    assert_eq(SkNx<N,float>::Max(a, fours), 4, 4, 5, 6);

    // Test some comparisons.  This is not exhaustive.
    REPORTER_ASSERT(r, (a == b).allTrue());
    REPORTER_ASSERT(r, (a+b == a*b-b).anyTrue());
    REPORTER_ASSERT(r, !(a+b == a*b-b).allTrue());
    REPORTER_ASSERT(r, !(a+b == a*b).anyTrue());
    REPORTER_ASSERT(r, !(a != b).anyTrue());
    REPORTER_ASSERT(r, (a < fours).anyTrue());
    REPORTER_ASSERT(r, (a <= fours).anyTrue());
    REPORTER_ASSERT(r, !(a > fours).allTrue());
    REPORTER_ASSERT(r, !(a >= fours).allTrue());
}

DEF_TEST(SkNf, r) {
    test_Nf<2>(r);
    test_Nf<4>(r);
}

template <int N, typename T>
void test_Ni(skiatest::Reporter* r) {
    auto assert_eq = [&](const SkNx<N,T>& v, T a, T b, T c, T d, T e, T f, T g, T h) {
        T vals[8];
        v.store(vals);

        switch (N) {
          case 8: REPORTER_ASSERT(r, vals[4] == e && vals[5] == f && vals[6] == g && vals[7] == h);
          case 4: REPORTER_ASSERT(r, vals[2] == c && vals[3] == d);
          case 2: REPORTER_ASSERT(r, vals[0] == a && vals[1] == b);
        }
        switch (N) {
          case 8: REPORTER_ASSERT(r, v[4] == e && v[5] == f &&
                                     v[6] == g && v[7] == h);
          case 4: REPORTER_ASSERT(r, v[2] == c && v[3] == d);
          case 2: REPORTER_ASSERT(r, v[0] == a && v[1] == b);
        }
    };

    T vals[] = { 1,2,3,4,5,6,7,8 };
    SkNx<N,T> a = SkNx<N,T>::Load(vals),
              b(a),
              c = a;
    SkNx<N,T> d;
    d = a;

    assert_eq(a, 1,2,3,4,5,6,7,8);
    assert_eq(b, 1,2,3,4,5,6,7,8);
    assert_eq(c, 1,2,3,4,5,6,7,8);
    assert_eq(d, 1,2,3,4,5,6,7,8);

    assert_eq(a+a, 2,4,6,8,10,12,14,16);
    assert_eq(a*a, 1,4,9,16,25,36,49,64);
    assert_eq(a*a-a, 0,2,6,12,20,30,42,56);

    assert_eq(a >> 2, 0,0,0,1,1,1,1,2);
    assert_eq(a << 1, 2,4,6,8,10,12,14,16);

    REPORTER_ASSERT(r, a[1] == 2);
}

DEF_TEST(SkNx, r) {
    test_Ni<2, uint16_t>(r);
    test_Ni<4, uint16_t>(r);
    test_Ni<8, uint16_t>(r);

    test_Ni<2, int>(r);
    test_Ni<4, int>(r);
    test_Ni<8, int>(r);
}

DEF_TEST(SkNi_min_lt, r) {
    // Exhaustively check the 8x8 bit space.
    for (int a = 0; a < (1<<8); a++) {
    for (int b = 0; b < (1<<8); b++) {
        Sk16b aw(a), bw(b);
        REPORTER_ASSERT(r, Sk16b::Min(aw, bw)[0] == SkTMin(a, b));
        REPORTER_ASSERT(r, !(aw < bw)[0] == !(a < b));
    }}

    // Exhausting the 16x16 bit space is kind of slow, so only do that in release builds.
#ifdef SK_DEBUG
    SkRandom rand;
    for (int i = 0; i < (1<<16); i++) {
        uint16_t a = rand.nextU() >> 16,
                 b = rand.nextU() >> 16;
        REPORTER_ASSERT(r, Sk16h::Min(Sk16h(a), Sk16h(b))[0] == SkTMin(a, b));
    }
#else
    for (int a = 0; a < (1<<16); a++) {
    for (int b = 0; b < (1<<16); b++) {
        REPORTER_ASSERT(r, Sk16h::Min(Sk16h(a), Sk16h(b))[0] == SkTMin(a, b));
    }}
#endif
}

DEF_TEST(SkNi_saturatedAdd, r) {
    for (int a = 0; a < (1<<8); a++) {
    for (int b = 0; b < (1<<8); b++) {
        int exact = a+b;
        if (exact > 255) { exact = 255; }
        if (exact <   0) { exact =   0; }

        REPORTER_ASSERT(r, Sk16b(a).saturatedAdd(Sk16b(b))[0] == exact);
    }
    }
}

DEF_TEST(Sk4px_muldiv255round, r) {
    for (int a = 0; a < (1<<8); a++) {
    for (int b = 0; b < (1<<8); b++) {
        int exact = (a*b+127)/255;

        // Duplicate a and b 16x each.
        auto av = Sk4px::DupAlpha(a),
             bv = Sk4px::DupAlpha(b);

        // This way should always be exactly correct.
        int correct = (av * bv).div255()[0];
        REPORTER_ASSERT(r, correct == exact);

        // We're a bit more flexible on this method: correct for 0 or 255, otherwise off by <=1.
        int fast = av.approxMulDiv255(bv)[0];
        REPORTER_ASSERT(r, fast-exact >= -1 && fast-exact <= 1);
        if (a == 0 || a == 255 || b == 0 || b == 255) {
            REPORTER_ASSERT(r, fast == exact);
        }
    }
    }
}

DEF_TEST(Sk4px_widening, r) {
    SkPMColor colors[] = {
        SkPreMultiplyColor(0xff00ff00),
        SkPreMultiplyColor(0x40008000),
        SkPreMultiplyColor(0x7f020406),
        SkPreMultiplyColor(0x00000000),
    };
    auto packed = Sk4px::Load4(colors);

    auto wideLo = packed.widenLo(),
         wideHi = packed.widenHi(),
         wideLoHi    = packed.widenLoHi(),
         wideLoHiAlt = wideLo + wideHi;
    REPORTER_ASSERT(r, 0 == memcmp(&wideLoHi, &wideLoHiAlt, sizeof(wideLoHi)));
}

DEF_TEST(SkNx_abs, r) {
    auto fs = Sk4f(0.0f, -0.0f, 2.0f, -4.0f).abs();
    REPORTER_ASSERT(r, fs[0] == 0.0f);
    REPORTER_ASSERT(r, fs[1] == 0.0f);
    REPORTER_ASSERT(r, fs[2] == 2.0f);
    REPORTER_ASSERT(r, fs[3] == 4.0f);
}

DEF_TEST(SkNx_floor, r) {
    auto fs = Sk4f(0.4f, -0.4f, 0.6f, -0.6f).floor();
    REPORTER_ASSERT(r, fs[0] ==  0.0f);
    REPORTER_ASSERT(r, fs[1] == -1.0f);
    REPORTER_ASSERT(r, fs[2] ==  0.0f);
    REPORTER_ASSERT(r, fs[3] == -1.0f);
}

DEF_TEST(SkNx_shuffle, r) {
    Sk4f f4(0,10,20,30);

    Sk2f f2 = SkNx_shuffle<2,1>(f4);
    REPORTER_ASSERT(r, f2[0] == 20);
    REPORTER_ASSERT(r, f2[1] == 10);

    f4 = SkNx_shuffle<0,1,1,0>(f2);
    REPORTER_ASSERT(r, f4[0] == 20);
    REPORTER_ASSERT(r, f4[1] == 10);
    REPORTER_ASSERT(r, f4[2] == 10);
    REPORTER_ASSERT(r, f4[3] == 20);
}

DEF_TEST(SkNx_int_float, r) {
    Sk4f f(-2.3f, 1.0f, 0.45f, 0.6f);

    Sk4i i = SkNx_cast<int>(f);
    REPORTER_ASSERT(r, i[0] == -2);
    REPORTER_ASSERT(r, i[1] ==  1);
    REPORTER_ASSERT(r, i[2] ==  0);
    REPORTER_ASSERT(r, i[3] ==  0);

    f = SkNx_cast<float>(i);
    REPORTER_ASSERT(r, f[0] == -2.0f);
    REPORTER_ASSERT(r, f[1] ==  1.0f);
    REPORTER_ASSERT(r, f[2] ==  0.0f);
    REPORTER_ASSERT(r, f[3] ==  0.0f);
}

#include "SkRandom.h"

DEF_TEST(SkNx_u16_float, r) {
    {
        // u16 --> float
        auto h4 = Sk4h(15, 17, 257, 65535);
        auto f4 = SkNx_cast<float>(h4);
        REPORTER_ASSERT(r, f4[0] == 15.0f);
        REPORTER_ASSERT(r, f4[1] == 17.0f);
        REPORTER_ASSERT(r, f4[2] == 257.0f);
        REPORTER_ASSERT(r, f4[3] == 65535.0f);
    }
    {
        // float -> u16
        auto f4 = Sk4f(15, 17, 257, 65535);
        auto h4 = SkNx_cast<uint16_t>(f4);
        REPORTER_ASSERT(r, h4[0] == 15);
        REPORTER_ASSERT(r, h4[1] == 17);
        REPORTER_ASSERT(r, h4[2] == 257);
        REPORTER_ASSERT(r, h4[3] == 65535);
    }

    // starting with any u16 value, we should be able to have a perfect round-trip in/out of floats
    //
    SkRandom rand;
    for (int i = 0; i < 10000; ++i) {
        const uint16_t s16[4] {
            (uint16_t)rand.nextU16(), (uint16_t)rand.nextU16(),
            (uint16_t)rand.nextU16(), (uint16_t)rand.nextU16(),
        };
        auto u4_0 = Sk4h::Load(s16);
        auto f4 = SkNx_cast<float>(u4_0);
        auto u4_1 = SkNx_cast<uint16_t>(f4);
        uint16_t d16[4];
        u4_1.store(d16);
        REPORTER_ASSERT(r, !memcmp(s16, d16, sizeof(s16)));
    }
}