blob: 857440c9f93cf133a6fb1690e5831f1de92a5dcb [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@google.com0abb4992011-10-06 20:04:36 +0000201static void test_blend(skiatest::Reporter* reporter) {
202 for (int src = 0; src <= 255; src++) {
203 for (int dst = 0; dst <= 255; dst++) {
204 for (int a = 0; a <= 255; a++) {
205 int r0 = SkAlphaBlend255(src, dst, a);
206 float f1 = float_blend(src, dst, a / 255.f);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000207 int r1 = SkScalarRoundToInt(f1);
reed@google.com772813a2011-03-30 22:34:45 +0000208
reed@google.com0abb4992011-10-06 20:04:36 +0000209 if (r0 != r1) {
210 float diff = sk_float_abs(f1 - r1);
211 diff = sk_float_abs(diff - 0.5f);
212 if (diff > (1 / 255.f)) {
halcanary7d571242016-02-24 17:59:16 -0800213 ERRORF(reporter, "src:%d dst:%d a:%d "
214 "result:%d float:%g\n", src, dst, a, r0, f1);
reed@google.com0abb4992011-10-06 20:04:36 +0000215 }
216 }
reed@google.com772813a2011-03-30 22:34:45 +0000217 }
218 }
219 }
220}
reed@google.com772813a2011-03-30 22:34:45 +0000221
reed@android.comed673312009-02-27 16:24:51 +0000222static void check_length(skiatest::Reporter* reporter,
223 const SkPoint& p, SkScalar targetLen) {
reed@android.comed673312009-02-27 16:24:51 +0000224 float x = SkScalarToFloat(p.fX);
225 float y = SkScalarToFloat(p.fY);
226 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000227
reed@android.comed673312009-02-27 16:24:51 +0000228 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000229
reed@android.comed673312009-02-27 16:24:51 +0000230 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000231}
232
reed@google.com077910e2011-02-08 21:56:39 +0000233static void unittest_isfinite(skiatest::Reporter* reporter) {
epoger@google.combf083a92011-06-08 18:26:08 +0000234 float nan = sk_float_asin(2);
Mike Reed026d20f2018-05-14 16:51:32 -0400235 float inf = SK_ScalarInfinity;
tomhudson@google.com75589252012-04-10 17:42:21 +0000236 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000237
reed@google.com077910e2011-02-08 21:56:39 +0000238 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000239 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
240 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
241 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
reed@android.comd4134452011-02-09 02:24:26 +0000242
243 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000244 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
245 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
246 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000247
reed@google.com077910e2011-02-08 21:56:39 +0000248 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000249 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
250 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
251 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000252}
253
jvanverth93679922014-11-26 13:15:59 -0800254static void unittest_half(skiatest::Reporter* reporter) {
255 static const float gFloats[] = {
256 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
257 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
258 };
259
260 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
261 SkHalf h = SkFloatToHalf(gFloats[i]);
262 float f = SkHalfToFloat(h);
263 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
264 }
265
266 // check some special values
267 union FloatUnion {
268 uint32_t fU;
269 float fF;
270 };
271
272 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
273 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
274 float f = SkHalfToFloat(h);
275 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
276
277 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
278 h = SkFloatToHalf(largestNegativeHalf.fF);
279 f = SkHalfToFloat(h);
280 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
281
282 static const FloatUnion smallestPositiveHalf = { 102 << 23 };
283 h = SkFloatToHalf(smallestPositiveHalf.fF);
284 f = SkHalfToFloat(h);
285 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
286
287 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
288 h = SkFloatToHalf(overflowHalf.fF);
289 f = SkHalfToFloat(h);
290 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
291
292 static const FloatUnion underflowHalf = { 101 << 23 };
293 h = SkFloatToHalf(underflowHalf.fF);
294 f = SkHalfToFloat(h);
295 REPORTER_ASSERT(reporter, f == 0.0f );
296
297 static const FloatUnion inf32 = { 255 << 23 };
298 h = SkFloatToHalf(inf32.fF);
299 f = SkHalfToFloat(h);
300 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
301
302 static const FloatUnion nan32 = { 255 << 23 | 1 };
303 h = SkFloatToHalf(nan32.fF);
304 f = SkHalfToFloat(h);
305 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
306
307}
308
mtkleina766ca82016-01-26 07:40:30 -0800309template <typename RSqrtFn>
310static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
jvanverth29c69792015-07-23 11:14:29 -0700311 const float maxRelativeError = 6.50196699e-4f;
312
313 // test close to 0 up to 1
314 float input = 0.000001f;
315 for (int i = 0; i < 1000; ++i) {
316 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800317 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700318 float relativeError = sk_float_abs(exact - estimate)/exact;
319 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
320 input += 0.001f;
321 }
322
323 // test 1 to ~100
324 input = 1.0f;
325 for (int i = 0; i < 1000; ++i) {
326 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800327 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700328 float relativeError = sk_float_abs(exact - estimate)/exact;
329 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
330 input += 0.01f;
331 }
332
333 // test some big numbers
334 input = 1000000.0f;
335 for (int i = 0; i < 100; ++i) {
336 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800337 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700338 float relativeError = sk_float_abs(exact - estimate)/exact;
339 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
340 input += 754326.f;
341 }
342}
343
reed@android.comed673312009-02-27 16:24:51 +0000344static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000345 for (int a = 0; a <= 255; a++) {
346 for (int b = 0; b <= 255; b++) {
347 int ab = a * b;
348 float s = ab / 255.0f;
349 int round = (int)floorf(s + 0.5f);
350 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000351
reed@android.comed673312009-02-27 16:24:51 +0000352 int iround = SkMulDiv255Round(a, b);
353 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000354
reed@android.comed673312009-02-27 16:24:51 +0000355 REPORTER_ASSERT(reporter, iround == round);
356 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000357
reed@android.comed673312009-02-27 16:24:51 +0000358 REPORTER_ASSERT(reporter, itrunc <= iround);
359 REPORTER_ASSERT(reporter, iround <= a);
360 REPORTER_ASSERT(reporter, iround <= b);
361 }
362 }
reed@android.comed673312009-02-27 16:24:51 +0000363}
364
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000365static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
366 for (int c = 0; c <= 255; c++) {
367 for (int a = 0; a <= 255; a++) {
368 int product = (c * a + 255);
369 int expected_ceiling = (product + (product >> 8)) >> 8;
370 int webkit_ceiling = (c * a + 254) / 255;
371 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
372 int skia_ceiling = SkMulDiv255Ceiling(c, a);
373 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
374 }
375 }
376}
377
reed@android.comf0ad0862010-02-09 19:18:38 +0000378static void test_copysign(skiatest::Reporter* reporter) {
379 static const int32_t gTriples[] = {
380 // x, y, expected result
381 0, 0, 0,
382 0, 1, 0,
383 0, -1, 0,
384 1, 0, 1,
385 1, 1, 1,
386 1, -1, -1,
387 -1, 0, 1,
388 -1, 1, 1,
389 -1, -1, -1,
390 };
391 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
392 REPORTER_ASSERT(reporter,
393 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000394 float x = (float)gTriples[i];
395 float y = (float)gTriples[i+1];
396 float expected = (float)gTriples[i+2];
397 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000398 }
399
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000400 SkRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000401 for (int j = 0; j < 1000; j++) {
402 int ix = rand.nextS();
403 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
404 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
405 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
406 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
407
408 SkScalar sx = rand.nextSScalar1();
409 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
410 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
411 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
412 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
413 }
414}
415
Mike Reed026d20f2018-05-14 16:51:32 -0400416static void huge_vector_normalize(skiatest::Reporter* reporter) {
417 // these values should fail (overflow/underflow) trying to normalize
418 const SkVector fail[] = {
419 { 0, 0 },
420 { SK_ScalarInfinity, 0 }, { 0, SK_ScalarInfinity },
421 { 0, SK_ScalarNaN }, { SK_ScalarNaN, 0 },
422 };
423 for (SkVector v : fail) {
424 SkVector v2 = v;
425 if (v2.setLength(1.0f)) {
426 REPORTER_ASSERT(reporter, !v.setLength(1.0f));
427 }
428 }
429}
430
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000431DEF_TEST(Math, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000432 int i;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000433 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000434
reed@android.comed673312009-02-27 16:24:51 +0000435 // these should assert
436#if 0
437 SkToS8(128);
438 SkToS8(-129);
439 SkToU8(256);
440 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000441
reed@android.comed673312009-02-27 16:24:51 +0000442 SkToS16(32768);
443 SkToS16(-32769);
444 SkToU16(65536);
445 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000446
reed@android.comed673312009-02-27 16:24:51 +0000447 if (sizeof(size_t) > 4) {
448 SkToS32(4*1024*1024);
449 SkToS32(-4*1024*1024);
450 SkToU32(5*1024*1024);
451 SkToU32(-5);
452 }
453#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000454
reed@android.comed673312009-02-27 16:24:51 +0000455 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000456 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000457 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000458
reed@android.comed673312009-02-27 16:24:51 +0000459 {
460 SkScalar x = SK_ScalarNaN;
461 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
462 }
reed@android.com80e39a72009-04-02 16:59:40 +0000463
reed@android.comed673312009-02-27 16:24:51 +0000464 for (i = 0; i < 1000; i++) {
465 int value = rand.nextS16();
466 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000467
reed@android.comed673312009-02-27 16:24:51 +0000468 int clamp = SkClampMax(value, max);
469 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
470 REPORTER_ASSERT(reporter, clamp == clamp2);
471 }
reed@android.com80e39a72009-04-02 16:59:40 +0000472
reed@android.come72fee52009-11-16 14:52:01 +0000473 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000474 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000475
tomhudson@google.com75589252012-04-10 17:42:21 +0000476 // These random values are being treated as 32-bit-patterns, not as
477 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000478 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000479 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000480 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000481 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000482 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000483 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000484 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000485 check_length(reporter, p, SK_Scalar1);
486 }
reed@android.com80e39a72009-04-02 16:59:40 +0000487
reed@android.comed673312009-02-27 16:24:51 +0000488 {
489 SkFixed result = SkFixedDiv(100, 100);
490 REPORTER_ASSERT(reporter, result == SK_Fixed1);
491 result = SkFixedDiv(1, SK_Fixed1);
492 REPORTER_ASSERT(reporter, result == 1);
liyuqian0d2c2342016-07-13 13:34:46 -0700493 result = SkFixedDiv(10 - 1, SK_Fixed1 * 3);
494 REPORTER_ASSERT(reporter, result == 3);
reed@android.comed673312009-02-27 16:24:51 +0000495 }
reed@android.com80e39a72009-04-02 16:59:40 +0000496
liyuqian3f490cc2016-10-20 11:23:09 -0700497 {
498 REPORTER_ASSERT(reporter, (SkFixedRoundToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
499 REPORTER_ASSERT(reporter, (SkFixedFloorToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
500 REPORTER_ASSERT(reporter, (SkFixedCeilToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
501 }
502
Mike Reed026d20f2018-05-14 16:51:32 -0400503 huge_vector_normalize(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000504 unittest_isfinite(reporter);
jvanverth93679922014-11-26 13:15:59 -0800505 unittest_half(reporter);
mtkleina766ca82016-01-26 07:40:30 -0800506 test_rsqrt(reporter, sk_float_rsqrt);
507 test_rsqrt(reporter, sk_float_rsqrt_portable);
reed@android.com80e39a72009-04-02 16:59:40 +0000508
reed@android.come72fee52009-11-16 14:52:01 +0000509 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000510 SkFixed numer = rand.nextS();
511 SkFixed denom = rand.nextS();
512 SkFixed result = SkFixedDiv(numer, denom);
caryclark3127c992015-12-09 12:02:30 -0800513 int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000514
reed@android.comed673312009-02-27 16:24:51 +0000515 (void)SkCLZ(numer);
516 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000517
reed@android.comed673312009-02-27 16:24:51 +0000518 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
519 if (check > SK_MaxS32) {
520 check = SK_MaxS32;
521 } else if (check < -SK_MaxS32) {
522 check = SK_MinS32;
523 }
mtklein97ca98d2015-03-18 11:32:21 -0700524 if (result != (int32_t)check) {
525 ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
526 }
reed@android.comed673312009-02-27 16:24:51 +0000527 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.comed673312009-02-27 16:24:51 +0000528 }
reed@android.com80e39a72009-04-02 16:59:40 +0000529
reed@google.com0abb4992011-10-06 20:04:36 +0000530 test_blend(reporter);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000531
humper@google.com05af1af2013-01-07 16:47:43 +0000532 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000533
reed@google.com8d7e39c2011-11-09 17:12:08 +0000534 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000535 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000536
537 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000538 test_clz(reporter);
Yuqian Lice1d2932016-11-18 10:18:15 -0500539 test_quick_div(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000540}
541
reed@google.comc9f81662013-05-03 18:06:31 +0000542template <typename T> struct PairRec {
543 T fYin;
544 T fYang;
545};
546
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000547DEF_TEST(TestEndian, reporter) {
reed@google.comc9f81662013-05-03 18:06:31 +0000548 static const PairRec<uint16_t> g16[] = {
549 { 0x0, 0x0 },
550 { 0xFFFF, 0xFFFF },
551 { 0x1122, 0x2211 },
552 };
553 static const PairRec<uint32_t> g32[] = {
554 { 0x0, 0x0 },
555 { 0xFFFFFFFF, 0xFFFFFFFF },
556 { 0x11223344, 0x44332211 },
557 };
558 static const PairRec<uint64_t> g64[] = {
559 { 0x0, 0x0 },
560 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
561 { 0x1122334455667788ULL, 0x8877665544332211ULL },
562 };
563
564 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
565 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
566 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
567
568 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
569 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
570 }
571 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
572 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
573 }
574 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
575 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
576 }
577}
578
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000579template <typename T>
580static void test_divmod(skiatest::Reporter* r) {
581 const struct {
582 T numer;
583 T denom;
584 } kEdgeCases[] = {
585 {(T)17, (T)17},
586 {(T)17, (T)4},
587 {(T)0, (T)17},
588 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
589 {(T)-17, (T)-17},
590 {(T)-17, (T)4},
591 {(T)17, (T)-4},
592 {(T)-17, (T)-4},
593 };
594
595 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
596 const T numer = kEdgeCases[i].numer;
597 const T denom = kEdgeCases[i].denom;
598 T div, mod;
599 SkTDivMod(numer, denom, &div, &mod);
600 REPORTER_ASSERT(r, numer/denom == div);
601 REPORTER_ASSERT(r, numer%denom == mod);
602 }
603
604 SkRandom rand;
605 for (size_t i = 0; i < 10000; i++) {
606 const T numer = (T)rand.nextS();
607 T denom = 0;
608 while (0 == denom) {
609 denom = (T)rand.nextS();
610 }
611 T div, mod;
612 SkTDivMod(numer, denom, &div, &mod);
613 REPORTER_ASSERT(r, numer/denom == div);
614 REPORTER_ASSERT(r, numer%denom == mod);
615 }
616}
617
618DEF_TEST(divmod_u8, r) {
619 test_divmod<uint8_t>(r);
620}
621
622DEF_TEST(divmod_u16, r) {
623 test_divmod<uint16_t>(r);
624}
625
626DEF_TEST(divmod_u32, r) {
627 test_divmod<uint32_t>(r);
628}
629
630DEF_TEST(divmod_u64, r) {
631 test_divmod<uint64_t>(r);
632}
633
634DEF_TEST(divmod_s8, r) {
635 test_divmod<int8_t>(r);
636}
637
638DEF_TEST(divmod_s16, r) {
639 test_divmod<int16_t>(r);
640}
641
642DEF_TEST(divmod_s32, r) {
643 test_divmod<int32_t>(r);
644}
645
646DEF_TEST(divmod_s64, r) {
647 test_divmod<int64_t>(r);
648}
Robert Phillips9e380472016-10-28 12:15:03 -0400649
650static void test_nextsizepow2(skiatest::Reporter* r, size_t test, size_t expectedAns) {
651 size_t ans = GrNextSizePow2(test);
652
653 REPORTER_ASSERT(r, ans == expectedAns);
654 //SkDebugf("0x%zx -> 0x%zx (0x%zx)\n", test, ans, expectedAns);
655}
656
657DEF_TEST(GrNextSizePow2, reporter) {
658 constexpr int kNumSizeTBits = 8 * sizeof(size_t);
659
660 size_t test = 0, expectedAns = 1;
661
662 test_nextsizepow2(reporter, test, expectedAns);
663
664 test = 1; expectedAns = 1;
665
666 for (int i = 1; i < kNumSizeTBits; ++i) {
667 test_nextsizepow2(reporter, test, expectedAns);
668
669 test++;
670 expectedAns <<= 1;
671
672 test_nextsizepow2(reporter, test, expectedAns);
673
674 test = expectedAns;
675 }
676
677 // For the remaining three tests there is no higher power (of 2)
678 test = 0x1;
679 test <<= kNumSizeTBits-1;
680 test_nextsizepow2(reporter, test, test);
681
682 test++;
683 test_nextsizepow2(reporter, test, test);
684
685 test_nextsizepow2(reporter, SIZE_MAX, SIZE_MAX);
686}
Mike Reed828f1d52017-08-09 11:06:53 -0400687
Mike Reed3d5a6b52018-01-31 15:55:47 -0500688DEF_TEST(FloatSaturate32, reporter) {
Mike Reed828f1d52017-08-09 11:06:53 -0400689 const struct {
690 float fFloat;
691 int fExpectedInt;
692 } recs[] = {
693 { 0, 0 },
694 { 100.5f, 100 },
695 { (float)SK_MaxS32, SK_MaxS32FitsInFloat },
696 { (float)SK_MinS32, SK_MinS32FitsInFloat },
697 { SK_MaxS32 * 100.0f, SK_MaxS32FitsInFloat },
698 { SK_MinS32 * 100.0f, SK_MinS32FitsInFloat },
699 { SK_ScalarInfinity, SK_MaxS32FitsInFloat },
700 { SK_ScalarNegativeInfinity, SK_MinS32FitsInFloat },
701 { SK_ScalarNaN, SK_MaxS32FitsInFloat },
702 };
703
704 for (auto r : recs) {
705 int i = sk_float_saturate2int(r.fFloat);
706 REPORTER_ASSERT(reporter, r.fExpectedInt == i);
Mike Reedf6188422018-03-05 11:49:51 -0500707
708 // ensure that these bound even non-finite values (including NaN)
709
710 SkScalar mx = SkTMax<SkScalar>(r.fFloat, 50);
711 REPORTER_ASSERT(reporter, mx >= 50);
712
713 SkScalar mn = SkTMin<SkScalar>(r.fFloat, 50);
714 REPORTER_ASSERT(reporter, mn <= 50);
715
716 SkScalar p = SkTPin<SkScalar>(r.fFloat, 0, 100);
717 REPORTER_ASSERT(reporter, p >= 0 && p <= 100);
Mike Reed828f1d52017-08-09 11:06:53 -0400718 }
719}
Mike Reede0762232017-10-10 14:49:35 -0400720
Mike Reed3d5a6b52018-01-31 15:55:47 -0500721DEF_TEST(FloatSaturate64, reporter) {
722 const struct {
723 float fFloat;
724 int64_t fExpected64;
725 } recs[] = {
726 { 0, 0 },
727 { 100.5f, 100 },
728 { (float)SK_MaxS64, SK_MaxS64FitsInFloat },
729 { (float)SK_MinS64, SK_MinS64FitsInFloat },
730 { SK_MaxS64 * 100.0f, SK_MaxS64FitsInFloat },
731 { SK_MinS64 * 100.0f, SK_MinS64FitsInFloat },
732 { SK_ScalarInfinity, SK_MaxS64FitsInFloat },
733 { SK_ScalarNegativeInfinity, SK_MinS64FitsInFloat },
734 { SK_ScalarNaN, SK_MaxS64FitsInFloat },
735 };
736
737 for (auto r : recs) {
738 int64_t i = sk_float_saturate2int64(r.fFloat);
739 REPORTER_ASSERT(reporter, r.fExpected64 == i);
740 }
741}
742
743DEF_TEST(DoubleSaturate32, reporter) {
Mike Reede0762232017-10-10 14:49:35 -0400744 const struct {
745 double fDouble;
746 int fExpectedInt;
747 } recs[] = {
748 { 0, 0 },
749 { 100.5, 100 },
750 { SK_MaxS32, SK_MaxS32 },
751 { SK_MinS32, SK_MinS32 },
752 { SK_MaxS32 - 1, SK_MaxS32 - 1 },
753 { SK_MinS32 + 1, SK_MinS32 + 1 },
754 { SK_MaxS32 * 100.0, SK_MaxS32 },
755 { SK_MinS32 * 100.0, SK_MinS32 },
756 { SK_ScalarInfinity, SK_MaxS32 },
757 { SK_ScalarNegativeInfinity, SK_MinS32 },
758 { SK_ScalarNaN, SK_MaxS32 },
759 };
760
761 for (auto r : recs) {
762 int i = sk_double_saturate2int(r.fDouble);
763 REPORTER_ASSERT(reporter, r.fExpectedInt == i);
764 }
765}
Mike Kleind8853ec2018-03-10 11:34:53 -0500766
767#if defined(__ARM_NEON)
768 #include <arm_neon.h>
769
770 DEF_TEST(NeonU16Div255, r) {
771
772 for (int v = 0; v <= 255*255; v++) {
773 int want = (v + 127)/255;
774
775 uint16x8_t V = vdupq_n_u16(v);
776 int got = vrshrq_n_u16(vrsraq_n_u16(V, V, 8), 8)[0];
777
778 if (got != want) {
779 SkDebugf("%d -> %d, want %d\n", v, got, want);
780 }
781 REPORTER_ASSERT(r, got == want);
782 }
783 }
784
785#endif