blob: 11e47d761332fc737cc3ec9a5a2a485659fdb4e4 [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));
reed@google.coma7d74612012-05-30 12:30:09 +000022 return (x + big) - big;
23}
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
76static void test_blend31() {
77 int failed = 0;
78 int death = 0;
79 for (int src = 0; src <= 255; src++) {
80 for (int dst = 0; dst <= 255; dst++) {
81 for (int a = 0; a <= 31; a++) {
82// int r0 = blend31(src, dst, a);
83// int r0 = blend31_round(src, dst, a);
84// int r0 = blend31_old(src, dst, a);
85 int r0 = blend31_slow(src, dst, a);
86
87 float f = float_blend(src, dst, a / 31.f);
88 int r1 = (int)f;
89 int r2 = SkScalarRoundToInt(SkFloatToScalar(f));
90
91 if (r0 != r1 && r0 != r2) {
92 printf("src:%d dst:%d a:%d result:%d float:%g\n",
93 src, dst, a, r0, f);
94 failed += 1;
95 }
96 if (r0 > 255) {
97 death += 1;
98 printf("death src:%d dst:%d a:%d result:%d float:%g\n",
99 src, dst, a, r0, f);
100 }
101 }
102 }
103 }
104 SkDebugf("---- failed %d death %d\n", failed, death);
105}
106
reed@google.com0abb4992011-10-06 20:04:36 +0000107static void test_blend(skiatest::Reporter* reporter) {
108 for (int src = 0; src <= 255; src++) {
109 for (int dst = 0; dst <= 255; dst++) {
110 for (int a = 0; a <= 255; a++) {
111 int r0 = SkAlphaBlend255(src, dst, a);
112 float f1 = float_blend(src, dst, a / 255.f);
reed@google.com111c19b2011-10-06 20:13:22 +0000113 int r1 = SkScalarRoundToInt(SkFloatToScalar(f1));
reed@google.com772813a2011-03-30 22:34:45 +0000114
reed@google.com0abb4992011-10-06 20:04:36 +0000115 if (r0 != r1) {
116 float diff = sk_float_abs(f1 - r1);
117 diff = sk_float_abs(diff - 0.5f);
118 if (diff > (1 / 255.f)) {
119#ifdef SK_DEBUG
120 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
121 src, dst, a, r0, f1);
122#endif
123 REPORTER_ASSERT(reporter, false);
124 }
125 }
reed@google.com772813a2011-03-30 22:34:45 +0000126 }
127 }
128 }
129}
reed@google.com772813a2011-03-30 22:34:45 +0000130
reed@android.comed673312009-02-27 16:24:51 +0000131#if defined(SkLONGLONG)
132static int symmetric_fixmul(int a, int b) {
133 int sa = SkExtractSign(a);
134 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +0000135
reed@android.comed673312009-02-27 16:24:51 +0000136 a = SkApplySign(a, sa);
137 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +0000138
reed@android.comed673312009-02-27 16:24:51 +0000139#if 1
140 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +0000141
reed@android.comed673312009-02-27 16:24:51 +0000142 return SkApplySign(c, sa ^ sb);
143#else
144 SkLONGLONG ab = (SkLONGLONG)a * b;
145 if (sa ^ sb) {
146 ab = -ab;
147 }
148 return ab >> 16;
149#endif
150}
151#endif
152
153static void check_length(skiatest::Reporter* reporter,
154 const SkPoint& p, SkScalar targetLen) {
155#ifdef SK_CAN_USE_FLOAT
156 float x = SkScalarToFloat(p.fX);
157 float y = SkScalarToFloat(p.fY);
158 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000159
reed@android.comed673312009-02-27 16:24:51 +0000160 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000161
reed@android.comed673312009-02-27 16:24:51 +0000162 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
163#endif
164}
165
166#if defined(SK_CAN_USE_FLOAT)
167
168static float nextFloat(SkRandom& rand) {
169 SkFloatIntUnion data;
170 data.fSignBitInt = rand.nextU();
171 return data.fFloat;
172}
173
174/* returns true if a == b as resulting from (int)x. Since it is undefined
175 what to do if the float exceeds 2^32-1, we check for that explicitly.
176 */
177static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
178 if (!(x == x)) { // NAN
179 return si == SK_MaxS32 || si == SK_MinS32;
180 }
181 // for out of range, C is undefined, but skia always should return NaN32
182 if (x > SK_MaxS32) {
183 return si == SK_MaxS32;
184 }
185 if (x < -SK_MaxS32) {
186 return si == SK_MinS32;
187 }
188 return si == ni;
189}
190
191static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
192 float x, uint32_t ni, uint32_t si) {
193 if (!equal_float_native_skia(x, ni, si)) {
194 SkString desc;
195 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si);
196 reporter->reportFailed(desc);
197 }
198}
199
200static void test_float_cast(skiatest::Reporter* reporter, float x) {
201 int ix = (int)x;
202 int iix = SkFloatToIntCast(x);
203 assert_float_equal(reporter, "cast", x, ix, iix);
204}
205
206static void test_float_floor(skiatest::Reporter* reporter, float x) {
207 int ix = (int)floor(x);
208 int iix = SkFloatToIntFloor(x);
209 assert_float_equal(reporter, "floor", x, ix, iix);
210}
211
212static void test_float_round(skiatest::Reporter* reporter, float x) {
213 double xx = x + 0.5; // need intermediate double to avoid temp loss
214 int ix = (int)floor(xx);
215 int iix = SkFloatToIntRound(x);
216 assert_float_equal(reporter, "round", x, ix, iix);
217}
218
219static void test_float_ceil(skiatest::Reporter* reporter, float x) {
220 int ix = (int)ceil(x);
221 int iix = SkFloatToIntCeil(x);
222 assert_float_equal(reporter, "ceil", x, ix, iix);
223}
224
225static void test_float_conversions(skiatest::Reporter* reporter, float x) {
226 test_float_cast(reporter, x);
227 test_float_floor(reporter, x);
228 test_float_round(reporter, x);
229 test_float_ceil(reporter, x);
230}
231
232static void test_int2float(skiatest::Reporter* reporter, int ival) {
233 float x0 = (float)ival;
234 float x1 = SkIntToFloatCast(ival);
235 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
236 REPORTER_ASSERT(reporter, x0 == x1);
237 REPORTER_ASSERT(reporter, x0 == x2);
238}
239
240static void unittest_fastfloat(skiatest::Reporter* reporter) {
241 SkRandom rand;
242 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000243
reed@android.comed673312009-02-27 16:24:51 +0000244 static const float gFloats[] = {
245 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
246 0.000000001f, 1000000000.f, // doesn't overflow
247 0.0000000001f, 10000000000.f // does overflow
248 };
249 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000250 test_float_conversions(reporter, gFloats[i]);
251 test_float_conversions(reporter, -gFloats[i]);
252 }
reed@android.com80e39a72009-04-02 16:59:40 +0000253
reed@android.comed673312009-02-27 16:24:51 +0000254 for (int outer = 0; outer < 100; outer++) {
255 rand.setSeed(outer);
256 for (i = 0; i < 100000; i++) {
257 float x = nextFloat(rand);
258 test_float_conversions(reporter, x);
259 }
reed@android.com80e39a72009-04-02 16:59:40 +0000260
reed@android.comed673312009-02-27 16:24:51 +0000261 test_int2float(reporter, 0);
262 test_int2float(reporter, 1);
263 test_int2float(reporter, -1);
264 for (i = 0; i < 100000; i++) {
265 // for now only test ints that are 24bits or less, since we don't
266 // round (down) large ints the same as IEEE...
267 int ival = rand.nextU() & 0xFFFFFF;
268 test_int2float(reporter, ival);
269 test_int2float(reporter, -ival);
270 }
271 }
272}
273
reed@android.comd4134452011-02-09 02:24:26 +0000274#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000275static float make_zero() {
276 return sk_float_sin(0);
277}
reed@android.comd4134452011-02-09 02:24:26 +0000278#endif
reed@google.com077910e2011-02-08 21:56:39 +0000279
280static void unittest_isfinite(skiatest::Reporter* reporter) {
281#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000282 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000283 float inf = 1.0f / make_zero();
284 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000285
reed@google.com077910e2011-02-08 21:56:39 +0000286 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000287 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
288 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
289 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
290#else
291 SkFixed nan = SK_FixedNaN;
292 SkFixed big = SK_FixedMax;
293#endif
294
295 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000296 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
297 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
298 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
reed@android.comd4134452011-02-09 02:24:26 +0000299
reed@google.com077910e2011-02-08 21:56:39 +0000300 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000301 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
302 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
303 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000304}
305
reed@android.comed673312009-02-27 16:24:51 +0000306#endif
307
308static void test_muldiv255(skiatest::Reporter* reporter) {
309#ifdef SK_CAN_USE_FLOAT
310 for (int a = 0; a <= 255; a++) {
311 for (int b = 0; b <= 255; b++) {
312 int ab = a * b;
313 float s = ab / 255.0f;
314 int round = (int)floorf(s + 0.5f);
315 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000316
reed@android.comed673312009-02-27 16:24:51 +0000317 int iround = SkMulDiv255Round(a, b);
318 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000319
reed@android.comed673312009-02-27 16:24:51 +0000320 REPORTER_ASSERT(reporter, iround == round);
321 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000322
reed@android.comed673312009-02-27 16:24:51 +0000323 REPORTER_ASSERT(reporter, itrunc <= iround);
324 REPORTER_ASSERT(reporter, iround <= a);
325 REPORTER_ASSERT(reporter, iround <= b);
326 }
327 }
328#endif
329}
330
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000331static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
332 for (int c = 0; c <= 255; c++) {
333 for (int a = 0; a <= 255; a++) {
334 int product = (c * a + 255);
335 int expected_ceiling = (product + (product >> 8)) >> 8;
336 int webkit_ceiling = (c * a + 254) / 255;
337 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
338 int skia_ceiling = SkMulDiv255Ceiling(c, a);
339 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
340 }
341 }
342}
343
reed@android.comf0ad0862010-02-09 19:18:38 +0000344static void test_copysign(skiatest::Reporter* reporter) {
345 static const int32_t gTriples[] = {
346 // x, y, expected result
347 0, 0, 0,
348 0, 1, 0,
349 0, -1, 0,
350 1, 0, 1,
351 1, 1, 1,
352 1, -1, -1,
353 -1, 0, 1,
354 -1, 1, 1,
355 -1, -1, -1,
356 };
357 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
358 REPORTER_ASSERT(reporter,
359 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
360#ifdef SK_CAN_USE_FLOAT
361 float x = (float)gTriples[i];
362 float y = (float)gTriples[i+1];
363 float expected = (float)gTriples[i+2];
364 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
365#endif
366 }
367
368 SkRandom rand;
369 for (int j = 0; j < 1000; j++) {
370 int ix = rand.nextS();
371 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
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
376 SkScalar sx = rand.nextSScalar1();
377 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
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 }
382}
383
reed@android.com80e39a72009-04-02 16:59:40 +0000384static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000385 int i;
386 int32_t x;
387 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000388
reed@android.comed673312009-02-27 16:24:51 +0000389 // these should assert
390#if 0
391 SkToS8(128);
392 SkToS8(-129);
393 SkToU8(256);
394 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000395
reed@android.comed673312009-02-27 16:24:51 +0000396 SkToS16(32768);
397 SkToS16(-32769);
398 SkToU16(65536);
399 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000400
reed@android.comed673312009-02-27 16:24:51 +0000401 if (sizeof(size_t) > 4) {
402 SkToS32(4*1024*1024);
403 SkToS32(-4*1024*1024);
404 SkToU32(5*1024*1024);
405 SkToU32(-5);
406 }
407#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000408
reed@android.comed673312009-02-27 16:24:51 +0000409 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000410 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000411 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000412
reed@android.comed673312009-02-27 16:24:51 +0000413 {
414 SkScalar x = SK_ScalarNaN;
415 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
416 }
reed@android.com80e39a72009-04-02 16:59:40 +0000417
reed@android.comed673312009-02-27 16:24:51 +0000418 for (i = 1; i <= 10; i++) {
419 x = SkCubeRootBits(i*i*i, 11);
420 REPORTER_ASSERT(reporter, x == i);
421 }
reed@android.com80e39a72009-04-02 16:59:40 +0000422
reed@android.comed673312009-02-27 16:24:51 +0000423 x = SkFixedSqrt(SK_Fixed1);
424 REPORTER_ASSERT(reporter, x == SK_Fixed1);
425 x = SkFixedSqrt(SK_Fixed1/4);
426 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
427 x = SkFixedSqrt(SK_Fixed1*4);
428 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000429
reed@android.comed673312009-02-27 16:24:51 +0000430 x = SkFractSqrt(SK_Fract1);
431 REPORTER_ASSERT(reporter, x == SK_Fract1);
432 x = SkFractSqrt(SK_Fract1/4);
433 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
434 x = SkFractSqrt(SK_Fract1/16);
435 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000436
reed@android.comed673312009-02-27 16:24:51 +0000437 for (i = 1; i < 100; i++) {
438 x = SkFixedSqrt(SK_Fixed1 * i * i);
439 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
440 }
reed@android.com80e39a72009-04-02 16:59:40 +0000441
reed@android.comed673312009-02-27 16:24:51 +0000442 for (i = 0; i < 1000; i++) {
443 int value = rand.nextS16();
444 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000445
reed@android.comed673312009-02-27 16:24:51 +0000446 int clamp = SkClampMax(value, max);
447 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
448 REPORTER_ASSERT(reporter, clamp == clamp2);
449 }
reed@android.com80e39a72009-04-02 16:59:40 +0000450
reed@android.come72fee52009-11-16 14:52:01 +0000451 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000452 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000453
tomhudson@google.com75589252012-04-10 17:42:21 +0000454 // These random values are being treated as 32-bit-patterns, not as
455 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000456 p.setLength((SkScalar) rand.nextS(),
457 (SkScalar) rand.nextS(),
458 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000459 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000460 p.setLength((SkScalar) (rand.nextS() >> 13),
461 (SkScalar) (rand.nextS() >> 13),
462 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000463 check_length(reporter, p, SK_Scalar1);
464 }
reed@android.com80e39a72009-04-02 16:59:40 +0000465
reed@android.comed673312009-02-27 16:24:51 +0000466 {
467 SkFixed result = SkFixedDiv(100, 100);
468 REPORTER_ASSERT(reporter, result == SK_Fixed1);
469 result = SkFixedDiv(1, SK_Fixed1);
470 REPORTER_ASSERT(reporter, result == 1);
471 }
reed@android.com80e39a72009-04-02 16:59:40 +0000472
reed@android.comed673312009-02-27 16:24:51 +0000473#ifdef SK_CAN_USE_FLOAT
474 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000475 unittest_isfinite(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000476#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000477
reed@android.comed673312009-02-27 16:24:51 +0000478#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000479 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000480 SkFixed numer = rand.nextS();
481 SkFixed denom = rand.nextS();
482 SkFixed result = SkFixedDiv(numer, denom);
483 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000484
reed@android.comed673312009-02-27 16:24:51 +0000485 (void)SkCLZ(numer);
486 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000487
reed@android.comed673312009-02-27 16:24:51 +0000488 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
489 if (check > SK_MaxS32) {
490 check = SK_MaxS32;
491 } else if (check < -SK_MaxS32) {
492 check = SK_MinS32;
493 }
494 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000495
reed@android.comed673312009-02-27 16:24:51 +0000496 result = SkFractDiv(numer, denom);
497 check = ((SkLONGLONG)numer << 30) / 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 // make them <= 2^24, so we don't overflow in fixmul
508 numer = numer << 8 >> 8;
509 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000510
reed@android.comed673312009-02-27 16:24:51 +0000511 result = SkFixedMul(numer, denom);
512 SkFixed r2 = symmetric_fixmul(numer, denom);
513 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000514
reed@android.comed673312009-02-27 16:24:51 +0000515 result = SkFixedMul(numer, numer);
516 r2 = SkFixedSquare(numer);
517 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000518
reed@android.comed673312009-02-27 16:24:51 +0000519#ifdef SK_CAN_USE_FLOAT
520 if (numer >= 0 && denom >= 0) {
521 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000522 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
523 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000524 SkFixed mean2 = SkFloatToFixed(fm);
525 int diff = SkAbs32(mean - mean2);
526 REPORTER_ASSERT(reporter, diff <= 1);
527 }
reed@android.com80e39a72009-04-02 16:59:40 +0000528
reed@android.comed673312009-02-27 16:24:51 +0000529 {
530 SkFixed mod = SkFixedMod(numer, denom);
531 float n = SkFixedToFloat(numer);
532 float d = SkFixedToFloat(denom);
533 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000534 // ensure the same sign
535 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000536 int diff = SkAbs32(mod - SkFloatToFixed(m));
537 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
538 }
539#endif
540 }
541#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000542
reed@android.comed673312009-02-27 16:24:51 +0000543#ifdef SK_CAN_USE_FLOAT
reed@android.come72fee52009-11-16 14:52:01 +0000544 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000545 SkFract x = rand.nextU() >> 1;
546 double xx = (double)x / SK_Fract1;
547 SkFract xr = SkFractSqrt(x);
548 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000549 REPORTER_ASSERT(reporter, xr == check ||
550 xr == check-1 ||
551 xr == check+1);
552
reed@android.comed673312009-02-27 16:24:51 +0000553 xr = SkFixedSqrt(x);
554 xx = (double)x / SK_Fixed1;
555 check = SkFloatToFixed(sqrt(xx));
556 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000557
reed@android.comed673312009-02-27 16:24:51 +0000558 xr = SkSqrt32(x);
559 xx = (double)x;
560 check = (int32_t)sqrt(xx);
561 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
562 }
563#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000564
reed@android.comed673312009-02-27 16:24:51 +0000565#if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT)
566 {
567 SkFixed s, c;
568 s = SkFixedSinCos(0, &c);
569 REPORTER_ASSERT(reporter, s == 0);
570 REPORTER_ASSERT(reporter, c == SK_Fixed1);
571 }
reed@android.com80e39a72009-04-02 16:59:40 +0000572
reed@android.comed673312009-02-27 16:24:51 +0000573 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000574 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000575 SkFixed rads = rand.nextS() >> 10;
576 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000577
reed@android.comed673312009-02-27 16:24:51 +0000578 SkFixed s, c;
579 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000580
reed@android.comed673312009-02-27 16:24:51 +0000581 double fs = sin(frads);
582 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000583
reed@android.comed673312009-02-27 16:24:51 +0000584 SkFixed is = SkFloatToFixed(fs);
585 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000586
reed@android.comed673312009-02-27 16:24:51 +0000587 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
588 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
589 }
590 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
591#endif
reed@google.com772813a2011-03-30 22:34:45 +0000592
reed@google.com0abb4992011-10-06 20:04:36 +0000593#ifdef SK_SCALAR_IS_FLOAT
594 test_blend(reporter);
595#endif
reed@google.com8d7e39c2011-11-09 17:12:08 +0000596
reed@google.coma7d74612012-05-30 12:30:09 +0000597#ifdef SK_CAN_USE_FLOAT
598 test_floor(reporter);
599#endif
600
reed@google.com8d7e39c2011-11-09 17:12:08 +0000601 // disable for now
602// test_blend31();
reed@android.comed673312009-02-27 16:24:51 +0000603}
604
reed@android.comd8730ea2009-02-27 22:06:06 +0000605#include "TestClassDef.h"
606DEFINE_TESTCLASS("Math", MathTestClass, TestMath)