blob: 3deb79a69e82cd598b8ffafd067e463103142484 [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"
reed@android.comc846ede2010-04-13 15:29:15 +00009#include "SkFloatingPoint.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000010#include "SkMath.h"
reed@android.comed673312009-02-27 16:24:51 +000011#include "SkPoint.h"
12#include "SkRandom.h"
reed@google.com0abb4992011-10-06 20:04:36 +000013#include "SkColorPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000014
reed@google.coma7d74612012-05-30 12:30:09 +000015static float sk_fsel(float pred, float result_ge, float result_lt) {
16 return pred >= 0 ? result_ge : result_lt;
17}
18
19static float fast_floor(float x) {
reed@google.comc20bc252012-05-30 13:48:14 +000020// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
21 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
robertphillips@google.com00bf06a2012-05-30 15:19:17 +000022 return (float)(x + big) - big;
reed@google.coma7d74612012-05-30 12:30:09 +000023}
24
25static float std_floor(float x) {
26 return sk_float_floor(x);
27}
28
29static void test_floor_value(skiatest::Reporter* reporter, float value) {
30 float fast = fast_floor(value);
31 float std = std_floor(value);
32 REPORTER_ASSERT(reporter, std == fast);
33// SkDebugf("value[%1.9f] std[%g] fast[%g] equal[%d]\n",
34// value, std, fast, std == fast);
35}
36
37static void test_floor(skiatest::Reporter* reporter) {
38 static const float gVals[] = {
39 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
40 };
41
42 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
43 test_floor_value(reporter, gVals[i]);
44// test_floor_value(reporter, -gVals[i]);
45 }
46}
47
48///////////////////////////////////////////////////////////////////////////////
49
reed@google.com0abb4992011-10-06 20:04:36 +000050static float float_blend(int src, int dst, float unit) {
51 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +000052}
53
reed@google.com8d7e39c2011-11-09 17:12:08 +000054static int blend31(int src, int dst, int a31) {
55 return dst + ((src - dst) * a31 * 2114 >> 16);
56 // return dst + ((src - dst) * a31 * 33 >> 10);
57}
58
59static int blend31_slow(int src, int dst, int a31) {
60 int prod = src * a31 + (31 - a31) * dst + 16;
61 prod = (prod + (prod >> 5)) >> 5;
62 return prod;
63}
64
65static int blend31_round(int src, int dst, int a31) {
66 int prod = (src - dst) * a31 + 16;
67 prod = (prod + (prod >> 5)) >> 5;
68 return dst + prod;
69}
70
71static int blend31_old(int src, int dst, int a31) {
72 a31 += a31 >> 4;
73 return dst + ((src - dst) * a31 >> 5);
74}
75
caryclark@google.com42639cd2012-06-06 12:03:39 +000076// suppress unused code warning
77static int (*blend_functions[])(int, int, int) = {
78 blend31,
79 blend31_slow,
80 blend31_round,
81 blend31_old
82};
83
reed@google.com8d7e39c2011-11-09 17:12:08 +000084static void test_blend31() {
85 int failed = 0;
86 int death = 0;
caryclark@google.com42639cd2012-06-06 12:03:39 +000087 if (false) { // avoid bit rot, suppress warning
88 failed = (*blend_functions[0])(0,0,0);
89 }
reed@google.com8d7e39c2011-11-09 17:12:08 +000090 for (int src = 0; src <= 255; src++) {
91 for (int dst = 0; dst <= 255; dst++) {
92 for (int a = 0; a <= 31; a++) {
93// int r0 = blend31(src, dst, a);
94// int r0 = blend31_round(src, dst, a);
95// int r0 = blend31_old(src, dst, a);
96 int r0 = blend31_slow(src, dst, a);
97
98 float f = float_blend(src, dst, a / 31.f);
99 int r1 = (int)f;
100 int r2 = SkScalarRoundToInt(SkFloatToScalar(f));
101
102 if (r0 != r1 && r0 != r2) {
103 printf("src:%d dst:%d a:%d result:%d float:%g\n",
104 src, dst, a, r0, f);
105 failed += 1;
106 }
107 if (r0 > 255) {
108 death += 1;
109 printf("death src:%d dst:%d a:%d result:%d float:%g\n",
110 src, dst, a, r0, f);
111 }
112 }
113 }
114 }
115 SkDebugf("---- failed %d death %d\n", failed, death);
116}
117
reed@google.com0abb4992011-10-06 20:04:36 +0000118static void test_blend(skiatest::Reporter* reporter) {
119 for (int src = 0; src <= 255; src++) {
120 for (int dst = 0; dst <= 255; dst++) {
121 for (int a = 0; a <= 255; a++) {
122 int r0 = SkAlphaBlend255(src, dst, a);
123 float f1 = float_blend(src, dst, a / 255.f);
reed@google.com111c19b2011-10-06 20:13:22 +0000124 int r1 = SkScalarRoundToInt(SkFloatToScalar(f1));
reed@google.com772813a2011-03-30 22:34:45 +0000125
reed@google.com0abb4992011-10-06 20:04:36 +0000126 if (r0 != r1) {
127 float diff = sk_float_abs(f1 - r1);
128 diff = sk_float_abs(diff - 0.5f);
129 if (diff > (1 / 255.f)) {
130#ifdef SK_DEBUG
131 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
132 src, dst, a, r0, f1);
133#endif
134 REPORTER_ASSERT(reporter, false);
135 }
136 }
reed@google.com772813a2011-03-30 22:34:45 +0000137 }
138 }
139 }
140}
reed@google.com772813a2011-03-30 22:34:45 +0000141
reed@android.comed673312009-02-27 16:24:51 +0000142#if defined(SkLONGLONG)
143static int symmetric_fixmul(int a, int b) {
144 int sa = SkExtractSign(a);
145 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +0000146
reed@android.comed673312009-02-27 16:24:51 +0000147 a = SkApplySign(a, sa);
148 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +0000149
reed@android.comed673312009-02-27 16:24:51 +0000150#if 1
151 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +0000152
reed@android.comed673312009-02-27 16:24:51 +0000153 return SkApplySign(c, sa ^ sb);
154#else
155 SkLONGLONG ab = (SkLONGLONG)a * b;
156 if (sa ^ sb) {
157 ab = -ab;
158 }
159 return ab >> 16;
160#endif
161}
162#endif
163
164static void check_length(skiatest::Reporter* reporter,
165 const SkPoint& p, SkScalar targetLen) {
166#ifdef SK_CAN_USE_FLOAT
167 float x = SkScalarToFloat(p.fX);
168 float y = SkScalarToFloat(p.fY);
169 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000170
reed@android.comed673312009-02-27 16:24:51 +0000171 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000172
reed@android.comed673312009-02-27 16:24:51 +0000173 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
174#endif
175}
176
177#if defined(SK_CAN_USE_FLOAT)
178
179static float nextFloat(SkRandom& rand) {
180 SkFloatIntUnion data;
181 data.fSignBitInt = rand.nextU();
182 return data.fFloat;
183}
184
185/* returns true if a == b as resulting from (int)x. Since it is undefined
186 what to do if the float exceeds 2^32-1, we check for that explicitly.
187 */
188static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
189 if (!(x == x)) { // NAN
190 return si == SK_MaxS32 || si == SK_MinS32;
191 }
192 // for out of range, C is undefined, but skia always should return NaN32
193 if (x > SK_MaxS32) {
194 return si == SK_MaxS32;
195 }
196 if (x < -SK_MaxS32) {
197 return si == SK_MinS32;
198 }
199 return si == ni;
200}
201
202static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
203 float x, uint32_t ni, uint32_t si) {
204 if (!equal_float_native_skia(x, ni, si)) {
205 SkString desc;
206 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si);
207 reporter->reportFailed(desc);
208 }
209}
210
211static void test_float_cast(skiatest::Reporter* reporter, float x) {
212 int ix = (int)x;
213 int iix = SkFloatToIntCast(x);
214 assert_float_equal(reporter, "cast", x, ix, iix);
215}
216
217static void test_float_floor(skiatest::Reporter* reporter, float x) {
218 int ix = (int)floor(x);
219 int iix = SkFloatToIntFloor(x);
220 assert_float_equal(reporter, "floor", x, ix, iix);
221}
222
223static void test_float_round(skiatest::Reporter* reporter, float x) {
224 double xx = x + 0.5; // need intermediate double to avoid temp loss
225 int ix = (int)floor(xx);
226 int iix = SkFloatToIntRound(x);
227 assert_float_equal(reporter, "round", x, ix, iix);
228}
229
230static void test_float_ceil(skiatest::Reporter* reporter, float x) {
231 int ix = (int)ceil(x);
232 int iix = SkFloatToIntCeil(x);
233 assert_float_equal(reporter, "ceil", x, ix, iix);
234}
235
236static void test_float_conversions(skiatest::Reporter* reporter, float x) {
237 test_float_cast(reporter, x);
238 test_float_floor(reporter, x);
239 test_float_round(reporter, x);
240 test_float_ceil(reporter, x);
241}
242
243static void test_int2float(skiatest::Reporter* reporter, int ival) {
244 float x0 = (float)ival;
245 float x1 = SkIntToFloatCast(ival);
246 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
247 REPORTER_ASSERT(reporter, x0 == x1);
248 REPORTER_ASSERT(reporter, x0 == x2);
249}
250
251static void unittest_fastfloat(skiatest::Reporter* reporter) {
252 SkRandom rand;
253 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000254
reed@android.comed673312009-02-27 16:24:51 +0000255 static const float gFloats[] = {
256 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
257 0.000000001f, 1000000000.f, // doesn't overflow
258 0.0000000001f, 10000000000.f // does overflow
259 };
260 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000261 test_float_conversions(reporter, gFloats[i]);
262 test_float_conversions(reporter, -gFloats[i]);
263 }
reed@android.com80e39a72009-04-02 16:59:40 +0000264
reed@android.comed673312009-02-27 16:24:51 +0000265 for (int outer = 0; outer < 100; outer++) {
266 rand.setSeed(outer);
267 for (i = 0; i < 100000; i++) {
268 float x = nextFloat(rand);
269 test_float_conversions(reporter, x);
270 }
reed@android.com80e39a72009-04-02 16:59:40 +0000271
reed@android.comed673312009-02-27 16:24:51 +0000272 test_int2float(reporter, 0);
273 test_int2float(reporter, 1);
274 test_int2float(reporter, -1);
275 for (i = 0; i < 100000; i++) {
276 // for now only test ints that are 24bits or less, since we don't
277 // round (down) large ints the same as IEEE...
278 int ival = rand.nextU() & 0xFFFFFF;
279 test_int2float(reporter, ival);
280 test_int2float(reporter, -ival);
281 }
282 }
283}
284
reed@android.comd4134452011-02-09 02:24:26 +0000285#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000286static float make_zero() {
287 return sk_float_sin(0);
288}
reed@android.comd4134452011-02-09 02:24:26 +0000289#endif
reed@google.com077910e2011-02-08 21:56:39 +0000290
291static void unittest_isfinite(skiatest::Reporter* reporter) {
292#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000293 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000294 float inf = 1.0f / make_zero();
295 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000296
reed@google.com077910e2011-02-08 21:56:39 +0000297 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000298 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
299 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
300 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
301#else
302 SkFixed nan = SK_FixedNaN;
303 SkFixed big = SK_FixedMax;
304#endif
305
306 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000307 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
308 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
309 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
reed@android.comd4134452011-02-09 02:24:26 +0000310
reed@google.com077910e2011-02-08 21:56:39 +0000311 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000312 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
313 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
314 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000315}
316
reed@android.comed673312009-02-27 16:24:51 +0000317#endif
318
319static void test_muldiv255(skiatest::Reporter* reporter) {
320#ifdef SK_CAN_USE_FLOAT
321 for (int a = 0; a <= 255; a++) {
322 for (int b = 0; b <= 255; b++) {
323 int ab = a * b;
324 float s = ab / 255.0f;
325 int round = (int)floorf(s + 0.5f);
326 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000327
reed@android.comed673312009-02-27 16:24:51 +0000328 int iround = SkMulDiv255Round(a, b);
329 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000330
reed@android.comed673312009-02-27 16:24:51 +0000331 REPORTER_ASSERT(reporter, iround == round);
332 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000333
reed@android.comed673312009-02-27 16:24:51 +0000334 REPORTER_ASSERT(reporter, itrunc <= iround);
335 REPORTER_ASSERT(reporter, iround <= a);
336 REPORTER_ASSERT(reporter, iround <= b);
337 }
338 }
339#endif
340}
341
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000342static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
343 for (int c = 0; c <= 255; c++) {
344 for (int a = 0; a <= 255; a++) {
345 int product = (c * a + 255);
346 int expected_ceiling = (product + (product >> 8)) >> 8;
347 int webkit_ceiling = (c * a + 254) / 255;
348 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
349 int skia_ceiling = SkMulDiv255Ceiling(c, a);
350 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
351 }
352 }
353}
354
reed@android.comf0ad0862010-02-09 19:18:38 +0000355static void test_copysign(skiatest::Reporter* reporter) {
356 static const int32_t gTriples[] = {
357 // x, y, expected result
358 0, 0, 0,
359 0, 1, 0,
360 0, -1, 0,
361 1, 0, 1,
362 1, 1, 1,
363 1, -1, -1,
364 -1, 0, 1,
365 -1, 1, 1,
366 -1, -1, -1,
367 };
368 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
369 REPORTER_ASSERT(reporter,
370 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
371#ifdef SK_CAN_USE_FLOAT
372 float x = (float)gTriples[i];
373 float y = (float)gTriples[i+1];
374 float expected = (float)gTriples[i+2];
375 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
376#endif
377 }
378
379 SkRandom rand;
380 for (int j = 0; j < 1000; j++) {
381 int ix = rand.nextS();
382 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
383 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
384 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
385 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
386
387 SkScalar sx = rand.nextSScalar1();
388 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
389 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
390 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
391 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
392 }
393}
394
reed@android.com80e39a72009-04-02 16:59:40 +0000395static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000396 int i;
397 int32_t x;
398 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000399
reed@android.comed673312009-02-27 16:24:51 +0000400 // these should assert
401#if 0
402 SkToS8(128);
403 SkToS8(-129);
404 SkToU8(256);
405 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000406
reed@android.comed673312009-02-27 16:24:51 +0000407 SkToS16(32768);
408 SkToS16(-32769);
409 SkToU16(65536);
410 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000411
reed@android.comed673312009-02-27 16:24:51 +0000412 if (sizeof(size_t) > 4) {
413 SkToS32(4*1024*1024);
414 SkToS32(-4*1024*1024);
415 SkToU32(5*1024*1024);
416 SkToU32(-5);
417 }
418#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000419
reed@android.comed673312009-02-27 16:24:51 +0000420 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000421 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000422 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000423
reed@android.comed673312009-02-27 16:24:51 +0000424 {
425 SkScalar x = SK_ScalarNaN;
426 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
427 }
reed@android.com80e39a72009-04-02 16:59:40 +0000428
reed@android.comed673312009-02-27 16:24:51 +0000429 for (i = 1; i <= 10; i++) {
430 x = SkCubeRootBits(i*i*i, 11);
431 REPORTER_ASSERT(reporter, x == i);
432 }
reed@android.com80e39a72009-04-02 16:59:40 +0000433
reed@android.comed673312009-02-27 16:24:51 +0000434 x = SkFixedSqrt(SK_Fixed1);
435 REPORTER_ASSERT(reporter, x == SK_Fixed1);
436 x = SkFixedSqrt(SK_Fixed1/4);
437 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
438 x = SkFixedSqrt(SK_Fixed1*4);
439 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000440
reed@android.comed673312009-02-27 16:24:51 +0000441 x = SkFractSqrt(SK_Fract1);
442 REPORTER_ASSERT(reporter, x == SK_Fract1);
443 x = SkFractSqrt(SK_Fract1/4);
444 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
445 x = SkFractSqrt(SK_Fract1/16);
446 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000447
reed@android.comed673312009-02-27 16:24:51 +0000448 for (i = 1; i < 100; i++) {
449 x = SkFixedSqrt(SK_Fixed1 * i * i);
450 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
451 }
reed@android.com80e39a72009-04-02 16:59:40 +0000452
reed@android.comed673312009-02-27 16:24:51 +0000453 for (i = 0; i < 1000; i++) {
454 int value = rand.nextS16();
455 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000456
reed@android.comed673312009-02-27 16:24:51 +0000457 int clamp = SkClampMax(value, max);
458 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
459 REPORTER_ASSERT(reporter, clamp == clamp2);
460 }
reed@android.com80e39a72009-04-02 16:59:40 +0000461
reed@android.come72fee52009-11-16 14:52:01 +0000462 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000463 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000464
tomhudson@google.com75589252012-04-10 17:42:21 +0000465 // These random values are being treated as 32-bit-patterns, not as
466 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000467 p.setLength((SkScalar) rand.nextS(),
468 (SkScalar) rand.nextS(),
469 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000470 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000471 p.setLength((SkScalar) (rand.nextS() >> 13),
472 (SkScalar) (rand.nextS() >> 13),
473 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000474 check_length(reporter, p, SK_Scalar1);
475 }
reed@android.com80e39a72009-04-02 16:59:40 +0000476
reed@android.comed673312009-02-27 16:24:51 +0000477 {
478 SkFixed result = SkFixedDiv(100, 100);
479 REPORTER_ASSERT(reporter, result == SK_Fixed1);
480 result = SkFixedDiv(1, SK_Fixed1);
481 REPORTER_ASSERT(reporter, result == 1);
482 }
reed@android.com80e39a72009-04-02 16:59:40 +0000483
reed@android.comed673312009-02-27 16:24:51 +0000484#ifdef SK_CAN_USE_FLOAT
485 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000486 unittest_isfinite(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000487#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000488
reed@android.comed673312009-02-27 16:24:51 +0000489#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000490 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000491 SkFixed numer = rand.nextS();
492 SkFixed denom = rand.nextS();
493 SkFixed result = SkFixedDiv(numer, denom);
494 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000495
reed@android.comed673312009-02-27 16:24:51 +0000496 (void)SkCLZ(numer);
497 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000498
reed@android.comed673312009-02-27 16:24:51 +0000499 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
500 if (check > SK_MaxS32) {
501 check = SK_MaxS32;
502 } else if (check < -SK_MaxS32) {
503 check = SK_MinS32;
504 }
505 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000506
reed@android.comed673312009-02-27 16:24:51 +0000507 result = SkFractDiv(numer, denom);
508 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000509
reed@android.comed673312009-02-27 16:24:51 +0000510 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
511 if (check > SK_MaxS32) {
512 check = SK_MaxS32;
513 } else if (check < -SK_MaxS32) {
514 check = SK_MinS32;
515 }
516 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000517
reed@android.comed673312009-02-27 16:24:51 +0000518 // make them <= 2^24, so we don't overflow in fixmul
519 numer = numer << 8 >> 8;
520 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000521
reed@android.comed673312009-02-27 16:24:51 +0000522 result = SkFixedMul(numer, denom);
523 SkFixed r2 = symmetric_fixmul(numer, denom);
524 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000525
reed@android.comed673312009-02-27 16:24:51 +0000526 result = SkFixedMul(numer, numer);
527 r2 = SkFixedSquare(numer);
528 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000529
reed@android.comed673312009-02-27 16:24:51 +0000530#ifdef SK_CAN_USE_FLOAT
531 if (numer >= 0 && denom >= 0) {
532 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000533 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
534 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000535 SkFixed mean2 = SkFloatToFixed(fm);
536 int diff = SkAbs32(mean - mean2);
537 REPORTER_ASSERT(reporter, diff <= 1);
538 }
reed@android.com80e39a72009-04-02 16:59:40 +0000539
reed@android.comed673312009-02-27 16:24:51 +0000540 {
541 SkFixed mod = SkFixedMod(numer, denom);
542 float n = SkFixedToFloat(numer);
543 float d = SkFixedToFloat(denom);
544 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000545 // ensure the same sign
546 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000547 int diff = SkAbs32(mod - SkFloatToFixed(m));
548 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
549 }
550#endif
551 }
552#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000553
reed@android.comed673312009-02-27 16:24:51 +0000554#ifdef SK_CAN_USE_FLOAT
reed@android.come72fee52009-11-16 14:52:01 +0000555 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000556 SkFract x = rand.nextU() >> 1;
557 double xx = (double)x / SK_Fract1;
558 SkFract xr = SkFractSqrt(x);
559 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000560 REPORTER_ASSERT(reporter, xr == check ||
561 xr == check-1 ||
562 xr == check+1);
563
reed@android.comed673312009-02-27 16:24:51 +0000564 xr = SkFixedSqrt(x);
565 xx = (double)x / SK_Fixed1;
566 check = SkFloatToFixed(sqrt(xx));
567 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000568
reed@android.comed673312009-02-27 16:24:51 +0000569 xr = SkSqrt32(x);
570 xx = (double)x;
571 check = (int32_t)sqrt(xx);
572 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
573 }
574#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000575
reed@android.comed673312009-02-27 16:24:51 +0000576#if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT)
577 {
578 SkFixed s, c;
579 s = SkFixedSinCos(0, &c);
580 REPORTER_ASSERT(reporter, s == 0);
581 REPORTER_ASSERT(reporter, c == SK_Fixed1);
582 }
reed@android.com80e39a72009-04-02 16:59:40 +0000583
reed@android.comed673312009-02-27 16:24:51 +0000584 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000585 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000586 SkFixed rads = rand.nextS() >> 10;
587 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000588
reed@android.comed673312009-02-27 16:24:51 +0000589 SkFixed s, c;
590 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000591
reed@android.comed673312009-02-27 16:24:51 +0000592 double fs = sin(frads);
593 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000594
reed@android.comed673312009-02-27 16:24:51 +0000595 SkFixed is = SkFloatToFixed(fs);
596 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000597
reed@android.comed673312009-02-27 16:24:51 +0000598 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
599 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
600 }
601 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
602#endif
reed@google.com772813a2011-03-30 22:34:45 +0000603
reed@google.com0abb4992011-10-06 20:04:36 +0000604#ifdef SK_SCALAR_IS_FLOAT
605 test_blend(reporter);
606#endif
reed@google.com8d7e39c2011-11-09 17:12:08 +0000607
reed@google.coma7d74612012-05-30 12:30:09 +0000608#ifdef SK_CAN_USE_FLOAT
609 test_floor(reporter);
610#endif
611
reed@google.com8d7e39c2011-11-09 17:12:08 +0000612 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000613 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@android.comed673312009-02-27 16:24:51 +0000614}
615
reed@android.comd8730ea2009-02-27 22:06:06 +0000616#include "TestClassDef.h"
617DEFINE_TESTCLASS("Math", MathTestClass, TestMath)