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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/private/SkNx.h" |
| 9 | #include "include/utils/SkRandom.h" |
| 10 | #include "src/core/Sk4px.h" |
| 11 | #include "tests/Test.h" |
mtklein | c9adb05 | 2015-03-30 10:50:27 -0700 | [diff] [blame] | 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) { |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 87 | 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); |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 95 | } |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 96 | switch (N) { |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 97 | 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); |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 105 | } |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | T vals[] = { 1,2,3,4,5,6,7,8 }; |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 109 | SkNx<N,T> a = SkNx<N,T>::Load(vals), |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 110 | b(a), |
| 111 | c = a; |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 112 | SkNx<N,T> d; |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 113 | 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 | |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 127 | REPORTER_ASSERT(r, a[1] == 2); |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 128 | } |
| 129 | |
mtklein | 6c221b4 | 2015-11-20 13:53:19 -0800 | [diff] [blame] | 130 | DEF_TEST(SkNx, r) { |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 131 | test_Ni<2, uint16_t>(r); |
| 132 | test_Ni<4, uint16_t>(r); |
| 133 | test_Ni<8, uint16_t>(r); |
mtklein | 1113da7 | 2015-04-27 12:08:01 -0700 | [diff] [blame] | 134 | |
| 135 | test_Ni<2, int>(r); |
| 136 | test_Ni<4, int>(r); |
| 137 | test_Ni<8, int>(r); |
mtklein | 115acee | 2015-04-14 14:02:52 -0700 | [diff] [blame] | 138 | } |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 139 | |
mtklein | e20633e | 2015-07-13 12:06:33 -0700 | [diff] [blame] | 140 | DEF_TEST(SkNi_min_lt, r) { |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 141 | // Exhaustively check the 8x8 bit space. |
| 142 | for (int a = 0; a < (1<<8); a++) { |
| 143 | for (int b = 0; b < (1<<8); b++) { |
mtklein | e20633e | 2015-07-13 12:06:33 -0700 | [diff] [blame] | 144 | Sk16b aw(a), bw(b); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 145 | REPORTER_ASSERT(r, Sk16b::Min(aw, bw)[0] == std::min(a, b)); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 146 | REPORTER_ASSERT(r, !(aw < bw)[0] == !(a < b)); |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 147 | }} |
| 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 Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 155 | REPORTER_ASSERT(r, Sk16h::Min(Sk16h(a), Sk16h(b))[0] == std::min(a, b)); |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 156 | } |
| 157 | #else |
| 158 | for (int a = 0; a < (1<<16); a++) { |
| 159 | for (int b = 0; b < (1<<16); b++) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 160 | REPORTER_ASSERT(r, Sk16h::Min(Sk16h(a), Sk16h(b))[0] == std::min(a, b)); |
mtklein | 27e517a | 2015-05-14 17:53:04 -0700 | [diff] [blame] | 161 | }} |
| 162 | #endif |
| 163 | } |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 164 | |
| 165 | DEF_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 | |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 172 | REPORTER_ASSERT(r, Sk16b(a).saturatedAdd(Sk16b(b))[0] == exact); |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
Herb Derby | 5eb1528 | 2017-10-10 17:14:18 -0400 | [diff] [blame] | 177 | DEF_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 | |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 191 | DEF_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 Klein | 7dfe6d9 | 2018-12-18 14:53:37 -0500 | [diff] [blame] | 197 | Sk4px av = Sk16b(a), |
| 198 | bv = Sk16b(b); |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 199 | |
| 200 | // This way should always be exactly correct. |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 201 | int correct = (av * bv).div255()[0]; |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 202 | 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. |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 205 | int fast = av.approxMulDiv255(bv)[0]; |
mtklein | 3747875 | 2015-06-15 10:58:42 -0700 | [diff] [blame] | 206 | 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 | } |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 213 | |
mtklein | c33065a | 2016-01-15 12:16:40 -0800 | [diff] [blame] | 214 | DEF_TEST(SkNx_abs, r) { |
| 215 | auto fs = Sk4f(0.0f, -0.0f, 2.0f, -4.0f).abs(); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 216 | 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 Dalton | 7732f4f | 2017-08-28 14:45:40 -0600 | [diff] [blame] | 220 | 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); |
mtklein | c33065a | 2016-01-15 12:16:40 -0800 | [diff] [blame] | 226 | } |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 227 | |
Yuqian Li | 7da6ba2 | 2017-07-12 13:36:05 -0400 | [diff] [blame] | 228 | DEF_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 | |
| 236 | DEF_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 Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 242 | REPORTER_ASSERT(r, min[i] == std::min(a[i], b[i])); |
| 243 | REPORTER_ASSERT(r, max[i] == std::max(a[i], b[i])); |
Yuqian Li | 7da6ba2 | 2017-07-12 13:36:05 -0400 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
mtklein | 126626e | 2016-02-09 15:41:36 -0800 | [diff] [blame] | 247 | DEF_TEST(SkNx_floor, r) { |
| 248 | auto fs = Sk4f(0.4f, -0.4f, 0.6f, -0.6f).floor(); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 249 | 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 Dalton | 89c5e88 | 2018-06-08 11:46:42 -0600 | [diff] [blame] | 253 | |
| 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); |
mtklein | 126626e | 2016-02-09 15:41:36 -0800 | [diff] [blame] | 261 | } |
| 262 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 263 | DEF_TEST(SkNx_shuffle, r) { |
| 264 | Sk4f f4(0,10,20,30); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 265 | |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 266 | 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); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 275 | } |
| 276 | |
mtklein | 0cf795f | 2016-02-17 07:23:36 -0800 | [diff] [blame] | 277 | DEF_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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 293 | #include "include/utils/SkRandom.h" |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 294 | |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 295 | DEF_TEST(SkNx_u16_float, r) { |
| 296 | { |
| 297 | // u16 --> float |
| 298 | auto h4 = Sk4h(15, 17, 257, 65535); |
| 299 | auto f4 = SkNx_cast<float>(h4); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 300 | 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); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 304 | } |
| 305 | { |
| 306 | // float -> u16 |
| 307 | auto f4 = Sk4f(15, 17, 257, 65535); |
| 308 | auto h4 = SkNx_cast<uint16_t>(f4); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 309 | 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); |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 313 | } |
| 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; |
mtklein | e4c0bee | 2016-02-09 10:35:27 -0800 | [diff] [blame] | 318 | for (int i = 0; i < 10000; ++i) { |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 319 | const uint16_t s16[4] { |
Mike Klein | 5595f6e | 2018-09-05 10:51:00 -0400 | [diff] [blame] | 320 | (uint16_t)(rand.nextU() >> 16), (uint16_t)(rand.nextU() >> 16), |
| 321 | (uint16_t)(rand.nextU() >> 16), (uint16_t)(rand.nextU() >> 16), |
mtklein | 629f25a | 2016-02-08 05:54:38 -0800 | [diff] [blame] | 322 | }; |
| 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 | } |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 331 | |
| 332 | // The SSE2 implementation of SkNx_cast<uint16_t>(Sk4i) is non-trivial, so worth a test. |
| 333 | DEF_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 | } |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 350 | |
| 351 | DEF_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 Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 360 | Sk4f::Load4(src, &a, &b, &c, &d); |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 361 | 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 Klein | 33cbfd7 | 2016-10-06 11:09:27 -0400 | [diff] [blame] | 379 | Sk4f::Store4(dst, a, b, c, d); |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 380 | REPORTER_ASSERT(r, 0 == memcmp(dst, src, 16 * sizeof(float))); |
| 381 | } |
Chris Dalton | 7732f4f | 2017-08-28 14:45:40 -0600 | [diff] [blame] | 382 | |
| 383 | DEF_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 | |
| 397 | DEF_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 Klein | 213d821 | 2017-11-30 12:07:20 -0500 | [diff] [blame] | 410 | |
| 411 | DEF_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 Dalton | 0cb7587 | 2017-12-01 13:23:05 -0700 | [diff] [blame] | 427 | |
Chris Dalton | 21f6437 | 2018-04-11 14:01:04 -0600 | [diff] [blame] | 428 | DEF_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 Dalton | 42f02aa | 2018-04-08 23:58:43 -0600 | [diff] [blame] | 441 | DEF_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 Dalton | 0cb7587 | 2017-12-01 13:23:05 -0700 | [diff] [blame] | 452 | DEF_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 Dalton | 6f8fa4e | 2018-02-06 17:55:30 -0700 | [diff] [blame] | 465 | |
| 466 | DEF_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 Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 471 | |
| 472 | float dst[8] = {-1, -1, -1, -1, -1, -1, -1, -1}; |
Chris Dalton | 6f8fa4e | 2018-02-06 17:55:30 -0700 | [diff] [blame] | 473 | 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 Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 482 | |
| 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 Dalton | 6f8fa4e | 2018-02-06 17:55:30 -0700 | [diff] [blame] | 495 | } |
Mike Klein | 68ff92f | 2018-03-26 13:04:14 -0400 | [diff] [blame] | 496 | |
Chris Dalton | e3fda93 | 2018-04-11 13:18:09 -0600 | [diff] [blame] | 497 | DEF_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 Klein | 68ff92f | 2018-03-26 13:04:14 -0400 | [diff] [blame] | 509 | DEF_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 | } |