blob: b41c47147dcc2cb684e844f397c063e86741dfa8 [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) {
reed@android.comed673312009-02-27 16:24:51 +0000166 float x = SkScalarToFloat(p.fX);
167 float y = SkScalarToFloat(p.fY);
168 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000169
reed@android.comed673312009-02-27 16:24:51 +0000170 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000171
reed@android.comed673312009-02-27 16:24:51 +0000172 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000173}
174
reed@android.comed673312009-02-27 16:24:51 +0000175static float nextFloat(SkRandom& rand) {
176 SkFloatIntUnion data;
177 data.fSignBitInt = rand.nextU();
178 return data.fFloat;
179}
180
181/* returns true if a == b as resulting from (int)x. Since it is undefined
182 what to do if the float exceeds 2^32-1, we check for that explicitly.
183 */
184static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
185 if (!(x == x)) { // NAN
186 return si == SK_MaxS32 || si == SK_MinS32;
187 }
188 // for out of range, C is undefined, but skia always should return NaN32
189 if (x > SK_MaxS32) {
190 return si == SK_MaxS32;
191 }
192 if (x < -SK_MaxS32) {
193 return si == SK_MinS32;
194 }
195 return si == ni;
196}
197
198static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
199 float x, uint32_t ni, uint32_t si) {
200 if (!equal_float_native_skia(x, ni, si)) {
201 SkString desc;
202 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si);
203 reporter->reportFailed(desc);
204 }
205}
206
207static void test_float_cast(skiatest::Reporter* reporter, float x) {
208 int ix = (int)x;
209 int iix = SkFloatToIntCast(x);
210 assert_float_equal(reporter, "cast", x, ix, iix);
211}
212
213static void test_float_floor(skiatest::Reporter* reporter, float x) {
214 int ix = (int)floor(x);
215 int iix = SkFloatToIntFloor(x);
216 assert_float_equal(reporter, "floor", x, ix, iix);
217}
218
219static void test_float_round(skiatest::Reporter* reporter, float x) {
220 double xx = x + 0.5; // need intermediate double to avoid temp loss
221 int ix = (int)floor(xx);
222 int iix = SkFloatToIntRound(x);
223 assert_float_equal(reporter, "round", x, ix, iix);
224}
225
226static void test_float_ceil(skiatest::Reporter* reporter, float x) {
227 int ix = (int)ceil(x);
228 int iix = SkFloatToIntCeil(x);
229 assert_float_equal(reporter, "ceil", x, ix, iix);
230}
231
232static void test_float_conversions(skiatest::Reporter* reporter, float x) {
233 test_float_cast(reporter, x);
234 test_float_floor(reporter, x);
235 test_float_round(reporter, x);
236 test_float_ceil(reporter, x);
237}
238
239static void test_int2float(skiatest::Reporter* reporter, int ival) {
240 float x0 = (float)ival;
241 float x1 = SkIntToFloatCast(ival);
242 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
243 REPORTER_ASSERT(reporter, x0 == x1);
244 REPORTER_ASSERT(reporter, x0 == x2);
245}
246
247static void unittest_fastfloat(skiatest::Reporter* reporter) {
248 SkRandom rand;
249 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000250
reed@android.comed673312009-02-27 16:24:51 +0000251 static const float gFloats[] = {
252 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
253 0.000000001f, 1000000000.f, // doesn't overflow
254 0.0000000001f, 10000000000.f // does overflow
255 };
256 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000257 test_float_conversions(reporter, gFloats[i]);
258 test_float_conversions(reporter, -gFloats[i]);
259 }
reed@android.com80e39a72009-04-02 16:59:40 +0000260
reed@android.comed673312009-02-27 16:24:51 +0000261 for (int outer = 0; outer < 100; outer++) {
262 rand.setSeed(outer);
263 for (i = 0; i < 100000; i++) {
264 float x = nextFloat(rand);
265 test_float_conversions(reporter, x);
266 }
reed@android.com80e39a72009-04-02 16:59:40 +0000267
reed@android.comed673312009-02-27 16:24:51 +0000268 test_int2float(reporter, 0);
269 test_int2float(reporter, 1);
270 test_int2float(reporter, -1);
271 for (i = 0; i < 100000; i++) {
272 // for now only test ints that are 24bits or less, since we don't
273 // round (down) large ints the same as IEEE...
274 int ival = rand.nextU() & 0xFFFFFF;
275 test_int2float(reporter, ival);
276 test_int2float(reporter, -ival);
277 }
278 }
279}
280
reed@android.comd4134452011-02-09 02:24:26 +0000281#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000282static float make_zero() {
283 return sk_float_sin(0);
284}
reed@android.comd4134452011-02-09 02:24:26 +0000285#endif
reed@google.com077910e2011-02-08 21:56:39 +0000286
287static void unittest_isfinite(skiatest::Reporter* reporter) {
288#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000289 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000290 float inf = 1.0f / make_zero();
291 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000292
reed@google.com077910e2011-02-08 21:56:39 +0000293 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000294 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
295 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
296 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
297#else
298 SkFixed nan = SK_FixedNaN;
299 SkFixed big = SK_FixedMax;
300#endif
301
302 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000303 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
304 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
305 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
reed@android.comd4134452011-02-09 02:24:26 +0000306
reed@google.com077910e2011-02-08 21:56:39 +0000307 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000308 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
309 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
310 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000311}
312
reed@android.comed673312009-02-27 16:24:51 +0000313static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000314 for (int a = 0; a <= 255; a++) {
315 for (int b = 0; b <= 255; b++) {
316 int ab = a * b;
317 float s = ab / 255.0f;
318 int round = (int)floorf(s + 0.5f);
319 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000320
reed@android.comed673312009-02-27 16:24:51 +0000321 int iround = SkMulDiv255Round(a, b);
322 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000323
reed@android.comed673312009-02-27 16:24:51 +0000324 REPORTER_ASSERT(reporter, iround == round);
325 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000326
reed@android.comed673312009-02-27 16:24:51 +0000327 REPORTER_ASSERT(reporter, itrunc <= iround);
328 REPORTER_ASSERT(reporter, iround <= a);
329 REPORTER_ASSERT(reporter, iround <= b);
330 }
331 }
reed@android.comed673312009-02-27 16:24:51 +0000332}
333
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000334static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
335 for (int c = 0; c <= 255; c++) {
336 for (int a = 0; a <= 255; a++) {
337 int product = (c * a + 255);
338 int expected_ceiling = (product + (product >> 8)) >> 8;
339 int webkit_ceiling = (c * a + 254) / 255;
340 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
341 int skia_ceiling = SkMulDiv255Ceiling(c, a);
342 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
343 }
344 }
345}
346
reed@android.comf0ad0862010-02-09 19:18:38 +0000347static void test_copysign(skiatest::Reporter* reporter) {
348 static const int32_t gTriples[] = {
349 // x, y, expected result
350 0, 0, 0,
351 0, 1, 0,
352 0, -1, 0,
353 1, 0, 1,
354 1, 1, 1,
355 1, -1, -1,
356 -1, 0, 1,
357 -1, 1, 1,
358 -1, -1, -1,
359 };
360 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
361 REPORTER_ASSERT(reporter,
362 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000363 float x = (float)gTriples[i];
364 float y = (float)gTriples[i+1];
365 float expected = (float)gTriples[i+2];
366 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000367 }
368
369 SkRandom rand;
370 for (int j = 0; j < 1000; j++) {
371 int ix = rand.nextS();
372 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
373 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
374 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
375 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
376
377 SkScalar sx = rand.nextSScalar1();
378 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
379 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
380 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
381 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
382 }
383}
384
reed@android.com80e39a72009-04-02 16:59:40 +0000385static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000386 int i;
387 int32_t x;
388 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000389
reed@android.comed673312009-02-27 16:24:51 +0000390 // these should assert
391#if 0
392 SkToS8(128);
393 SkToS8(-129);
394 SkToU8(256);
395 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000396
reed@android.comed673312009-02-27 16:24:51 +0000397 SkToS16(32768);
398 SkToS16(-32769);
399 SkToU16(65536);
400 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000401
reed@android.comed673312009-02-27 16:24:51 +0000402 if (sizeof(size_t) > 4) {
403 SkToS32(4*1024*1024);
404 SkToS32(-4*1024*1024);
405 SkToU32(5*1024*1024);
406 SkToU32(-5);
407 }
408#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000409
reed@android.comed673312009-02-27 16:24:51 +0000410 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000411 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000412 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000413
reed@android.comed673312009-02-27 16:24:51 +0000414 {
415 SkScalar x = SK_ScalarNaN;
416 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
417 }
reed@android.com80e39a72009-04-02 16:59:40 +0000418
reed@android.comed673312009-02-27 16:24:51 +0000419 for (i = 1; i <= 10; i++) {
420 x = SkCubeRootBits(i*i*i, 11);
421 REPORTER_ASSERT(reporter, x == i);
422 }
reed@android.com80e39a72009-04-02 16:59:40 +0000423
reed@android.comed673312009-02-27 16:24:51 +0000424 x = SkFixedSqrt(SK_Fixed1);
425 REPORTER_ASSERT(reporter, x == SK_Fixed1);
426 x = SkFixedSqrt(SK_Fixed1/4);
427 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
428 x = SkFixedSqrt(SK_Fixed1*4);
429 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000430
reed@android.comed673312009-02-27 16:24:51 +0000431 x = SkFractSqrt(SK_Fract1);
432 REPORTER_ASSERT(reporter, x == SK_Fract1);
433 x = SkFractSqrt(SK_Fract1/4);
434 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
435 x = SkFractSqrt(SK_Fract1/16);
436 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000437
reed@android.comed673312009-02-27 16:24:51 +0000438 for (i = 1; i < 100; i++) {
439 x = SkFixedSqrt(SK_Fixed1 * i * i);
440 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
441 }
reed@android.com80e39a72009-04-02 16:59:40 +0000442
reed@android.comed673312009-02-27 16:24:51 +0000443 for (i = 0; i < 1000; i++) {
444 int value = rand.nextS16();
445 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000446
reed@android.comed673312009-02-27 16:24:51 +0000447 int clamp = SkClampMax(value, max);
448 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
449 REPORTER_ASSERT(reporter, clamp == clamp2);
450 }
reed@android.com80e39a72009-04-02 16:59:40 +0000451
reed@android.come72fee52009-11-16 14:52:01 +0000452 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000453 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000454
tomhudson@google.com75589252012-04-10 17:42:21 +0000455 // These random values are being treated as 32-bit-patterns, not as
456 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000457 p.setLength((SkScalar) rand.nextS(),
458 (SkScalar) rand.nextS(),
459 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000460 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000461 p.setLength((SkScalar) (rand.nextS() >> 13),
462 (SkScalar) (rand.nextS() >> 13),
463 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000464 check_length(reporter, p, SK_Scalar1);
465 }
reed@android.com80e39a72009-04-02 16:59:40 +0000466
reed@android.comed673312009-02-27 16:24:51 +0000467 {
468 SkFixed result = SkFixedDiv(100, 100);
469 REPORTER_ASSERT(reporter, result == SK_Fixed1);
470 result = SkFixedDiv(1, SK_Fixed1);
471 REPORTER_ASSERT(reporter, result == 1);
472 }
reed@android.com80e39a72009-04-02 16:59:40 +0000473
reed@android.comed673312009-02-27 16:24:51 +0000474 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000475 unittest_isfinite(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000476
reed@android.comed673312009-02-27 16:24:51 +0000477#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000478 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000479 SkFixed numer = rand.nextS();
480 SkFixed denom = rand.nextS();
481 SkFixed result = SkFixedDiv(numer, denom);
482 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000483
reed@android.comed673312009-02-27 16:24:51 +0000484 (void)SkCLZ(numer);
485 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000486
reed@android.comed673312009-02-27 16:24:51 +0000487 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
488 if (check > SK_MaxS32) {
489 check = SK_MaxS32;
490 } else if (check < -SK_MaxS32) {
491 check = SK_MinS32;
492 }
493 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000494
reed@android.comed673312009-02-27 16:24:51 +0000495 result = SkFractDiv(numer, denom);
496 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000497
reed@android.comed673312009-02-27 16:24:51 +0000498 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
499 if (check > SK_MaxS32) {
500 check = SK_MaxS32;
501 } else if (check < -SK_MaxS32) {
502 check = SK_MinS32;
503 }
504 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000505
reed@android.comed673312009-02-27 16:24:51 +0000506 // make them <= 2^24, so we don't overflow in fixmul
507 numer = numer << 8 >> 8;
508 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000509
reed@android.comed673312009-02-27 16:24:51 +0000510 result = SkFixedMul(numer, denom);
511 SkFixed r2 = symmetric_fixmul(numer, denom);
512 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000513
reed@android.comed673312009-02-27 16:24:51 +0000514 result = SkFixedMul(numer, numer);
515 r2 = SkFixedSquare(numer);
516 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000517
reed@android.comed673312009-02-27 16:24:51 +0000518 if (numer >= 0 && denom >= 0) {
519 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000520 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
521 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000522 SkFixed mean2 = SkFloatToFixed(fm);
523 int diff = SkAbs32(mean - mean2);
524 REPORTER_ASSERT(reporter, diff <= 1);
525 }
reed@android.com80e39a72009-04-02 16:59:40 +0000526
reed@android.comed673312009-02-27 16:24:51 +0000527 {
528 SkFixed mod = SkFixedMod(numer, denom);
529 float n = SkFixedToFloat(numer);
530 float d = SkFixedToFloat(denom);
531 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000532 // ensure the same sign
533 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000534 int diff = SkAbs32(mod - SkFloatToFixed(m));
535 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
536 }
reed@android.comed673312009-02-27 16:24:51 +0000537 }
538#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000539
reed@android.come72fee52009-11-16 14:52:01 +0000540 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000541 SkFract x = rand.nextU() >> 1;
542 double xx = (double)x / SK_Fract1;
543 SkFract xr = SkFractSqrt(x);
544 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000545 REPORTER_ASSERT(reporter, xr == check ||
546 xr == check-1 ||
547 xr == check+1);
548
reed@android.comed673312009-02-27 16:24:51 +0000549 xr = SkFixedSqrt(x);
550 xx = (double)x / SK_Fixed1;
551 check = SkFloatToFixed(sqrt(xx));
552 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000553
reed@android.comed673312009-02-27 16:24:51 +0000554 xr = SkSqrt32(x);
555 xx = (double)x;
556 check = (int32_t)sqrt(xx);
557 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
558 }
reed@android.com80e39a72009-04-02 16:59:40 +0000559
reed@google.com7886ad32012-06-11 21:21:26 +0000560#if !defined(SK_SCALAR_IS_FLOAT)
reed@android.comed673312009-02-27 16:24:51 +0000561 {
562 SkFixed s, c;
563 s = SkFixedSinCos(0, &c);
564 REPORTER_ASSERT(reporter, s == 0);
565 REPORTER_ASSERT(reporter, c == SK_Fixed1);
566 }
reed@android.com80e39a72009-04-02 16:59:40 +0000567
reed@android.comed673312009-02-27 16:24:51 +0000568 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000569 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000570 SkFixed rads = rand.nextS() >> 10;
571 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000572
reed@android.comed673312009-02-27 16:24:51 +0000573 SkFixed s, c;
574 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000575
reed@android.comed673312009-02-27 16:24:51 +0000576 double fs = sin(frads);
577 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000578
reed@android.comed673312009-02-27 16:24:51 +0000579 SkFixed is = SkFloatToFixed(fs);
580 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000581
reed@android.comed673312009-02-27 16:24:51 +0000582 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
583 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
584 }
585 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
586#endif
reed@google.com772813a2011-03-30 22:34:45 +0000587
reed@google.com0abb4992011-10-06 20:04:36 +0000588#ifdef SK_SCALAR_IS_FLOAT
589 test_blend(reporter);
590#endif
reed@google.com8d7e39c2011-11-09 17:12:08 +0000591
reed@google.coma7d74612012-05-30 12:30:09 +0000592 test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000593
reed@google.com8d7e39c2011-11-09 17:12:08 +0000594 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000595 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@android.comed673312009-02-27 16:24:51 +0000596}
597
reed@android.comd8730ea2009-02-27 22:06:06 +0000598#include "TestClassDef.h"
599DEFINE_TESTCLASS("Math", MathTestClass, TestMath)