blob: 31baebc4302e555d38d501bba1fb1f7f59f86e30 [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
mtklein37478752015-06-15 10:58:42 -07008#include "Sk4px.h"
mtkleinc9adb052015-03-30 10:50:27 -07009#include "SkNx.h"
mtklein27e517a2015-05-14 17:53:04 -070010#include "SkRandom.h"
mtkleinc9adb052015-03-30 10:50:27 -070011#include "Test.h"
12
mtklein6f797092015-11-09 08:33:53 -080013template <int N>
mtkleinc9adb052015-03-30 10:50:27 -070014static void test_Nf(skiatest::Reporter* r) {
15
mtklein6f797092015-11-09 08:33:53 -080016 auto assert_nearly_eq = [&](float eps, const SkNf<N>& v, float a, float b, float c, float d) {
17 auto close = [=](float a, float b) { return fabsf(a-b) <= eps; };
18 float vals[4];
mtkleinc9adb052015-03-30 10:50:27 -070019 v.store(vals);
mtkleina156a8f2015-04-03 06:16:13 -070020 bool ok = close(vals[0], a) && close(vals[1], b)
21 && close(v.template kth<0>(), a) && close(v.template kth<1>(), b);
22 REPORTER_ASSERT(r, ok);
mtkleinc9adb052015-03-30 10:50:27 -070023 if (N == 4) {
mtkleina156a8f2015-04-03 06:16:13 -070024 ok = close(vals[2], c) && close(vals[3], d)
25 && close(v.template kth<2>(), c) && close(v.template kth<3>(), d);
26 REPORTER_ASSERT(r, ok);
mtkleinc9adb052015-03-30 10:50:27 -070027 }
28 };
mtklein6f797092015-11-09 08:33:53 -080029 auto assert_eq = [&](const SkNf<N>& v, float a, float b, float c, float d) {
mtkleinc9adb052015-03-30 10:50:27 -070030 return assert_nearly_eq(0, v, a,b,c,d);
31 };
32
mtklein6f797092015-11-09 08:33:53 -080033 float vals[] = {3, 4, 5, 6};
34 SkNf<N> a = SkNf<N>::Load(vals),
35 b(a),
36 c = a;
37 SkNf<N> d;
mtkleinc9adb052015-03-30 10:50:27 -070038 d = a;
39
40 assert_eq(a, 3, 4, 5, 6);
41 assert_eq(b, 3, 4, 5, 6);
42 assert_eq(c, 3, 4, 5, 6);
43 assert_eq(d, 3, 4, 5, 6);
44
45 assert_eq(a+b, 6, 8, 10, 12);
46 assert_eq(a*b, 9, 16, 25, 36);
47 assert_eq(a*b-b, 6, 12, 20, 30);
48 assert_eq((a*b).sqrt(), 3, 4, 5, 6);
49 assert_eq(a/b, 1, 1, 1, 1);
mtklein6f797092015-11-09 08:33:53 -080050 assert_eq(SkNf<N>(0)-a, -3, -4, -5, -6);
mtkleinc9adb052015-03-30 10:50:27 -070051
mtklein6f797092015-11-09 08:33:53 -080052 SkNf<N> fours(4);
mtkleinc9adb052015-03-30 10:50:27 -070053
54 assert_eq(fours.sqrt(), 2,2,2,2);
mtklein6f797092015-11-09 08:33:53 -080055 assert_nearly_eq(0.001f, fours.rsqrt0(), 0.5, 0.5, 0.5, 0.5);
56 assert_nearly_eq(0.001f, fours.rsqrt1(), 0.5, 0.5, 0.5, 0.5);
57 assert_nearly_eq(0.001f, fours.rsqrt2(), 0.5, 0.5, 0.5, 0.5);
mtkleinc9adb052015-03-30 10:50:27 -070058
mtklein6f797092015-11-09 08:33:53 -080059 assert_eq( fours. invert(), 0.25, 0.25, 0.25, 0.25);
60 assert_nearly_eq(0.001f, fours.approxInvert(), 0.25, 0.25, 0.25, 0.25);
mtkleinc9adb052015-03-30 10:50:27 -070061
mtklein6f797092015-11-09 08:33:53 -080062 assert_eq(SkNf<N>::Min(a, fours), 3, 4, 4, 4);
63 assert_eq(SkNf<N>::Max(a, fours), 4, 4, 5, 6);
mtkleinc9adb052015-03-30 10:50:27 -070064
65 // Test some comparisons. This is not exhaustive.
66 REPORTER_ASSERT(r, (a == b).allTrue());
67 REPORTER_ASSERT(r, (a+b == a*b-b).anyTrue());
68 REPORTER_ASSERT(r, !(a+b == a*b-b).allTrue());
69 REPORTER_ASSERT(r, !(a+b == a*b).anyTrue());
70 REPORTER_ASSERT(r, !(a != b).anyTrue());
71 REPORTER_ASSERT(r, (a < fours).anyTrue());
72 REPORTER_ASSERT(r, (a <= fours).anyTrue());
73 REPORTER_ASSERT(r, !(a > fours).allTrue());
74 REPORTER_ASSERT(r, !(a >= fours).allTrue());
75}
76
77DEF_TEST(SkNf, r) {
mtklein6f797092015-11-09 08:33:53 -080078 test_Nf<2>(r);
79 test_Nf<4>(r);
mtkleinc9adb052015-03-30 10:50:27 -070080}
mtklein115acee2015-04-14 14:02:52 -070081
82template <int N, typename T>
83void test_Ni(skiatest::Reporter* r) {
84 auto assert_eq = [&](const SkNi<N,T>& v, T a, T b, T c, T d, T e, T f, T g, T h) {
85 T vals[8];
86 v.store(vals);
87
88 switch (N) {
89 case 8: REPORTER_ASSERT(r, vals[4] == e && vals[5] == f && vals[6] == g && vals[7] == h);
90 case 4: REPORTER_ASSERT(r, vals[2] == c && vals[3] == d);
91 case 2: REPORTER_ASSERT(r, vals[0] == a && vals[1] == b);
92 }
mtklein1113da72015-04-27 12:08:01 -070093 switch (N) {
94 case 8: REPORTER_ASSERT(r, v.template kth<4>() == e && v.template kth<5>() == f &&
95 v.template kth<6>() == g && v.template kth<7>() == h);
96 case 4: REPORTER_ASSERT(r, v.template kth<2>() == c && v.template kth<3>() == d);
97 case 2: REPORTER_ASSERT(r, v.template kth<0>() == a && v.template kth<1>() == b);
98 }
mtklein115acee2015-04-14 14:02:52 -070099 };
100
101 T vals[] = { 1,2,3,4,5,6,7,8 };
102 SkNi<N,T> a = SkNi<N,T>::Load(vals),
103 b(a),
104 c = a;
105 SkNi<N,T> d;
106 d = a;
107
108 assert_eq(a, 1,2,3,4,5,6,7,8);
109 assert_eq(b, 1,2,3,4,5,6,7,8);
110 assert_eq(c, 1,2,3,4,5,6,7,8);
111 assert_eq(d, 1,2,3,4,5,6,7,8);
112
113 assert_eq(a+a, 2,4,6,8,10,12,14,16);
114 assert_eq(a*a, 1,4,9,16,25,36,49,64);
115 assert_eq(a*a-a, 0,2,6,12,20,30,42,56);
116
117 assert_eq(a >> 2, 0,0,0,1,1,1,1,2);
118 assert_eq(a << 1, 2,4,6,8,10,12,14,16);
119
120 REPORTER_ASSERT(r, a.template kth<1>() == 2);
121}
122
123DEF_TEST(SkNi, r) {
124 test_Ni<2, uint16_t>(r);
125 test_Ni<4, uint16_t>(r);
126 test_Ni<8, uint16_t>(r);
mtklein1113da72015-04-27 12:08:01 -0700127
128 test_Ni<2, int>(r);
129 test_Ni<4, int>(r);
130 test_Ni<8, int>(r);
mtklein115acee2015-04-14 14:02:52 -0700131}
mtklein27e517a2015-05-14 17:53:04 -0700132
mtkleine20633e2015-07-13 12:06:33 -0700133DEF_TEST(SkNi_min_lt, r) {
mtklein27e517a2015-05-14 17:53:04 -0700134 // Exhaustively check the 8x8 bit space.
135 for (int a = 0; a < (1<<8); a++) {
136 for (int b = 0; b < (1<<8); b++) {
mtkleine20633e2015-07-13 12:06:33 -0700137 Sk16b aw(a), bw(b);
138 REPORTER_ASSERT(r, Sk16b::Min(aw, bw).kth<0>() == SkTMin(a, b));
139 REPORTER_ASSERT(r, !(aw < bw).kth<0>() == !(a < b));
mtklein27e517a2015-05-14 17:53:04 -0700140 }}
141
142 // Exhausting the 16x16 bit space is kind of slow, so only do that in release builds.
143#ifdef SK_DEBUG
144 SkRandom rand;
145 for (int i = 0; i < (1<<16); i++) {
146 uint16_t a = rand.nextU() >> 16,
147 b = rand.nextU() >> 16;
148 REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b));
149 }
150#else
151 for (int a = 0; a < (1<<16); a++) {
152 for (int b = 0; b < (1<<16); b++) {
153 REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b));
154 }}
155#endif
156}
mtklein37478752015-06-15 10:58:42 -0700157
158DEF_TEST(SkNi_saturatedAdd, r) {
159 for (int a = 0; a < (1<<8); a++) {
160 for (int b = 0; b < (1<<8); b++) {
161 int exact = a+b;
162 if (exact > 255) { exact = 255; }
163 if (exact < 0) { exact = 0; }
164
165 REPORTER_ASSERT(r, Sk16b(a).saturatedAdd(Sk16b(b)).kth<0>() == exact);
166 }
167 }
168}
169
170DEF_TEST(Sk4px_muldiv255round, r) {
171 for (int a = 0; a < (1<<8); a++) {
172 for (int b = 0; b < (1<<8); b++) {
173 int exact = (a*b+127)/255;
174
175 // Duplicate a and b 16x each.
mtklein059ac002015-06-22 10:39:38 -0700176 auto av = Sk4px::DupAlpha(a),
177 bv = Sk4px::DupAlpha(b);
mtklein37478752015-06-15 10:58:42 -0700178
179 // This way should always be exactly correct.
mtklein059ac002015-06-22 10:39:38 -0700180 int correct = (av * bv).div255().kth<0>();
mtklein37478752015-06-15 10:58:42 -0700181 REPORTER_ASSERT(r, correct == exact);
182
183 // We're a bit more flexible on this method: correct for 0 or 255, otherwise off by <=1.
mtklein059ac002015-06-22 10:39:38 -0700184 int fast = av.approxMulDiv255(bv).kth<0>();
mtklein37478752015-06-15 10:58:42 -0700185 REPORTER_ASSERT(r, fast-exact >= -1 && fast-exact <= 1);
186 if (a == 0 || a == 255 || b == 0 || b == 255) {
187 REPORTER_ASSERT(r, fast == exact);
188 }
189 }
190 }
191}
mtklein4be181e2015-07-14 10:54:19 -0700192
193DEF_TEST(Sk4px_widening, r) {
194 SkPMColor colors[] = {
195 SkPreMultiplyColor(0xff00ff00),
196 SkPreMultiplyColor(0x40008000),
197 SkPreMultiplyColor(0x7f020406),
198 SkPreMultiplyColor(0x00000000),
199 };
200 auto packed = Sk4px::Load4(colors);
201
202 auto wideLo = packed.widenLo(),
203 wideHi = packed.widenHi(),
204 wideLoHi = packed.widenLoHi(),
205 wideLoHiAlt = wideLo + wideHi;
206 REPORTER_ASSERT(r, 0 == memcmp(&wideLoHi, &wideLoHiAlt, sizeof(wideLoHi)));
207}
mtkleina508f3c2015-09-01 06:29:45 -0700208
209DEF_TEST(Sk4f_toBytes, r) {
210 uint8_t bytes[4];
211
212 // toBytes truncates, not rounds.
213 Sk4f(0.7f).toBytes(bytes);
214 REPORTER_ASSERT(r, bytes[0] == 0);
215
216 // Clamping edge cases.
217 Sk4f(-2.0f, -0.7f, 255.9f, 256.0f).toBytes(bytes);
218 REPORTER_ASSERT(r, bytes[0] == 0);
219 REPORTER_ASSERT(r, bytes[1] == 0);
220 REPORTER_ASSERT(r, bytes[2] == 255);
221 REPORTER_ASSERT(r, bytes[3] == 255);
222}