blob: fe54594100bc97f62586a0dc33cbd444d64ffb62 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comed673312009-02-27 16:24:51 +00008#include "Test.h"
tomhudson@google.com8afae612012-08-14 15:03:35 +00009#include "SkFloatBits.h"
reed@android.comc846ede2010-04-13 15:29:15 +000010#include "SkFloatingPoint.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000011#include "SkMathPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000012#include "SkPoint.h"
13#include "SkRandom.h"
reed@google.com0abb4992011-10-06 20:04:36 +000014#include "SkColorPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000015
reed@google.comc21f86f2013-04-29 14:18:23 +000016static void test_clz(skiatest::Reporter* reporter) {
17 REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
18 REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
19 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
reed@google.com7729534d2013-04-29 14:43:50 +000020 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
skia.committer@gmail.com81521132013-04-30 07:01:03 +000021
reed@google.comc21f86f2013-04-29 14:18:23 +000022 SkRandom rand;
23 for (int i = 0; i < 1000; ++i) {
24 uint32_t mask = rand.nextU();
reed@google.combc57a292013-04-29 15:27:42 +000025 // need to get some zeros for testing, but in some obscure way so the
26 // compiler won't "see" that, and work-around calling the functions.
27 mask >>= (mask & 31);
reed@google.comc21f86f2013-04-29 14:18:23 +000028 int intri = SkCLZ(mask);
29 int porta = SkCLZ_portable(mask);
30 REPORTER_ASSERT(reporter, intri == porta);
31 }
32}
33
34///////////////////////////////////////////////////////////////////////////////
35
reed@google.coma7d74612012-05-30 12:30:09 +000036static float sk_fsel(float pred, float result_ge, float result_lt) {
37 return pred >= 0 ? result_ge : result_lt;
38}
39
40static float fast_floor(float x) {
reed@google.comc20bc252012-05-30 13:48:14 +000041// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
42 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
robertphillips@google.com00bf06a2012-05-30 15:19:17 +000043 return (float)(x + big) - big;
reed@google.coma7d74612012-05-30 12:30:09 +000044}
45
46static float std_floor(float x) {
47 return sk_float_floor(x);
48}
49
50static void test_floor_value(skiatest::Reporter* reporter, float value) {
51 float fast = fast_floor(value);
52 float std = std_floor(value);
53 REPORTER_ASSERT(reporter, std == fast);
54// SkDebugf("value[%1.9f] std[%g] fast[%g] equal[%d]\n",
55// value, std, fast, std == fast);
56}
57
58static void test_floor(skiatest::Reporter* reporter) {
59 static const float gVals[] = {
60 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
61 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
reed@google.coma7d74612012-05-30 12:30:09 +000063 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
64 test_floor_value(reporter, gVals[i]);
65// test_floor_value(reporter, -gVals[i]);
66 }
67}
68
69///////////////////////////////////////////////////////////////////////////////
70
reed@google.comea774d22013-04-22 20:21:56 +000071// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
72static void test_muldivround(skiatest::Reporter* reporter) {
73#if 0
74 // this "complete" test is too slow, so we test a random sampling of it
75
76 for (int a = 0; a <= 32767; ++a) {
77 for (int b = 0; b <= 32767; ++b) {
78 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
79 unsigned prod1 = SkMulDiv255Round(a, b);
80 SkASSERT(prod0 == prod1);
81 }
82 }
83#endif
84
85 SkRandom rand;
86 for (int i = 0; i < 10000; ++i) {
87 unsigned a = rand.nextU() & 0x7FFF;
88 unsigned b = rand.nextU() & 0x7FFF;
89
90 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
91 unsigned prod1 = SkMulDiv255Round(a, b);
92
93 REPORTER_ASSERT(reporter, prod0 == prod1);
94 }
95}
96
reed@google.com0abb4992011-10-06 20:04:36 +000097static float float_blend(int src, int dst, float unit) {
98 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +000099}
100
reed@google.com8d7e39c2011-11-09 17:12:08 +0000101static int blend31(int src, int dst, int a31) {
102 return dst + ((src - dst) * a31 * 2114 >> 16);
103 // return dst + ((src - dst) * a31 * 33 >> 10);
104}
105
106static int blend31_slow(int src, int dst, int a31) {
107 int prod = src * a31 + (31 - a31) * dst + 16;
108 prod = (prod + (prod >> 5)) >> 5;
109 return prod;
110}
111
112static int blend31_round(int src, int dst, int a31) {
113 int prod = (src - dst) * a31 + 16;
114 prod = (prod + (prod >> 5)) >> 5;
115 return dst + prod;
116}
117
118static int blend31_old(int src, int dst, int a31) {
119 a31 += a31 >> 4;
120 return dst + ((src - dst) * a31 >> 5);
121}
122
caryclark@google.com42639cd2012-06-06 12:03:39 +0000123// suppress unused code warning
124static int (*blend_functions[])(int, int, int) = {
125 blend31,
126 blend31_slow,
127 blend31_round,
128 blend31_old
129};
130
reed@google.com8d7e39c2011-11-09 17:12:08 +0000131static void test_blend31() {
132 int failed = 0;
133 int death = 0;
caryclark@google.com42639cd2012-06-06 12:03:39 +0000134 if (false) { // avoid bit rot, suppress warning
135 failed = (*blend_functions[0])(0,0,0);
136 }
reed@google.com8d7e39c2011-11-09 17:12:08 +0000137 for (int src = 0; src <= 255; src++) {
138 for (int dst = 0; dst <= 255; dst++) {
139 for (int a = 0; a <= 31; a++) {
140// int r0 = blend31(src, dst, a);
141// int r0 = blend31_round(src, dst, a);
142// int r0 = blend31_old(src, dst, a);
143 int r0 = blend31_slow(src, dst, a);
144
145 float f = float_blend(src, dst, a / 31.f);
146 int r1 = (int)f;
147 int r2 = SkScalarRoundToInt(SkFloatToScalar(f));
148
149 if (r0 != r1 && r0 != r2) {
150 printf("src:%d dst:%d a:%d result:%d float:%g\n",
151 src, dst, a, r0, f);
152 failed += 1;
153 }
154 if (r0 > 255) {
155 death += 1;
156 printf("death src:%d dst:%d a:%d result:%d float:%g\n",
157 src, dst, a, r0, f);
158 }
159 }
160 }
161 }
162 SkDebugf("---- failed %d death %d\n", failed, death);
163}
164
reed@google.com0abb4992011-10-06 20:04:36 +0000165static void test_blend(skiatest::Reporter* reporter) {
166 for (int src = 0; src <= 255; src++) {
167 for (int dst = 0; dst <= 255; dst++) {
168 for (int a = 0; a <= 255; a++) {
169 int r0 = SkAlphaBlend255(src, dst, a);
170 float f1 = float_blend(src, dst, a / 255.f);
reed@google.com111c19b2011-10-06 20:13:22 +0000171 int r1 = SkScalarRoundToInt(SkFloatToScalar(f1));
reed@google.com772813a2011-03-30 22:34:45 +0000172
reed@google.com0abb4992011-10-06 20:04:36 +0000173 if (r0 != r1) {
174 float diff = sk_float_abs(f1 - r1);
175 diff = sk_float_abs(diff - 0.5f);
176 if (diff > (1 / 255.f)) {
177#ifdef SK_DEBUG
178 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
179 src, dst, a, r0, f1);
180#endif
181 REPORTER_ASSERT(reporter, false);
182 }
183 }
reed@google.com772813a2011-03-30 22:34:45 +0000184 }
185 }
186 }
187}
reed@google.com772813a2011-03-30 22:34:45 +0000188
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000189#if defined(SkLONGLONG)
reed@android.comed673312009-02-27 16:24:51 +0000190static int symmetric_fixmul(int a, int b) {
191 int sa = SkExtractSign(a);
192 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +0000193
reed@android.comed673312009-02-27 16:24:51 +0000194 a = SkApplySign(a, sa);
195 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +0000196
reed@android.comed673312009-02-27 16:24:51 +0000197#if 1
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000198 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +0000199
reed@android.comed673312009-02-27 16:24:51 +0000200 return SkApplySign(c, sa ^ sb);
201#else
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000202 SkLONGLONG ab = (SkLONGLONG)a * b;
reed@android.comed673312009-02-27 16:24:51 +0000203 if (sa ^ sb) {
204 ab = -ab;
205 }
206 return ab >> 16;
207#endif
208}
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000209#endif
reed@android.comed673312009-02-27 16:24:51 +0000210
211static void check_length(skiatest::Reporter* reporter,
212 const SkPoint& p, SkScalar targetLen) {
reed@android.comed673312009-02-27 16:24:51 +0000213 float x = SkScalarToFloat(p.fX);
214 float y = SkScalarToFloat(p.fY);
215 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000216
reed@android.comed673312009-02-27 16:24:51 +0000217 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000218
reed@android.comed673312009-02-27 16:24:51 +0000219 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000220}
221
jvanverth@google.comc490f802013-03-04 13:56:38 +0000222static float nextFloat(SkMWCRandom& rand) {
reed@android.comed673312009-02-27 16:24:51 +0000223 SkFloatIntUnion data;
224 data.fSignBitInt = rand.nextU();
225 return data.fFloat;
226}
227
228/* returns true if a == b as resulting from (int)x. Since it is undefined
229 what to do if the float exceeds 2^32-1, we check for that explicitly.
230 */
231static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
232 if (!(x == x)) { // NAN
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000233 return ((int32_t)si) == SK_MaxS32 || ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000234 }
235 // for out of range, C is undefined, but skia always should return NaN32
236 if (x > SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000237 return ((int32_t)si) == SK_MaxS32;
reed@android.comed673312009-02-27 16:24:51 +0000238 }
239 if (x < -SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000240 return ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000241 }
242 return si == ni;
243}
244
245static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
246 float x, uint32_t ni, uint32_t si) {
247 if (!equal_float_native_skia(x, ni, si)) {
248 SkString desc;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000249 uint32_t xi = SkFloat2Bits(x);
250 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 +0000251 reporter->reportFailed(desc);
252 }
253}
254
255static void test_float_cast(skiatest::Reporter* reporter, float x) {
256 int ix = (int)x;
257 int iix = SkFloatToIntCast(x);
258 assert_float_equal(reporter, "cast", x, ix, iix);
259}
260
261static void test_float_floor(skiatest::Reporter* reporter, float x) {
262 int ix = (int)floor(x);
263 int iix = SkFloatToIntFloor(x);
264 assert_float_equal(reporter, "floor", x, ix, iix);
265}
266
267static void test_float_round(skiatest::Reporter* reporter, float x) {
268 double xx = x + 0.5; // need intermediate double to avoid temp loss
269 int ix = (int)floor(xx);
270 int iix = SkFloatToIntRound(x);
271 assert_float_equal(reporter, "round", x, ix, iix);
272}
273
274static void test_float_ceil(skiatest::Reporter* reporter, float x) {
275 int ix = (int)ceil(x);
276 int iix = SkFloatToIntCeil(x);
277 assert_float_equal(reporter, "ceil", x, ix, iix);
278}
279
280static void test_float_conversions(skiatest::Reporter* reporter, float x) {
281 test_float_cast(reporter, x);
282 test_float_floor(reporter, x);
283 test_float_round(reporter, x);
284 test_float_ceil(reporter, x);
285}
286
287static void test_int2float(skiatest::Reporter* reporter, int ival) {
288 float x0 = (float)ival;
289 float x1 = SkIntToFloatCast(ival);
290 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
291 REPORTER_ASSERT(reporter, x0 == x1);
292 REPORTER_ASSERT(reporter, x0 == x2);
293}
294
295static void unittest_fastfloat(skiatest::Reporter* reporter) {
jvanverth@google.comc490f802013-03-04 13:56:38 +0000296 SkMWCRandom rand;
reed@android.comed673312009-02-27 16:24:51 +0000297 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000298
reed@android.comed673312009-02-27 16:24:51 +0000299 static const float gFloats[] = {
300 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
301 0.000000001f, 1000000000.f, // doesn't overflow
302 0.0000000001f, 10000000000.f // does overflow
303 };
304 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000305 test_float_conversions(reporter, gFloats[i]);
306 test_float_conversions(reporter, -gFloats[i]);
307 }
reed@android.com80e39a72009-04-02 16:59:40 +0000308
reed@android.comed673312009-02-27 16:24:51 +0000309 for (int outer = 0; outer < 100; outer++) {
310 rand.setSeed(outer);
311 for (i = 0; i < 100000; i++) {
312 float x = nextFloat(rand);
313 test_float_conversions(reporter, x);
314 }
reed@android.com80e39a72009-04-02 16:59:40 +0000315
reed@android.comed673312009-02-27 16:24:51 +0000316 test_int2float(reporter, 0);
317 test_int2float(reporter, 1);
318 test_int2float(reporter, -1);
319 for (i = 0; i < 100000; i++) {
320 // for now only test ints that are 24bits or less, since we don't
321 // round (down) large ints the same as IEEE...
322 int ival = rand.nextU() & 0xFFFFFF;
323 test_int2float(reporter, ival);
324 test_int2float(reporter, -ival);
325 }
326 }
327}
328
reed@android.comd4134452011-02-09 02:24:26 +0000329#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000330static float make_zero() {
331 return sk_float_sin(0);
332}
reed@android.comd4134452011-02-09 02:24:26 +0000333#endif
reed@google.com077910e2011-02-08 21:56:39 +0000334
335static void unittest_isfinite(skiatest::Reporter* reporter) {
336#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000337 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000338 float inf = 1.0f / make_zero();
339 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000340
reed@google.com077910e2011-02-08 21:56:39 +0000341 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000342 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
343 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
344 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
345#else
346 SkFixed nan = SK_FixedNaN;
347 SkFixed big = SK_FixedMax;
348#endif
349
350 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000351 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
352 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
353 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000354
reed@google.com077910e2011-02-08 21:56:39 +0000355 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000356 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
357 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
358 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000359}
360
reed@android.comed673312009-02-27 16:24:51 +0000361static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000362 for (int a = 0; a <= 255; a++) {
363 for (int b = 0; b <= 255; b++) {
364 int ab = a * b;
365 float s = ab / 255.0f;
366 int round = (int)floorf(s + 0.5f);
367 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000368
reed@android.comed673312009-02-27 16:24:51 +0000369 int iround = SkMulDiv255Round(a, b);
370 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000371
reed@android.comed673312009-02-27 16:24:51 +0000372 REPORTER_ASSERT(reporter, iround == round);
373 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000374
reed@android.comed673312009-02-27 16:24:51 +0000375 REPORTER_ASSERT(reporter, itrunc <= iround);
376 REPORTER_ASSERT(reporter, iround <= a);
377 REPORTER_ASSERT(reporter, iround <= b);
378 }
379 }
reed@android.comed673312009-02-27 16:24:51 +0000380}
381
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000382static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
383 for (int c = 0; c <= 255; c++) {
384 for (int a = 0; a <= 255; a++) {
385 int product = (c * a + 255);
386 int expected_ceiling = (product + (product >> 8)) >> 8;
387 int webkit_ceiling = (c * a + 254) / 255;
388 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
389 int skia_ceiling = SkMulDiv255Ceiling(c, a);
390 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
391 }
392 }
393}
394
reed@android.comf0ad0862010-02-09 19:18:38 +0000395static void test_copysign(skiatest::Reporter* reporter) {
396 static const int32_t gTriples[] = {
397 // x, y, expected result
398 0, 0, 0,
399 0, 1, 0,
400 0, -1, 0,
401 1, 0, 1,
402 1, 1, 1,
403 1, -1, -1,
404 -1, 0, 1,
405 -1, 1, 1,
406 -1, -1, -1,
407 };
408 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
409 REPORTER_ASSERT(reporter,
410 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000411 float x = (float)gTriples[i];
412 float y = (float)gTriples[i+1];
413 float expected = (float)gTriples[i+2];
414 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000415 }
416
jvanverth@google.comc490f802013-03-04 13:56:38 +0000417 SkMWCRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000418 for (int j = 0; j < 1000; j++) {
419 int ix = rand.nextS();
420 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
421 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
422 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
423 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
424
425 SkScalar sx = rand.nextSScalar1();
426 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
427 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
428 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
429 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
430 }
431}
432
reed@android.com80e39a72009-04-02 16:59:40 +0000433static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000434 int i;
435 int32_t x;
jvanverth@google.comc490f802013-03-04 13:56:38 +0000436 SkMWCRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000437
reed@android.comed673312009-02-27 16:24:51 +0000438 // these should assert
439#if 0
440 SkToS8(128);
441 SkToS8(-129);
442 SkToU8(256);
443 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000444
reed@android.comed673312009-02-27 16:24:51 +0000445 SkToS16(32768);
446 SkToS16(-32769);
447 SkToU16(65536);
448 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000449
reed@android.comed673312009-02-27 16:24:51 +0000450 if (sizeof(size_t) > 4) {
451 SkToS32(4*1024*1024);
452 SkToS32(-4*1024*1024);
453 SkToU32(5*1024*1024);
454 SkToU32(-5);
455 }
456#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000457
reed@android.comed673312009-02-27 16:24:51 +0000458 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000459 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000460 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000461
reed@android.comed673312009-02-27 16:24:51 +0000462 {
463 SkScalar x = SK_ScalarNaN;
464 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
465 }
reed@android.com80e39a72009-04-02 16:59:40 +0000466
reed@android.comed673312009-02-27 16:24:51 +0000467 for (i = 1; i <= 10; i++) {
468 x = SkCubeRootBits(i*i*i, 11);
469 REPORTER_ASSERT(reporter, x == i);
470 }
reed@android.com80e39a72009-04-02 16:59:40 +0000471
reed@android.comed673312009-02-27 16:24:51 +0000472 x = SkFixedSqrt(SK_Fixed1);
473 REPORTER_ASSERT(reporter, x == SK_Fixed1);
474 x = SkFixedSqrt(SK_Fixed1/4);
475 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
476 x = SkFixedSqrt(SK_Fixed1*4);
477 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000478
reed@android.comed673312009-02-27 16:24:51 +0000479 x = SkFractSqrt(SK_Fract1);
480 REPORTER_ASSERT(reporter, x == SK_Fract1);
481 x = SkFractSqrt(SK_Fract1/4);
482 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
483 x = SkFractSqrt(SK_Fract1/16);
484 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000485
reed@android.comed673312009-02-27 16:24:51 +0000486 for (i = 1; i < 100; i++) {
487 x = SkFixedSqrt(SK_Fixed1 * i * i);
488 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
489 }
reed@android.com80e39a72009-04-02 16:59:40 +0000490
reed@android.comed673312009-02-27 16:24:51 +0000491 for (i = 0; i < 1000; i++) {
492 int value = rand.nextS16();
493 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000494
reed@android.comed673312009-02-27 16:24:51 +0000495 int clamp = SkClampMax(value, max);
496 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
497 REPORTER_ASSERT(reporter, clamp == clamp2);
498 }
reed@android.com80e39a72009-04-02 16:59:40 +0000499
reed@android.come72fee52009-11-16 14:52:01 +0000500 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000501 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000502
tomhudson@google.com75589252012-04-10 17:42:21 +0000503 // These random values are being treated as 32-bit-patterns, not as
504 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000505 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000506 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000507 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000508 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000509 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000510 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000511 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000512 check_length(reporter, p, SK_Scalar1);
513 }
reed@android.com80e39a72009-04-02 16:59:40 +0000514
reed@android.comed673312009-02-27 16:24:51 +0000515 {
516 SkFixed result = SkFixedDiv(100, 100);
517 REPORTER_ASSERT(reporter, result == SK_Fixed1);
518 result = SkFixedDiv(1, SK_Fixed1);
519 REPORTER_ASSERT(reporter, result == 1);
520 }
reed@android.com80e39a72009-04-02 16:59:40 +0000521
reed@android.comed673312009-02-27 16:24:51 +0000522 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000523 unittest_isfinite(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000524
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000525#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000526 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000527 SkFixed numer = rand.nextS();
528 SkFixed denom = rand.nextS();
529 SkFixed result = SkFixedDiv(numer, denom);
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000530 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000531
reed@android.comed673312009-02-27 16:24:51 +0000532 (void)SkCLZ(numer);
533 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000534
reed@android.comed673312009-02-27 16:24:51 +0000535 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
536 if (check > SK_MaxS32) {
537 check = SK_MaxS32;
538 } else if (check < -SK_MaxS32) {
539 check = SK_MinS32;
540 }
541 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000542
reed@android.comed673312009-02-27 16:24:51 +0000543 result = SkFractDiv(numer, denom);
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000544 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000545
reed@android.comed673312009-02-27 16:24:51 +0000546 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
547 if (check > SK_MaxS32) {
548 check = SK_MaxS32;
549 } else if (check < -SK_MaxS32) {
550 check = SK_MinS32;
551 }
552 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000553
reed@android.comed673312009-02-27 16:24:51 +0000554 // make them <= 2^24, so we don't overflow in fixmul
555 numer = numer << 8 >> 8;
556 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000557
reed@android.comed673312009-02-27 16:24:51 +0000558 result = SkFixedMul(numer, denom);
559 SkFixed r2 = symmetric_fixmul(numer, denom);
560 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000561
reed@android.comed673312009-02-27 16:24:51 +0000562 result = SkFixedMul(numer, numer);
563 r2 = SkFixedSquare(numer);
564 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000565
reed@android.comed673312009-02-27 16:24:51 +0000566 if (numer >= 0 && denom >= 0) {
567 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000568 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
569 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000570 SkFixed mean2 = SkFloatToFixed(fm);
571 int diff = SkAbs32(mean - mean2);
572 REPORTER_ASSERT(reporter, diff <= 1);
573 }
reed@android.com80e39a72009-04-02 16:59:40 +0000574
reed@android.comed673312009-02-27 16:24:51 +0000575 {
576 SkFixed mod = SkFixedMod(numer, denom);
577 float n = SkFixedToFloat(numer);
578 float d = SkFixedToFloat(denom);
579 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000580 // ensure the same sign
581 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000582 int diff = SkAbs32(mod - SkFloatToFixed(m));
583 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
584 }
reed@android.comed673312009-02-27 16:24:51 +0000585 }
robertphillips@google.com0e6e8cc2013-08-15 13:43:23 +0000586#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000587
reed@android.come72fee52009-11-16 14:52:01 +0000588 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000589 SkFract x = rand.nextU() >> 1;
590 double xx = (double)x / SK_Fract1;
591 SkFract xr = SkFractSqrt(x);
592 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000593 REPORTER_ASSERT(reporter, xr == check ||
594 xr == check-1 ||
595 xr == check+1);
596
reed@android.comed673312009-02-27 16:24:51 +0000597 xr = SkFixedSqrt(x);
598 xx = (double)x / SK_Fixed1;
599 check = SkFloatToFixed(sqrt(xx));
600 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000601
reed@android.comed673312009-02-27 16:24:51 +0000602 xr = SkSqrt32(x);
603 xx = (double)x;
604 check = (int32_t)sqrt(xx);
605 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
606 }
reed@android.com80e39a72009-04-02 16:59:40 +0000607
reed@google.com7886ad32012-06-11 21:21:26 +0000608#if !defined(SK_SCALAR_IS_FLOAT)
reed@android.comed673312009-02-27 16:24:51 +0000609 {
610 SkFixed s, c;
611 s = SkFixedSinCos(0, &c);
612 REPORTER_ASSERT(reporter, s == 0);
613 REPORTER_ASSERT(reporter, c == SK_Fixed1);
614 }
reed@android.com80e39a72009-04-02 16:59:40 +0000615
reed@android.comed673312009-02-27 16:24:51 +0000616 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000617 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000618 SkFixed rads = rand.nextS() >> 10;
619 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000620
reed@android.comed673312009-02-27 16:24:51 +0000621 SkFixed s, c;
622 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000623
reed@android.comed673312009-02-27 16:24:51 +0000624 double fs = sin(frads);
625 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000626
reed@android.comed673312009-02-27 16:24:51 +0000627 SkFixed is = SkFloatToFixed(fs);
628 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000629
reed@android.comed673312009-02-27 16:24:51 +0000630 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
631 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
632 }
633 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
634#endif
reed@google.com772813a2011-03-30 22:34:45 +0000635
reed@google.com0abb4992011-10-06 20:04:36 +0000636#ifdef SK_SCALAR_IS_FLOAT
637 test_blend(reporter);
638#endif
reed@google.com8d7e39c2011-11-09 17:12:08 +0000639
humper@google.com05af1af2013-01-07 16:47:43 +0000640 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000641
reed@google.com8d7e39c2011-11-09 17:12:08 +0000642 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000643 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000644
645 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000646 test_clz(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000647}
648
reed@android.comd8730ea2009-02-27 22:06:06 +0000649#include "TestClassDef.h"
650DEFINE_TESTCLASS("Math", MathTestClass, TestMath)
reed@google.comc9f81662013-05-03 18:06:31 +0000651
652///////////////////////////////////////////////////////////////////////////////
653
654#include "SkEndian.h"
655
656template <typename T> struct PairRec {
657 T fYin;
658 T fYang;
659};
660
661static void TestEndian(skiatest::Reporter* reporter) {
662 static const PairRec<uint16_t> g16[] = {
663 { 0x0, 0x0 },
664 { 0xFFFF, 0xFFFF },
665 { 0x1122, 0x2211 },
666 };
667 static const PairRec<uint32_t> g32[] = {
668 { 0x0, 0x0 },
669 { 0xFFFFFFFF, 0xFFFFFFFF },
670 { 0x11223344, 0x44332211 },
671 };
672 static const PairRec<uint64_t> g64[] = {
673 { 0x0, 0x0 },
674 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
675 { 0x1122334455667788ULL, 0x8877665544332211ULL },
676 };
677
678 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
679 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
680 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
681
682 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
683 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
684 }
685 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
686 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
687 }
688 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
689 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
690 }
691}
692
693DEFINE_TESTCLASS("Endian", EndianTestClass, TestEndian)