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

Side by Side Diff: tests/SkNxTest.cpp

Issue 1184113003: Thorough tests for saturatedAdd and mulDiv255Round. (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: fix portable saturatedAdd() Created 5 years, 6 months 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/core/SkNx.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 "SkNx.h" 9 #include "SkNx.h"
9 #include "SkRandom.h" 10 #include "SkRandom.h"
10 #include "Test.h" 11 #include "Test.h"
11 12
12 template <int N, typename T> 13 template <int N, typename T>
13 static void test_Nf(skiatest::Reporter* r) { 14 static void test_Nf(skiatest::Reporter* r) {
14 15
15 auto assert_nearly_eq = [&](double eps, const SkNf<N,T>& v, T a, T b, T c, T d) { 16 auto assert_nearly_eq = [&](double eps, const SkNf<N,T>& v, T a, T b, T c, T d) {
16 auto close = [=](T a, T b) { return fabs(a-b) <= eps; }; 17 auto close = [=](T a, T b) { return fabs(a-b) <= eps; };
17 T vals[4]; 18 T vals[4];
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 b = rand.nextU() >> 16; 148 b = rand.nextU() >> 16;
148 REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b)) ; 149 REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b)) ;
149 } 150 }
150 #else 151 #else
151 for (int a = 0; a < (1<<16); a++) { 152 for (int a = 0; a < (1<<16); a++) {
152 for (int b = 0; b < (1<<16); b++) { 153 for (int b = 0; b < (1<<16); b++) {
153 REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b)) ; 154 REPORTER_ASSERT(r, Sk8h::Min(Sk8h(a), Sk8h(b)).kth<0>() == SkTMin(a, b)) ;
154 }} 155 }}
155 #endif 156 #endif
156 } 157 }
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.
177 Sk4px av((SkAlpha)a),
178 bv((SkAlpha)b);
179
180 // This way should always be exactly correct.
181 int correct = av.mulWiden(bv).div255RoundNarrow().kth<0>();
182 REPORTER_ASSERT(r, correct == exact);
183
184 // We're a bit more flexible on this method: correct for 0 or 255, other wise off by <=1.
185 int fast = av.fastMulDiv255Round(bv).kth<0>();
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 }
OLDNEW
« no previous file with comments | « src/core/SkNx.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698