blob: fcbd63ddca85902b0f1c95ee2b4aed165068fb9d [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 float make_zero() {
233 return sk_float_sin(0);
234}
235
236static void unittest_isfinite(skiatest::Reporter* reporter) {
epoger@google.combf083a92011-06-08 18:26:08 +0000237 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000238 float inf = 1.0f / make_zero();
239 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000240
reed@google.com077910e2011-02-08 21:56:39 +0000241 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000242 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
243 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
244 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
reed@android.comd4134452011-02-09 02:24:26 +0000245
246 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000247 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
248 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
249 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000250
reed@google.com077910e2011-02-08 21:56:39 +0000251 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000252 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
253 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
254 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000255}
256
jvanverth93679922014-11-26 13:15:59 -0800257static void unittest_half(skiatest::Reporter* reporter) {
258 static const float gFloats[] = {
259 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
260 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
261 };
262
263 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
264 SkHalf h = SkFloatToHalf(gFloats[i]);
265 float f = SkHalfToFloat(h);
266 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
267 }
268
269 // check some special values
270 union FloatUnion {
271 uint32_t fU;
272 float fF;
273 };
274
275 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
276 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
277 float f = SkHalfToFloat(h);
278 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
279
280 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
281 h = SkFloatToHalf(largestNegativeHalf.fF);
282 f = SkHalfToFloat(h);
283 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
284
285 static const FloatUnion smallestPositiveHalf = { 102 << 23 };
286 h = SkFloatToHalf(smallestPositiveHalf.fF);
287 f = SkHalfToFloat(h);
288 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
289
290 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
291 h = SkFloatToHalf(overflowHalf.fF);
292 f = SkHalfToFloat(h);
293 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
294
295 static const FloatUnion underflowHalf = { 101 << 23 };
296 h = SkFloatToHalf(underflowHalf.fF);
297 f = SkHalfToFloat(h);
298 REPORTER_ASSERT(reporter, f == 0.0f );
299
300 static const FloatUnion inf32 = { 255 << 23 };
301 h = SkFloatToHalf(inf32.fF);
302 f = SkHalfToFloat(h);
303 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
304
305 static const FloatUnion nan32 = { 255 << 23 | 1 };
306 h = SkFloatToHalf(nan32.fF);
307 f = SkHalfToFloat(h);
308 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
309
310}
311
mtkleina766ca82016-01-26 07:40:30 -0800312template <typename RSqrtFn>
313static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
jvanverth29c69792015-07-23 11:14:29 -0700314 const float maxRelativeError = 6.50196699e-4f;
315
316 // test close to 0 up to 1
317 float input = 0.000001f;
318 for (int i = 0; i < 1000; ++i) {
319 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800320 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700321 float relativeError = sk_float_abs(exact - estimate)/exact;
322 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
323 input += 0.001f;
324 }
325
326 // test 1 to ~100
327 input = 1.0f;
328 for (int i = 0; i < 1000; ++i) {
329 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800330 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700331 float relativeError = sk_float_abs(exact - estimate)/exact;
332 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
333 input += 0.01f;
334 }
335
336 // test some big numbers
337 input = 1000000.0f;
338 for (int i = 0; i < 100; ++i) {
339 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800340 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700341 float relativeError = sk_float_abs(exact - estimate)/exact;
342 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
343 input += 754326.f;
344 }
345}
346
reed@android.comed673312009-02-27 16:24:51 +0000347static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000348 for (int a = 0; a <= 255; a++) {
349 for (int b = 0; b <= 255; b++) {
350 int ab = a * b;
351 float s = ab / 255.0f;
352 int round = (int)floorf(s + 0.5f);
353 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000354
reed@android.comed673312009-02-27 16:24:51 +0000355 int iround = SkMulDiv255Round(a, b);
356 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000357
reed@android.comed673312009-02-27 16:24:51 +0000358 REPORTER_ASSERT(reporter, iround == round);
359 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000360
reed@android.comed673312009-02-27 16:24:51 +0000361 REPORTER_ASSERT(reporter, itrunc <= iround);
362 REPORTER_ASSERT(reporter, iround <= a);
363 REPORTER_ASSERT(reporter, iround <= b);
364 }
365 }
reed@android.comed673312009-02-27 16:24:51 +0000366}
367
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000368static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
369 for (int c = 0; c <= 255; c++) {
370 for (int a = 0; a <= 255; a++) {
371 int product = (c * a + 255);
372 int expected_ceiling = (product + (product >> 8)) >> 8;
373 int webkit_ceiling = (c * a + 254) / 255;
374 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
375 int skia_ceiling = SkMulDiv255Ceiling(c, a);
376 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
377 }
378 }
379}
380
reed@android.comf0ad0862010-02-09 19:18:38 +0000381static void test_copysign(skiatest::Reporter* reporter) {
382 static const int32_t gTriples[] = {
383 // x, y, expected result
384 0, 0, 0,
385 0, 1, 0,
386 0, -1, 0,
387 1, 0, 1,
388 1, 1, 1,
389 1, -1, -1,
390 -1, 0, 1,
391 -1, 1, 1,
392 -1, -1, -1,
393 };
394 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
395 REPORTER_ASSERT(reporter,
396 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000397 float x = (float)gTriples[i];
398 float y = (float)gTriples[i+1];
399 float expected = (float)gTriples[i+2];
400 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000401 }
402
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000403 SkRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000404 for (int j = 0; j < 1000; j++) {
405 int ix = rand.nextS();
406 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
407 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
408 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
409 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
410
411 SkScalar sx = rand.nextSScalar1();
412 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
413 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
414 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
415 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
416 }
417}
418
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000419DEF_TEST(Math, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000420 int i;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000421 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000422
reed@android.comed673312009-02-27 16:24:51 +0000423 // these should assert
424#if 0
425 SkToS8(128);
426 SkToS8(-129);
427 SkToU8(256);
428 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000429
reed@android.comed673312009-02-27 16:24:51 +0000430 SkToS16(32768);
431 SkToS16(-32769);
432 SkToU16(65536);
433 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000434
reed@android.comed673312009-02-27 16:24:51 +0000435 if (sizeof(size_t) > 4) {
436 SkToS32(4*1024*1024);
437 SkToS32(-4*1024*1024);
438 SkToU32(5*1024*1024);
439 SkToU32(-5);
440 }
441#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000442
reed@android.comed673312009-02-27 16:24:51 +0000443 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000444 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000445 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000446
reed@android.comed673312009-02-27 16:24:51 +0000447 {
448 SkScalar x = SK_ScalarNaN;
449 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
450 }
reed@android.com80e39a72009-04-02 16:59:40 +0000451
reed@android.comed673312009-02-27 16:24:51 +0000452 for (i = 0; i < 1000; i++) {
453 int value = rand.nextS16();
454 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000455
reed@android.comed673312009-02-27 16:24:51 +0000456 int clamp = SkClampMax(value, max);
457 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
458 REPORTER_ASSERT(reporter, clamp == clamp2);
459 }
reed@android.com80e39a72009-04-02 16:59:40 +0000460
reed@android.come72fee52009-11-16 14:52:01 +0000461 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000462 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000463
tomhudson@google.com75589252012-04-10 17:42:21 +0000464 // These random values are being treated as 32-bit-patterns, not as
465 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000466 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000467 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000468 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000469 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000470 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000471 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000472 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000473 check_length(reporter, p, SK_Scalar1);
474 }
reed@android.com80e39a72009-04-02 16:59:40 +0000475
reed@android.comed673312009-02-27 16:24:51 +0000476 {
477 SkFixed result = SkFixedDiv(100, 100);
478 REPORTER_ASSERT(reporter, result == SK_Fixed1);
479 result = SkFixedDiv(1, SK_Fixed1);
480 REPORTER_ASSERT(reporter, result == 1);
liyuqian0d2c2342016-07-13 13:34:46 -0700481 result = SkFixedDiv(10 - 1, SK_Fixed1 * 3);
482 REPORTER_ASSERT(reporter, result == 3);
reed@android.comed673312009-02-27 16:24:51 +0000483 }
reed@android.com80e39a72009-04-02 16:59:40 +0000484
liyuqian3f490cc2016-10-20 11:23:09 -0700485 {
486 REPORTER_ASSERT(reporter, (SkFixedRoundToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
487 REPORTER_ASSERT(reporter, (SkFixedFloorToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
488 REPORTER_ASSERT(reporter, (SkFixedCeilToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
489 }
490
reed@google.com077910e2011-02-08 21:56:39 +0000491 unittest_isfinite(reporter);
jvanverth93679922014-11-26 13:15:59 -0800492 unittest_half(reporter);
mtkleina766ca82016-01-26 07:40:30 -0800493 test_rsqrt(reporter, sk_float_rsqrt);
494 test_rsqrt(reporter, sk_float_rsqrt_portable);
reed@android.com80e39a72009-04-02 16:59:40 +0000495
reed@android.come72fee52009-11-16 14:52:01 +0000496 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000497 SkFixed numer = rand.nextS();
498 SkFixed denom = rand.nextS();
499 SkFixed result = SkFixedDiv(numer, denom);
caryclark3127c992015-12-09 12:02:30 -0800500 int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000501
reed@android.comed673312009-02-27 16:24:51 +0000502 (void)SkCLZ(numer);
503 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000504
reed@android.comed673312009-02-27 16:24:51 +0000505 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
506 if (check > SK_MaxS32) {
507 check = SK_MaxS32;
508 } else if (check < -SK_MaxS32) {
509 check = SK_MinS32;
510 }
mtklein97ca98d2015-03-18 11:32:21 -0700511 if (result != (int32_t)check) {
512 ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
513 }
reed@android.comed673312009-02-27 16:24:51 +0000514 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.comed673312009-02-27 16:24:51 +0000515 }
reed@android.com80e39a72009-04-02 16:59:40 +0000516
reed@google.com0abb4992011-10-06 20:04:36 +0000517 test_blend(reporter);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000518
humper@google.com05af1af2013-01-07 16:47:43 +0000519 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000520
reed@google.com8d7e39c2011-11-09 17:12:08 +0000521 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000522 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000523
524 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000525 test_clz(reporter);
Yuqian Lice1d2932016-11-18 10:18:15 -0500526 test_quick_div(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000527}
528
reed@google.comc9f81662013-05-03 18:06:31 +0000529template <typename T> struct PairRec {
530 T fYin;
531 T fYang;
532};
533
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000534DEF_TEST(TestEndian, reporter) {
reed@google.comc9f81662013-05-03 18:06:31 +0000535 static const PairRec<uint16_t> g16[] = {
536 { 0x0, 0x0 },
537 { 0xFFFF, 0xFFFF },
538 { 0x1122, 0x2211 },
539 };
540 static const PairRec<uint32_t> g32[] = {
541 { 0x0, 0x0 },
542 { 0xFFFFFFFF, 0xFFFFFFFF },
543 { 0x11223344, 0x44332211 },
544 };
545 static const PairRec<uint64_t> g64[] = {
546 { 0x0, 0x0 },
547 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
548 { 0x1122334455667788ULL, 0x8877665544332211ULL },
549 };
550
551 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
552 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
553 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
554
555 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
556 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
557 }
558 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
559 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
560 }
561 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
562 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
563 }
564}
565
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000566template <typename T>
567static void test_divmod(skiatest::Reporter* r) {
568 const struct {
569 T numer;
570 T denom;
571 } kEdgeCases[] = {
572 {(T)17, (T)17},
573 {(T)17, (T)4},
574 {(T)0, (T)17},
575 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
576 {(T)-17, (T)-17},
577 {(T)-17, (T)4},
578 {(T)17, (T)-4},
579 {(T)-17, (T)-4},
580 };
581
582 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
583 const T numer = kEdgeCases[i].numer;
584 const T denom = kEdgeCases[i].denom;
585 T div, mod;
586 SkTDivMod(numer, denom, &div, &mod);
587 REPORTER_ASSERT(r, numer/denom == div);
588 REPORTER_ASSERT(r, numer%denom == mod);
589 }
590
591 SkRandom rand;
592 for (size_t i = 0; i < 10000; i++) {
593 const T numer = (T)rand.nextS();
594 T denom = 0;
595 while (0 == denom) {
596 denom = (T)rand.nextS();
597 }
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
605DEF_TEST(divmod_u8, r) {
606 test_divmod<uint8_t>(r);
607}
608
609DEF_TEST(divmod_u16, r) {
610 test_divmod<uint16_t>(r);
611}
612
613DEF_TEST(divmod_u32, r) {
614 test_divmod<uint32_t>(r);
615}
616
617DEF_TEST(divmod_u64, r) {
618 test_divmod<uint64_t>(r);
619}
620
621DEF_TEST(divmod_s8, r) {
622 test_divmod<int8_t>(r);
623}
624
625DEF_TEST(divmod_s16, r) {
626 test_divmod<int16_t>(r);
627}
628
629DEF_TEST(divmod_s32, r) {
630 test_divmod<int32_t>(r);
631}
632
633DEF_TEST(divmod_s64, r) {
634 test_divmod<int64_t>(r);
635}
Robert Phillips9e380472016-10-28 12:15:03 -0400636
637static void test_nextsizepow2(skiatest::Reporter* r, size_t test, size_t expectedAns) {
638 size_t ans = GrNextSizePow2(test);
639
640 REPORTER_ASSERT(r, ans == expectedAns);
641 //SkDebugf("0x%zx -> 0x%zx (0x%zx)\n", test, ans, expectedAns);
642}
643
644DEF_TEST(GrNextSizePow2, reporter) {
645 constexpr int kNumSizeTBits = 8 * sizeof(size_t);
646
647 size_t test = 0, expectedAns = 1;
648
649 test_nextsizepow2(reporter, test, expectedAns);
650
651 test = 1; expectedAns = 1;
652
653 for (int i = 1; i < kNumSizeTBits; ++i) {
654 test_nextsizepow2(reporter, test, expectedAns);
655
656 test++;
657 expectedAns <<= 1;
658
659 test_nextsizepow2(reporter, test, expectedAns);
660
661 test = expectedAns;
662 }
663
664 // For the remaining three tests there is no higher power (of 2)
665 test = 0x1;
666 test <<= kNumSizeTBits-1;
667 test_nextsizepow2(reporter, test, test);
668
669 test++;
670 test_nextsizepow2(reporter, test, test);
671
672 test_nextsizepow2(reporter, SIZE_MAX, SIZE_MAX);
673}
Mike Reed828f1d52017-08-09 11:06:53 -0400674
675DEF_TEST(FloatSaturate, reporter) {
676 const struct {
677 float fFloat;
678 int fExpectedInt;
679 } recs[] = {
680 { 0, 0 },
681 { 100.5f, 100 },
682 { (float)SK_MaxS32, SK_MaxS32FitsInFloat },
683 { (float)SK_MinS32, SK_MinS32FitsInFloat },
684 { SK_MaxS32 * 100.0f, SK_MaxS32FitsInFloat },
685 { SK_MinS32 * 100.0f, SK_MinS32FitsInFloat },
686 { SK_ScalarInfinity, SK_MaxS32FitsInFloat },
687 { SK_ScalarNegativeInfinity, SK_MinS32FitsInFloat },
688 { SK_ScalarNaN, SK_MaxS32FitsInFloat },
689 };
690
691 for (auto r : recs) {
692 int i = sk_float_saturate2int(r.fFloat);
693 REPORTER_ASSERT(reporter, r.fExpectedInt == i);
694 }
695}