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