mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 8 | #include "Sk4px.h" |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 9 | #include "SkNx.h" |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 10 | #include "SkRandom.h" |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 11 | #include "Test.h" |
| 12 | |
mtklein | 6f79709 | 2015-11-09 08:33:53 -0800 | [diff] [blame] | 13 | template <int N> |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 14 | static void test_Nf(skiatest::Reporter* r) { |
| 15 | |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 16 | auto assert_nearly_eq = [&](float eps, const SkNx<N, float>& v, |
| 17 | float a, float b, float c, float d) { |
mtklein | 6f79709 | 2015-11-09 08:33:53 -0800 | [diff] [blame] | 18 | auto close = [=](float a, float b) { return fabsf(a-b) <= eps; }; |
| 19 | float vals[4]; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 20 | v.store(vals); |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 21 | bool ok = close(vals[0], a) && close(vals[1], b) |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 22 | && close( v[0], a) && close( v[1], b); |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 23 | REPORTER_ASSERT(r, ok); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 24 | if (N == 4) { |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 25 | ok = close(vals[2], c) && close(vals[3], d) |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 26 | && close( v[2], c) && close( v[3], d); |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 27 | REPORTER_ASSERT(r, ok); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 28 | } |
| 29 | }; |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 30 | auto assert_eq = [&](const SkNx<N, float>& v, float a, float b, float c, float d) { |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 31 | return assert_nearly_eq(0, v, a,b,c,d); |
| 32 | }; |
| 33 | |
mtklein | 6f79709 | 2015-11-09 08:33:53 -0800 | [diff] [blame] | 34 | float vals[] = {3, 4, 5, 6}; |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 35 | SkNx<N,float> a = SkNx<N,float>::Load(vals), |
| 36 | b(a), |
| 37 | c = a; |
| 38 | SkNx<N,float> d; |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 39 | 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); |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 51 | assert_eq(SkNx<N,float>(0)-a, -3, -4, -5, -6); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 52 | |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 53 | SkNx<N,float> fours(4); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 54 | |
| 55 | assert_eq(fours.sqrt(), 2,2,2,2); |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 56 | assert_nearly_eq(0.001f, fours.rsqrt(), 0.5, 0.5, 0.5, 0.5); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 57 | |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 58 | assert_nearly_eq(0.001f, fours.invert(), 0.25, 0.25, 0.25, 0.25); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 59 | |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 60 | 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); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 62 | |
| 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 | |
| 75 | DEF_TEST(SkNf, r) { |
mtklein | 6f79709 | 2015-11-09 08:33:53 -0800 | [diff] [blame] | 76 | test_Nf<2>(r); |
| 77 | test_Nf<4>(r); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 78 | } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 79 | |
| 80 | template <int N, typename T> |
| 81 | void test_Ni(skiatest::Reporter* r) { |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 82 | auto assert_eq = [&](const SkNx<N,T>& v, T a, T b, T c, T d, T e, T f, T g, T h) { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 83 | T vals[8]; |
| 84 | v.store(vals); |
| 85 | |
| 86 | switch (N) { |
| 87 | case 8: REPORTER_ASSERT(r, vals[4] == e && vals[5] == f && vals[6] == g && vals[7] == h); |
| 88 | case 4: REPORTER_ASSERT(r, vals[2] == c && vals[3] == d); |
| 89 | case 2: REPORTER_ASSERT(r, vals[0] == a && vals[1] == b); |
| 90 | } |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 91 | switch (N) { |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 92 | case 8: REPORTER_ASSERT(r, v[4] == e && v[5] == f && |
| 93 | v[6] == g && v[7] == h); |
| 94 | case 4: REPORTER_ASSERT(r, v[2] == c && v[3] == d); |
| 95 | case 2: REPORTER_ASSERT(r, v[0] == a && v[1] == b); |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 96 | } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | T vals[] = { 1,2,3,4,5,6,7,8 }; |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 100 | SkNx<N,T> a = SkNx<N,T>::Load(vals), |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 101 | b(a), |
| 102 | c = a; |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 103 | SkNx<N,T> d; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 104 | d = a; |
| 105 | |
| 106 | assert_eq(a, 1,2,3,4,5,6,7,8); |
| 107 | assert_eq(b, 1,2,3,4,5,6,7,8); |
| 108 | assert_eq(c, 1,2,3,4,5,6,7,8); |
| 109 | assert_eq(d, 1,2,3,4,5,6,7,8); |
| 110 | |
| 111 | assert_eq(a+a, 2,4,6,8,10,12,14,16); |
| 112 | assert_eq(a*a, 1,4,9,16,25,36,49,64); |
| 113 | assert_eq(a*a-a, 0,2,6,12,20,30,42,56); |
| 114 | |
| 115 | assert_eq(a >> 2, 0,0,0,1,1,1,1,2); |
| 116 | assert_eq(a << 1, 2,4,6,8,10,12,14,16); |
| 117 | |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 118 | REPORTER_ASSERT(r, a[1] == 2); |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 119 | } |
| 120 | |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 121 | DEF_TEST(SkNx, r) { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 122 | test_Ni<2, uint16_t>(r); |
| 123 | test_Ni<4, uint16_t>(r); |
| 124 | test_Ni<8, uint16_t>(r); |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 125 | |
| 126 | test_Ni<2, int>(r); |
| 127 | test_Ni<4, int>(r); |
| 128 | test_Ni<8, int>(r); |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 129 | } |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 130 | |
mtklein | e20633e | 2015-07-13 12:06:33 -0700 | [diff] [blame] | 131 | DEF_TEST(SkNi_min_lt, r) { |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 132 | // Exhaustively check the 8x8 bit space. |
| 133 | for (int a = 0; a < (1<<8); a++) { |
| 134 | for (int b = 0; b < (1<<8); b++) { |
mtklein | e20633e | 2015-07-13 12:06:33 -0700 | [diff] [blame] | 135 | Sk16b aw(a), bw(b); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 136 | REPORTER_ASSERT(r, Sk16b::Min(aw, bw)[0] == SkTMin(a, b)); |
| 137 | REPORTER_ASSERT(r, !(aw < bw)[0] == !(a < b)); |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 138 | }} |
| 139 | |
| 140 | // Exhausting the 16x16 bit space is kind of slow, so only do that in release builds. |
| 141 | #ifdef SK_DEBUG |
| 142 | SkRandom rand; |
| 143 | for (int i = 0; i < (1<<16); i++) { |
| 144 | uint16_t a = rand.nextU() >> 16, |
| 145 | b = rand.nextU() >> 16; |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 146 | REPORTER_ASSERT(r, Sk16h::Min(Sk16h(a), Sk16h(b))[0] == SkTMin(a, b)); |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 147 | } |
| 148 | #else |
| 149 | for (int a = 0; a < (1<<16); a++) { |
| 150 | for (int b = 0; b < (1<<16); b++) { |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 151 | REPORTER_ASSERT(r, Sk16h::Min(Sk16h(a), Sk16h(b))[0] == SkTMin(a, b)); |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 152 | }} |
| 153 | #endif |
| 154 | } |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 155 | |
| 156 | DEF_TEST(SkNi_saturatedAdd, r) { |
| 157 | for (int a = 0; a < (1<<8); a++) { |
| 158 | for (int b = 0; b < (1<<8); b++) { |
| 159 | int exact = a+b; |
| 160 | if (exact > 255) { exact = 255; } |
| 161 | if (exact < 0) { exact = 0; } |
| 162 | |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 163 | REPORTER_ASSERT(r, Sk16b(a).saturatedAdd(Sk16b(b))[0] == exact); |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | DEF_TEST(Sk4px_muldiv255round, r) { |
| 169 | for (int a = 0; a < (1<<8); a++) { |
| 170 | for (int b = 0; b < (1<<8); b++) { |
| 171 | int exact = (a*b+127)/255; |
| 172 | |
| 173 | // Duplicate a and b 16x each. |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 174 | auto av = Sk4px::DupAlpha(a), |
| 175 | bv = Sk4px::DupAlpha(b); |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 176 | |
| 177 | // This way should always be exactly correct. |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 178 | int correct = (av * bv).div255()[0]; |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 179 | REPORTER_ASSERT(r, correct == exact); |
| 180 | |
| 181 | // We're a bit more flexible on this method: correct for 0 or 255, otherwise off by <=1. |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 182 | int fast = av.approxMulDiv255(bv)[0]; |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 183 | REPORTER_ASSERT(r, fast-exact >= -1 && fast-exact <= 1); |
| 184 | if (a == 0 || a == 255 || b == 0 || b == 255) { |
| 185 | REPORTER_ASSERT(r, fast == exact); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 190 | |
| 191 | DEF_TEST(Sk4px_widening, r) { |
| 192 | SkPMColor colors[] = { |
| 193 | SkPreMultiplyColor(0xff00ff00), |
| 194 | SkPreMultiplyColor(0x40008000), |
| 195 | SkPreMultiplyColor(0x7f020406), |
| 196 | SkPreMultiplyColor(0x00000000), |
| 197 | }; |
| 198 | auto packed = Sk4px::Load4(colors); |
| 199 | |
| 200 | auto wideLo = packed.widenLo(), |
| 201 | wideHi = packed.widenHi(), |
| 202 | wideLoHi = packed.widenLoHi(), |
| 203 | wideLoHiAlt = wideLo + wideHi; |
| 204 | REPORTER_ASSERT(r, 0 == memcmp(&wideLoHi, &wideLoHiAlt, sizeof(wideLoHi))); |
| 205 | } |
mtklein | a508f3c | 2015-09-01 06:29:45 -0700 | [diff] [blame] | 206 | |
mtklein | c33065a | 2016-01-15 12:16:40 -0800 | [diff] [blame] | 207 | DEF_TEST(SkNx_abs, r) { |
| 208 | auto fs = Sk4f(0.0f, -0.0f, 2.0f, -4.0f).abs(); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 209 | REPORTER_ASSERT(r, fs[0] == 0.0f); |
| 210 | REPORTER_ASSERT(r, fs[1] == 0.0f); |
| 211 | REPORTER_ASSERT(r, fs[2] == 2.0f); |
| 212 | REPORTER_ASSERT(r, fs[3] == 4.0f); |
mtklein | c33065a | 2016-01-15 12:16:40 -0800 | [diff] [blame] | 213 | } |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 214 | |
mtklein | 126626e | 2016-02-09 15:41:36 -0800 | [diff] [blame] | 215 | DEF_TEST(SkNx_floor, r) { |
| 216 | auto fs = Sk4f(0.4f, -0.4f, 0.6f, -0.6f).floor(); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 217 | REPORTER_ASSERT(r, fs[0] == 0.0f); |
| 218 | REPORTER_ASSERT(r, fs[1] == -1.0f); |
| 219 | REPORTER_ASSERT(r, fs[2] == 0.0f); |
| 220 | REPORTER_ASSERT(r, fs[3] == -1.0f); |
mtklein | 126626e | 2016-02-09 15:41:36 -0800 | [diff] [blame] | 221 | } |
| 222 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 223 | DEF_TEST(SkNx_shuffle, r) { |
| 224 | Sk4f f4(0,10,20,30); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 225 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 226 | Sk2f f2 = SkNx_shuffle<2,1>(f4); |
| 227 | REPORTER_ASSERT(r, f2[0] == 20); |
| 228 | REPORTER_ASSERT(r, f2[1] == 10); |
| 229 | |
| 230 | f4 = SkNx_shuffle<0,1,1,0>(f2); |
| 231 | REPORTER_ASSERT(r, f4[0] == 20); |
| 232 | REPORTER_ASSERT(r, f4[1] == 10); |
| 233 | REPORTER_ASSERT(r, f4[2] == 10); |
| 234 | REPORTER_ASSERT(r, f4[3] == 20); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 235 | } |
| 236 | |
mtklein | 0cf795f | 2016-02-17 07:23:36 -0800 | [diff] [blame] | 237 | DEF_TEST(SkNx_int_float, r) { |
| 238 | Sk4f f(-2.3f, 1.0f, 0.45f, 0.6f); |
| 239 | |
| 240 | Sk4i i = SkNx_cast<int>(f); |
| 241 | REPORTER_ASSERT(r, i[0] == -2); |
| 242 | REPORTER_ASSERT(r, i[1] == 1); |
| 243 | REPORTER_ASSERT(r, i[2] == 0); |
| 244 | REPORTER_ASSERT(r, i[3] == 0); |
| 245 | |
| 246 | f = SkNx_cast<float>(i); |
| 247 | REPORTER_ASSERT(r, f[0] == -2.0f); |
| 248 | REPORTER_ASSERT(r, f[1] == 1.0f); |
| 249 | REPORTER_ASSERT(r, f[2] == 0.0f); |
| 250 | REPORTER_ASSERT(r, f[3] == 0.0f); |
| 251 | } |
| 252 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 253 | #include "SkRandom.h" |
| 254 | |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 255 | DEF_TEST(SkNx_u16_float, r) { |
| 256 | { |
| 257 | // u16 --> float |
| 258 | auto h4 = Sk4h(15, 17, 257, 65535); |
| 259 | auto f4 = SkNx_cast<float>(h4); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 260 | REPORTER_ASSERT(r, f4[0] == 15.0f); |
| 261 | REPORTER_ASSERT(r, f4[1] == 17.0f); |
| 262 | REPORTER_ASSERT(r, f4[2] == 257.0f); |
| 263 | REPORTER_ASSERT(r, f4[3] == 65535.0f); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 264 | } |
| 265 | { |
| 266 | // float -> u16 |
| 267 | auto f4 = Sk4f(15, 17, 257, 65535); |
| 268 | auto h4 = SkNx_cast<uint16_t>(f4); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 269 | REPORTER_ASSERT(r, h4[0] == 15); |
| 270 | REPORTER_ASSERT(r, h4[1] == 17); |
| 271 | REPORTER_ASSERT(r, h4[2] == 257); |
| 272 | REPORTER_ASSERT(r, h4[3] == 65535); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // starting with any u16 value, we should be able to have a perfect round-trip in/out of floats |
| 276 | // |
| 277 | SkRandom rand; |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 278 | for (int i = 0; i < 10000; ++i) { |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 279 | const uint16_t s16[4] { |
| 280 | (uint16_t)rand.nextU16(), (uint16_t)rand.nextU16(), |
| 281 | (uint16_t)rand.nextU16(), (uint16_t)rand.nextU16(), |
| 282 | }; |
| 283 | auto u4_0 = Sk4h::Load(s16); |
| 284 | auto f4 = SkNx_cast<float>(u4_0); |
| 285 | auto u4_1 = SkNx_cast<uint16_t>(f4); |
| 286 | uint16_t d16[4]; |
| 287 | u4_1.store(d16); |
| 288 | REPORTER_ASSERT(r, !memcmp(s16, d16, sizeof(s16))); |
| 289 | } |
| 290 | } |