blob: f4693262f9358a1c6994389f2cb09173e77c8517 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
Cary Clarka4083c92017-09-15 11:59:23 -04008#include "SkColorData.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "SkEndian.h"
Yuqian Lice1d2932016-11-18 10:18:15 -050010#include "SkFDot6.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070011#include "SkFixed.h"
jvanverth93679922014-11-26 13:15:59 -080012#include "SkHalf.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000013#include "SkMathPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000014#include "SkPoint.h"
15#include "SkRandom.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040016#include "SkTo.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "Test.h"
reed@android.comed673312009-02-27 16:24:51 +000018
reed@google.comc21f86f2013-04-29 14:18:23 +000019static void test_clz(skiatest::Reporter* reporter) {
20 REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
21 REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
22 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
reed@google.com7729534d2013-04-29 14:43:50 +000023 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
skia.committer@gmail.com81521132013-04-30 07:01:03 +000024
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000025 SkRandom rand;
reed@google.comc21f86f2013-04-29 14:18:23 +000026 for (int i = 0; i < 1000; ++i) {
27 uint32_t mask = rand.nextU();
reed@google.combc57a292013-04-29 15:27:42 +000028 // need to get some zeros for testing, but in some obscure way so the
29 // compiler won't "see" that, and work-around calling the functions.
30 mask >>= (mask & 31);
reed@google.comc21f86f2013-04-29 14:18:23 +000031 int intri = SkCLZ(mask);
32 int porta = SkCLZ_portable(mask);
33 REPORTER_ASSERT(reporter, intri == porta);
34 }
35}
36
Yuqian Lice1d2932016-11-18 10:18:15 -050037static void test_quick_div(skiatest::Reporter* reporter) {
38 /*
39 The inverse table is generated by turning on SkDebugf in the following test code
40 */
41 SkFixed storage[kInverseTableSize * 2];
42 SkFixed* table = storage + kInverseTableSize;
43
44 // SkDebugf("static const int gFDot6INVERSE[] = {");
45 for (SkFDot6 i=-kInverseTableSize; i<kInverseTableSize; i++) {
46 if (i != 0) {
47 table[i] = SkFDot6Div(SK_FDot6One, i);
48 REPORTER_ASSERT(reporter, table[i] == gFDot6INVERSE[i + kInverseTableSize]);
49 }
50 // SkDebugf("%d, ", table[i]);
51 }
52 // SkDebugf("}\n");
53
54
55 for (SkFDot6 a = -1024; a <= 1024; a++) {
56 for (SkFDot6 b = -1024; b <= 1024; b++) {
57 if (b != 0) {
58 SkFixed ourAnswer = QuickSkFDot6Div(a, b);
59 SkFixed directAnswer = SkFDot6Div(a, b);
60 REPORTER_ASSERT(reporter,
61 (directAnswer == 0 && ourAnswer == 0) ||
62 SkFixedDiv(SkAbs32(directAnswer - ourAnswer), SkAbs32(directAnswer)) <= 1 << 10
63 );
64 }
65 }
66 }
67}
68
reed@google.comc21f86f2013-04-29 14:18:23 +000069///////////////////////////////////////////////////////////////////////////////
70
reed@google.coma7d74612012-05-30 12:30:09 +000071static float sk_fsel(float pred, float result_ge, float result_lt) {
72 return pred >= 0 ? result_ge : result_lt;
73}
74
75static float fast_floor(float x) {
reed@google.comc20bc252012-05-30 13:48:14 +000076// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
77 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
robertphillips@google.com00bf06a2012-05-30 15:19:17 +000078 return (float)(x + big) - big;
reed@google.coma7d74612012-05-30 12:30:09 +000079}
80
81static float std_floor(float x) {
82 return sk_float_floor(x);
83}
84
85static void test_floor_value(skiatest::Reporter* reporter, float value) {
86 float fast = fast_floor(value);
87 float std = std_floor(value);
halcanary7d571242016-02-24 17:59:16 -080088 if (std != fast) {
89 ERRORF(reporter, "fast_floor(%.9g) == %.9g != %.9g == std_floor(%.9g)",
90 value, fast, std, value);
91 }
reed@google.coma7d74612012-05-30 12:30:09 +000092}
93
94static void test_floor(skiatest::Reporter* reporter) {
95 static const float gVals[] = {
96 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
97 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000098
reed@google.coma7d74612012-05-30 12:30:09 +000099 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
100 test_floor_value(reporter, gVals[i]);
101// test_floor_value(reporter, -gVals[i]);
102 }
103}
104
105///////////////////////////////////////////////////////////////////////////////
106
reed@google.comea774d22013-04-22 20:21:56 +0000107// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
108static void test_muldivround(skiatest::Reporter* reporter) {
109#if 0
110 // this "complete" test is too slow, so we test a random sampling of it
111
112 for (int a = 0; a <= 32767; ++a) {
113 for (int b = 0; b <= 32767; ++b) {
114 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
115 unsigned prod1 = SkMulDiv255Round(a, b);
116 SkASSERT(prod0 == prod1);
117 }
118 }
119#endif
120
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000121 SkRandom rand;
reed@google.comea774d22013-04-22 20:21:56 +0000122 for (int i = 0; i < 10000; ++i) {
123 unsigned a = rand.nextU() & 0x7FFF;
124 unsigned b = rand.nextU() & 0x7FFF;
125
126 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
127 unsigned prod1 = SkMulDiv255Round(a, b);
128
129 REPORTER_ASSERT(reporter, prod0 == prod1);
130 }
131}
132
reed@google.com0abb4992011-10-06 20:04:36 +0000133static float float_blend(int src, int dst, float unit) {
134 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +0000135}
136
reed@google.com8d7e39c2011-11-09 17:12:08 +0000137static int blend31(int src, int dst, int a31) {
138 return dst + ((src - dst) * a31 * 2114 >> 16);
139 // return dst + ((src - dst) * a31 * 33 >> 10);
140}
141
142static int blend31_slow(int src, int dst, int a31) {
143 int prod = src * a31 + (31 - a31) * dst + 16;
144 prod = (prod + (prod >> 5)) >> 5;
145 return prod;
146}
147
148static int blend31_round(int src, int dst, int a31) {
149 int prod = (src - dst) * a31 + 16;
150 prod = (prod + (prod >> 5)) >> 5;
151 return dst + prod;
152}
153
154static int blend31_old(int src, int dst, int a31) {
155 a31 += a31 >> 4;
156 return dst + ((src - dst) * a31 >> 5);
157}
158
caryclark@google.com42639cd2012-06-06 12:03:39 +0000159// suppress unused code warning
160static int (*blend_functions[])(int, int, int) = {
161 blend31,
162 blend31_slow,
163 blend31_round,
164 blend31_old
165};
166
reed@google.com8d7e39c2011-11-09 17:12:08 +0000167static void test_blend31() {
168 int failed = 0;
169 int death = 0;
caryclark@google.com42639cd2012-06-06 12:03:39 +0000170 if (false) { // avoid bit rot, suppress warning
171 failed = (*blend_functions[0])(0,0,0);
172 }
reed@google.com8d7e39c2011-11-09 17:12:08 +0000173 for (int src = 0; src <= 255; src++) {
174 for (int dst = 0; dst <= 255; dst++) {
175 for (int a = 0; a <= 31; a++) {
176// int r0 = blend31(src, dst, a);
177// int r0 = blend31_round(src, dst, a);
178// int r0 = blend31_old(src, dst, a);
179 int r0 = blend31_slow(src, dst, a);
180
181 float f = float_blend(src, dst, a / 31.f);
182 int r1 = (int)f;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000183 int r2 = SkScalarRoundToInt(f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000184
185 if (r0 != r1 && r0 != r2) {
bungeman@google.comfab44db2013-10-11 18:50:45 +0000186 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
halcanary7d571242016-02-24 17:59:16 -0800187 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000188 failed += 1;
189 }
190 if (r0 > 255) {
191 death += 1;
bungeman@google.comfab44db2013-10-11 18:50:45 +0000192 SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
193 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000194 }
195 }
196 }
197 }
198 SkDebugf("---- failed %d death %d\n", failed, death);
199}
200
reed@android.comed673312009-02-27 16:24:51 +0000201static void check_length(skiatest::Reporter* reporter,
202 const SkPoint& p, SkScalar targetLen) {
reed@android.comed673312009-02-27 16:24:51 +0000203 float x = SkScalarToFloat(p.fX);
204 float y = SkScalarToFloat(p.fY);
205 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000206
reed@android.comed673312009-02-27 16:24:51 +0000207 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000208
reed@android.comed673312009-02-27 16:24:51 +0000209 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000210}
211
reed@google.com077910e2011-02-08 21:56:39 +0000212static void unittest_isfinite(skiatest::Reporter* reporter) {
epoger@google.combf083a92011-06-08 18:26:08 +0000213 float nan = sk_float_asin(2);
Mike Reed026d20f2018-05-14 16:51:32 -0400214 float inf = SK_ScalarInfinity;
tomhudson@google.com75589252012-04-10 17:42:21 +0000215 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000216
reed@google.com077910e2011-02-08 21:56:39 +0000217 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000218 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
219 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
220 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
reed@android.comd4134452011-02-09 02:24:26 +0000221
222 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000223 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
224 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
225 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000226
reed@google.com077910e2011-02-08 21:56:39 +0000227 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000228 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
229 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
230 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000231}
232
jvanverth93679922014-11-26 13:15:59 -0800233static void unittest_half(skiatest::Reporter* reporter) {
234 static const float gFloats[] = {
235 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
236 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
237 };
238
239 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
240 SkHalf h = SkFloatToHalf(gFloats[i]);
241 float f = SkHalfToFloat(h);
242 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
243 }
244
245 // check some special values
246 union FloatUnion {
247 uint32_t fU;
248 float fF;
249 };
250
251 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
252 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
253 float f = SkHalfToFloat(h);
254 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
255
256 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
257 h = SkFloatToHalf(largestNegativeHalf.fF);
258 f = SkHalfToFloat(h);
259 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
260
261 static const FloatUnion smallestPositiveHalf = { 102 << 23 };
262 h = SkFloatToHalf(smallestPositiveHalf.fF);
263 f = SkHalfToFloat(h);
264 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
265
266 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
267 h = SkFloatToHalf(overflowHalf.fF);
268 f = SkHalfToFloat(h);
269 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
270
271 static const FloatUnion underflowHalf = { 101 << 23 };
272 h = SkFloatToHalf(underflowHalf.fF);
273 f = SkHalfToFloat(h);
274 REPORTER_ASSERT(reporter, f == 0.0f );
275
276 static const FloatUnion inf32 = { 255 << 23 };
277 h = SkFloatToHalf(inf32.fF);
278 f = SkHalfToFloat(h);
279 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
280
281 static const FloatUnion nan32 = { 255 << 23 | 1 };
282 h = SkFloatToHalf(nan32.fF);
283 f = SkHalfToFloat(h);
284 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
285
286}
287
mtkleina766ca82016-01-26 07:40:30 -0800288template <typename RSqrtFn>
289static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
jvanverth29c69792015-07-23 11:14:29 -0700290 const float maxRelativeError = 6.50196699e-4f;
291
292 // test close to 0 up to 1
293 float input = 0.000001f;
294 for (int i = 0; i < 1000; ++i) {
295 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800296 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700297 float relativeError = sk_float_abs(exact - estimate)/exact;
298 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
299 input += 0.001f;
300 }
301
302 // test 1 to ~100
303 input = 1.0f;
304 for (int i = 0; i < 1000; ++i) {
305 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800306 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700307 float relativeError = sk_float_abs(exact - estimate)/exact;
308 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
309 input += 0.01f;
310 }
311
312 // test some big numbers
313 input = 1000000.0f;
314 for (int i = 0; i < 100; ++i) {
315 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800316 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700317 float relativeError = sk_float_abs(exact - estimate)/exact;
318 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
319 input += 754326.f;
320 }
321}
322
reed@android.comed673312009-02-27 16:24:51 +0000323static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000324 for (int a = 0; a <= 255; a++) {
325 for (int b = 0; b <= 255; b++) {
326 int ab = a * b;
327 float s = ab / 255.0f;
328 int round = (int)floorf(s + 0.5f);
329 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000330
reed@android.comed673312009-02-27 16:24:51 +0000331 int iround = SkMulDiv255Round(a, b);
332 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000333
reed@android.comed673312009-02-27 16:24:51 +0000334 REPORTER_ASSERT(reporter, iround == round);
335 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000336
reed@android.comed673312009-02-27 16:24:51 +0000337 REPORTER_ASSERT(reporter, itrunc <= iround);
338 REPORTER_ASSERT(reporter, iround <= a);
339 REPORTER_ASSERT(reporter, iround <= b);
340 }
341 }
reed@android.comed673312009-02-27 16:24:51 +0000342}
343
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000344static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
345 for (int c = 0; c <= 255; c++) {
346 for (int a = 0; a <= 255; a++) {
347 int product = (c * a + 255);
348 int expected_ceiling = (product + (product >> 8)) >> 8;
349 int webkit_ceiling = (c * a + 254) / 255;
350 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
351 int skia_ceiling = SkMulDiv255Ceiling(c, a);
352 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
353 }
354 }
355}
356
reed@android.comf0ad0862010-02-09 19:18:38 +0000357static void test_copysign(skiatest::Reporter* reporter) {
358 static const int32_t gTriples[] = {
359 // x, y, expected result
360 0, 0, 0,
361 0, 1, 0,
362 0, -1, 0,
363 1, 0, 1,
364 1, 1, 1,
365 1, -1, -1,
366 -1, 0, 1,
367 -1, 1, 1,
368 -1, -1, -1,
369 };
370 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
371 REPORTER_ASSERT(reporter,
372 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000373 float x = (float)gTriples[i];
374 float y = (float)gTriples[i+1];
375 float expected = (float)gTriples[i+2];
376 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000377 }
378
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000379 SkRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000380 for (int j = 0; j < 1000; j++) {
381 int ix = rand.nextS();
382 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
383 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
384 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
385 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
386
387 SkScalar sx = rand.nextSScalar1();
388 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
389 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
390 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
391 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
392 }
393}
394
Mike Reed026d20f2018-05-14 16:51:32 -0400395static void huge_vector_normalize(skiatest::Reporter* reporter) {
396 // these values should fail (overflow/underflow) trying to normalize
397 const SkVector fail[] = {
398 { 0, 0 },
399 { SK_ScalarInfinity, 0 }, { 0, SK_ScalarInfinity },
400 { 0, SK_ScalarNaN }, { SK_ScalarNaN, 0 },
401 };
402 for (SkVector v : fail) {
403 SkVector v2 = v;
404 if (v2.setLength(1.0f)) {
405 REPORTER_ASSERT(reporter, !v.setLength(1.0f));
406 }
407 }
408}
409
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000410DEF_TEST(Math, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000411 int i;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000412 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000413
reed@android.comed673312009-02-27 16:24:51 +0000414 // these should assert
415#if 0
416 SkToS8(128);
417 SkToS8(-129);
418 SkToU8(256);
419 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000420
reed@android.comed673312009-02-27 16:24:51 +0000421 SkToS16(32768);
422 SkToS16(-32769);
423 SkToU16(65536);
424 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000425
reed@android.comed673312009-02-27 16:24:51 +0000426 if (sizeof(size_t) > 4) {
427 SkToS32(4*1024*1024);
428 SkToS32(-4*1024*1024);
429 SkToU32(5*1024*1024);
430 SkToU32(-5);
431 }
432#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000433
reed@android.comed673312009-02-27 16:24:51 +0000434 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000435 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000436 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000437
reed@android.comed673312009-02-27 16:24:51 +0000438 {
439 SkScalar x = SK_ScalarNaN;
440 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
441 }
reed@android.com80e39a72009-04-02 16:59:40 +0000442
reed@android.comed673312009-02-27 16:24:51 +0000443 for (i = 0; i < 1000; i++) {
Mike Klein5595f6e2018-09-05 10:51:00 -0400444 int value = rand.nextS() >> 16;
445 int max = rand.nextU() >> 16;
reed@android.com80e39a72009-04-02 16:59:40 +0000446
reed@android.comed673312009-02-27 16:24:51 +0000447 int clamp = SkClampMax(value, max);
448 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
449 REPORTER_ASSERT(reporter, clamp == clamp2);
450 }
reed@android.com80e39a72009-04-02 16:59:40 +0000451
reed@android.come72fee52009-11-16 14:52:01 +0000452 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000453 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000454
tomhudson@google.com75589252012-04-10 17:42:21 +0000455 // These random values are being treated as 32-bit-patterns, not as
456 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000457 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000458 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000459 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000460 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000461 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000462 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000463 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000464 check_length(reporter, p, SK_Scalar1);
465 }
reed@android.com80e39a72009-04-02 16:59:40 +0000466
reed@android.comed673312009-02-27 16:24:51 +0000467 {
468 SkFixed result = SkFixedDiv(100, 100);
469 REPORTER_ASSERT(reporter, result == SK_Fixed1);
470 result = SkFixedDiv(1, SK_Fixed1);
471 REPORTER_ASSERT(reporter, result == 1);
liyuqian0d2c2342016-07-13 13:34:46 -0700472 result = SkFixedDiv(10 - 1, SK_Fixed1 * 3);
473 REPORTER_ASSERT(reporter, result == 3);
reed@android.comed673312009-02-27 16:24:51 +0000474 }
reed@android.com80e39a72009-04-02 16:59:40 +0000475
liyuqian3f490cc2016-10-20 11:23:09 -0700476 {
477 REPORTER_ASSERT(reporter, (SkFixedRoundToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
478 REPORTER_ASSERT(reporter, (SkFixedFloorToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
479 REPORTER_ASSERT(reporter, (SkFixedCeilToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
480 }
481
Mike Reed026d20f2018-05-14 16:51:32 -0400482 huge_vector_normalize(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000483 unittest_isfinite(reporter);
jvanverth93679922014-11-26 13:15:59 -0800484 unittest_half(reporter);
mtkleina766ca82016-01-26 07:40:30 -0800485 test_rsqrt(reporter, sk_float_rsqrt);
486 test_rsqrt(reporter, sk_float_rsqrt_portable);
reed@android.com80e39a72009-04-02 16:59:40 +0000487
reed@android.come72fee52009-11-16 14:52:01 +0000488 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000489 SkFixed numer = rand.nextS();
490 SkFixed denom = rand.nextS();
491 SkFixed result = SkFixedDiv(numer, denom);
caryclark3127c992015-12-09 12:02:30 -0800492 int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000493
reed@android.comed673312009-02-27 16:24:51 +0000494 (void)SkCLZ(numer);
495 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000496
reed@android.comed673312009-02-27 16:24:51 +0000497 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
498 if (check > SK_MaxS32) {
499 check = SK_MaxS32;
500 } else if (check < -SK_MaxS32) {
501 check = SK_MinS32;
502 }
mtklein97ca98d2015-03-18 11:32:21 -0700503 if (result != (int32_t)check) {
504 ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
505 }
reed@android.comed673312009-02-27 16:24:51 +0000506 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.comed673312009-02-27 16:24:51 +0000507 }
reed@android.com80e39a72009-04-02 16:59:40 +0000508
humper@google.com05af1af2013-01-07 16:47:43 +0000509 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000510
reed@google.com8d7e39c2011-11-09 17:12:08 +0000511 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000512 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000513
514 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000515 test_clz(reporter);
Yuqian Lice1d2932016-11-18 10:18:15 -0500516 test_quick_div(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000517}
518
reed@google.comc9f81662013-05-03 18:06:31 +0000519template <typename T> struct PairRec {
520 T fYin;
521 T fYang;
522};
523
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000524DEF_TEST(TestEndian, reporter) {
reed@google.comc9f81662013-05-03 18:06:31 +0000525 static const PairRec<uint16_t> g16[] = {
526 { 0x0, 0x0 },
527 { 0xFFFF, 0xFFFF },
528 { 0x1122, 0x2211 },
529 };
530 static const PairRec<uint32_t> g32[] = {
531 { 0x0, 0x0 },
532 { 0xFFFFFFFF, 0xFFFFFFFF },
533 { 0x11223344, 0x44332211 },
534 };
535 static const PairRec<uint64_t> g64[] = {
536 { 0x0, 0x0 },
537 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
538 { 0x1122334455667788ULL, 0x8877665544332211ULL },
539 };
540
541 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
542 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
543 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
544
545 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
546 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
547 }
548 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
549 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
550 }
551 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
552 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
553 }
554}
555
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000556template <typename T>
557static void test_divmod(skiatest::Reporter* r) {
558 const struct {
559 T numer;
560 T denom;
561 } kEdgeCases[] = {
562 {(T)17, (T)17},
563 {(T)17, (T)4},
564 {(T)0, (T)17},
565 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
566 {(T)-17, (T)-17},
567 {(T)-17, (T)4},
568 {(T)17, (T)-4},
569 {(T)-17, (T)-4},
570 };
571
572 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
573 const T numer = kEdgeCases[i].numer;
574 const T denom = kEdgeCases[i].denom;
575 T div, mod;
576 SkTDivMod(numer, denom, &div, &mod);
577 REPORTER_ASSERT(r, numer/denom == div);
578 REPORTER_ASSERT(r, numer%denom == mod);
579 }
580
581 SkRandom rand;
582 for (size_t i = 0; i < 10000; i++) {
583 const T numer = (T)rand.nextS();
584 T denom = 0;
585 while (0 == denom) {
586 denom = (T)rand.nextS();
587 }
588 T div, mod;
589 SkTDivMod(numer, denom, &div, &mod);
590 REPORTER_ASSERT(r, numer/denom == div);
591 REPORTER_ASSERT(r, numer%denom == mod);
592 }
593}
594
595DEF_TEST(divmod_u8, r) {
596 test_divmod<uint8_t>(r);
597}
598
599DEF_TEST(divmod_u16, r) {
600 test_divmod<uint16_t>(r);
601}
602
603DEF_TEST(divmod_u32, r) {
604 test_divmod<uint32_t>(r);
605}
606
607DEF_TEST(divmod_u64, r) {
608 test_divmod<uint64_t>(r);
609}
610
611DEF_TEST(divmod_s8, r) {
612 test_divmod<int8_t>(r);
613}
614
615DEF_TEST(divmod_s16, r) {
616 test_divmod<int16_t>(r);
617}
618
619DEF_TEST(divmod_s32, r) {
620 test_divmod<int32_t>(r);
621}
622
623DEF_TEST(divmod_s64, r) {
624 test_divmod<int64_t>(r);
625}
Robert Phillips9e380472016-10-28 12:15:03 -0400626
627static void test_nextsizepow2(skiatest::Reporter* r, size_t test, size_t expectedAns) {
628 size_t ans = GrNextSizePow2(test);
629
630 REPORTER_ASSERT(r, ans == expectedAns);
631 //SkDebugf("0x%zx -> 0x%zx (0x%zx)\n", test, ans, expectedAns);
632}
633
634DEF_TEST(GrNextSizePow2, reporter) {
635 constexpr int kNumSizeTBits = 8 * sizeof(size_t);
636
637 size_t test = 0, expectedAns = 1;
638
639 test_nextsizepow2(reporter, test, expectedAns);
640
641 test = 1; expectedAns = 1;
642
643 for (int i = 1; i < kNumSizeTBits; ++i) {
644 test_nextsizepow2(reporter, test, expectedAns);
645
646 test++;
647 expectedAns <<= 1;
648
649 test_nextsizepow2(reporter, test, expectedAns);
650
651 test = expectedAns;
652 }
653
654 // For the remaining three tests there is no higher power (of 2)
655 test = 0x1;
656 test <<= kNumSizeTBits-1;
657 test_nextsizepow2(reporter, test, test);
658
659 test++;
660 test_nextsizepow2(reporter, test, test);
661
662 test_nextsizepow2(reporter, SIZE_MAX, SIZE_MAX);
663}
Mike Reed828f1d52017-08-09 11:06:53 -0400664
Mike Reed3d5a6b52018-01-31 15:55:47 -0500665DEF_TEST(FloatSaturate32, reporter) {
Mike Reed828f1d52017-08-09 11:06:53 -0400666 const struct {
667 float fFloat;
668 int fExpectedInt;
669 } recs[] = {
670 { 0, 0 },
671 { 100.5f, 100 },
672 { (float)SK_MaxS32, SK_MaxS32FitsInFloat },
673 { (float)SK_MinS32, SK_MinS32FitsInFloat },
674 { SK_MaxS32 * 100.0f, SK_MaxS32FitsInFloat },
675 { SK_MinS32 * 100.0f, SK_MinS32FitsInFloat },
676 { SK_ScalarInfinity, SK_MaxS32FitsInFloat },
677 { SK_ScalarNegativeInfinity, SK_MinS32FitsInFloat },
678 { SK_ScalarNaN, SK_MaxS32FitsInFloat },
679 };
680
681 for (auto r : recs) {
682 int i = sk_float_saturate2int(r.fFloat);
683 REPORTER_ASSERT(reporter, r.fExpectedInt == i);
Mike Reedf6188422018-03-05 11:49:51 -0500684
685 // ensure that these bound even non-finite values (including NaN)
686
687 SkScalar mx = SkTMax<SkScalar>(r.fFloat, 50);
688 REPORTER_ASSERT(reporter, mx >= 50);
689
690 SkScalar mn = SkTMin<SkScalar>(r.fFloat, 50);
691 REPORTER_ASSERT(reporter, mn <= 50);
692
693 SkScalar p = SkTPin<SkScalar>(r.fFloat, 0, 100);
694 REPORTER_ASSERT(reporter, p >= 0 && p <= 100);
Mike Reed828f1d52017-08-09 11:06:53 -0400695 }
696}
Mike Reede0762232017-10-10 14:49:35 -0400697
Mike Reed3d5a6b52018-01-31 15:55:47 -0500698DEF_TEST(FloatSaturate64, reporter) {
699 const struct {
700 float fFloat;
701 int64_t fExpected64;
702 } recs[] = {
703 { 0, 0 },
704 { 100.5f, 100 },
705 { (float)SK_MaxS64, SK_MaxS64FitsInFloat },
706 { (float)SK_MinS64, SK_MinS64FitsInFloat },
707 { SK_MaxS64 * 100.0f, SK_MaxS64FitsInFloat },
708 { SK_MinS64 * 100.0f, SK_MinS64FitsInFloat },
709 { SK_ScalarInfinity, SK_MaxS64FitsInFloat },
710 { SK_ScalarNegativeInfinity, SK_MinS64FitsInFloat },
711 { SK_ScalarNaN, SK_MaxS64FitsInFloat },
712 };
713
714 for (auto r : recs) {
715 int64_t i = sk_float_saturate2int64(r.fFloat);
716 REPORTER_ASSERT(reporter, r.fExpected64 == i);
717 }
718}
719
720DEF_TEST(DoubleSaturate32, reporter) {
Mike Reede0762232017-10-10 14:49:35 -0400721 const struct {
722 double fDouble;
723 int fExpectedInt;
724 } recs[] = {
725 { 0, 0 },
726 { 100.5, 100 },
727 { SK_MaxS32, SK_MaxS32 },
728 { SK_MinS32, SK_MinS32 },
729 { SK_MaxS32 - 1, SK_MaxS32 - 1 },
730 { SK_MinS32 + 1, SK_MinS32 + 1 },
731 { SK_MaxS32 * 100.0, SK_MaxS32 },
732 { SK_MinS32 * 100.0, SK_MinS32 },
733 { SK_ScalarInfinity, SK_MaxS32 },
734 { SK_ScalarNegativeInfinity, SK_MinS32 },
735 { SK_ScalarNaN, SK_MaxS32 },
736 };
737
738 for (auto r : recs) {
739 int i = sk_double_saturate2int(r.fDouble);
740 REPORTER_ASSERT(reporter, r.fExpectedInt == i);
741 }
742}
Mike Kleind8853ec2018-03-10 11:34:53 -0500743
744#if defined(__ARM_NEON)
745 #include <arm_neon.h>
746
747 DEF_TEST(NeonU16Div255, r) {
748
749 for (int v = 0; v <= 255*255; v++) {
750 int want = (v + 127)/255;
751
752 uint16x8_t V = vdupq_n_u16(v);
753 int got = vrshrq_n_u16(vrsraq_n_u16(V, V, 8), 8)[0];
754
755 if (got != want) {
756 SkDebugf("%d -> %d, want %d\n", v, got, want);
757 }
758 REPORTER_ASSERT(r, got == want);
759 }
760 }
761
762#endif