Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: tests/SkNxTest.cpp

Issue 1464623002: Add SkNx_cast(). (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: derp, is stands for _ints_ Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/opts/SkNx_sse.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "Sk4px.h" 8 #include "Sk4px.h"
9 #include "SkNx.h" 9 #include "SkNx.h"
10 #include "SkRandom.h" 10 #include "SkRandom.h"
11 #include "Test.h" 11 #include "Test.h"
12 12
13 template <int N> 13 template <int N>
14 static void test_Nf(skiatest::Reporter* r) { 14 static void test_Nf(skiatest::Reporter* r) {
15 15
16 auto assert_nearly_eq = [&](float eps, const SkNf<N>& v, float a, float b, f loat c, float d) { 16 auto assert_nearly_eq = [&](float eps, const SkNx<N, float>& v,
17 float a, float b, float c, float d) {
17 auto close = [=](float a, float b) { return fabsf(a-b) <= eps; }; 18 auto close = [=](float a, float b) { return fabsf(a-b) <= eps; };
18 float vals[4]; 19 float vals[4];
19 v.store(vals); 20 v.store(vals);
20 bool ok = close(vals[0], a) && close(vals[1], b) 21 bool ok = close(vals[0], a) && close(vals[1], b)
21 && close(v.template kth<0>(), a) && close(v.template kth<1>(), b) ; 22 && close(v.template kth<0>(), a) && close(v.template kth<1>(), b) ;
22 REPORTER_ASSERT(r, ok); 23 REPORTER_ASSERT(r, ok);
23 if (N == 4) { 24 if (N == 4) {
24 ok = close(vals[2], c) && close(vals[3], d) 25 ok = close(vals[2], c) && close(vals[3], d)
25 && close(v.template kth<2>(), c) && close(v.template kth<3>(), d); 26 && close(v.template kth<2>(), c) && close(v.template kth<3>(), d);
26 REPORTER_ASSERT(r, ok); 27 REPORTER_ASSERT(r, ok);
27 } 28 }
28 }; 29 };
29 auto assert_eq = [&](const SkNf<N>& v, float a, float b, float c, float d) { 30 auto assert_eq = [&](const SkNx<N, float>& v, float a, float b, float c, flo at d) {
30 return assert_nearly_eq(0, v, a,b,c,d); 31 return assert_nearly_eq(0, v, a,b,c,d);
31 }; 32 };
32 33
33 float vals[] = {3, 4, 5, 6}; 34 float vals[] = {3, 4, 5, 6};
34 SkNf<N> a = SkNf<N>::Load(vals), 35 SkNx<N,float> a = SkNx<N,float>::Load(vals),
35 b(a), 36 b(a),
36 c = a; 37 c = a;
37 SkNf<N> d; 38 SkNx<N,float> d;
38 d = a; 39 d = a;
39 40
40 assert_eq(a, 3, 4, 5, 6); 41 assert_eq(a, 3, 4, 5, 6);
41 assert_eq(b, 3, 4, 5, 6); 42 assert_eq(b, 3, 4, 5, 6);
42 assert_eq(c, 3, 4, 5, 6); 43 assert_eq(c, 3, 4, 5, 6);
43 assert_eq(d, 3, 4, 5, 6); 44 assert_eq(d, 3, 4, 5, 6);
44 45
45 assert_eq(a+b, 6, 8, 10, 12); 46 assert_eq(a+b, 6, 8, 10, 12);
46 assert_eq(a*b, 9, 16, 25, 36); 47 assert_eq(a*b, 9, 16, 25, 36);
47 assert_eq(a*b-b, 6, 12, 20, 30); 48 assert_eq(a*b-b, 6, 12, 20, 30);
48 assert_eq((a*b).sqrt(), 3, 4, 5, 6); 49 assert_eq((a*b).sqrt(), 3, 4, 5, 6);
49 assert_eq(a/b, 1, 1, 1, 1); 50 assert_eq(a/b, 1, 1, 1, 1);
50 assert_eq(SkNf<N>(0)-a, -3, -4, -5, -6); 51 assert_eq(SkNx<N,float>(0)-a, -3, -4, -5, -6);
51 52
52 SkNf<N> fours(4); 53 SkNx<N,float> fours(4);
53 54
54 assert_eq(fours.sqrt(), 2,2,2,2); 55 assert_eq(fours.sqrt(), 2,2,2,2);
55 assert_nearly_eq(0.001f, fours.rsqrt0(), 0.5, 0.5, 0.5, 0.5); 56 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.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); 58 assert_nearly_eq(0.001f, fours.rsqrt2(), 0.5, 0.5, 0.5, 0.5);
58 59
59 assert_eq( fours. invert(), 0.25, 0.25, 0.25, 0.25); 60 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); 61 assert_nearly_eq(0.001f, fours.approxInvert(), 0.25, 0.25, 0.25, 0.25);
61 62
62 assert_eq(SkNf<N>::Min(a, fours), 3, 4, 4, 4); 63 assert_eq(SkNx<N,float>::Min(a, fours), 3, 4, 4, 4);
63 assert_eq(SkNf<N>::Max(a, fours), 4, 4, 5, 6); 64 assert_eq(SkNx<N,float>::Max(a, fours), 4, 4, 5, 6);
64 65
65 // Test some comparisons. This is not exhaustive. 66 // Test some comparisons. This is not exhaustive.
66 REPORTER_ASSERT(r, (a == b).allTrue()); 67 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).anyTrue());
68 REPORTER_ASSERT(r, !(a+b == a*b-b).allTrue()); 69 REPORTER_ASSERT(r, !(a+b == a*b-b).allTrue());
69 REPORTER_ASSERT(r, !(a+b == a*b).anyTrue()); 70 REPORTER_ASSERT(r, !(a+b == a*b).anyTrue());
70 REPORTER_ASSERT(r, !(a != b).anyTrue()); 71 REPORTER_ASSERT(r, !(a != b).anyTrue());
71 REPORTER_ASSERT(r, (a < fours).anyTrue()); 72 REPORTER_ASSERT(r, (a < fours).anyTrue());
72 REPORTER_ASSERT(r, (a <= fours).anyTrue()); 73 REPORTER_ASSERT(r, (a <= fours).anyTrue());
73 REPORTER_ASSERT(r, !(a > fours).allTrue()); 74 REPORTER_ASSERT(r, !(a > fours).allTrue());
74 REPORTER_ASSERT(r, !(a >= fours).allTrue()); 75 REPORTER_ASSERT(r, !(a >= fours).allTrue());
75 } 76 }
76 77
77 DEF_TEST(SkNf, r) { 78 DEF_TEST(SkNf, r) {
78 test_Nf<2>(r); 79 test_Nf<2>(r);
79 test_Nf<4>(r); 80 test_Nf<4>(r);
80 } 81 }
81 82
82 template <int N, typename T> 83 template <int N, typename T>
83 void test_Ni(skiatest::Reporter* r) { 84 void 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 auto assert_eq = [&](const SkNx<N,T>& v, T a, T b, T c, T d, T e, T f, T g, T h) {
85 T vals[8]; 86 T vals[8];
86 v.store(vals); 87 v.store(vals);
87 88
88 switch (N) { 89 switch (N) {
89 case 8: REPORTER_ASSERT(r, vals[4] == e && vals[5] == f && vals[6] == g && vals[7] == h); 90 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 4: REPORTER_ASSERT(r, vals[2] == c && vals[3] == d);
91 case 2: REPORTER_ASSERT(r, vals[0] == a && vals[1] == b); 92 case 2: REPORTER_ASSERT(r, vals[0] == a && vals[1] == b);
92 } 93 }
93 switch (N) { 94 switch (N) {
94 case 8: REPORTER_ASSERT(r, v.template kth<4>() == e && v.template kth< 5>() == f && 95 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 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 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 case 2: REPORTER_ASSERT(r, v.template kth<0>() == a && v.template kth< 1>() == b);
98 } 99 }
99 }; 100 };
100 101
101 T vals[] = { 1,2,3,4,5,6,7,8 }; 102 T vals[] = { 1,2,3,4,5,6,7,8 };
102 SkNi<N,T> a = SkNi<N,T>::Load(vals), 103 SkNx<N,T> a = SkNx<N,T>::Load(vals),
103 b(a), 104 b(a),
104 c = a; 105 c = a;
105 SkNi<N,T> d; 106 SkNx<N,T> d;
106 d = a; 107 d = a;
107 108
108 assert_eq(a, 1,2,3,4,5,6,7,8); 109 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(b, 1,2,3,4,5,6,7,8);
110 assert_eq(c, 1,2,3,4,5,6,7,8); 111 assert_eq(c, 1,2,3,4,5,6,7,8);
111 assert_eq(d, 1,2,3,4,5,6,7,8); 112 assert_eq(d, 1,2,3,4,5,6,7,8);
112 113
113 assert_eq(a+a, 2,4,6,8,10,12,14,16); 114 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, 1,4,9,16,25,36,49,64);
115 assert_eq(a*a-a, 0,2,6,12,20,30,42,56); 116 assert_eq(a*a-a, 0,2,6,12,20,30,42,56);
116 117
117 assert_eq(a >> 2, 0,0,0,1,1,1,1,2); 118 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 assert_eq(a << 1, 2,4,6,8,10,12,14,16);
119 120
120 REPORTER_ASSERT(r, a.template kth<1>() == 2); 121 REPORTER_ASSERT(r, a.template kth<1>() == 2);
121 } 122 }
122 123
123 DEF_TEST(SkNi, r) { 124 DEF_TEST(SkNx, r) {
124 test_Ni<2, uint16_t>(r); 125 test_Ni<2, uint16_t>(r);
125 test_Ni<4, uint16_t>(r); 126 test_Ni<4, uint16_t>(r);
126 test_Ni<8, uint16_t>(r); 127 test_Ni<8, uint16_t>(r);
127 128
128 test_Ni<2, int>(r); 129 test_Ni<2, int>(r);
129 test_Ni<4, int>(r); 130 test_Ni<4, int>(r);
130 test_Ni<8, int>(r); 131 test_Ni<8, int>(r);
131 } 132 }
132 133
133 DEF_TEST(SkNi_min_lt, r) { 134 DEF_TEST(SkNi_min_lt, r) {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 Sk4f(0.7f).toBytes(bytes); 214 Sk4f(0.7f).toBytes(bytes);
214 REPORTER_ASSERT(r, bytes[0] == 0); 215 REPORTER_ASSERT(r, bytes[0] == 0);
215 216
216 // Clamping edge cases. 217 // Clamping edge cases.
217 Sk4f(-2.0f, -0.7f, 255.9f, 256.0f).toBytes(bytes); 218 Sk4f(-2.0f, -0.7f, 255.9f, 256.0f).toBytes(bytes);
218 REPORTER_ASSERT(r, bytes[0] == 0); 219 REPORTER_ASSERT(r, bytes[0] == 0);
219 REPORTER_ASSERT(r, bytes[1] == 0); 220 REPORTER_ASSERT(r, bytes[1] == 0);
220 REPORTER_ASSERT(r, bytes[2] == 255); 221 REPORTER_ASSERT(r, bytes[2] == 255);
221 REPORTER_ASSERT(r, bytes[3] == 255); 222 REPORTER_ASSERT(r, bytes[3] == 255);
222 } 223 }
224
225 DEF_TEST(SkNx_cast, r) {
226 Sk4f fs(-1.7f, -1.4f, 0.5f, 1.9f);
227 Sk4i is = SkNx_cast<int>(fs);
228
229 REPORTER_ASSERT(r, is.kth<0>() == -1);
230 REPORTER_ASSERT(r, is.kth<1>() == -1);
231 REPORTER_ASSERT(r, is.kth<2>() == 0);
232 REPORTER_ASSERT(r, is.kth<3>() == 1);
233 }
OLDNEW
« no previous file with comments | « src/opts/SkNx_sse.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698