blob: e4596da8aa696887dfcc17b59385bfc8976d299f [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.comc3b69722012-05-30 12:43:59 +000020#ifdef SK_BUILD_FOR_WIN
21 return sk_float_floor(x);
22#else
reed@google.coma7d74612012-05-30 12:30:09 +000023 float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
24 return (x + big) - big;
reed@google.comc3b69722012-05-30 12:43:59 +000025#endif
reed@google.coma7d74612012-05-30 12:30:09 +000026}
27
28static float std_floor(float x) {
29 return sk_float_floor(x);
30}
31
32static void test_floor_value(skiatest::Reporter* reporter, float value) {
33 float fast = fast_floor(value);
34 float std = std_floor(value);
35 REPORTER_ASSERT(reporter, std == fast);
36// SkDebugf("value[%1.9f] std[%g] fast[%g] equal[%d]\n",
37// value, std, fast, std == fast);
38}
39
40static void test_floor(skiatest::Reporter* reporter) {
41 static const float gVals[] = {
42 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
43 };
44
45 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
46 test_floor_value(reporter, gVals[i]);
47// test_floor_value(reporter, -gVals[i]);
48 }
49}
50
51///////////////////////////////////////////////////////////////////////////////
52
reed@google.com0abb4992011-10-06 20:04:36 +000053static float float_blend(int src, int dst, float unit) {
54 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +000055}
56
reed@google.com8d7e39c2011-11-09 17:12:08 +000057static int blend31(int src, int dst, int a31) {
58 return dst + ((src - dst) * a31 * 2114 >> 16);
59 // return dst + ((src - dst) * a31 * 33 >> 10);
60}
61
62static int blend31_slow(int src, int dst, int a31) {
63 int prod = src * a31 + (31 - a31) * dst + 16;
64 prod = (prod + (prod >> 5)) >> 5;
65 return prod;
66}
67
68static int blend31_round(int src, int dst, int a31) {
69 int prod = (src - dst) * a31 + 16;
70 prod = (prod + (prod >> 5)) >> 5;
71 return dst + prod;
72}
73
74static int blend31_old(int src, int dst, int a31) {
75 a31 += a31 >> 4;
76 return dst + ((src - dst) * a31 >> 5);
77}
78
79static void test_blend31() {
80 int failed = 0;
81 int death = 0;
82 for (int src = 0; src <= 255; src++) {
83 for (int dst = 0; dst <= 255; dst++) {
84 for (int a = 0; a <= 31; a++) {
85// int r0 = blend31(src, dst, a);
86// int r0 = blend31_round(src, dst, a);
87// int r0 = blend31_old(src, dst, a);
88 int r0 = blend31_slow(src, dst, a);
89
90 float f = float_blend(src, dst, a / 31.f);
91 int r1 = (int)f;
92 int r2 = SkScalarRoundToInt(SkFloatToScalar(f));
93
94 if (r0 != r1 && r0 != r2) {
95 printf("src:%d dst:%d a:%d result:%d float:%g\n",
96 src, dst, a, r0, f);
97 failed += 1;
98 }
99 if (r0 > 255) {
100 death += 1;
101 printf("death src:%d dst:%d a:%d result:%d float:%g\n",
102 src, dst, a, r0, f);
103 }
104 }
105 }
106 }
107 SkDebugf("---- failed %d death %d\n", failed, death);
108}
109
reed@google.com0abb4992011-10-06 20:04:36 +0000110static void test_blend(skiatest::Reporter* reporter) {
111 for (int src = 0; src <= 255; src++) {
112 for (int dst = 0; dst <= 255; dst++) {
113 for (int a = 0; a <= 255; a++) {
114 int r0 = SkAlphaBlend255(src, dst, a);
115 float f1 = float_blend(src, dst, a / 255.f);
reed@google.com111c19b2011-10-06 20:13:22 +0000116 int r1 = SkScalarRoundToInt(SkFloatToScalar(f1));
reed@google.com772813a2011-03-30 22:34:45 +0000117
reed@google.com0abb4992011-10-06 20:04:36 +0000118 if (r0 != r1) {
119 float diff = sk_float_abs(f1 - r1);
120 diff = sk_float_abs(diff - 0.5f);
121 if (diff > (1 / 255.f)) {
122#ifdef SK_DEBUG
123 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
124 src, dst, a, r0, f1);
125#endif
126 REPORTER_ASSERT(reporter, false);
127 }
128 }
reed@google.com772813a2011-03-30 22:34:45 +0000129 }
130 }
131 }
132}
reed@google.com772813a2011-03-30 22:34:45 +0000133
reed@android.comed673312009-02-27 16:24:51 +0000134#if defined(SkLONGLONG)
135static int symmetric_fixmul(int a, int b) {
136 int sa = SkExtractSign(a);
137 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +0000138
reed@android.comed673312009-02-27 16:24:51 +0000139 a = SkApplySign(a, sa);
140 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +0000141
reed@android.comed673312009-02-27 16:24:51 +0000142#if 1
143 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +0000144
reed@android.comed673312009-02-27 16:24:51 +0000145 return SkApplySign(c, sa ^ sb);
146#else
147 SkLONGLONG ab = (SkLONGLONG)a * b;
148 if (sa ^ sb) {
149 ab = -ab;
150 }
151 return ab >> 16;
152#endif
153}
154#endif
155
156static void check_length(skiatest::Reporter* reporter,
157 const SkPoint& p, SkScalar targetLen) {
158#ifdef SK_CAN_USE_FLOAT
159 float x = SkScalarToFloat(p.fX);
160 float y = SkScalarToFloat(p.fY);
161 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000162
reed@android.comed673312009-02-27 16:24:51 +0000163 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000164
reed@android.comed673312009-02-27 16:24:51 +0000165 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
166#endif
167}
168
169#if defined(SK_CAN_USE_FLOAT)
170
171static float nextFloat(SkRandom& rand) {
172 SkFloatIntUnion data;
173 data.fSignBitInt = rand.nextU();
174 return data.fFloat;
175}
176
177/* returns true if a == b as resulting from (int)x. Since it is undefined
178 what to do if the float exceeds 2^32-1, we check for that explicitly.
179 */
180static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
181 if (!(x == x)) { // NAN
182 return si == SK_MaxS32 || si == SK_MinS32;
183 }
184 // for out of range, C is undefined, but skia always should return NaN32
185 if (x > SK_MaxS32) {
186 return si == SK_MaxS32;
187 }
188 if (x < -SK_MaxS32) {
189 return si == SK_MinS32;
190 }
191 return si == ni;
192}
193
194static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
195 float x, uint32_t ni, uint32_t si) {
196 if (!equal_float_native_skia(x, ni, si)) {
197 SkString desc;
198 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si);
199 reporter->reportFailed(desc);
200 }
201}
202
203static void test_float_cast(skiatest::Reporter* reporter, float x) {
204 int ix = (int)x;
205 int iix = SkFloatToIntCast(x);
206 assert_float_equal(reporter, "cast", x, ix, iix);
207}
208
209static void test_float_floor(skiatest::Reporter* reporter, float x) {
210 int ix = (int)floor(x);
211 int iix = SkFloatToIntFloor(x);
212 assert_float_equal(reporter, "floor", x, ix, iix);
213}
214
215static void test_float_round(skiatest::Reporter* reporter, float x) {
216 double xx = x + 0.5; // need intermediate double to avoid temp loss
217 int ix = (int)floor(xx);
218 int iix = SkFloatToIntRound(x);
219 assert_float_equal(reporter, "round", x, ix, iix);
220}
221
222static void test_float_ceil(skiatest::Reporter* reporter, float x) {
223 int ix = (int)ceil(x);
224 int iix = SkFloatToIntCeil(x);
225 assert_float_equal(reporter, "ceil", x, ix, iix);
226}
227
228static void test_float_conversions(skiatest::Reporter* reporter, float x) {
229 test_float_cast(reporter, x);
230 test_float_floor(reporter, x);
231 test_float_round(reporter, x);
232 test_float_ceil(reporter, x);
233}
234
235static void test_int2float(skiatest::Reporter* reporter, int ival) {
236 float x0 = (float)ival;
237 float x1 = SkIntToFloatCast(ival);
238 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
239 REPORTER_ASSERT(reporter, x0 == x1);
240 REPORTER_ASSERT(reporter, x0 == x2);
241}
242
243static void unittest_fastfloat(skiatest::Reporter* reporter) {
244 SkRandom rand;
245 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000246
reed@android.comed673312009-02-27 16:24:51 +0000247 static const float gFloats[] = {
248 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
249 0.000000001f, 1000000000.f, // doesn't overflow
250 0.0000000001f, 10000000000.f // does overflow
251 };
252 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000253 test_float_conversions(reporter, gFloats[i]);
254 test_float_conversions(reporter, -gFloats[i]);
255 }
reed@android.com80e39a72009-04-02 16:59:40 +0000256
reed@android.comed673312009-02-27 16:24:51 +0000257 for (int outer = 0; outer < 100; outer++) {
258 rand.setSeed(outer);
259 for (i = 0; i < 100000; i++) {
260 float x = nextFloat(rand);
261 test_float_conversions(reporter, x);
262 }
reed@android.com80e39a72009-04-02 16:59:40 +0000263
reed@android.comed673312009-02-27 16:24:51 +0000264 test_int2float(reporter, 0);
265 test_int2float(reporter, 1);
266 test_int2float(reporter, -1);
267 for (i = 0; i < 100000; i++) {
268 // for now only test ints that are 24bits or less, since we don't
269 // round (down) large ints the same as IEEE...
270 int ival = rand.nextU() & 0xFFFFFF;
271 test_int2float(reporter, ival);
272 test_int2float(reporter, -ival);
273 }
274 }
275}
276
reed@android.comd4134452011-02-09 02:24:26 +0000277#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000278static float make_zero() {
279 return sk_float_sin(0);
280}
reed@android.comd4134452011-02-09 02:24:26 +0000281#endif
reed@google.com077910e2011-02-08 21:56:39 +0000282
283static void unittest_isfinite(skiatest::Reporter* reporter) {
284#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000285 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000286 float inf = 1.0f / make_zero();
287 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000288
reed@google.com077910e2011-02-08 21:56:39 +0000289 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000290 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
291 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
292 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
293#else
294 SkFixed nan = SK_FixedNaN;
295 SkFixed big = SK_FixedMax;
296#endif
297
298 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000299 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
300 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
301 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
reed@android.comd4134452011-02-09 02:24:26 +0000302
reed@google.com077910e2011-02-08 21:56:39 +0000303 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000304 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
305 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
306 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000307}
308
reed@android.comed673312009-02-27 16:24:51 +0000309#endif
310
311static void test_muldiv255(skiatest::Reporter* reporter) {
312#ifdef SK_CAN_USE_FLOAT
313 for (int a = 0; a <= 255; a++) {
314 for (int b = 0; b <= 255; b++) {
315 int ab = a * b;
316 float s = ab / 255.0f;
317 int round = (int)floorf(s + 0.5f);
318 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000319
reed@android.comed673312009-02-27 16:24:51 +0000320 int iround = SkMulDiv255Round(a, b);
321 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000322
reed@android.comed673312009-02-27 16:24:51 +0000323 REPORTER_ASSERT(reporter, iround == round);
324 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000325
reed@android.comed673312009-02-27 16:24:51 +0000326 REPORTER_ASSERT(reporter, itrunc <= iround);
327 REPORTER_ASSERT(reporter, iround <= a);
328 REPORTER_ASSERT(reporter, iround <= b);
329 }
330 }
331#endif
332}
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]);
363#ifdef SK_CAN_USE_FLOAT
364 float x = (float)gTriples[i];
365 float y = (float)gTriples[i+1];
366 float expected = (float)gTriples[i+2];
367 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
368#endif
369 }
370
371 SkRandom rand;
372 for (int j = 0; j < 1000; j++) {
373 int ix = rand.nextS();
374 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
375 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
376 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
377 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
378
379 SkScalar sx = rand.nextSScalar1();
380 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
381 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
382 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
383 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
384 }
385}
386
reed@android.com80e39a72009-04-02 16:59:40 +0000387static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000388 int i;
389 int32_t x;
390 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000391
reed@android.comed673312009-02-27 16:24:51 +0000392 // these should assert
393#if 0
394 SkToS8(128);
395 SkToS8(-129);
396 SkToU8(256);
397 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000398
reed@android.comed673312009-02-27 16:24:51 +0000399 SkToS16(32768);
400 SkToS16(-32769);
401 SkToU16(65536);
402 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000403
reed@android.comed673312009-02-27 16:24:51 +0000404 if (sizeof(size_t) > 4) {
405 SkToS32(4*1024*1024);
406 SkToS32(-4*1024*1024);
407 SkToU32(5*1024*1024);
408 SkToU32(-5);
409 }
410#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000411
reed@android.comed673312009-02-27 16:24:51 +0000412 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000413 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000414 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000415
reed@android.comed673312009-02-27 16:24:51 +0000416 {
417 SkScalar x = SK_ScalarNaN;
418 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
419 }
reed@android.com80e39a72009-04-02 16:59:40 +0000420
reed@android.comed673312009-02-27 16:24:51 +0000421 for (i = 1; i <= 10; i++) {
422 x = SkCubeRootBits(i*i*i, 11);
423 REPORTER_ASSERT(reporter, x == i);
424 }
reed@android.com80e39a72009-04-02 16:59:40 +0000425
reed@android.comed673312009-02-27 16:24:51 +0000426 x = SkFixedSqrt(SK_Fixed1);
427 REPORTER_ASSERT(reporter, x == SK_Fixed1);
428 x = SkFixedSqrt(SK_Fixed1/4);
429 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
430 x = SkFixedSqrt(SK_Fixed1*4);
431 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000432
reed@android.comed673312009-02-27 16:24:51 +0000433 x = SkFractSqrt(SK_Fract1);
434 REPORTER_ASSERT(reporter, x == SK_Fract1);
435 x = SkFractSqrt(SK_Fract1/4);
436 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
437 x = SkFractSqrt(SK_Fract1/16);
438 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000439
reed@android.comed673312009-02-27 16:24:51 +0000440 for (i = 1; i < 100; i++) {
441 x = SkFixedSqrt(SK_Fixed1 * i * i);
442 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
443 }
reed@android.com80e39a72009-04-02 16:59:40 +0000444
reed@android.comed673312009-02-27 16:24:51 +0000445 for (i = 0; i < 1000; i++) {
446 int value = rand.nextS16();
447 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000448
reed@android.comed673312009-02-27 16:24:51 +0000449 int clamp = SkClampMax(value, max);
450 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
451 REPORTER_ASSERT(reporter, clamp == clamp2);
452 }
reed@android.com80e39a72009-04-02 16:59:40 +0000453
reed@android.come72fee52009-11-16 14:52:01 +0000454 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000455 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000456
tomhudson@google.com75589252012-04-10 17:42:21 +0000457 // These random values are being treated as 32-bit-patterns, not as
458 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000459 p.setLength((SkScalar) rand.nextS(),
460 (SkScalar) rand.nextS(),
461 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000462 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000463 p.setLength((SkScalar) (rand.nextS() >> 13),
464 (SkScalar) (rand.nextS() >> 13),
465 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000466 check_length(reporter, p, SK_Scalar1);
467 }
reed@android.com80e39a72009-04-02 16:59:40 +0000468
reed@android.comed673312009-02-27 16:24:51 +0000469 {
470 SkFixed result = SkFixedDiv(100, 100);
471 REPORTER_ASSERT(reporter, result == SK_Fixed1);
472 result = SkFixedDiv(1, SK_Fixed1);
473 REPORTER_ASSERT(reporter, result == 1);
474 }
reed@android.com80e39a72009-04-02 16:59:40 +0000475
reed@android.comed673312009-02-27 16:24:51 +0000476#ifdef SK_CAN_USE_FLOAT
477 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000478 unittest_isfinite(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000479#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000480
reed@android.comed673312009-02-27 16:24:51 +0000481#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000482 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000483 SkFixed numer = rand.nextS();
484 SkFixed denom = rand.nextS();
485 SkFixed result = SkFixedDiv(numer, denom);
486 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000487
reed@android.comed673312009-02-27 16:24:51 +0000488 (void)SkCLZ(numer);
489 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000490
reed@android.comed673312009-02-27 16:24:51 +0000491 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
492 if (check > SK_MaxS32) {
493 check = SK_MaxS32;
494 } else if (check < -SK_MaxS32) {
495 check = SK_MinS32;
496 }
497 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000498
reed@android.comed673312009-02-27 16:24:51 +0000499 result = SkFractDiv(numer, denom);
500 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000501
reed@android.comed673312009-02-27 16:24:51 +0000502 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
503 if (check > SK_MaxS32) {
504 check = SK_MaxS32;
505 } else if (check < -SK_MaxS32) {
506 check = SK_MinS32;
507 }
508 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000509
reed@android.comed673312009-02-27 16:24:51 +0000510 // make them <= 2^24, so we don't overflow in fixmul
511 numer = numer << 8 >> 8;
512 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000513
reed@android.comed673312009-02-27 16:24:51 +0000514 result = SkFixedMul(numer, denom);
515 SkFixed r2 = symmetric_fixmul(numer, denom);
516 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000517
reed@android.comed673312009-02-27 16:24:51 +0000518 result = SkFixedMul(numer, numer);
519 r2 = SkFixedSquare(numer);
520 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000521
reed@android.comed673312009-02-27 16:24:51 +0000522#ifdef SK_CAN_USE_FLOAT
523 if (numer >= 0 && denom >= 0) {
524 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000525 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
526 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000527 SkFixed mean2 = SkFloatToFixed(fm);
528 int diff = SkAbs32(mean - mean2);
529 REPORTER_ASSERT(reporter, diff <= 1);
530 }
reed@android.com80e39a72009-04-02 16:59:40 +0000531
reed@android.comed673312009-02-27 16:24:51 +0000532 {
533 SkFixed mod = SkFixedMod(numer, denom);
534 float n = SkFixedToFloat(numer);
535 float d = SkFixedToFloat(denom);
536 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000537 // ensure the same sign
538 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000539 int diff = SkAbs32(mod - SkFloatToFixed(m));
540 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
541 }
542#endif
543 }
544#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000545
reed@android.comed673312009-02-27 16:24:51 +0000546#ifdef SK_CAN_USE_FLOAT
reed@android.come72fee52009-11-16 14:52:01 +0000547 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000548 SkFract x = rand.nextU() >> 1;
549 double xx = (double)x / SK_Fract1;
550 SkFract xr = SkFractSqrt(x);
551 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000552 REPORTER_ASSERT(reporter, xr == check ||
553 xr == check-1 ||
554 xr == check+1);
555
reed@android.comed673312009-02-27 16:24:51 +0000556 xr = SkFixedSqrt(x);
557 xx = (double)x / SK_Fixed1;
558 check = SkFloatToFixed(sqrt(xx));
559 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000560
reed@android.comed673312009-02-27 16:24:51 +0000561 xr = SkSqrt32(x);
562 xx = (double)x;
563 check = (int32_t)sqrt(xx);
564 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
565 }
566#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000567
reed@android.comed673312009-02-27 16:24:51 +0000568#if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT)
569 {
570 SkFixed s, c;
571 s = SkFixedSinCos(0, &c);
572 REPORTER_ASSERT(reporter, s == 0);
573 REPORTER_ASSERT(reporter, c == SK_Fixed1);
574 }
reed@android.com80e39a72009-04-02 16:59:40 +0000575
reed@android.comed673312009-02-27 16:24:51 +0000576 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000577 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000578 SkFixed rads = rand.nextS() >> 10;
579 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000580
reed@android.comed673312009-02-27 16:24:51 +0000581 SkFixed s, c;
582 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000583
reed@android.comed673312009-02-27 16:24:51 +0000584 double fs = sin(frads);
585 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000586
reed@android.comed673312009-02-27 16:24:51 +0000587 SkFixed is = SkFloatToFixed(fs);
588 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000589
reed@android.comed673312009-02-27 16:24:51 +0000590 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
591 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
592 }
593 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
594#endif
reed@google.com772813a2011-03-30 22:34:45 +0000595
reed@google.com0abb4992011-10-06 20:04:36 +0000596#ifdef SK_SCALAR_IS_FLOAT
597 test_blend(reporter);
598#endif
reed@google.com8d7e39c2011-11-09 17:12:08 +0000599
reed@google.coma7d74612012-05-30 12:30:09 +0000600#ifdef SK_CAN_USE_FLOAT
601 test_floor(reporter);
602#endif
603
reed@google.com8d7e39c2011-11-09 17:12:08 +0000604 // disable for now
605// test_blend31();
reed@android.comed673312009-02-27 16:24:51 +0000606}
607
reed@android.comd8730ea2009-02-27 22:06:06 +0000608#include "TestClassDef.h"
609DEFINE_TESTCLASS("Math", MathTestClass, TestMath)