blob: 605e8024baf6b88708eef920109083dc0d981795 [file] [log] [blame]
mtkleinc9adb052015-03-30 10:50:27 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkNx.h"
9#include "include/utils/SkRandom.h"
10#include "src/core/Sk4px.h"
11#include "tests/Test.h"
mtkleinc9adb052015-03-30 10:50:27 -070012
mtklein6f797092015-11-09 08:33:53 -080013template <int N>
mtkleinc9adb052015-03-30 10:50:27 -070014static void test_Nf(skiatest::Reporter* r) {
15
mtklein6c221b42015-11-20 13:53:19 -080016 auto assert_nearly_eq = [&](float eps, const SkNx<N, float>& v,
17 float a, float b, float c, float d) {
mtklein6f797092015-11-09 08:33:53 -080018 auto close = [=](float a, float b) { return fabsf(a-b) <= eps; };
19 float vals[4];
mtkleinc9adb052015-03-30 10:50:27 -070020 v.store(vals);
mtkleina156a8f2015-04-03 06:16:13 -070021 bool ok = close(vals[0], a) && close(vals[1], b)
mtklein7c249e52016-02-21 10:54:19 -080022 && close( v[0], a) && close( v[1], b);
mtkleina156a8f2015-04-03 06:16:13 -070023 REPORTER_ASSERT(r, ok);
mtkleinc9adb052015-03-30 10:50:27 -070024 if (N == 4) {
mtkleina156a8f2015-04-03 06:16:13 -070025 ok = close(vals[2], c) && close(vals[3], d)
mtklein7c249e52016-02-21 10:54:19 -080026 && close( v[2], c) && close( v[3], d);
mtkleina156a8f2015-04-03 06:16:13 -070027 REPORTER_ASSERT(r, ok);
mtkleinc9adb052015-03-30 10:50:27 -070028 }
29 };
mtklein6c221b42015-11-20 13:53:19 -080030 auto assert_eq = [&](const SkNx<N, float>& v, float a, float b, float c, float d) {
mtkleinc9adb052015-03-30 10:50:27 -070031 return assert_nearly_eq(0, v, a,b,c,d);
32 };
33
mtklein6f797092015-11-09 08:33:53 -080034 float vals[] = {3, 4, 5, 6};
mtklein6c221b42015-11-20 13:53:19 -080035 SkNx<N,float> a = SkNx<N,float>::Load(vals),
36 b(a),
37 c = a;
38 SkNx<N,float> d;
mtkleinc9adb052015-03-30 10:50:27 -070039 d = a;
40
41 assert_eq(a, 3, 4, 5, 6);
42 assert_eq(b, 3, 4, 5, 6);
43 assert_eq(c, 3, 4, 5, 6);
44 assert_eq(d, 3, 4, 5, 6);
45
46 assert_eq(a+b, 6, 8, 10, 12);
47 assert_eq(a*b, 9, 16, 25, 36);
48 assert_eq(a*b-b, 6, 12, 20, 30);
49 assert_eq((a*b).sqrt(), 3, 4, 5, 6);
50 assert_eq(a/b, 1, 1, 1, 1);
mtklein6c221b42015-11-20 13:53:19 -080051 assert_eq(SkNx<N,float>(0)-a, -3, -4, -5, -6);
mtkleinc9adb052015-03-30 10:50:27 -070052
mtklein6c221b42015-11-20 13:53:19 -080053 SkNx<N,float> fours(4);
mtkleinc9adb052015-03-30 10:50:27 -070054
55 assert_eq(fours.sqrt(), 2,2,2,2);
mtkleinf8f90e42016-03-21 10:04:46 -070056 assert_nearly_eq(0.001f, fours.rsqrt(), 0.5, 0.5, 0.5, 0.5);
mtkleinc9adb052015-03-30 10:50:27 -070057
mtkleinf8f90e42016-03-21 10:04:46 -070058 assert_nearly_eq(0.001f, fours.invert(), 0.25, 0.25, 0.25, 0.25);
mtkleinc9adb052015-03-30 10:50:27 -070059
mtklein6c221b42015-11-20 13:53:19 -080060 assert_eq(SkNx<N,float>::Min(a, fours), 3, 4, 4, 4);
61 assert_eq(SkNx<N,float>::Max(a, fours), 4, 4, 5, 6);
mtkleinc9adb052015-03-30 10:50:27 -070062
63 // Test some comparisons. This is not exhaustive.
64 REPORTER_ASSERT(r, (a == b).allTrue());
65 REPORTER_ASSERT(r, (a+b == a*b-b).anyTrue());
66 REPORTER_ASSERT(r, !(a+b == a*b-b).allTrue());
67 REPORTER_ASSERT(r, !(a+b == a*b).anyTrue());
68 REPORTER_ASSERT(r, !(a != b).anyTrue());
69 REPORTER_ASSERT(r, (a < fours).anyTrue());
70 REPORTER_ASSERT(r, (a <= fours).anyTrue());
71 REPORTER_ASSERT(r, !(a > fours).allTrue());
72 REPORTER_ASSERT(r, !(a >= fours).allTrue());
73}
74
75DEF_TEST(SkNf, r) {
mtklein6f797092015-11-09 08:33:53 -080076 test_Nf<2>(r);
77 test_Nf<4>(r);
mtkleinc9adb052015-03-30 10:50:27 -070078}
mtklein115acee2015-04-14 14:02:52 -070079
80template <int N, typename T>
81void test_Ni(skiatest::Reporter* r) {
mtklein6c221b42015-11-20 13:53:19 -080082 auto assert_eq = [&](const SkNx<N,T>& v, T a, T b, T c, T d, T e, T f, T g, T h) {
mtklein115acee2015-04-14 14:02:52 -070083 T vals[8];
84 v.store(vals);
85
86 switch (N) {
John Stiles30212b72020-06-11 17:55:07 -040087 case 8:
88 REPORTER_ASSERT(r, vals[4] == e && vals[5] == f && vals[6] == g && vals[7] == h);
89 [[fallthrough]];
90 case 4:
91 REPORTER_ASSERT(r, vals[2] == c && vals[3] == d);
92 [[fallthrough]];
93 case 2:
94 REPORTER_ASSERT(r, vals[0] == a && vals[1] == b);
mtklein115acee2015-04-14 14:02:52 -070095 }
mtklein1113da72015-04-27 12:08:01 -070096 switch (N) {
John Stiles30212b72020-06-11 17:55:07 -040097 case 8:
98 REPORTER_ASSERT(r, v[4] == e && v[5] == f && v[6] == g && v[7] == h);
99 [[fallthrough]];
100 case 4:
101 REPORTER_ASSERT(r, v[2] == c && v[3] == d);
102 [[fallthrough]];
103 case 2:
104 REPORTER_ASSERT(r, v[0] == a && v[1] == b);
mtklein1113da72015-04-27 12:08:01 -0700105 }
mtklein115acee2015-04-14 14:02:52 -0700106 };
107
108 T vals[] = { 1,2,3,4,5,6,7,8 };
mtklein6c221b42015-11-20 13:53:19 -0800109 SkNx<N,T> a = SkNx<N,T>::Load(vals),
mtklein115acee2015-04-14 14:02:52 -0700110 b(a),
111 c = a;
mtklein6c221b42015-11-20 13:53:19 -0800112 SkNx<N,T> d;
mtklein115acee2015-04-14 14:02:52 -0700113 d = a;
114
115 assert_eq(a, 1,2,3,4,5,6,7,8);
116 assert_eq(b, 1,2,3,4,5,6,7,8);
117 assert_eq(c, 1,2,3,4,5,6,7,8);
118 assert_eq(d, 1,2,3,4,5,6,7,8);
119
120 assert_eq(a+a, 2,4,6,8,10,12,14,16);
121 assert_eq(a*a, 1,4,9,16,25,36,49,64);
122 assert_eq(a*a-a, 0,2,6,12,20,30,42,56);
123
124 assert_eq(a >> 2, 0,0,0,1,1,1,1,2);
125 assert_eq(a << 1, 2,4,6,8,10,12,14,16);
126
mtklein7c249e52016-02-21 10:54:19 -0800127 REPORTER_ASSERT(r, a[1] == 2);
mtklein115acee2015-04-14 14:02:52 -0700128}
129
mtklein6c221b42015-11-20 13:53:19 -0800130DEF_TEST(SkNx, r) {
mtklein115acee2015-04-14 14:02:52 -0700131 test_Ni<2, uint16_t>(r);
132 test_Ni<4, uint16_t>(r);
133 test_Ni<8, uint16_t>(r);
mtklein1113da72015-04-27 12:08:01 -0700134
135 test_Ni<2, int>(r);
136 test_Ni<4, int>(r);
137 test_Ni<8, int>(r);
mtklein115acee2015-04-14 14:02:52 -0700138}
mtklein27e517a2015-05-14 17:53:04 -0700139
mtkleine20633e2015-07-13 12:06:33 -0700140DEF_TEST(SkNi_min_lt, r) {
mtklein27e517a2015-05-14 17:53:04 -0700141 // Exhaustively check the 8x8 bit space.
142 for (int a = 0; a < (1<<8); a++) {
143 for (int b = 0; b < (1<<8); b++) {
mtkleine20633e2015-07-13 12:06:33 -0700144 Sk16b aw(a), bw(b);
Brian Osman788b9162020-02-07 10:36:46 -0500145 REPORTER_ASSERT(r, Sk16b::Min(aw, bw)[0] == std::min(a, b));
mtklein7c249e52016-02-21 10:54:19 -0800146 REPORTER_ASSERT(r, !(aw < bw)[0] == !(a < b));
mtklein27e517a2015-05-14 17:53:04 -0700147 }}
148
149 // Exhausting the 16x16 bit space is kind of slow, so only do that in release builds.
150#ifdef SK_DEBUG
151 SkRandom rand;
152 for (int i = 0; i < (1<<16); i++) {
153 uint16_t a = rand.nextU() >> 16,
154 b = rand.nextU() >> 16;
Brian Osman788b9162020-02-07 10:36:46 -0500155 REPORTER_ASSERT(r, Sk16h::Min(Sk16h(a), Sk16h(b))[0] == std::min(a, b));
mtklein27e517a2015-05-14 17:53:04 -0700156 }
157#else
158 for (int a = 0; a < (1<<16); a++) {
159 for (int b = 0; b < (1<<16); b++) {
Brian Osman788b9162020-02-07 10:36:46 -0500160 REPORTER_ASSERT(r, Sk16h::Min(Sk16h(a), Sk16h(b))[0] == std::min(a, b));
mtklein27e517a2015-05-14 17:53:04 -0700161 }}
162#endif
163}
mtklein37478752015-06-15 10:58:42 -0700164
165DEF_TEST(SkNi_saturatedAdd, r) {
166 for (int a = 0; a < (1<<8); a++) {
167 for (int b = 0; b < (1<<8); b++) {
168 int exact = a+b;
169 if (exact > 255) { exact = 255; }
170 if (exact < 0) { exact = 0; }
171
mtklein7c249e52016-02-21 10:54:19 -0800172 REPORTER_ASSERT(r, Sk16b(a).saturatedAdd(Sk16b(b))[0] == exact);
mtklein37478752015-06-15 10:58:42 -0700173 }
174 }
175}
176
Herb Derby5eb15282017-10-10 17:14:18 -0400177DEF_TEST(SkNi_mulHi, r) {
178 // First 8 primes.
179 Sk4u a{ 0x00020000, 0x00030000, 0x00050000, 0x00070000 };
180 Sk4u b{ 0x000b0000, 0x000d0000, 0x00110000, 0x00130000 };
181
182 Sk4u q{22, 39, 85, 133};
183
184 Sk4u c = a.mulHi(b);
185 REPORTER_ASSERT(r, c[0] == q[0]);
186 REPORTER_ASSERT(r, c[1] == q[1]);
187 REPORTER_ASSERT(r, c[2] == q[2]);
188 REPORTER_ASSERT(r, c[3] == q[3]);
189}
190
mtklein37478752015-06-15 10:58:42 -0700191DEF_TEST(Sk4px_muldiv255round, r) {
192 for (int a = 0; a < (1<<8); a++) {
193 for (int b = 0; b < (1<<8); b++) {
194 int exact = (a*b+127)/255;
195
196 // Duplicate a and b 16x each.
Mike Klein7dfe6d92018-12-18 14:53:37 -0500197 Sk4px av = Sk16b(a),
198 bv = Sk16b(b);
mtklein37478752015-06-15 10:58:42 -0700199
200 // This way should always be exactly correct.
mtklein7c249e52016-02-21 10:54:19 -0800201 int correct = (av * bv).div255()[0];
mtklein37478752015-06-15 10:58:42 -0700202 REPORTER_ASSERT(r, correct == exact);
203
204 // We're a bit more flexible on this method: correct for 0 or 255, otherwise off by <=1.
mtklein7c249e52016-02-21 10:54:19 -0800205 int fast = av.approxMulDiv255(bv)[0];
mtklein37478752015-06-15 10:58:42 -0700206 REPORTER_ASSERT(r, fast-exact >= -1 && fast-exact <= 1);
207 if (a == 0 || a == 255 || b == 0 || b == 255) {
208 REPORTER_ASSERT(r, fast == exact);
209 }
210 }
211 }
212}
mtklein4be181e2015-07-14 10:54:19 -0700213
mtkleinc33065a2016-01-15 12:16:40 -0800214DEF_TEST(SkNx_abs, r) {
215 auto fs = Sk4f(0.0f, -0.0f, 2.0f, -4.0f).abs();
mtklein7c249e52016-02-21 10:54:19 -0800216 REPORTER_ASSERT(r, fs[0] == 0.0f);
217 REPORTER_ASSERT(r, fs[1] == 0.0f);
218 REPORTER_ASSERT(r, fs[2] == 2.0f);
219 REPORTER_ASSERT(r, fs[3] == 4.0f);
Chris Dalton7732f4f2017-08-28 14:45:40 -0600220 auto fshi = Sk2f(0.0f, -0.0f).abs();
221 auto fslo = Sk2f(2.0f, -4.0f).abs();
222 REPORTER_ASSERT(r, fshi[0] == 0.0f);
223 REPORTER_ASSERT(r, fshi[1] == 0.0f);
224 REPORTER_ASSERT(r, fslo[0] == 2.0f);
225 REPORTER_ASSERT(r, fslo[1] == 4.0f);
mtkleinc33065a2016-01-15 12:16:40 -0800226}
mtklein629f25a2016-02-08 05:54:38 -0800227
Yuqian Li7da6ba22017-07-12 13:36:05 -0400228DEF_TEST(Sk4i_abs, r) {
229 auto is = Sk4i(0, -1, 2, -2147483647).abs();
230 REPORTER_ASSERT(r, is[0] == 0);
231 REPORTER_ASSERT(r, is[1] == 1);
232 REPORTER_ASSERT(r, is[2] == 2);
233 REPORTER_ASSERT(r, is[3] == 2147483647);
234}
235
236DEF_TEST(Sk4i_minmax, r) {
237 auto a = Sk4i(0, 2, 4, 6);
238 auto b = Sk4i(1, 1, 3, 7);
239 auto min = Sk4i::Min(a, b);
240 auto max = Sk4i::Max(a, b);
241 for(int i = 0; i < 4; ++i) {
Brian Osman788b9162020-02-07 10:36:46 -0500242 REPORTER_ASSERT(r, min[i] == std::min(a[i], b[i]));
243 REPORTER_ASSERT(r, max[i] == std::max(a[i], b[i]));
Yuqian Li7da6ba22017-07-12 13:36:05 -0400244 }
245}
246
mtklein126626e2016-02-09 15:41:36 -0800247DEF_TEST(SkNx_floor, r) {
248 auto fs = Sk4f(0.4f, -0.4f, 0.6f, -0.6f).floor();
mtklein7c249e52016-02-21 10:54:19 -0800249 REPORTER_ASSERT(r, fs[0] == 0.0f);
250 REPORTER_ASSERT(r, fs[1] == -1.0f);
251 REPORTER_ASSERT(r, fs[2] == 0.0f);
252 REPORTER_ASSERT(r, fs[3] == -1.0f);
Chris Dalton89c5e882018-06-08 11:46:42 -0600253
254 auto fs2 = Sk2f(0.4f, -0.4f).floor();
255 REPORTER_ASSERT(r, fs2[0] == 0.0f);
256 REPORTER_ASSERT(r, fs2[1] == -1.0f);
257
258 auto fs3 = Sk2f(0.6f, -0.6f).floor();
259 REPORTER_ASSERT(r, fs3[0] == 0.0f);
260 REPORTER_ASSERT(r, fs3[1] == -1.0f);
mtklein126626e2016-02-09 15:41:36 -0800261}
262
mtkleine4c0bee2016-02-09 10:35:27 -0800263DEF_TEST(SkNx_shuffle, r) {
264 Sk4f f4(0,10,20,30);
mtklein629f25a2016-02-08 05:54:38 -0800265
mtkleine4c0bee2016-02-09 10:35:27 -0800266 Sk2f f2 = SkNx_shuffle<2,1>(f4);
267 REPORTER_ASSERT(r, f2[0] == 20);
268 REPORTER_ASSERT(r, f2[1] == 10);
269
270 f4 = SkNx_shuffle<0,1,1,0>(f2);
271 REPORTER_ASSERT(r, f4[0] == 20);
272 REPORTER_ASSERT(r, f4[1] == 10);
273 REPORTER_ASSERT(r, f4[2] == 10);
274 REPORTER_ASSERT(r, f4[3] == 20);
mtklein629f25a2016-02-08 05:54:38 -0800275}
276
mtklein0cf795f2016-02-17 07:23:36 -0800277DEF_TEST(SkNx_int_float, r) {
278 Sk4f f(-2.3f, 1.0f, 0.45f, 0.6f);
279
280 Sk4i i = SkNx_cast<int>(f);
281 REPORTER_ASSERT(r, i[0] == -2);
282 REPORTER_ASSERT(r, i[1] == 1);
283 REPORTER_ASSERT(r, i[2] == 0);
284 REPORTER_ASSERT(r, i[3] == 0);
285
286 f = SkNx_cast<float>(i);
287 REPORTER_ASSERT(r, f[0] == -2.0f);
288 REPORTER_ASSERT(r, f[1] == 1.0f);
289 REPORTER_ASSERT(r, f[2] == 0.0f);
290 REPORTER_ASSERT(r, f[3] == 0.0f);
291}
292
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500293#include "include/utils/SkRandom.h"
mtkleine4c0bee2016-02-09 10:35:27 -0800294
mtklein629f25a2016-02-08 05:54:38 -0800295DEF_TEST(SkNx_u16_float, r) {
296 {
297 // u16 --> float
298 auto h4 = Sk4h(15, 17, 257, 65535);
299 auto f4 = SkNx_cast<float>(h4);
mtklein7c249e52016-02-21 10:54:19 -0800300 REPORTER_ASSERT(r, f4[0] == 15.0f);
301 REPORTER_ASSERT(r, f4[1] == 17.0f);
302 REPORTER_ASSERT(r, f4[2] == 257.0f);
303 REPORTER_ASSERT(r, f4[3] == 65535.0f);
mtklein629f25a2016-02-08 05:54:38 -0800304 }
305 {
306 // float -> u16
307 auto f4 = Sk4f(15, 17, 257, 65535);
308 auto h4 = SkNx_cast<uint16_t>(f4);
mtklein7c249e52016-02-21 10:54:19 -0800309 REPORTER_ASSERT(r, h4[0] == 15);
310 REPORTER_ASSERT(r, h4[1] == 17);
311 REPORTER_ASSERT(r, h4[2] == 257);
312 REPORTER_ASSERT(r, h4[3] == 65535);
mtklein629f25a2016-02-08 05:54:38 -0800313 }
314
315 // starting with any u16 value, we should be able to have a perfect round-trip in/out of floats
316 //
317 SkRandom rand;
mtkleine4c0bee2016-02-09 10:35:27 -0800318 for (int i = 0; i < 10000; ++i) {
mtklein629f25a2016-02-08 05:54:38 -0800319 const uint16_t s16[4] {
Mike Klein5595f6e2018-09-05 10:51:00 -0400320 (uint16_t)(rand.nextU() >> 16), (uint16_t)(rand.nextU() >> 16),
321 (uint16_t)(rand.nextU() >> 16), (uint16_t)(rand.nextU() >> 16),
mtklein629f25a2016-02-08 05:54:38 -0800322 };
323 auto u4_0 = Sk4h::Load(s16);
324 auto f4 = SkNx_cast<float>(u4_0);
325 auto u4_1 = SkNx_cast<uint16_t>(f4);
326 uint16_t d16[4];
327 u4_1.store(d16);
328 REPORTER_ASSERT(r, !memcmp(s16, d16, sizeof(s16)));
329 }
330}
mtklein58e389b2016-07-15 07:00:11 -0700331
332// The SSE2 implementation of SkNx_cast<uint16_t>(Sk4i) is non-trivial, so worth a test.
333DEF_TEST(SkNx_int_u16, r) {
334 // These are pretty hard to get wrong.
335 for (int i = 0; i <= 0x7fff; i++) {
336 uint16_t expected = (uint16_t)i;
337 uint16_t actual = SkNx_cast<uint16_t>(Sk4i(i))[0];
338
339 REPORTER_ASSERT(r, expected == actual);
340 }
341
342 // A naive implementation with _mm_packs_epi32 would succeed up to 0x7fff but fail here:
343 for (int i = 0x8000; (1) && i <= 0xffff; i++) {
344 uint16_t expected = (uint16_t)i;
345 uint16_t actual = SkNx_cast<uint16_t>(Sk4i(i))[0];
346
347 REPORTER_ASSERT(r, expected == actual);
348 }
349}
msarettc0444612016-09-16 11:45:58 -0700350
351DEF_TEST(SkNx_4fLoad4Store4, r) {
352 float src[] = {
353 0.0f, 1.0f, 2.0f, 3.0f,
354 4.0f, 5.0f, 6.0f, 7.0f,
355 8.0f, 9.0f, 10.0f, 11.0f,
356 12.0f, 13.0f, 14.0f, 15.0f
357 };
358
359 Sk4f a, b, c, d;
Mike Klein33cbfd72016-10-06 11:09:27 -0400360 Sk4f::Load4(src, &a, &b, &c, &d);
msarettc0444612016-09-16 11:45:58 -0700361 REPORTER_ASSERT(r, 0.0f == a[0]);
362 REPORTER_ASSERT(r, 4.0f == a[1]);
363 REPORTER_ASSERT(r, 8.0f == a[2]);
364 REPORTER_ASSERT(r, 12.0f == a[3]);
365 REPORTER_ASSERT(r, 1.0f == b[0]);
366 REPORTER_ASSERT(r, 5.0f == b[1]);
367 REPORTER_ASSERT(r, 9.0f == b[2]);
368 REPORTER_ASSERT(r, 13.0f == b[3]);
369 REPORTER_ASSERT(r, 2.0f == c[0]);
370 REPORTER_ASSERT(r, 6.0f == c[1]);
371 REPORTER_ASSERT(r, 10.0f == c[2]);
372 REPORTER_ASSERT(r, 14.0f == c[3]);
373 REPORTER_ASSERT(r, 3.0f == d[0]);
374 REPORTER_ASSERT(r, 7.0f == d[1]);
375 REPORTER_ASSERT(r, 11.0f == d[2]);
376 REPORTER_ASSERT(r, 15.0f == d[3]);
377
378 float dst[16];
Mike Klein33cbfd72016-10-06 11:09:27 -0400379 Sk4f::Store4(dst, a, b, c, d);
msarettc0444612016-09-16 11:45:58 -0700380 REPORTER_ASSERT(r, 0 == memcmp(dst, src, 16 * sizeof(float)));
381}
Chris Dalton7732f4f2017-08-28 14:45:40 -0600382
383DEF_TEST(SkNx_neg, r) {
384 auto fs = -Sk4f(0.0f, -0.0f, 2.0f, -4.0f);
385 REPORTER_ASSERT(r, fs[0] == 0.0f);
386 REPORTER_ASSERT(r, fs[1] == 0.0f);
387 REPORTER_ASSERT(r, fs[2] == -2.0f);
388 REPORTER_ASSERT(r, fs[3] == 4.0f);
389 auto fshi = -Sk2f(0.0f, -0.0f);
390 auto fslo = -Sk2f(2.0f, -4.0f);
391 REPORTER_ASSERT(r, fshi[0] == 0.0f);
392 REPORTER_ASSERT(r, fshi[1] == 0.0f);
393 REPORTER_ASSERT(r, fslo[0] == -2.0f);
394 REPORTER_ASSERT(r, fslo[1] == 4.0f);
395}
396
397DEF_TEST(SkNx_thenElse, r) {
398 auto fs = (Sk4f(0.0f, -0.0f, 2.0f, -4.0f) < 0).thenElse(-1, 1);
399 REPORTER_ASSERT(r, fs[0] == 1);
400 REPORTER_ASSERT(r, fs[1] == 1);
401 REPORTER_ASSERT(r, fs[2] == 1);
402 REPORTER_ASSERT(r, fs[3] == -1);
403 auto fshi = (Sk2f(0.0f, -0.0f) < 0).thenElse(-1, 1);
404 auto fslo = (Sk2f(2.0f, -4.0f) < 0).thenElse(-1, 1);
405 REPORTER_ASSERT(r, fshi[0] == 1);
406 REPORTER_ASSERT(r, fshi[1] == 1);
407 REPORTER_ASSERT(r, fslo[0] == 1);
408 REPORTER_ASSERT(r, fslo[1] == -1);
409}
Mike Klein213d8212017-11-30 12:07:20 -0500410
411DEF_TEST(Sk4f_Load2, r) {
412 float xy[8] = { 0,1,2,3,4,5,6,7 };
413
414 Sk4f x,y;
415 Sk4f::Load2(xy, &x,&y);
416
417 REPORTER_ASSERT(r, x[0] == 0);
418 REPORTER_ASSERT(r, x[1] == 2);
419 REPORTER_ASSERT(r, x[2] == 4);
420 REPORTER_ASSERT(r, x[3] == 6);
421
422 REPORTER_ASSERT(r, y[0] == 1);
423 REPORTER_ASSERT(r, y[1] == 3);
424 REPORTER_ASSERT(r, y[2] == 5);
425 REPORTER_ASSERT(r, y[3] == 7);
426}
Chris Dalton0cb75872017-12-01 13:23:05 -0700427
Chris Dalton21f64372018-04-11 14:01:04 -0600428DEF_TEST(Sk2f_Load2, r) {
429 float xy[4] = { 0,1,2,3 };
430
431 Sk2f x,y;
432 Sk2f::Load2(xy, &x,&y);
433
434 REPORTER_ASSERT(r, x[0] == 0);
435 REPORTER_ASSERT(r, x[1] == 2);
436
437 REPORTER_ASSERT(r, y[0] == 1);
438 REPORTER_ASSERT(r, y[1] == 3);
439}
440
Chris Dalton42f02aa2018-04-08 23:58:43 -0600441DEF_TEST(Sk2f_Store2, r) {
442 Sk2f p0{0, 2};
443 Sk2f p1{1, 3};
444 float dst[4];
445 Sk2f::Store2(dst, p0, p1);
446 REPORTER_ASSERT(r, dst[0] == 0);
447 REPORTER_ASSERT(r, dst[1] == 1);
448 REPORTER_ASSERT(r, dst[2] == 2);
449 REPORTER_ASSERT(r, dst[3] == 3);
450}
451
Chris Dalton0cb75872017-12-01 13:23:05 -0700452DEF_TEST(Sk2f_Store3, r) {
453 Sk2f p0{0, 3};
454 Sk2f p1{1, 4};
455 Sk2f p2{2, 5};
456 float dst[6];
457 Sk2f::Store3(dst, p0, p1, p2);
458 REPORTER_ASSERT(r, dst[0] == 0);
459 REPORTER_ASSERT(r, dst[1] == 1);
460 REPORTER_ASSERT(r, dst[2] == 2);
461 REPORTER_ASSERT(r, dst[3] == 3);
462 REPORTER_ASSERT(r, dst[4] == 4);
463 REPORTER_ASSERT(r, dst[5] == 5);
464}
Chris Dalton6f8fa4e2018-02-06 17:55:30 -0700465
466DEF_TEST(Sk2f_Store4, r) {
467 Sk2f p0{0, 4};
468 Sk2f p1{1, 5};
469 Sk2f p2{2, 6};
470 Sk2f p3{3, 7};
Chris Dalton9f2dab02018-04-18 14:07:03 -0600471
472 float dst[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
Chris Dalton6f8fa4e2018-02-06 17:55:30 -0700473 Sk2f::Store4(dst, p0, p1, p2, p3);
474 REPORTER_ASSERT(r, dst[0] == 0);
475 REPORTER_ASSERT(r, dst[1] == 1);
476 REPORTER_ASSERT(r, dst[2] == 2);
477 REPORTER_ASSERT(r, dst[3] == 3);
478 REPORTER_ASSERT(r, dst[4] == 4);
479 REPORTER_ASSERT(r, dst[5] == 5);
480 REPORTER_ASSERT(r, dst[6] == 6);
481 REPORTER_ASSERT(r, dst[7] == 7);
Chris Dalton9f2dab02018-04-18 14:07:03 -0600482
483 // Ensure transposing to Sk4f works.
484 Sk4f dst4f[2] = {{-1, -1, -1, -1}, {-1, -1, -1, -1}};
485 Sk2f::Store4(dst4f, p0, p1, p2, p3);
486 REPORTER_ASSERT(r, dst4f[0][0] == 0);
487 REPORTER_ASSERT(r, dst4f[0][1] == 1);
488 REPORTER_ASSERT(r, dst4f[0][2] == 2);
489 REPORTER_ASSERT(r, dst4f[0][3] == 3);
490 REPORTER_ASSERT(r, dst4f[1][0] == 4);
491 REPORTER_ASSERT(r, dst4f[1][1] == 5);
492 REPORTER_ASSERT(r, dst4f[1][2] == 6);
493 REPORTER_ASSERT(r, dst4f[1][3] == 7);
494
Chris Dalton6f8fa4e2018-02-06 17:55:30 -0700495}
Mike Klein68ff92f2018-03-26 13:04:14 -0400496
Chris Daltone3fda932018-04-11 13:18:09 -0600497DEF_TEST(Sk4f_minmax, r) {
498 REPORTER_ASSERT(r, 3 == Sk4f(0,1,2,3).max());
499 REPORTER_ASSERT(r, 2 == Sk4f(1,-5,2,-1).max());
500 REPORTER_ASSERT(r, -1 == Sk4f(-2,-1,-6,-3).max());
501 REPORTER_ASSERT(r, 3 == Sk4f(3,2,1,0).max());
502
503 REPORTER_ASSERT(r, 0 == Sk4f(0,1,2,3).min());
504 REPORTER_ASSERT(r, -5 == Sk4f(1,-5,2,-1).min());
505 REPORTER_ASSERT(r, -6 == Sk4f(-2,-1,-6,-3).min());
506 REPORTER_ASSERT(r, 0 == Sk4f(3,2,1,0).min());
507}
508
Mike Klein68ff92f2018-03-26 13:04:14 -0400509DEF_TEST(SkNf_anyTrue_allTrue, r) {
510 REPORTER_ASSERT(r, (Sk2f{1,2} < Sk2f{3,4}).anyTrue());
511 REPORTER_ASSERT(r, (Sk2f{1,2} < Sk2f{3,4}).allTrue());
512 REPORTER_ASSERT(r, (Sk2f{3,2} < Sk2f{1,4}).anyTrue());
513 REPORTER_ASSERT(r, !(Sk2f{3,2} < Sk2f{1,4}).allTrue());
514 REPORTER_ASSERT(r, !(Sk2f{3,4} < Sk2f{1,2}).anyTrue());
515
516 REPORTER_ASSERT(r, (Sk4f{1,2,3,4} < Sk4f{3,4,5,6}).anyTrue());
517 REPORTER_ASSERT(r, (Sk4f{1,2,3,4} < Sk4f{3,4,5,6}).allTrue());
518 REPORTER_ASSERT(r, (Sk4f{1,2,3,4} < Sk4f{1,4,1,1}).anyTrue());
519 REPORTER_ASSERT(r, !(Sk4f{1,2,3,4} < Sk4f{1,4,1,1}).allTrue());
520 REPORTER_ASSERT(r, !(Sk4f{3,4,5,6} < Sk4f{1,2,3,4}).anyTrue());
521}