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 | |
| 13 | template <int N, typename T> |
| 14 | static void test_Nf(skiatest::Reporter* r) { |
| 15 | |
| 16 | auto assert_nearly_eq = [&](double eps, const SkNf<N,T>& v, T a, T b, T c, T d) { |
| 17 | auto close = [=](T a, T b) { return fabs(a-b) <= eps; }; |
| 18 | T vals[4]; |
| 19 | v.store(vals); |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 20 | 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); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 23 | if (N == 4) { |
mtklein | a156a8f | 2015-04-03 06:16:13 -0700 | [diff] [blame] | 24 | 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); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 27 | } |
| 28 | }; |
| 29 | auto assert_eq = [&](const SkNf<N,T>& v, T a, T b, T c, T d) { |
| 30 | return assert_nearly_eq(0, v, a,b,c,d); |
| 31 | }; |
| 32 | |
| 33 | T vals[] = {3, 4, 5, 6}; |
| 34 | SkNf<N,T> a = SkNf<N,T>::Load(vals), |
| 35 | b(a), |
| 36 | c = a; |
| 37 | SkNf<N,T> d; |
| 38 | 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); |
mtklein | f2fe0e0 | 2015-06-10 08:57:28 -0700 | [diff] [blame] | 50 | assert_eq(SkNf<N,T>(0)-a, -3, -4, -5, -6); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 51 | |
| 52 | SkNf<N,T> fours(4); |
| 53 | |
| 54 | assert_eq(fours.sqrt(), 2,2,2,2); |
mtklein | d7c014f | 2015-04-27 14:22:32 -0700 | [diff] [blame] | 55 | assert_nearly_eq(0.001, fours.rsqrt0(), 0.5, 0.5, 0.5, 0.5); |
| 56 | assert_nearly_eq(0.001, fours.rsqrt1(), 0.5, 0.5, 0.5, 0.5); |
| 57 | assert_nearly_eq(0.001, fours.rsqrt2(), 0.5, 0.5, 0.5, 0.5); |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 58 | |
| 59 | assert_eq( fours. invert(), 0.25, 0.25, 0.25, 0.25); |
| 60 | assert_nearly_eq(0.001, fours.approxInvert(), 0.25, 0.25, 0.25, 0.25); |
| 61 | |
| 62 | assert_eq(SkNf<N,T>::Min(a, fours), 3, 4, 4, 4); |
| 63 | assert_eq(SkNf<N,T>::Max(a, fours), 4, 4, 5, 6); |
| 64 | |
| 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 | |
| 77 | DEF_TEST(SkNf, r) { |
| 78 | test_Nf<2, float>(r); |
| 79 | test_Nf<2, double>(r); |
| 80 | |
| 81 | test_Nf<4, float>(r); |
| 82 | test_Nf<4, double>(r); |
| 83 | } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 84 | |
| 85 | template <int N, typename T> |
| 86 | void test_Ni(skiatest::Reporter* r) { |
| 87 | auto assert_eq = [&](const SkNi<N,T>& v, T a, T b, T c, T d, T e, T f, T g, T h) { |
| 88 | T vals[8]; |
| 89 | v.store(vals); |
| 90 | |
| 91 | switch (N) { |
| 92 | case 8: REPORTER_ASSERT(r, vals[4] == e && vals[5] == f && vals[6] == g && vals[7] == h); |
| 93 | case 4: REPORTER_ASSERT(r, vals[2] == c && vals[3] == d); |
| 94 | case 2: REPORTER_ASSERT(r, vals[0] == a && vals[1] == b); |
| 95 | } |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 96 | switch (N) { |
| 97 | case 8: REPORTER_ASSERT(r, v.template kth<4>() == e && v.template kth<5>() == f && |
| 98 | v.template kth<6>() == g && v.template kth<7>() == h); |
| 99 | case 4: REPORTER_ASSERT(r, v.template kth<2>() == c && v.template kth<3>() == d); |
| 100 | case 2: REPORTER_ASSERT(r, v.template kth<0>() == a && v.template kth<1>() == b); |
| 101 | } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | T vals[] = { 1,2,3,4,5,6,7,8 }; |
| 105 | SkNi<N,T> a = SkNi<N,T>::Load(vals), |
| 106 | b(a), |
| 107 | c = a; |
| 108 | SkNi<N,T> d; |
| 109 | d = a; |
| 110 | |
| 111 | assert_eq(a, 1,2,3,4,5,6,7,8); |
| 112 | assert_eq(b, 1,2,3,4,5,6,7,8); |
| 113 | assert_eq(c, 1,2,3,4,5,6,7,8); |
| 114 | assert_eq(d, 1,2,3,4,5,6,7,8); |
| 115 | |
| 116 | assert_eq(a+a, 2,4,6,8,10,12,14,16); |
| 117 | assert_eq(a*a, 1,4,9,16,25,36,49,64); |
| 118 | assert_eq(a*a-a, 0,2,6,12,20,30,42,56); |
| 119 | |
| 120 | assert_eq(a >> 2, 0,0,0,1,1,1,1,2); |
| 121 | assert_eq(a << 1, 2,4,6,8,10,12,14,16); |
| 122 | |
| 123 | REPORTER_ASSERT(r, a.template kth<1>() == 2); |
| 124 | } |
| 125 | |
| 126 | DEF_TEST(SkNi, r) { |
| 127 | test_Ni<2, uint16_t>(r); |
| 128 | test_Ni<4, uint16_t>(r); |
| 129 | test_Ni<8, uint16_t>(r); |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 130 | |
| 131 | test_Ni<2, int>(r); |
| 132 | test_Ni<4, int>(r); |
| 133 | test_Ni<8, int>(r); |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 134 | } |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 135 | |
mtklein | e20633e | 2015-07-13 12:06:33 -0700 | [diff] [blame] | 136 | DEF_TEST(SkNi_min_lt, r) { |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 137 | // Exhaustively check the 8x8 bit space. |
| 138 | for (int a = 0; a < (1<<8); a++) { |
| 139 | for (int b = 0; b < (1<<8); b++) { |
mtklein | e20633e | 2015-07-13 12:06:33 -0700 | [diff] [blame] | 140 | Sk16b aw(a), bw(b); |
| 141 | REPORTER_ASSERT(r, Sk16b::Min(aw, bw).kth<0>() == SkTMin(a, b)); |
| 142 | REPORTER_ASSERT(r, !(aw < bw).kth<0>() == !(a < b)); |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 143 | }} |
| 144 | |
| 145 | // Exhausting the 16x16 bit space is kind of slow, so only do that in release builds. |
| 146 | #ifdef SK_DEBUG |
| 147 | SkRandom rand; |
| 148 | for (int i = 0; i < (1<<16); i++) { |
| 149 | uint16_t a = rand.nextU() >> 16, |
| 150 | b = rand.nextU() >> 16; |
| 151 | REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b)); |
| 152 | } |
| 153 | #else |
| 154 | for (int a = 0; a < (1<<16); a++) { |
| 155 | for (int b = 0; b < (1<<16); b++) { |
| 156 | REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b)); |
| 157 | }} |
| 158 | #endif |
| 159 | } |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 160 | |
| 161 | DEF_TEST(SkNi_saturatedAdd, r) { |
| 162 | for (int a = 0; a < (1<<8); a++) { |
| 163 | for (int b = 0; b < (1<<8); b++) { |
| 164 | int exact = a+b; |
| 165 | if (exact > 255) { exact = 255; } |
| 166 | if (exact < 0) { exact = 0; } |
| 167 | |
| 168 | REPORTER_ASSERT(r, Sk16b(a).saturatedAdd(Sk16b(b)).kth<0>() == exact); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | DEF_TEST(Sk4px_muldiv255round, r) { |
| 174 | for (int a = 0; a < (1<<8); a++) { |
| 175 | for (int b = 0; b < (1<<8); b++) { |
| 176 | int exact = (a*b+127)/255; |
| 177 | |
| 178 | // Duplicate a and b 16x each. |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 179 | auto av = Sk4px::DupAlpha(a), |
| 180 | bv = Sk4px::DupAlpha(b); |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 181 | |
| 182 | // This way should always be exactly correct. |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 183 | int correct = (av * bv).div255().kth<0>(); |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 184 | REPORTER_ASSERT(r, correct == exact); |
| 185 | |
| 186 | // 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] | 187 | int fast = av.approxMulDiv255(bv).kth<0>(); |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 188 | REPORTER_ASSERT(r, fast-exact >= -1 && fast-exact <= 1); |
| 189 | if (a == 0 || a == 255 || b == 0 || b == 255) { |
| 190 | REPORTER_ASSERT(r, fast == exact); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 195 | |
| 196 | DEF_TEST(Sk4px_widening, r) { |
| 197 | SkPMColor colors[] = { |
| 198 | SkPreMultiplyColor(0xff00ff00), |
| 199 | SkPreMultiplyColor(0x40008000), |
| 200 | SkPreMultiplyColor(0x7f020406), |
| 201 | SkPreMultiplyColor(0x00000000), |
| 202 | }; |
| 203 | auto packed = Sk4px::Load4(colors); |
| 204 | |
| 205 | auto wideLo = packed.widenLo(), |
| 206 | wideHi = packed.widenHi(), |
| 207 | wideLoHi = packed.widenLoHi(), |
| 208 | wideLoHiAlt = wideLo + wideHi; |
| 209 | REPORTER_ASSERT(r, 0 == memcmp(&wideLoHi, &wideLoHiAlt, sizeof(wideLoHi))); |
| 210 | } |
mtklein | a508f3c | 2015-09-01 06:29:45 -0700 | [diff] [blame] | 211 | |
| 212 | DEF_TEST(Sk4f_toBytes, r) { |
| 213 | uint8_t bytes[4]; |
| 214 | |
| 215 | // toBytes truncates, not rounds. |
| 216 | Sk4f(0.7f).toBytes(bytes); |
| 217 | REPORTER_ASSERT(r, bytes[0] == 0); |
| 218 | |
| 219 | // Clamping edge cases. |
| 220 | Sk4f(-2.0f, -0.7f, 255.9f, 256.0f).toBytes(bytes); |
| 221 | REPORTER_ASSERT(r, bytes[0] == 0); |
| 222 | REPORTER_ASSERT(r, bytes[1] == 0); |
| 223 | REPORTER_ASSERT(r, bytes[2] == 255); |
| 224 | REPORTER_ASSERT(r, bytes[3] == 255); |
| 225 | } |