blob: d6649542c2c33fc8ee89afda0cbf437e840f337b [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
reed@android.comed673312009-02-27 16:24:51 +00008#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
10#include "SkColorPriv.h"
11#include "SkEndian.h"
tomhudson@google.com8afae612012-08-14 15:03:35 +000012#include "SkFloatBits.h"
reed@android.comc846ede2010-04-13 15:29:15 +000013#include "SkFloatingPoint.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000014#include "SkMathPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000015#include "SkPoint.h"
16#include "SkRandom.h"
17
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
36///////////////////////////////////////////////////////////////////////////////
37
reed@google.coma7d74612012-05-30 12:30:09 +000038static float sk_fsel(float pred, float result_ge, float result_lt) {
39 return pred >= 0 ? result_ge : result_lt;
40}
41
42static float fast_floor(float x) {
reed@google.comc20bc252012-05-30 13:48:14 +000043// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
44 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
robertphillips@google.com00bf06a2012-05-30 15:19:17 +000045 return (float)(x + big) - big;
reed@google.coma7d74612012-05-30 12:30:09 +000046}
47
48static float std_floor(float x) {
49 return sk_float_floor(x);
50}
51
52static void test_floor_value(skiatest::Reporter* reporter, float value) {
53 float fast = fast_floor(value);
54 float std = std_floor(value);
55 REPORTER_ASSERT(reporter, std == fast);
56// SkDebugf("value[%1.9f] std[%g] fast[%g] equal[%d]\n",
57// value, std, fast, std == fast);
58}
59
60static void test_floor(skiatest::Reporter* reporter) {
61 static const float gVals[] = {
62 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
63 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000064
reed@google.coma7d74612012-05-30 12:30:09 +000065 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
66 test_floor_value(reporter, gVals[i]);
67// test_floor_value(reporter, -gVals[i]);
68 }
69}
70
71///////////////////////////////////////////////////////////////////////////////
72
reed@google.comea774d22013-04-22 20:21:56 +000073// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
74static void test_muldivround(skiatest::Reporter* reporter) {
75#if 0
76 // this "complete" test is too slow, so we test a random sampling of it
77
78 for (int a = 0; a <= 32767; ++a) {
79 for (int b = 0; b <= 32767; ++b) {
80 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
81 unsigned prod1 = SkMulDiv255Round(a, b);
82 SkASSERT(prod0 == prod1);
83 }
84 }
85#endif
86
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000087 SkRandom rand;
reed@google.comea774d22013-04-22 20:21:56 +000088 for (int i = 0; i < 10000; ++i) {
89 unsigned a = rand.nextU() & 0x7FFF;
90 unsigned b = rand.nextU() & 0x7FFF;
91
92 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
93 unsigned prod1 = SkMulDiv255Round(a, b);
94
95 REPORTER_ASSERT(reporter, prod0 == prod1);
96 }
97}
98
reed@google.com0abb4992011-10-06 20:04:36 +000099static float float_blend(int src, int dst, float unit) {
100 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +0000101}
102
reed@google.com8d7e39c2011-11-09 17:12:08 +0000103static int blend31(int src, int dst, int a31) {
104 return dst + ((src - dst) * a31 * 2114 >> 16);
105 // return dst + ((src - dst) * a31 * 33 >> 10);
106}
107
108static int blend31_slow(int src, int dst, int a31) {
109 int prod = src * a31 + (31 - a31) * dst + 16;
110 prod = (prod + (prod >> 5)) >> 5;
111 return prod;
112}
113
114static int blend31_round(int src, int dst, int a31) {
115 int prod = (src - dst) * a31 + 16;
116 prod = (prod + (prod >> 5)) >> 5;
117 return dst + prod;
118}
119
120static int blend31_old(int src, int dst, int a31) {
121 a31 += a31 >> 4;
122 return dst + ((src - dst) * a31 >> 5);
123}
124
caryclark@google.com42639cd2012-06-06 12:03:39 +0000125// suppress unused code warning
126static int (*blend_functions[])(int, int, int) = {
127 blend31,
128 blend31_slow,
129 blend31_round,
130 blend31_old
131};
132
reed@google.com8d7e39c2011-11-09 17:12:08 +0000133static void test_blend31() {
134 int failed = 0;
135 int death = 0;
caryclark@google.com42639cd2012-06-06 12:03:39 +0000136 if (false) { // avoid bit rot, suppress warning
137 failed = (*blend_functions[0])(0,0,0);
138 }
reed@google.com8d7e39c2011-11-09 17:12:08 +0000139 for (int src = 0; src <= 255; src++) {
140 for (int dst = 0; dst <= 255; dst++) {
141 for (int a = 0; a <= 31; a++) {
142// int r0 = blend31(src, dst, a);
143// int r0 = blend31_round(src, dst, a);
144// int r0 = blend31_old(src, dst, a);
145 int r0 = blend31_slow(src, dst, a);
146
147 float f = float_blend(src, dst, a / 31.f);
148 int r1 = (int)f;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000149 int r2 = SkScalarRoundToInt(f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000150
151 if (r0 != r1 && r0 != r2) {
bungeman@google.comfab44db2013-10-11 18:50:45 +0000152 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
153 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000154 failed += 1;
155 }
156 if (r0 > 255) {
157 death += 1;
bungeman@google.comfab44db2013-10-11 18:50:45 +0000158 SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
159 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000160 }
161 }
162 }
163 }
164 SkDebugf("---- failed %d death %d\n", failed, death);
165}
166
reed@google.com0abb4992011-10-06 20:04:36 +0000167static void test_blend(skiatest::Reporter* reporter) {
168 for (int src = 0; src <= 255; src++) {
169 for (int dst = 0; dst <= 255; dst++) {
170 for (int a = 0; a <= 255; a++) {
171 int r0 = SkAlphaBlend255(src, dst, a);
172 float f1 = float_blend(src, dst, a / 255.f);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000173 int r1 = SkScalarRoundToInt(f1);
reed@google.com772813a2011-03-30 22:34:45 +0000174
reed@google.com0abb4992011-10-06 20:04:36 +0000175 if (r0 != r1) {
176 float diff = sk_float_abs(f1 - r1);
177 diff = sk_float_abs(diff - 0.5f);
178 if (diff > (1 / 255.f)) {
179#ifdef SK_DEBUG
180 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
181 src, dst, a, r0, f1);
182#endif
183 REPORTER_ASSERT(reporter, false);
184 }
185 }
reed@google.com772813a2011-03-30 22:34:45 +0000186 }
187 }
188 }
189}
reed@google.com772813a2011-03-30 22:34:45 +0000190
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000191#if defined(SkLONGLONG)
reed@android.comed673312009-02-27 16:24:51 +0000192static int symmetric_fixmul(int a, int b) {
193 int sa = SkExtractSign(a);
194 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +0000195
reed@android.comed673312009-02-27 16:24:51 +0000196 a = SkApplySign(a, sa);
197 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +0000198
reed@android.comed673312009-02-27 16:24:51 +0000199#if 1
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000200 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +0000201
reed@android.comed673312009-02-27 16:24:51 +0000202 return SkApplySign(c, sa ^ sb);
203#else
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000204 SkLONGLONG ab = (SkLONGLONG)a * b;
reed@android.comed673312009-02-27 16:24:51 +0000205 if (sa ^ sb) {
206 ab = -ab;
207 }
208 return ab >> 16;
209#endif
210}
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000211#endif
reed@android.comed673312009-02-27 16:24:51 +0000212
213static void check_length(skiatest::Reporter* reporter,
214 const SkPoint& p, SkScalar targetLen) {
reed@android.comed673312009-02-27 16:24:51 +0000215 float x = SkScalarToFloat(p.fX);
216 float y = SkScalarToFloat(p.fY);
217 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000218
reed@android.comed673312009-02-27 16:24:51 +0000219 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000220
reed@android.comed673312009-02-27 16:24:51 +0000221 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000222}
223
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000224static float nextFloat(SkRandom& rand) {
reed@android.comed673312009-02-27 16:24:51 +0000225 SkFloatIntUnion data;
226 data.fSignBitInt = rand.nextU();
227 return data.fFloat;
228}
229
230/* returns true if a == b as resulting from (int)x. Since it is undefined
231 what to do if the float exceeds 2^32-1, we check for that explicitly.
232 */
233static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
234 if (!(x == x)) { // NAN
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000235 return ((int32_t)si) == SK_MaxS32 || ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000236 }
237 // for out of range, C is undefined, but skia always should return NaN32
238 if (x > SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000239 return ((int32_t)si) == SK_MaxS32;
reed@android.comed673312009-02-27 16:24:51 +0000240 }
241 if (x < -SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000242 return ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000243 }
244 return si == ni;
245}
246
247static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
248 float x, uint32_t ni, uint32_t si) {
249 if (!equal_float_native_skia(x, ni, si)) {
250 SkString desc;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000251 uint32_t xi = SkFloat2Bits(x);
252 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, xi, ni, si);
reed@android.comed673312009-02-27 16:24:51 +0000253 reporter->reportFailed(desc);
254 }
255}
256
257static void test_float_cast(skiatest::Reporter* reporter, float x) {
258 int ix = (int)x;
259 int iix = SkFloatToIntCast(x);
260 assert_float_equal(reporter, "cast", x, ix, iix);
261}
262
263static void test_float_floor(skiatest::Reporter* reporter, float x) {
264 int ix = (int)floor(x);
265 int iix = SkFloatToIntFloor(x);
266 assert_float_equal(reporter, "floor", x, ix, iix);
267}
268
269static void test_float_round(skiatest::Reporter* reporter, float x) {
270 double xx = x + 0.5; // need intermediate double to avoid temp loss
271 int ix = (int)floor(xx);
272 int iix = SkFloatToIntRound(x);
273 assert_float_equal(reporter, "round", x, ix, iix);
274}
275
276static void test_float_ceil(skiatest::Reporter* reporter, float x) {
277 int ix = (int)ceil(x);
278 int iix = SkFloatToIntCeil(x);
279 assert_float_equal(reporter, "ceil", x, ix, iix);
280}
281
282static void test_float_conversions(skiatest::Reporter* reporter, float x) {
283 test_float_cast(reporter, x);
284 test_float_floor(reporter, x);
285 test_float_round(reporter, x);
286 test_float_ceil(reporter, x);
287}
288
289static void test_int2float(skiatest::Reporter* reporter, int ival) {
290 float x0 = (float)ival;
291 float x1 = SkIntToFloatCast(ival);
292 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
293 REPORTER_ASSERT(reporter, x0 == x1);
294 REPORTER_ASSERT(reporter, x0 == x2);
295}
296
297static void unittest_fastfloat(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000298 SkRandom rand;
reed@android.comed673312009-02-27 16:24:51 +0000299 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000300
reed@android.comed673312009-02-27 16:24:51 +0000301 static const float gFloats[] = {
302 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
303 0.000000001f, 1000000000.f, // doesn't overflow
304 0.0000000001f, 10000000000.f // does overflow
305 };
306 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000307 test_float_conversions(reporter, gFloats[i]);
308 test_float_conversions(reporter, -gFloats[i]);
309 }
reed@android.com80e39a72009-04-02 16:59:40 +0000310
reed@android.comed673312009-02-27 16:24:51 +0000311 for (int outer = 0; outer < 100; outer++) {
312 rand.setSeed(outer);
313 for (i = 0; i < 100000; i++) {
314 float x = nextFloat(rand);
315 test_float_conversions(reporter, x);
316 }
reed@android.com80e39a72009-04-02 16:59:40 +0000317
reed@android.comed673312009-02-27 16:24:51 +0000318 test_int2float(reporter, 0);
319 test_int2float(reporter, 1);
320 test_int2float(reporter, -1);
321 for (i = 0; i < 100000; i++) {
322 // for now only test ints that are 24bits or less, since we don't
323 // round (down) large ints the same as IEEE...
324 int ival = rand.nextU() & 0xFFFFFF;
325 test_int2float(reporter, ival);
326 test_int2float(reporter, -ival);
327 }
328 }
329}
330
reed@google.com077910e2011-02-08 21:56:39 +0000331static float make_zero() {
332 return sk_float_sin(0);
333}
334
335static void unittest_isfinite(skiatest::Reporter* reporter) {
epoger@google.combf083a92011-06-08 18:26:08 +0000336 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000337 float inf = 1.0f / make_zero();
338 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000339
reed@google.com077910e2011-02-08 21:56:39 +0000340 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000341 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
342 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
343 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
reed@android.comd4134452011-02-09 02:24:26 +0000344
345 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000346 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
347 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
348 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000349
reed@google.com077910e2011-02-08 21:56:39 +0000350 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000351 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
352 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
353 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000354}
355
reed@android.comed673312009-02-27 16:24:51 +0000356static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000357 for (int a = 0; a <= 255; a++) {
358 for (int b = 0; b <= 255; b++) {
359 int ab = a * b;
360 float s = ab / 255.0f;
361 int round = (int)floorf(s + 0.5f);
362 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000363
reed@android.comed673312009-02-27 16:24:51 +0000364 int iround = SkMulDiv255Round(a, b);
365 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000366
reed@android.comed673312009-02-27 16:24:51 +0000367 REPORTER_ASSERT(reporter, iround == round);
368 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000369
reed@android.comed673312009-02-27 16:24:51 +0000370 REPORTER_ASSERT(reporter, itrunc <= iround);
371 REPORTER_ASSERT(reporter, iround <= a);
372 REPORTER_ASSERT(reporter, iround <= b);
373 }
374 }
reed@android.comed673312009-02-27 16:24:51 +0000375}
376
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000377static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
378 for (int c = 0; c <= 255; c++) {
379 for (int a = 0; a <= 255; a++) {
380 int product = (c * a + 255);
381 int expected_ceiling = (product + (product >> 8)) >> 8;
382 int webkit_ceiling = (c * a + 254) / 255;
383 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
384 int skia_ceiling = SkMulDiv255Ceiling(c, a);
385 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
386 }
387 }
388}
389
reed@android.comf0ad0862010-02-09 19:18:38 +0000390static void test_copysign(skiatest::Reporter* reporter) {
391 static const int32_t gTriples[] = {
392 // x, y, expected result
393 0, 0, 0,
394 0, 1, 0,
395 0, -1, 0,
396 1, 0, 1,
397 1, 1, 1,
398 1, -1, -1,
399 -1, 0, 1,
400 -1, 1, 1,
401 -1, -1, -1,
402 };
403 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
404 REPORTER_ASSERT(reporter,
405 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000406 float x = (float)gTriples[i];
407 float y = (float)gTriples[i+1];
408 float expected = (float)gTriples[i+2];
409 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000410 }
411
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000412 SkRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000413 for (int j = 0; j < 1000; j++) {
414 int ix = rand.nextS();
415 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
416 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
417 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
418 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
419
420 SkScalar sx = rand.nextSScalar1();
421 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
422 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
423 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
424 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
425 }
426}
427
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000428DEF_TEST(Math, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000429 int i;
430 int32_t x;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000431 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000432
reed@android.comed673312009-02-27 16:24:51 +0000433 // these should assert
434#if 0
435 SkToS8(128);
436 SkToS8(-129);
437 SkToU8(256);
438 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000439
reed@android.comed673312009-02-27 16:24:51 +0000440 SkToS16(32768);
441 SkToS16(-32769);
442 SkToU16(65536);
443 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000444
reed@android.comed673312009-02-27 16:24:51 +0000445 if (sizeof(size_t) > 4) {
446 SkToS32(4*1024*1024);
447 SkToS32(-4*1024*1024);
448 SkToU32(5*1024*1024);
449 SkToU32(-5);
450 }
451#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000452
reed@android.comed673312009-02-27 16:24:51 +0000453 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000454 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000455 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000456
reed@android.comed673312009-02-27 16:24:51 +0000457 {
458 SkScalar x = SK_ScalarNaN;
459 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
460 }
reed@android.com80e39a72009-04-02 16:59:40 +0000461
reed@android.comed673312009-02-27 16:24:51 +0000462 for (i = 1; i <= 10; i++) {
463 x = SkCubeRootBits(i*i*i, 11);
464 REPORTER_ASSERT(reporter, x == i);
465 }
reed@android.com80e39a72009-04-02 16:59:40 +0000466
reed@android.comed673312009-02-27 16:24:51 +0000467 x = SkFixedSqrt(SK_Fixed1);
468 REPORTER_ASSERT(reporter, x == SK_Fixed1);
469 x = SkFixedSqrt(SK_Fixed1/4);
470 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
471 x = SkFixedSqrt(SK_Fixed1*4);
472 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000473
reed@android.comed673312009-02-27 16:24:51 +0000474 x = SkFractSqrt(SK_Fract1);
475 REPORTER_ASSERT(reporter, x == SK_Fract1);
476 x = SkFractSqrt(SK_Fract1/4);
477 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
478 x = SkFractSqrt(SK_Fract1/16);
479 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000480
reed@android.comed673312009-02-27 16:24:51 +0000481 for (i = 1; i < 100; i++) {
482 x = SkFixedSqrt(SK_Fixed1 * i * i);
483 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
484 }
reed@android.com80e39a72009-04-02 16:59:40 +0000485
reed@android.comed673312009-02-27 16:24:51 +0000486 for (i = 0; i < 1000; i++) {
487 int value = rand.nextS16();
488 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000489
reed@android.comed673312009-02-27 16:24:51 +0000490 int clamp = SkClampMax(value, max);
491 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
492 REPORTER_ASSERT(reporter, clamp == clamp2);
493 }
reed@android.com80e39a72009-04-02 16:59:40 +0000494
reed@android.come72fee52009-11-16 14:52:01 +0000495 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000496 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000497
tomhudson@google.com75589252012-04-10 17:42:21 +0000498 // These random values are being treated as 32-bit-patterns, not as
499 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000500 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000501 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000502 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000503 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000504 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000505 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000506 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000507 check_length(reporter, p, SK_Scalar1);
508 }
reed@android.com80e39a72009-04-02 16:59:40 +0000509
reed@android.comed673312009-02-27 16:24:51 +0000510 {
511 SkFixed result = SkFixedDiv(100, 100);
512 REPORTER_ASSERT(reporter, result == SK_Fixed1);
513 result = SkFixedDiv(1, SK_Fixed1);
514 REPORTER_ASSERT(reporter, result == 1);
515 }
reed@android.com80e39a72009-04-02 16:59:40 +0000516
reed@android.comed673312009-02-27 16:24:51 +0000517 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000518 unittest_isfinite(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000519
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000520#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000521 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000522 SkFixed numer = rand.nextS();
523 SkFixed denom = rand.nextS();
524 SkFixed result = SkFixedDiv(numer, denom);
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000525 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000526
reed@android.comed673312009-02-27 16:24:51 +0000527 (void)SkCLZ(numer);
528 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000529
reed@android.comed673312009-02-27 16:24:51 +0000530 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
531 if (check > SK_MaxS32) {
532 check = SK_MaxS32;
533 } else if (check < -SK_MaxS32) {
534 check = SK_MinS32;
535 }
536 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000537
reed@android.comed673312009-02-27 16:24:51 +0000538 result = SkFractDiv(numer, denom);
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000539 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000540
reed@android.comed673312009-02-27 16:24:51 +0000541 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
542 if (check > SK_MaxS32) {
543 check = SK_MaxS32;
544 } else if (check < -SK_MaxS32) {
545 check = SK_MinS32;
546 }
547 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000548
reed@android.comed673312009-02-27 16:24:51 +0000549 // make them <= 2^24, so we don't overflow in fixmul
550 numer = numer << 8 >> 8;
551 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000552
reed@android.comed673312009-02-27 16:24:51 +0000553 result = SkFixedMul(numer, denom);
554 SkFixed r2 = symmetric_fixmul(numer, denom);
555 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000556
reed@android.comed673312009-02-27 16:24:51 +0000557 result = SkFixedMul(numer, numer);
558 r2 = SkFixedSquare(numer);
559 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000560
reed@android.comed673312009-02-27 16:24:51 +0000561 if (numer >= 0 && denom >= 0) {
562 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000563 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
564 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000565 SkFixed mean2 = SkFloatToFixed(fm);
566 int diff = SkAbs32(mean - mean2);
567 REPORTER_ASSERT(reporter, diff <= 1);
568 }
reed@android.com80e39a72009-04-02 16:59:40 +0000569
reed@android.comed673312009-02-27 16:24:51 +0000570 {
571 SkFixed mod = SkFixedMod(numer, denom);
572 float n = SkFixedToFloat(numer);
573 float d = SkFixedToFloat(denom);
574 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000575 // ensure the same sign
576 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000577 int diff = SkAbs32(mod - SkFloatToFixed(m));
578 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
579 }
reed@android.comed673312009-02-27 16:24:51 +0000580 }
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000581#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000582
reed@android.come72fee52009-11-16 14:52:01 +0000583 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000584 SkFract x = rand.nextU() >> 1;
585 double xx = (double)x / SK_Fract1;
586 SkFract xr = SkFractSqrt(x);
587 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000588 REPORTER_ASSERT(reporter, xr == check ||
589 xr == check-1 ||
590 xr == check+1);
591
reed@android.comed673312009-02-27 16:24:51 +0000592 xr = SkFixedSqrt(x);
593 xx = (double)x / SK_Fixed1;
594 check = SkFloatToFixed(sqrt(xx));
595 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000596
reed@android.comed673312009-02-27 16:24:51 +0000597 xr = SkSqrt32(x);
598 xx = (double)x;
599 check = (int32_t)sqrt(xx);
600 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
601 }
reed@android.com80e39a72009-04-02 16:59:40 +0000602
reed@google.com0abb4992011-10-06 20:04:36 +0000603 test_blend(reporter);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000604
humper@google.com05af1af2013-01-07 16:47:43 +0000605 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000606
reed@google.com8d7e39c2011-11-09 17:12:08 +0000607 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000608 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000609
610 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000611 test_clz(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000612}
613
reed@google.comc9f81662013-05-03 18:06:31 +0000614template <typename T> struct PairRec {
615 T fYin;
616 T fYang;
617};
618
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000619DEF_TEST(TestEndian, reporter) {
reed@google.comc9f81662013-05-03 18:06:31 +0000620 static const PairRec<uint16_t> g16[] = {
621 { 0x0, 0x0 },
622 { 0xFFFF, 0xFFFF },
623 { 0x1122, 0x2211 },
624 };
625 static const PairRec<uint32_t> g32[] = {
626 { 0x0, 0x0 },
627 { 0xFFFFFFFF, 0xFFFFFFFF },
628 { 0x11223344, 0x44332211 },
629 };
630 static const PairRec<uint64_t> g64[] = {
631 { 0x0, 0x0 },
632 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
633 { 0x1122334455667788ULL, 0x8877665544332211ULL },
634 };
635
636 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
637 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
638 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
639
640 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
641 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
642 }
643 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
644 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
645 }
646 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
647 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
648 }
649}
650
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000651template <typename T>
652static void test_divmod(skiatest::Reporter* r) {
653 const struct {
654 T numer;
655 T denom;
656 } kEdgeCases[] = {
657 {(T)17, (T)17},
658 {(T)17, (T)4},
659 {(T)0, (T)17},
660 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
661 {(T)-17, (T)-17},
662 {(T)-17, (T)4},
663 {(T)17, (T)-4},
664 {(T)-17, (T)-4},
665 };
666
667 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
668 const T numer = kEdgeCases[i].numer;
669 const T denom = kEdgeCases[i].denom;
670 T div, mod;
671 SkTDivMod(numer, denom, &div, &mod);
672 REPORTER_ASSERT(r, numer/denom == div);
673 REPORTER_ASSERT(r, numer%denom == mod);
674 }
675
676 SkRandom rand;
677 for (size_t i = 0; i < 10000; i++) {
678 const T numer = (T)rand.nextS();
679 T denom = 0;
680 while (0 == denom) {
681 denom = (T)rand.nextS();
682 }
683 T div, mod;
684 SkTDivMod(numer, denom, &div, &mod);
685 REPORTER_ASSERT(r, numer/denom == div);
686 REPORTER_ASSERT(r, numer%denom == mod);
687 }
688}
689
690DEF_TEST(divmod_u8, r) {
691 test_divmod<uint8_t>(r);
692}
693
694DEF_TEST(divmod_u16, r) {
695 test_divmod<uint16_t>(r);
696}
697
698DEF_TEST(divmod_u32, r) {
699 test_divmod<uint32_t>(r);
700}
701
702DEF_TEST(divmod_u64, r) {
703 test_divmod<uint64_t>(r);
704}
705
706DEF_TEST(divmod_s8, r) {
707 test_divmod<int8_t>(r);
708}
709
710DEF_TEST(divmod_s16, r) {
711 test_divmod<int16_t>(r);
712}
713
714DEF_TEST(divmod_s32, r) {
715 test_divmod<int32_t>(r);
716}
717
718DEF_TEST(divmod_s64, r) {
719 test_divmod<int64_t>(r);
720}