blob: b5d933d987ab1bca3862eb15746b55903416d056 [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"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000016#include "Test.h"
reed@android.comed673312009-02-27 16:24:51 +000017
reed@google.comc21f86f2013-04-29 14:18:23 +000018static void test_clz(skiatest::Reporter* reporter) {
19 REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
20 REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
21 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
reed@google.com7729534d2013-04-29 14:43:50 +000022 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
skia.committer@gmail.com81521132013-04-30 07:01:03 +000023
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000024 SkRandom rand;
reed@google.comc21f86f2013-04-29 14:18:23 +000025 for (int i = 0; i < 1000; ++i) {
26 uint32_t mask = rand.nextU();
reed@google.combc57a292013-04-29 15:27:42 +000027 // need to get some zeros for testing, but in some obscure way so the
28 // compiler won't "see" that, and work-around calling the functions.
29 mask >>= (mask & 31);
reed@google.comc21f86f2013-04-29 14:18:23 +000030 int intri = SkCLZ(mask);
31 int porta = SkCLZ_portable(mask);
32 REPORTER_ASSERT(reporter, intri == porta);
33 }
34}
35
Yuqian Lice1d2932016-11-18 10:18:15 -050036static void test_quick_div(skiatest::Reporter* reporter) {
37 /*
38 The inverse table is generated by turning on SkDebugf in the following test code
39 */
40 SkFixed storage[kInverseTableSize * 2];
41 SkFixed* table = storage + kInverseTableSize;
42
43 // SkDebugf("static const int gFDot6INVERSE[] = {");
44 for (SkFDot6 i=-kInverseTableSize; i<kInverseTableSize; i++) {
45 if (i != 0) {
46 table[i] = SkFDot6Div(SK_FDot6One, i);
47 REPORTER_ASSERT(reporter, table[i] == gFDot6INVERSE[i + kInverseTableSize]);
48 }
49 // SkDebugf("%d, ", table[i]);
50 }
51 // SkDebugf("}\n");
52
53
54 for (SkFDot6 a = -1024; a <= 1024; a++) {
55 for (SkFDot6 b = -1024; b <= 1024; b++) {
56 if (b != 0) {
57 SkFixed ourAnswer = QuickSkFDot6Div(a, b);
58 SkFixed directAnswer = SkFDot6Div(a, b);
59 REPORTER_ASSERT(reporter,
60 (directAnswer == 0 && ourAnswer == 0) ||
61 SkFixedDiv(SkAbs32(directAnswer - ourAnswer), SkAbs32(directAnswer)) <= 1 << 10
62 );
63 }
64 }
65 }
66}
67
reed@google.comc21f86f2013-04-29 14:18:23 +000068///////////////////////////////////////////////////////////////////////////////
69
reed@google.coma7d74612012-05-30 12:30:09 +000070static float sk_fsel(float pred, float result_ge, float result_lt) {
71 return pred >= 0 ? result_ge : result_lt;
72}
73
74static float fast_floor(float x) {
reed@google.comc20bc252012-05-30 13:48:14 +000075// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
76 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
robertphillips@google.com00bf06a2012-05-30 15:19:17 +000077 return (float)(x + big) - big;
reed@google.coma7d74612012-05-30 12:30:09 +000078}
79
80static float std_floor(float x) {
81 return sk_float_floor(x);
82}
83
84static void test_floor_value(skiatest::Reporter* reporter, float value) {
85 float fast = fast_floor(value);
86 float std = std_floor(value);
halcanary7d571242016-02-24 17:59:16 -080087 if (std != fast) {
88 ERRORF(reporter, "fast_floor(%.9g) == %.9g != %.9g == std_floor(%.9g)",
89 value, fast, std, value);
90 }
reed@google.coma7d74612012-05-30 12:30:09 +000091}
92
93static void test_floor(skiatest::Reporter* reporter) {
94 static const float gVals[] = {
95 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
96 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000097
reed@google.coma7d74612012-05-30 12:30:09 +000098 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
99 test_floor_value(reporter, gVals[i]);
100// test_floor_value(reporter, -gVals[i]);
101 }
102}
103
104///////////////////////////////////////////////////////////////////////////////
105
reed@google.comea774d22013-04-22 20:21:56 +0000106// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
107static void test_muldivround(skiatest::Reporter* reporter) {
108#if 0
109 // this "complete" test is too slow, so we test a random sampling of it
110
111 for (int a = 0; a <= 32767; ++a) {
112 for (int b = 0; b <= 32767; ++b) {
113 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
114 unsigned prod1 = SkMulDiv255Round(a, b);
115 SkASSERT(prod0 == prod1);
116 }
117 }
118#endif
119
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000120 SkRandom rand;
reed@google.comea774d22013-04-22 20:21:56 +0000121 for (int i = 0; i < 10000; ++i) {
122 unsigned a = rand.nextU() & 0x7FFF;
123 unsigned b = rand.nextU() & 0x7FFF;
124
125 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
126 unsigned prod1 = SkMulDiv255Round(a, b);
127
128 REPORTER_ASSERT(reporter, prod0 == prod1);
129 }
130}
131
reed@google.com0abb4992011-10-06 20:04:36 +0000132static float float_blend(int src, int dst, float unit) {
133 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +0000134}
135
reed@google.com8d7e39c2011-11-09 17:12:08 +0000136static int blend31(int src, int dst, int a31) {
137 return dst + ((src - dst) * a31 * 2114 >> 16);
138 // return dst + ((src - dst) * a31 * 33 >> 10);
139}
140
141static int blend31_slow(int src, int dst, int a31) {
142 int prod = src * a31 + (31 - a31) * dst + 16;
143 prod = (prod + (prod >> 5)) >> 5;
144 return prod;
145}
146
147static int blend31_round(int src, int dst, int a31) {
148 int prod = (src - dst) * a31 + 16;
149 prod = (prod + (prod >> 5)) >> 5;
150 return dst + prod;
151}
152
153static int blend31_old(int src, int dst, int a31) {
154 a31 += a31 >> 4;
155 return dst + ((src - dst) * a31 >> 5);
156}
157
caryclark@google.com42639cd2012-06-06 12:03:39 +0000158// suppress unused code warning
159static int (*blend_functions[])(int, int, int) = {
160 blend31,
161 blend31_slow,
162 blend31_round,
163 blend31_old
164};
165
reed@google.com8d7e39c2011-11-09 17:12:08 +0000166static void test_blend31() {
167 int failed = 0;
168 int death = 0;
caryclark@google.com42639cd2012-06-06 12:03:39 +0000169 if (false) { // avoid bit rot, suppress warning
170 failed = (*blend_functions[0])(0,0,0);
171 }
reed@google.com8d7e39c2011-11-09 17:12:08 +0000172 for (int src = 0; src <= 255; src++) {
173 for (int dst = 0; dst <= 255; dst++) {
174 for (int a = 0; a <= 31; a++) {
175// int r0 = blend31(src, dst, a);
176// int r0 = blend31_round(src, dst, a);
177// int r0 = blend31_old(src, dst, a);
178 int r0 = blend31_slow(src, dst, a);
179
180 float f = float_blend(src, dst, a / 31.f);
181 int r1 = (int)f;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000182 int r2 = SkScalarRoundToInt(f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000183
184 if (r0 != r1 && r0 != r2) {
bungeman@google.comfab44db2013-10-11 18:50:45 +0000185 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
halcanary7d571242016-02-24 17:59:16 -0800186 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000187 failed += 1;
188 }
189 if (r0 > 255) {
190 death += 1;
bungeman@google.comfab44db2013-10-11 18:50:45 +0000191 SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
192 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000193 }
194 }
195 }
196 }
197 SkDebugf("---- failed %d death %d\n", failed, death);
198}
199
reed@google.com0abb4992011-10-06 20:04:36 +0000200static void test_blend(skiatest::Reporter* reporter) {
201 for (int src = 0; src <= 255; src++) {
202 for (int dst = 0; dst <= 255; dst++) {
203 for (int a = 0; a <= 255; a++) {
204 int r0 = SkAlphaBlend255(src, dst, a);
205 float f1 = float_blend(src, dst, a / 255.f);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000206 int r1 = SkScalarRoundToInt(f1);
reed@google.com772813a2011-03-30 22:34:45 +0000207
reed@google.com0abb4992011-10-06 20:04:36 +0000208 if (r0 != r1) {
209 float diff = sk_float_abs(f1 - r1);
210 diff = sk_float_abs(diff - 0.5f);
211 if (diff > (1 / 255.f)) {
halcanary7d571242016-02-24 17:59:16 -0800212 ERRORF(reporter, "src:%d dst:%d a:%d "
213 "result:%d float:%g\n", src, dst, a, r0, f1);
reed@google.com0abb4992011-10-06 20:04:36 +0000214 }
215 }
reed@google.com772813a2011-03-30 22:34:45 +0000216 }
217 }
218 }
219}
reed@google.com772813a2011-03-30 22:34:45 +0000220
reed@android.comed673312009-02-27 16:24:51 +0000221static void check_length(skiatest::Reporter* reporter,
222 const SkPoint& p, SkScalar targetLen) {
reed@android.comed673312009-02-27 16:24:51 +0000223 float x = SkScalarToFloat(p.fX);
224 float y = SkScalarToFloat(p.fY);
225 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000226
reed@android.comed673312009-02-27 16:24:51 +0000227 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000228
reed@android.comed673312009-02-27 16:24:51 +0000229 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000230}
231
reed@google.com077910e2011-02-08 21:56:39 +0000232static void unittest_isfinite(skiatest::Reporter* reporter) {
epoger@google.combf083a92011-06-08 18:26:08 +0000233 float nan = sk_float_asin(2);
Mike Reed026d20f2018-05-14 16:51:32 -0400234 float inf = SK_ScalarInfinity;
tomhudson@google.com75589252012-04-10 17:42:21 +0000235 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000236
reed@google.com077910e2011-02-08 21:56:39 +0000237 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000238 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
239 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
240 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
reed@android.comd4134452011-02-09 02:24:26 +0000241
242 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000243 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
244 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
245 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000246
reed@google.com077910e2011-02-08 21:56:39 +0000247 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000248 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
249 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
250 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000251}
252
jvanverth93679922014-11-26 13:15:59 -0800253static void unittest_half(skiatest::Reporter* reporter) {
254 static const float gFloats[] = {
255 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
256 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
257 };
258
259 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
260 SkHalf h = SkFloatToHalf(gFloats[i]);
261 float f = SkHalfToFloat(h);
262 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
263 }
264
265 // check some special values
266 union FloatUnion {
267 uint32_t fU;
268 float fF;
269 };
270
271 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
272 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
273 float f = SkHalfToFloat(h);
274 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
275
276 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
277 h = SkFloatToHalf(largestNegativeHalf.fF);
278 f = SkHalfToFloat(h);
279 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
280
281 static const FloatUnion smallestPositiveHalf = { 102 << 23 };
282 h = SkFloatToHalf(smallestPositiveHalf.fF);
283 f = SkHalfToFloat(h);
284 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
285
286 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
287 h = SkFloatToHalf(overflowHalf.fF);
288 f = SkHalfToFloat(h);
289 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
290
291 static const FloatUnion underflowHalf = { 101 << 23 };
292 h = SkFloatToHalf(underflowHalf.fF);
293 f = SkHalfToFloat(h);
294 REPORTER_ASSERT(reporter, f == 0.0f );
295
296 static const FloatUnion inf32 = { 255 << 23 };
297 h = SkFloatToHalf(inf32.fF);
298 f = SkHalfToFloat(h);
299 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
300
301 static const FloatUnion nan32 = { 255 << 23 | 1 };
302 h = SkFloatToHalf(nan32.fF);
303 f = SkHalfToFloat(h);
304 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
305
306}
307
mtkleina766ca82016-01-26 07:40:30 -0800308template <typename RSqrtFn>
309static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
jvanverth29c69792015-07-23 11:14:29 -0700310 const float maxRelativeError = 6.50196699e-4f;
311
312 // test close to 0 up to 1
313 float input = 0.000001f;
314 for (int i = 0; i < 1000; ++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 += 0.001f;
320 }
321
322 // test 1 to ~100
323 input = 1.0f;
324 for (int i = 0; i < 1000; ++i) {
325 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800326 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700327 float relativeError = sk_float_abs(exact - estimate)/exact;
328 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
329 input += 0.01f;
330 }
331
332 // test some big numbers
333 input = 1000000.0f;
334 for (int i = 0; i < 100; ++i) {
335 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800336 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700337 float relativeError = sk_float_abs(exact - estimate)/exact;
338 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
339 input += 754326.f;
340 }
341}
342
reed@android.comed673312009-02-27 16:24:51 +0000343static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000344 for (int a = 0; a <= 255; a++) {
345 for (int b = 0; b <= 255; b++) {
346 int ab = a * b;
347 float s = ab / 255.0f;
348 int round = (int)floorf(s + 0.5f);
349 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000350
reed@android.comed673312009-02-27 16:24:51 +0000351 int iround = SkMulDiv255Round(a, b);
352 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000353
reed@android.comed673312009-02-27 16:24:51 +0000354 REPORTER_ASSERT(reporter, iround == round);
355 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000356
reed@android.comed673312009-02-27 16:24:51 +0000357 REPORTER_ASSERT(reporter, itrunc <= iround);
358 REPORTER_ASSERT(reporter, iround <= a);
359 REPORTER_ASSERT(reporter, iround <= b);
360 }
361 }
reed@android.comed673312009-02-27 16:24:51 +0000362}
363
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000364static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
365 for (int c = 0; c <= 255; c++) {
366 for (int a = 0; a <= 255; a++) {
367 int product = (c * a + 255);
368 int expected_ceiling = (product + (product >> 8)) >> 8;
369 int webkit_ceiling = (c * a + 254) / 255;
370 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
371 int skia_ceiling = SkMulDiv255Ceiling(c, a);
372 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
373 }
374 }
375}
376
reed@android.comf0ad0862010-02-09 19:18:38 +0000377static void test_copysign(skiatest::Reporter* reporter) {
378 static const int32_t gTriples[] = {
379 // x, y, expected result
380 0, 0, 0,
381 0, 1, 0,
382 0, -1, 0,
383 1, 0, 1,
384 1, 1, 1,
385 1, -1, -1,
386 -1, 0, 1,
387 -1, 1, 1,
388 -1, -1, -1,
389 };
390 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
391 REPORTER_ASSERT(reporter,
392 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000393 float x = (float)gTriples[i];
394 float y = (float)gTriples[i+1];
395 float expected = (float)gTriples[i+2];
396 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000397 }
398
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000399 SkRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000400 for (int j = 0; j < 1000; j++) {
401 int ix = rand.nextS();
402 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
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
407 SkScalar sx = rand.nextSScalar1();
408 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
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 }
413}
414
Mike Reed026d20f2018-05-14 16:51:32 -0400415static void huge_vector_normalize(skiatest::Reporter* reporter) {
416 // these values should fail (overflow/underflow) trying to normalize
417 const SkVector fail[] = {
418 { 0, 0 },
419 { SK_ScalarInfinity, 0 }, { 0, SK_ScalarInfinity },
420 { 0, SK_ScalarNaN }, { SK_ScalarNaN, 0 },
421 };
422 for (SkVector v : fail) {
423 SkVector v2 = v;
424 if (v2.setLength(1.0f)) {
425 REPORTER_ASSERT(reporter, !v.setLength(1.0f));
426 }
427 }
428}
429
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000430DEF_TEST(Math, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000431 int i;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000432 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000433
reed@android.comed673312009-02-27 16:24:51 +0000434 // these should assert
435#if 0
436 SkToS8(128);
437 SkToS8(-129);
438 SkToU8(256);
439 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000440
reed@android.comed673312009-02-27 16:24:51 +0000441 SkToS16(32768);
442 SkToS16(-32769);
443 SkToU16(65536);
444 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000445
reed@android.comed673312009-02-27 16:24:51 +0000446 if (sizeof(size_t) > 4) {
447 SkToS32(4*1024*1024);
448 SkToS32(-4*1024*1024);
449 SkToU32(5*1024*1024);
450 SkToU32(-5);
451 }
452#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000453
reed@android.comed673312009-02-27 16:24:51 +0000454 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000455 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000456 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000457
reed@android.comed673312009-02-27 16:24:51 +0000458 {
459 SkScalar x = SK_ScalarNaN;
460 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
461 }
reed@android.com80e39a72009-04-02 16:59:40 +0000462
reed@android.comed673312009-02-27 16:24:51 +0000463 for (i = 0; i < 1000; i++) {
464 int value = rand.nextS16();
465 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000466
reed@android.comed673312009-02-27 16:24:51 +0000467 int clamp = SkClampMax(value, max);
468 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
469 REPORTER_ASSERT(reporter, clamp == clamp2);
470 }
reed@android.com80e39a72009-04-02 16:59:40 +0000471
reed@android.come72fee52009-11-16 14:52:01 +0000472 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000473 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000474
tomhudson@google.com75589252012-04-10 17:42:21 +0000475 // These random values are being treated as 32-bit-patterns, not as
476 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000477 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000478 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000479 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000480 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000481 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000482 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000483 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000484 check_length(reporter, p, SK_Scalar1);
485 }
reed@android.com80e39a72009-04-02 16:59:40 +0000486
reed@android.comed673312009-02-27 16:24:51 +0000487 {
488 SkFixed result = SkFixedDiv(100, 100);
489 REPORTER_ASSERT(reporter, result == SK_Fixed1);
490 result = SkFixedDiv(1, SK_Fixed1);
491 REPORTER_ASSERT(reporter, result == 1);
liyuqian0d2c2342016-07-13 13:34:46 -0700492 result = SkFixedDiv(10 - 1, SK_Fixed1 * 3);
493 REPORTER_ASSERT(reporter, result == 3);
reed@android.comed673312009-02-27 16:24:51 +0000494 }
reed@android.com80e39a72009-04-02 16:59:40 +0000495
liyuqian3f490cc2016-10-20 11:23:09 -0700496 {
497 REPORTER_ASSERT(reporter, (SkFixedRoundToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
498 REPORTER_ASSERT(reporter, (SkFixedFloorToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
499 REPORTER_ASSERT(reporter, (SkFixedCeilToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
500 }
501
Mike Reed026d20f2018-05-14 16:51:32 -0400502 huge_vector_normalize(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000503 unittest_isfinite(reporter);
jvanverth93679922014-11-26 13:15:59 -0800504 unittest_half(reporter);
mtkleina766ca82016-01-26 07:40:30 -0800505 test_rsqrt(reporter, sk_float_rsqrt);
506 test_rsqrt(reporter, sk_float_rsqrt_portable);
reed@android.com80e39a72009-04-02 16:59:40 +0000507
reed@android.come72fee52009-11-16 14:52:01 +0000508 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000509 SkFixed numer = rand.nextS();
510 SkFixed denom = rand.nextS();
511 SkFixed result = SkFixedDiv(numer, denom);
caryclark3127c992015-12-09 12:02:30 -0800512 int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000513
reed@android.comed673312009-02-27 16:24:51 +0000514 (void)SkCLZ(numer);
515 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000516
reed@android.comed673312009-02-27 16:24:51 +0000517 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
518 if (check > SK_MaxS32) {
519 check = SK_MaxS32;
520 } else if (check < -SK_MaxS32) {
521 check = SK_MinS32;
522 }
mtklein97ca98d2015-03-18 11:32:21 -0700523 if (result != (int32_t)check) {
524 ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
525 }
reed@android.comed673312009-02-27 16:24:51 +0000526 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.comed673312009-02-27 16:24:51 +0000527 }
reed@android.com80e39a72009-04-02 16:59:40 +0000528
reed@google.com0abb4992011-10-06 20:04:36 +0000529 test_blend(reporter);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000530
humper@google.com05af1af2013-01-07 16:47:43 +0000531 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000532
reed@google.com8d7e39c2011-11-09 17:12:08 +0000533 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000534 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000535
536 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000537 test_clz(reporter);
Yuqian Lice1d2932016-11-18 10:18:15 -0500538 test_quick_div(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000539}
540
reed@google.comc9f81662013-05-03 18:06:31 +0000541template <typename T> struct PairRec {
542 T fYin;
543 T fYang;
544};
545
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000546DEF_TEST(TestEndian, reporter) {
reed@google.comc9f81662013-05-03 18:06:31 +0000547 static const PairRec<uint16_t> g16[] = {
548 { 0x0, 0x0 },
549 { 0xFFFF, 0xFFFF },
550 { 0x1122, 0x2211 },
551 };
552 static const PairRec<uint32_t> g32[] = {
553 { 0x0, 0x0 },
554 { 0xFFFFFFFF, 0xFFFFFFFF },
555 { 0x11223344, 0x44332211 },
556 };
557 static const PairRec<uint64_t> g64[] = {
558 { 0x0, 0x0 },
559 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
560 { 0x1122334455667788ULL, 0x8877665544332211ULL },
561 };
562
563 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
564 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
565 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
566
567 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
568 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
569 }
570 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
571 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
572 }
573 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
574 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
575 }
576}
577
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000578template <typename T>
579static void test_divmod(skiatest::Reporter* r) {
580 const struct {
581 T numer;
582 T denom;
583 } kEdgeCases[] = {
584 {(T)17, (T)17},
585 {(T)17, (T)4},
586 {(T)0, (T)17},
587 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
588 {(T)-17, (T)-17},
589 {(T)-17, (T)4},
590 {(T)17, (T)-4},
591 {(T)-17, (T)-4},
592 };
593
594 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
595 const T numer = kEdgeCases[i].numer;
596 const T denom = kEdgeCases[i].denom;
597 T div, mod;
598 SkTDivMod(numer, denom, &div, &mod);
599 REPORTER_ASSERT(r, numer/denom == div);
600 REPORTER_ASSERT(r, numer%denom == mod);
601 }
602
603 SkRandom rand;
604 for (size_t i = 0; i < 10000; i++) {
605 const T numer = (T)rand.nextS();
606 T denom = 0;
607 while (0 == denom) {
608 denom = (T)rand.nextS();
609 }
610 T div, mod;
611 SkTDivMod(numer, denom, &div, &mod);
612 REPORTER_ASSERT(r, numer/denom == div);
613 REPORTER_ASSERT(r, numer%denom == mod);
614 }
615}
616
617DEF_TEST(divmod_u8, r) {
618 test_divmod<uint8_t>(r);
619}
620
621DEF_TEST(divmod_u16, r) {
622 test_divmod<uint16_t>(r);
623}
624
625DEF_TEST(divmod_u32, r) {
626 test_divmod<uint32_t>(r);
627}
628
629DEF_TEST(divmod_u64, r) {
630 test_divmod<uint64_t>(r);
631}
632
633DEF_TEST(divmod_s8, r) {
634 test_divmod<int8_t>(r);
635}
636
637DEF_TEST(divmod_s16, r) {
638 test_divmod<int16_t>(r);
639}
640
641DEF_TEST(divmod_s32, r) {
642 test_divmod<int32_t>(r);
643}
644
645DEF_TEST(divmod_s64, r) {
646 test_divmod<int64_t>(r);
647}
Robert Phillips9e380472016-10-28 12:15:03 -0400648
649static void test_nextsizepow2(skiatest::Reporter* r, size_t test, size_t expectedAns) {
650 size_t ans = GrNextSizePow2(test);
651
652 REPORTER_ASSERT(r, ans == expectedAns);
653 //SkDebugf("0x%zx -> 0x%zx (0x%zx)\n", test, ans, expectedAns);
654}
655
656DEF_TEST(GrNextSizePow2, reporter) {
657 constexpr int kNumSizeTBits = 8 * sizeof(size_t);
658
659 size_t test = 0, expectedAns = 1;
660
661 test_nextsizepow2(reporter, test, expectedAns);
662
663 test = 1; expectedAns = 1;
664
665 for (int i = 1; i < kNumSizeTBits; ++i) {
666 test_nextsizepow2(reporter, test, expectedAns);
667
668 test++;
669 expectedAns <<= 1;
670
671 test_nextsizepow2(reporter, test, expectedAns);
672
673 test = expectedAns;
674 }
675
676 // For the remaining three tests there is no higher power (of 2)
677 test = 0x1;
678 test <<= kNumSizeTBits-1;
679 test_nextsizepow2(reporter, test, test);
680
681 test++;
682 test_nextsizepow2(reporter, test, test);
683
684 test_nextsizepow2(reporter, SIZE_MAX, SIZE_MAX);
685}
Mike Reed828f1d52017-08-09 11:06:53 -0400686
Mike Reed3d5a6b52018-01-31 15:55:47 -0500687DEF_TEST(FloatSaturate32, reporter) {
Mike Reed828f1d52017-08-09 11:06:53 -0400688 const struct {
689 float fFloat;
690 int fExpectedInt;
691 } recs[] = {
692 { 0, 0 },
693 { 100.5f, 100 },
694 { (float)SK_MaxS32, SK_MaxS32FitsInFloat },
695 { (float)SK_MinS32, SK_MinS32FitsInFloat },
696 { SK_MaxS32 * 100.0f, SK_MaxS32FitsInFloat },
697 { SK_MinS32 * 100.0f, SK_MinS32FitsInFloat },
698 { SK_ScalarInfinity, SK_MaxS32FitsInFloat },
699 { SK_ScalarNegativeInfinity, SK_MinS32FitsInFloat },
700 { SK_ScalarNaN, SK_MaxS32FitsInFloat },
701 };
702
703 for (auto r : recs) {
704 int i = sk_float_saturate2int(r.fFloat);
705 REPORTER_ASSERT(reporter, r.fExpectedInt == i);
Mike Reedf6188422018-03-05 11:49:51 -0500706
707 // ensure that these bound even non-finite values (including NaN)
708
709 SkScalar mx = SkTMax<SkScalar>(r.fFloat, 50);
710 REPORTER_ASSERT(reporter, mx >= 50);
711
712 SkScalar mn = SkTMin<SkScalar>(r.fFloat, 50);
713 REPORTER_ASSERT(reporter, mn <= 50);
714
715 SkScalar p = SkTPin<SkScalar>(r.fFloat, 0, 100);
716 REPORTER_ASSERT(reporter, p >= 0 && p <= 100);
Mike Reed828f1d52017-08-09 11:06:53 -0400717 }
718}
Mike Reede0762232017-10-10 14:49:35 -0400719
Mike Reed3d5a6b52018-01-31 15:55:47 -0500720DEF_TEST(FloatSaturate64, reporter) {
721 const struct {
722 float fFloat;
723 int64_t fExpected64;
724 } recs[] = {
725 { 0, 0 },
726 { 100.5f, 100 },
727 { (float)SK_MaxS64, SK_MaxS64FitsInFloat },
728 { (float)SK_MinS64, SK_MinS64FitsInFloat },
729 { SK_MaxS64 * 100.0f, SK_MaxS64FitsInFloat },
730 { SK_MinS64 * 100.0f, SK_MinS64FitsInFloat },
731 { SK_ScalarInfinity, SK_MaxS64FitsInFloat },
732 { SK_ScalarNegativeInfinity, SK_MinS64FitsInFloat },
733 { SK_ScalarNaN, SK_MaxS64FitsInFloat },
734 };
735
736 for (auto r : recs) {
737 int64_t i = sk_float_saturate2int64(r.fFloat);
738 REPORTER_ASSERT(reporter, r.fExpected64 == i);
739 }
740}
741
742DEF_TEST(DoubleSaturate32, reporter) {
Mike Reede0762232017-10-10 14:49:35 -0400743 const struct {
744 double fDouble;
745 int fExpectedInt;
746 } recs[] = {
747 { 0, 0 },
748 { 100.5, 100 },
749 { SK_MaxS32, SK_MaxS32 },
750 { SK_MinS32, SK_MinS32 },
751 { SK_MaxS32 - 1, SK_MaxS32 - 1 },
752 { SK_MinS32 + 1, SK_MinS32 + 1 },
753 { SK_MaxS32 * 100.0, SK_MaxS32 },
754 { SK_MinS32 * 100.0, SK_MinS32 },
755 { SK_ScalarInfinity, SK_MaxS32 },
756 { SK_ScalarNegativeInfinity, SK_MinS32 },
757 { SK_ScalarNaN, SK_MaxS32 },
758 };
759
760 for (auto r : recs) {
761 int i = sk_double_saturate2int(r.fDouble);
762 REPORTER_ASSERT(reporter, r.fExpectedInt == i);
763 }
764}
Mike Kleind8853ec2018-03-10 11:34:53 -0500765
766#if defined(__ARM_NEON)
767 #include <arm_neon.h>
768
769 DEF_TEST(NeonU16Div255, r) {
770
771 for (int v = 0; v <= 255*255; v++) {
772 int want = (v + 127)/255;
773
774 uint16x8_t V = vdupq_n_u16(v);
775 int got = vrshrq_n_u16(vrsraq_n_u16(V, V, 8), 8)[0];
776
777 if (got != want) {
778 SkDebugf("%d -> %d, want %d\n", v, got, want);
779 }
780 REPORTER_ASSERT(r, got == want);
781 }
782 }
783
784#endif