blob: eec9dbe826839ed397d645cd184ef1dae1c2f8fb [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.com0abb4992011-10-06 20:04:36 +000015static float float_blend(int src, int dst, float unit) {
16 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +000017}
18
reed@google.com8d7e39c2011-11-09 17:12:08 +000019static int blend31(int src, int dst, int a31) {
20 return dst + ((src - dst) * a31 * 2114 >> 16);
21 // return dst + ((src - dst) * a31 * 33 >> 10);
22}
23
24static int blend31_slow(int src, int dst, int a31) {
25 int prod = src * a31 + (31 - a31) * dst + 16;
26 prod = (prod + (prod >> 5)) >> 5;
27 return prod;
28}
29
30static int blend31_round(int src, int dst, int a31) {
31 int prod = (src - dst) * a31 + 16;
32 prod = (prod + (prod >> 5)) >> 5;
33 return dst + prod;
34}
35
36static int blend31_old(int src, int dst, int a31) {
37 a31 += a31 >> 4;
38 return dst + ((src - dst) * a31 >> 5);
39}
40
41static void test_blend31() {
42 int failed = 0;
43 int death = 0;
44 for (int src = 0; src <= 255; src++) {
45 for (int dst = 0; dst <= 255; dst++) {
46 for (int a = 0; a <= 31; a++) {
47// int r0 = blend31(src, dst, a);
48// int r0 = blend31_round(src, dst, a);
49// int r0 = blend31_old(src, dst, a);
50 int r0 = blend31_slow(src, dst, a);
51
52 float f = float_blend(src, dst, a / 31.f);
53 int r1 = (int)f;
54 int r2 = SkScalarRoundToInt(SkFloatToScalar(f));
55
56 if (r0 != r1 && r0 != r2) {
57 printf("src:%d dst:%d a:%d result:%d float:%g\n",
58 src, dst, a, r0, f);
59 failed += 1;
60 }
61 if (r0 > 255) {
62 death += 1;
63 printf("death src:%d dst:%d a:%d result:%d float:%g\n",
64 src, dst, a, r0, f);
65 }
66 }
67 }
68 }
69 SkDebugf("---- failed %d death %d\n", failed, death);
70}
71
reed@google.com0abb4992011-10-06 20:04:36 +000072static void test_blend(skiatest::Reporter* reporter) {
73 for (int src = 0; src <= 255; src++) {
74 for (int dst = 0; dst <= 255; dst++) {
75 for (int a = 0; a <= 255; a++) {
76 int r0 = SkAlphaBlend255(src, dst, a);
77 float f1 = float_blend(src, dst, a / 255.f);
reed@google.com111c19b2011-10-06 20:13:22 +000078 int r1 = SkScalarRoundToInt(SkFloatToScalar(f1));
reed@google.com772813a2011-03-30 22:34:45 +000079
reed@google.com0abb4992011-10-06 20:04:36 +000080 if (r0 != r1) {
81 float diff = sk_float_abs(f1 - r1);
82 diff = sk_float_abs(diff - 0.5f);
83 if (diff > (1 / 255.f)) {
84#ifdef SK_DEBUG
85 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
86 src, dst, a, r0, f1);
87#endif
88 REPORTER_ASSERT(reporter, false);
89 }
90 }
reed@google.com772813a2011-03-30 22:34:45 +000091 }
92 }
93 }
94}
reed@google.com772813a2011-03-30 22:34:45 +000095
reed@android.comed673312009-02-27 16:24:51 +000096#if defined(SkLONGLONG)
97static int symmetric_fixmul(int a, int b) {
98 int sa = SkExtractSign(a);
99 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +0000100
reed@android.comed673312009-02-27 16:24:51 +0000101 a = SkApplySign(a, sa);
102 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +0000103
reed@android.comed673312009-02-27 16:24:51 +0000104#if 1
105 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +0000106
reed@android.comed673312009-02-27 16:24:51 +0000107 return SkApplySign(c, sa ^ sb);
108#else
109 SkLONGLONG ab = (SkLONGLONG)a * b;
110 if (sa ^ sb) {
111 ab = -ab;
112 }
113 return ab >> 16;
114#endif
115}
116#endif
117
118static void check_length(skiatest::Reporter* reporter,
119 const SkPoint& p, SkScalar targetLen) {
120#ifdef SK_CAN_USE_FLOAT
121 float x = SkScalarToFloat(p.fX);
122 float y = SkScalarToFloat(p.fY);
123 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000124
reed@android.comed673312009-02-27 16:24:51 +0000125 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000126
reed@android.comed673312009-02-27 16:24:51 +0000127 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
128#endif
129}
130
131#if defined(SK_CAN_USE_FLOAT)
132
133static float nextFloat(SkRandom& rand) {
134 SkFloatIntUnion data;
135 data.fSignBitInt = rand.nextU();
136 return data.fFloat;
137}
138
139/* returns true if a == b as resulting from (int)x. Since it is undefined
140 what to do if the float exceeds 2^32-1, we check for that explicitly.
141 */
142static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
143 if (!(x == x)) { // NAN
144 return si == SK_MaxS32 || si == SK_MinS32;
145 }
146 // for out of range, C is undefined, but skia always should return NaN32
147 if (x > SK_MaxS32) {
148 return si == SK_MaxS32;
149 }
150 if (x < -SK_MaxS32) {
151 return si == SK_MinS32;
152 }
153 return si == ni;
154}
155
156static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
157 float x, uint32_t ni, uint32_t si) {
158 if (!equal_float_native_skia(x, ni, si)) {
159 SkString desc;
160 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si);
161 reporter->reportFailed(desc);
162 }
163}
164
165static void test_float_cast(skiatest::Reporter* reporter, float x) {
166 int ix = (int)x;
167 int iix = SkFloatToIntCast(x);
168 assert_float_equal(reporter, "cast", x, ix, iix);
169}
170
171static void test_float_floor(skiatest::Reporter* reporter, float x) {
172 int ix = (int)floor(x);
173 int iix = SkFloatToIntFloor(x);
174 assert_float_equal(reporter, "floor", x, ix, iix);
175}
176
177static void test_float_round(skiatest::Reporter* reporter, float x) {
178 double xx = x + 0.5; // need intermediate double to avoid temp loss
179 int ix = (int)floor(xx);
180 int iix = SkFloatToIntRound(x);
181 assert_float_equal(reporter, "round", x, ix, iix);
182}
183
184static void test_float_ceil(skiatest::Reporter* reporter, float x) {
185 int ix = (int)ceil(x);
186 int iix = SkFloatToIntCeil(x);
187 assert_float_equal(reporter, "ceil", x, ix, iix);
188}
189
190static void test_float_conversions(skiatest::Reporter* reporter, float x) {
191 test_float_cast(reporter, x);
192 test_float_floor(reporter, x);
193 test_float_round(reporter, x);
194 test_float_ceil(reporter, x);
195}
196
197static void test_int2float(skiatest::Reporter* reporter, int ival) {
198 float x0 = (float)ival;
199 float x1 = SkIntToFloatCast(ival);
200 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
201 REPORTER_ASSERT(reporter, x0 == x1);
202 REPORTER_ASSERT(reporter, x0 == x2);
203}
204
205static void unittest_fastfloat(skiatest::Reporter* reporter) {
206 SkRandom rand;
207 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000208
reed@android.comed673312009-02-27 16:24:51 +0000209 static const float gFloats[] = {
210 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
211 0.000000001f, 1000000000.f, // doesn't overflow
212 0.0000000001f, 10000000000.f // does overflow
213 };
214 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000215 test_float_conversions(reporter, gFloats[i]);
216 test_float_conversions(reporter, -gFloats[i]);
217 }
reed@android.com80e39a72009-04-02 16:59:40 +0000218
reed@android.comed673312009-02-27 16:24:51 +0000219 for (int outer = 0; outer < 100; outer++) {
220 rand.setSeed(outer);
221 for (i = 0; i < 100000; i++) {
222 float x = nextFloat(rand);
223 test_float_conversions(reporter, x);
224 }
reed@android.com80e39a72009-04-02 16:59:40 +0000225
reed@android.comed673312009-02-27 16:24:51 +0000226 test_int2float(reporter, 0);
227 test_int2float(reporter, 1);
228 test_int2float(reporter, -1);
229 for (i = 0; i < 100000; i++) {
230 // for now only test ints that are 24bits or less, since we don't
231 // round (down) large ints the same as IEEE...
232 int ival = rand.nextU() & 0xFFFFFF;
233 test_int2float(reporter, ival);
234 test_int2float(reporter, -ival);
235 }
236 }
237}
238
reed@android.comd4134452011-02-09 02:24:26 +0000239#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000240static float make_zero() {
241 return sk_float_sin(0);
242}
reed@android.comd4134452011-02-09 02:24:26 +0000243#endif
reed@google.com077910e2011-02-08 21:56:39 +0000244
245static void unittest_isfinite(skiatest::Reporter* reporter) {
246#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000247 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000248 float inf = 1.0f / make_zero();
249 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000250
reed@google.com077910e2011-02-08 21:56:39 +0000251 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000252 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
253 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
254 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
255#else
256 SkFixed nan = SK_FixedNaN;
257 SkFixed big = SK_FixedMax;
258#endif
259
260 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000261 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
262 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
263 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
reed@android.comd4134452011-02-09 02:24:26 +0000264
reed@google.com077910e2011-02-08 21:56:39 +0000265 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000266 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
267 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
268 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000269}
270
reed@android.comed673312009-02-27 16:24:51 +0000271#endif
272
273static void test_muldiv255(skiatest::Reporter* reporter) {
274#ifdef SK_CAN_USE_FLOAT
275 for (int a = 0; a <= 255; a++) {
276 for (int b = 0; b <= 255; b++) {
277 int ab = a * b;
278 float s = ab / 255.0f;
279 int round = (int)floorf(s + 0.5f);
280 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000281
reed@android.comed673312009-02-27 16:24:51 +0000282 int iround = SkMulDiv255Round(a, b);
283 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000284
reed@android.comed673312009-02-27 16:24:51 +0000285 REPORTER_ASSERT(reporter, iround == round);
286 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000287
reed@android.comed673312009-02-27 16:24:51 +0000288 REPORTER_ASSERT(reporter, itrunc <= iround);
289 REPORTER_ASSERT(reporter, iround <= a);
290 REPORTER_ASSERT(reporter, iround <= b);
291 }
292 }
293#endif
294}
295
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000296static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
297 for (int c = 0; c <= 255; c++) {
298 for (int a = 0; a <= 255; a++) {
299 int product = (c * a + 255);
300 int expected_ceiling = (product + (product >> 8)) >> 8;
301 int webkit_ceiling = (c * a + 254) / 255;
302 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
303 int skia_ceiling = SkMulDiv255Ceiling(c, a);
304 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
305 }
306 }
307}
308
reed@android.comf0ad0862010-02-09 19:18:38 +0000309static void test_copysign(skiatest::Reporter* reporter) {
310 static const int32_t gTriples[] = {
311 // x, y, expected result
312 0, 0, 0,
313 0, 1, 0,
314 0, -1, 0,
315 1, 0, 1,
316 1, 1, 1,
317 1, -1, -1,
318 -1, 0, 1,
319 -1, 1, 1,
320 -1, -1, -1,
321 };
322 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
323 REPORTER_ASSERT(reporter,
324 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
325#ifdef SK_CAN_USE_FLOAT
326 float x = (float)gTriples[i];
327 float y = (float)gTriples[i+1];
328 float expected = (float)gTriples[i+2];
329 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
330#endif
331 }
332
333 SkRandom rand;
334 for (int j = 0; j < 1000; j++) {
335 int ix = rand.nextS();
336 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
337 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
338 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
339 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
340
341 SkScalar sx = rand.nextSScalar1();
342 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
343 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
344 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
345 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
346 }
347}
348
reed@android.com80e39a72009-04-02 16:59:40 +0000349static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000350 int i;
351 int32_t x;
352 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000353
reed@android.comed673312009-02-27 16:24:51 +0000354 // these should assert
355#if 0
356 SkToS8(128);
357 SkToS8(-129);
358 SkToU8(256);
359 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000360
reed@android.comed673312009-02-27 16:24:51 +0000361 SkToS16(32768);
362 SkToS16(-32769);
363 SkToU16(65536);
364 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000365
reed@android.comed673312009-02-27 16:24:51 +0000366 if (sizeof(size_t) > 4) {
367 SkToS32(4*1024*1024);
368 SkToS32(-4*1024*1024);
369 SkToU32(5*1024*1024);
370 SkToU32(-5);
371 }
372#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000373
reed@android.comed673312009-02-27 16:24:51 +0000374 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000375 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000376 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000377
reed@android.comed673312009-02-27 16:24:51 +0000378 {
379 SkScalar x = SK_ScalarNaN;
380 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
381 }
reed@android.com80e39a72009-04-02 16:59:40 +0000382
reed@android.comed673312009-02-27 16:24:51 +0000383 for (i = 1; i <= 10; i++) {
384 x = SkCubeRootBits(i*i*i, 11);
385 REPORTER_ASSERT(reporter, x == i);
386 }
reed@android.com80e39a72009-04-02 16:59:40 +0000387
reed@android.comed673312009-02-27 16:24:51 +0000388 x = SkFixedSqrt(SK_Fixed1);
389 REPORTER_ASSERT(reporter, x == SK_Fixed1);
390 x = SkFixedSqrt(SK_Fixed1/4);
391 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
392 x = SkFixedSqrt(SK_Fixed1*4);
393 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000394
reed@android.comed673312009-02-27 16:24:51 +0000395 x = SkFractSqrt(SK_Fract1);
396 REPORTER_ASSERT(reporter, x == SK_Fract1);
397 x = SkFractSqrt(SK_Fract1/4);
398 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
399 x = SkFractSqrt(SK_Fract1/16);
400 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000401
reed@android.comed673312009-02-27 16:24:51 +0000402 for (i = 1; i < 100; i++) {
403 x = SkFixedSqrt(SK_Fixed1 * i * i);
404 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
405 }
reed@android.com80e39a72009-04-02 16:59:40 +0000406
reed@android.comed673312009-02-27 16:24:51 +0000407 for (i = 0; i < 1000; i++) {
408 int value = rand.nextS16();
409 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000410
reed@android.comed673312009-02-27 16:24:51 +0000411 int clamp = SkClampMax(value, max);
412 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
413 REPORTER_ASSERT(reporter, clamp == clamp2);
414 }
reed@android.com80e39a72009-04-02 16:59:40 +0000415
reed@android.come72fee52009-11-16 14:52:01 +0000416 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000417 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000418
tomhudson@google.com75589252012-04-10 17:42:21 +0000419 // These random values are being treated as 32-bit-patterns, not as
420 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000421 p.setLength((SkScalar) rand.nextS(),
422 (SkScalar) rand.nextS(),
423 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000424 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000425 p.setLength((SkScalar) (rand.nextS() >> 13),
426 (SkScalar) (rand.nextS() >> 13),
427 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000428 check_length(reporter, p, SK_Scalar1);
429 }
reed@android.com80e39a72009-04-02 16:59:40 +0000430
reed@android.comed673312009-02-27 16:24:51 +0000431 {
432 SkFixed result = SkFixedDiv(100, 100);
433 REPORTER_ASSERT(reporter, result == SK_Fixed1);
434 result = SkFixedDiv(1, SK_Fixed1);
435 REPORTER_ASSERT(reporter, result == 1);
436 }
reed@android.com80e39a72009-04-02 16:59:40 +0000437
reed@android.comed673312009-02-27 16:24:51 +0000438#ifdef SK_CAN_USE_FLOAT
439 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000440 unittest_isfinite(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000441#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000442
reed@android.comed673312009-02-27 16:24:51 +0000443#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000444 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000445 SkFixed numer = rand.nextS();
446 SkFixed denom = rand.nextS();
447 SkFixed result = SkFixedDiv(numer, denom);
448 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000449
reed@android.comed673312009-02-27 16:24:51 +0000450 (void)SkCLZ(numer);
451 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000452
reed@android.comed673312009-02-27 16:24:51 +0000453 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
454 if (check > SK_MaxS32) {
455 check = SK_MaxS32;
456 } else if (check < -SK_MaxS32) {
457 check = SK_MinS32;
458 }
459 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000460
reed@android.comed673312009-02-27 16:24:51 +0000461 result = SkFractDiv(numer, denom);
462 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000463
reed@android.comed673312009-02-27 16:24:51 +0000464 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
465 if (check > SK_MaxS32) {
466 check = SK_MaxS32;
467 } else if (check < -SK_MaxS32) {
468 check = SK_MinS32;
469 }
470 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000471
reed@android.comed673312009-02-27 16:24:51 +0000472 // make them <= 2^24, so we don't overflow in fixmul
473 numer = numer << 8 >> 8;
474 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000475
reed@android.comed673312009-02-27 16:24:51 +0000476 result = SkFixedMul(numer, denom);
477 SkFixed r2 = symmetric_fixmul(numer, denom);
478 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000479
reed@android.comed673312009-02-27 16:24:51 +0000480 result = SkFixedMul(numer, numer);
481 r2 = SkFixedSquare(numer);
482 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000483
reed@android.comed673312009-02-27 16:24:51 +0000484#ifdef SK_CAN_USE_FLOAT
485 if (numer >= 0 && denom >= 0) {
486 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000487 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
488 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000489 SkFixed mean2 = SkFloatToFixed(fm);
490 int diff = SkAbs32(mean - mean2);
491 REPORTER_ASSERT(reporter, diff <= 1);
492 }
reed@android.com80e39a72009-04-02 16:59:40 +0000493
reed@android.comed673312009-02-27 16:24:51 +0000494 {
495 SkFixed mod = SkFixedMod(numer, denom);
496 float n = SkFixedToFloat(numer);
497 float d = SkFixedToFloat(denom);
498 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000499 // ensure the same sign
500 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000501 int diff = SkAbs32(mod - SkFloatToFixed(m));
502 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
503 }
504#endif
505 }
506#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000507
reed@android.comed673312009-02-27 16:24:51 +0000508#ifdef SK_CAN_USE_FLOAT
reed@android.come72fee52009-11-16 14:52:01 +0000509 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000510 SkFract x = rand.nextU() >> 1;
511 double xx = (double)x / SK_Fract1;
512 SkFract xr = SkFractSqrt(x);
513 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000514 REPORTER_ASSERT(reporter, xr == check ||
515 xr == check-1 ||
516 xr == check+1);
517
reed@android.comed673312009-02-27 16:24:51 +0000518 xr = SkFixedSqrt(x);
519 xx = (double)x / SK_Fixed1;
520 check = SkFloatToFixed(sqrt(xx));
521 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000522
reed@android.comed673312009-02-27 16:24:51 +0000523 xr = SkSqrt32(x);
524 xx = (double)x;
525 check = (int32_t)sqrt(xx);
526 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
527 }
528#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000529
reed@android.comed673312009-02-27 16:24:51 +0000530#if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT)
531 {
532 SkFixed s, c;
533 s = SkFixedSinCos(0, &c);
534 REPORTER_ASSERT(reporter, s == 0);
535 REPORTER_ASSERT(reporter, c == SK_Fixed1);
536 }
reed@android.com80e39a72009-04-02 16:59:40 +0000537
reed@android.comed673312009-02-27 16:24:51 +0000538 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000539 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000540 SkFixed rads = rand.nextS() >> 10;
541 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000542
reed@android.comed673312009-02-27 16:24:51 +0000543 SkFixed s, c;
544 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000545
reed@android.comed673312009-02-27 16:24:51 +0000546 double fs = sin(frads);
547 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000548
reed@android.comed673312009-02-27 16:24:51 +0000549 SkFixed is = SkFloatToFixed(fs);
550 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000551
reed@android.comed673312009-02-27 16:24:51 +0000552 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
553 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
554 }
555 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
556#endif
reed@google.com772813a2011-03-30 22:34:45 +0000557
reed@google.com0abb4992011-10-06 20:04:36 +0000558#ifdef SK_SCALAR_IS_FLOAT
559 test_blend(reporter);
560#endif
reed@google.com8d7e39c2011-11-09 17:12:08 +0000561
562 // disable for now
563// test_blend31();
reed@android.comed673312009-02-27 16:24:51 +0000564}
565
reed@android.comd8730ea2009-02-27 22:06:06 +0000566#include "TestClassDef.h"
567DEFINE_TESTCLASS("Math", MathTestClass, TestMath)