blob: 4ad83d1829f566a249c002caf692febc249ed594 [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"
tomhudson@google.com8afae612012-08-14 15:03:35 +00009#include "SkFloatBits.h"
reed@android.comc846ede2010-04-13 15:29:15 +000010#include "SkFloatingPoint.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000011#include "SkMathPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000012#include "SkPoint.h"
13#include "SkRandom.h"
reed@google.com0abb4992011-10-06 20:04:36 +000014#include "SkColorPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000015
reed@google.comc21f86f2013-04-29 14:18:23 +000016static void test_clz(skiatest::Reporter* reporter) {
17 REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
18 REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
19 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
20 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0UL));
21
22 SkRandom rand;
23 for (int i = 0; i < 1000; ++i) {
24 uint32_t mask = rand.nextU();
25 int intri = SkCLZ(mask);
26 int porta = SkCLZ_portable(mask);
27 REPORTER_ASSERT(reporter, intri == porta);
28 }
29}
30
31///////////////////////////////////////////////////////////////////////////////
32
reed@google.coma7d74612012-05-30 12:30:09 +000033static float sk_fsel(float pred, float result_ge, float result_lt) {
34 return pred >= 0 ? result_ge : result_lt;
35}
36
37static float fast_floor(float x) {
reed@google.comc20bc252012-05-30 13:48:14 +000038// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
39 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
robertphillips@google.com00bf06a2012-05-30 15:19:17 +000040 return (float)(x + big) - big;
reed@google.coma7d74612012-05-30 12:30:09 +000041}
42
43static float std_floor(float x) {
44 return sk_float_floor(x);
45}
46
47static void test_floor_value(skiatest::Reporter* reporter, float value) {
48 float fast = fast_floor(value);
49 float std = std_floor(value);
50 REPORTER_ASSERT(reporter, std == fast);
51// SkDebugf("value[%1.9f] std[%g] fast[%g] equal[%d]\n",
52// value, std, fast, std == fast);
53}
54
55static void test_floor(skiatest::Reporter* reporter) {
56 static const float gVals[] = {
57 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
58 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000059
reed@google.coma7d74612012-05-30 12:30:09 +000060 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
61 test_floor_value(reporter, gVals[i]);
62// test_floor_value(reporter, -gVals[i]);
63 }
64}
65
66///////////////////////////////////////////////////////////////////////////////
67
reed@google.comea774d22013-04-22 20:21:56 +000068// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
69static void test_muldivround(skiatest::Reporter* reporter) {
70#if 0
71 // this "complete" test is too slow, so we test a random sampling of it
72
73 for (int a = 0; a <= 32767; ++a) {
74 for (int b = 0; b <= 32767; ++b) {
75 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
76 unsigned prod1 = SkMulDiv255Round(a, b);
77 SkASSERT(prod0 == prod1);
78 }
79 }
80#endif
81
82 SkRandom rand;
83 for (int i = 0; i < 10000; ++i) {
84 unsigned a = rand.nextU() & 0x7FFF;
85 unsigned b = rand.nextU() & 0x7FFF;
86
87 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
88 unsigned prod1 = SkMulDiv255Round(a, b);
89
90 REPORTER_ASSERT(reporter, prod0 == prod1);
91 }
92}
93
reed@google.com0abb4992011-10-06 20:04:36 +000094static float float_blend(int src, int dst, float unit) {
95 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +000096}
97
reed@google.com8d7e39c2011-11-09 17:12:08 +000098static int blend31(int src, int dst, int a31) {
99 return dst + ((src - dst) * a31 * 2114 >> 16);
100 // return dst + ((src - dst) * a31 * 33 >> 10);
101}
102
103static int blend31_slow(int src, int dst, int a31) {
104 int prod = src * a31 + (31 - a31) * dst + 16;
105 prod = (prod + (prod >> 5)) >> 5;
106 return prod;
107}
108
109static int blend31_round(int src, int dst, int a31) {
110 int prod = (src - dst) * a31 + 16;
111 prod = (prod + (prod >> 5)) >> 5;
112 return dst + prod;
113}
114
115static int blend31_old(int src, int dst, int a31) {
116 a31 += a31 >> 4;
117 return dst + ((src - dst) * a31 >> 5);
118}
119
caryclark@google.com42639cd2012-06-06 12:03:39 +0000120// suppress unused code warning
121static int (*blend_functions[])(int, int, int) = {
122 blend31,
123 blend31_slow,
124 blend31_round,
125 blend31_old
126};
127
reed@google.com8d7e39c2011-11-09 17:12:08 +0000128static void test_blend31() {
129 int failed = 0;
130 int death = 0;
caryclark@google.com42639cd2012-06-06 12:03:39 +0000131 if (false) { // avoid bit rot, suppress warning
132 failed = (*blend_functions[0])(0,0,0);
133 }
reed@google.com8d7e39c2011-11-09 17:12:08 +0000134 for (int src = 0; src <= 255; src++) {
135 for (int dst = 0; dst <= 255; dst++) {
136 for (int a = 0; a <= 31; a++) {
137// int r0 = blend31(src, dst, a);
138// int r0 = blend31_round(src, dst, a);
139// int r0 = blend31_old(src, dst, a);
140 int r0 = blend31_slow(src, dst, a);
141
142 float f = float_blend(src, dst, a / 31.f);
143 int r1 = (int)f;
144 int r2 = SkScalarRoundToInt(SkFloatToScalar(f));
145
146 if (r0 != r1 && r0 != r2) {
147 printf("src:%d dst:%d a:%d result:%d float:%g\n",
148 src, dst, a, r0, f);
149 failed += 1;
150 }
151 if (r0 > 255) {
152 death += 1;
153 printf("death src:%d dst:%d a:%d result:%d float:%g\n",
154 src, dst, a, r0, f);
155 }
156 }
157 }
158 }
159 SkDebugf("---- failed %d death %d\n", failed, death);
160}
161
reed@google.com0abb4992011-10-06 20:04:36 +0000162static void test_blend(skiatest::Reporter* reporter) {
163 for (int src = 0; src <= 255; src++) {
164 for (int dst = 0; dst <= 255; dst++) {
165 for (int a = 0; a <= 255; a++) {
166 int r0 = SkAlphaBlend255(src, dst, a);
167 float f1 = float_blend(src, dst, a / 255.f);
reed@google.com111c19b2011-10-06 20:13:22 +0000168 int r1 = SkScalarRoundToInt(SkFloatToScalar(f1));
reed@google.com772813a2011-03-30 22:34:45 +0000169
reed@google.com0abb4992011-10-06 20:04:36 +0000170 if (r0 != r1) {
171 float diff = sk_float_abs(f1 - r1);
172 diff = sk_float_abs(diff - 0.5f);
173 if (diff > (1 / 255.f)) {
174#ifdef SK_DEBUG
175 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
176 src, dst, a, r0, f1);
177#endif
178 REPORTER_ASSERT(reporter, false);
179 }
180 }
reed@google.com772813a2011-03-30 22:34:45 +0000181 }
182 }
183 }
184}
reed@google.com772813a2011-03-30 22:34:45 +0000185
reed@android.comed673312009-02-27 16:24:51 +0000186#if defined(SkLONGLONG)
187static int symmetric_fixmul(int a, int b) {
188 int sa = SkExtractSign(a);
189 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +0000190
reed@android.comed673312009-02-27 16:24:51 +0000191 a = SkApplySign(a, sa);
192 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +0000193
reed@android.comed673312009-02-27 16:24:51 +0000194#if 1
195 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +0000196
reed@android.comed673312009-02-27 16:24:51 +0000197 return SkApplySign(c, sa ^ sb);
198#else
199 SkLONGLONG ab = (SkLONGLONG)a * b;
200 if (sa ^ sb) {
201 ab = -ab;
202 }
203 return ab >> 16;
204#endif
205}
206#endif
207
208static void check_length(skiatest::Reporter* reporter,
209 const SkPoint& p, SkScalar targetLen) {
reed@android.comed673312009-02-27 16:24:51 +0000210 float x = SkScalarToFloat(p.fX);
211 float y = SkScalarToFloat(p.fY);
212 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000213
reed@android.comed673312009-02-27 16:24:51 +0000214 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000215
reed@android.comed673312009-02-27 16:24:51 +0000216 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000217}
218
jvanverth@google.comc490f802013-03-04 13:56:38 +0000219static float nextFloat(SkMWCRandom& rand) {
reed@android.comed673312009-02-27 16:24:51 +0000220 SkFloatIntUnion data;
221 data.fSignBitInt = rand.nextU();
222 return data.fFloat;
223}
224
225/* returns true if a == b as resulting from (int)x. Since it is undefined
226 what to do if the float exceeds 2^32-1, we check for that explicitly.
227 */
228static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
229 if (!(x == x)) { // NAN
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000230 return ((int32_t)si) == SK_MaxS32 || ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000231 }
232 // for out of range, C is undefined, but skia always should return NaN32
233 if (x > SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000234 return ((int32_t)si) == SK_MaxS32;
reed@android.comed673312009-02-27 16:24:51 +0000235 }
236 if (x < -SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000237 return ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000238 }
239 return si == ni;
240}
241
242static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
243 float x, uint32_t ni, uint32_t si) {
244 if (!equal_float_native_skia(x, ni, si)) {
245 SkString desc;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000246 uint32_t xi = SkFloat2Bits(x);
247 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, xi, ni, si);
reed@android.comed673312009-02-27 16:24:51 +0000248 reporter->reportFailed(desc);
249 }
250}
251
252static void test_float_cast(skiatest::Reporter* reporter, float x) {
253 int ix = (int)x;
254 int iix = SkFloatToIntCast(x);
255 assert_float_equal(reporter, "cast", x, ix, iix);
256}
257
258static void test_float_floor(skiatest::Reporter* reporter, float x) {
259 int ix = (int)floor(x);
260 int iix = SkFloatToIntFloor(x);
261 assert_float_equal(reporter, "floor", x, ix, iix);
262}
263
264static void test_float_round(skiatest::Reporter* reporter, float x) {
265 double xx = x + 0.5; // need intermediate double to avoid temp loss
266 int ix = (int)floor(xx);
267 int iix = SkFloatToIntRound(x);
268 assert_float_equal(reporter, "round", x, ix, iix);
269}
270
271static void test_float_ceil(skiatest::Reporter* reporter, float x) {
272 int ix = (int)ceil(x);
273 int iix = SkFloatToIntCeil(x);
274 assert_float_equal(reporter, "ceil", x, ix, iix);
275}
276
277static void test_float_conversions(skiatest::Reporter* reporter, float x) {
278 test_float_cast(reporter, x);
279 test_float_floor(reporter, x);
280 test_float_round(reporter, x);
281 test_float_ceil(reporter, x);
282}
283
284static void test_int2float(skiatest::Reporter* reporter, int ival) {
285 float x0 = (float)ival;
286 float x1 = SkIntToFloatCast(ival);
287 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
288 REPORTER_ASSERT(reporter, x0 == x1);
289 REPORTER_ASSERT(reporter, x0 == x2);
290}
291
292static void unittest_fastfloat(skiatest::Reporter* reporter) {
jvanverth@google.comc490f802013-03-04 13:56:38 +0000293 SkMWCRandom rand;
reed@android.comed673312009-02-27 16:24:51 +0000294 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000295
reed@android.comed673312009-02-27 16:24:51 +0000296 static const float gFloats[] = {
297 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
298 0.000000001f, 1000000000.f, // doesn't overflow
299 0.0000000001f, 10000000000.f // does overflow
300 };
301 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000302 test_float_conversions(reporter, gFloats[i]);
303 test_float_conversions(reporter, -gFloats[i]);
304 }
reed@android.com80e39a72009-04-02 16:59:40 +0000305
reed@android.comed673312009-02-27 16:24:51 +0000306 for (int outer = 0; outer < 100; outer++) {
307 rand.setSeed(outer);
308 for (i = 0; i < 100000; i++) {
309 float x = nextFloat(rand);
310 test_float_conversions(reporter, x);
311 }
reed@android.com80e39a72009-04-02 16:59:40 +0000312
reed@android.comed673312009-02-27 16:24:51 +0000313 test_int2float(reporter, 0);
314 test_int2float(reporter, 1);
315 test_int2float(reporter, -1);
316 for (i = 0; i < 100000; i++) {
317 // for now only test ints that are 24bits or less, since we don't
318 // round (down) large ints the same as IEEE...
319 int ival = rand.nextU() & 0xFFFFFF;
320 test_int2float(reporter, ival);
321 test_int2float(reporter, -ival);
322 }
323 }
324}
325
reed@android.comd4134452011-02-09 02:24:26 +0000326#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000327static float make_zero() {
328 return sk_float_sin(0);
329}
reed@android.comd4134452011-02-09 02:24:26 +0000330#endif
reed@google.com077910e2011-02-08 21:56:39 +0000331
332static void unittest_isfinite(skiatest::Reporter* reporter) {
333#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000334 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000335 float inf = 1.0f / make_zero();
336 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000337
reed@google.com077910e2011-02-08 21:56:39 +0000338 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000339 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
340 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
341 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
342#else
343 SkFixed nan = SK_FixedNaN;
344 SkFixed big = SK_FixedMax;
345#endif
346
347 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000348 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
349 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
350 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000351
reed@google.com077910e2011-02-08 21:56:39 +0000352 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000353 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
354 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
355 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000356}
357
reed@android.comed673312009-02-27 16:24:51 +0000358static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000359 for (int a = 0; a <= 255; a++) {
360 for (int b = 0; b <= 255; b++) {
361 int ab = a * b;
362 float s = ab / 255.0f;
363 int round = (int)floorf(s + 0.5f);
364 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000365
reed@android.comed673312009-02-27 16:24:51 +0000366 int iround = SkMulDiv255Round(a, b);
367 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000368
reed@android.comed673312009-02-27 16:24:51 +0000369 REPORTER_ASSERT(reporter, iround == round);
370 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000371
reed@android.comed673312009-02-27 16:24:51 +0000372 REPORTER_ASSERT(reporter, itrunc <= iround);
373 REPORTER_ASSERT(reporter, iround <= a);
374 REPORTER_ASSERT(reporter, iround <= b);
375 }
376 }
reed@android.comed673312009-02-27 16:24:51 +0000377}
378
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000379static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
380 for (int c = 0; c <= 255; c++) {
381 for (int a = 0; a <= 255; a++) {
382 int product = (c * a + 255);
383 int expected_ceiling = (product + (product >> 8)) >> 8;
384 int webkit_ceiling = (c * a + 254) / 255;
385 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
386 int skia_ceiling = SkMulDiv255Ceiling(c, a);
387 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
388 }
389 }
390}
391
reed@android.comf0ad0862010-02-09 19:18:38 +0000392static void test_copysign(skiatest::Reporter* reporter) {
393 static const int32_t gTriples[] = {
394 // x, y, expected result
395 0, 0, 0,
396 0, 1, 0,
397 0, -1, 0,
398 1, 0, 1,
399 1, 1, 1,
400 1, -1, -1,
401 -1, 0, 1,
402 -1, 1, 1,
403 -1, -1, -1,
404 };
405 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
406 REPORTER_ASSERT(reporter,
407 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000408 float x = (float)gTriples[i];
409 float y = (float)gTriples[i+1];
410 float expected = (float)gTriples[i+2];
411 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000412 }
413
jvanverth@google.comc490f802013-03-04 13:56:38 +0000414 SkMWCRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000415 for (int j = 0; j < 1000; j++) {
416 int ix = rand.nextS();
417 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
418 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
419 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
420 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
421
422 SkScalar sx = rand.nextSScalar1();
423 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
424 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
425 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
426 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
427 }
428}
429
reed@android.com80e39a72009-04-02 16:59:40 +0000430static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000431 int i;
432 int32_t x;
jvanverth@google.comc490f802013-03-04 13:56:38 +0000433 SkMWCRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000434
reed@android.comed673312009-02-27 16:24:51 +0000435 // these should assert
436#if 0
437 SkToS8(128);
438 SkToS8(-129);
439 SkToU8(256);
440 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000441
reed@android.comed673312009-02-27 16:24:51 +0000442 SkToS16(32768);
443 SkToS16(-32769);
444 SkToU16(65536);
445 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000446
reed@android.comed673312009-02-27 16:24:51 +0000447 if (sizeof(size_t) > 4) {
448 SkToS32(4*1024*1024);
449 SkToS32(-4*1024*1024);
450 SkToU32(5*1024*1024);
451 SkToU32(-5);
452 }
453#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000454
reed@android.comed673312009-02-27 16:24:51 +0000455 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000456 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000457 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000458
reed@android.comed673312009-02-27 16:24:51 +0000459 {
460 SkScalar x = SK_ScalarNaN;
461 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
462 }
reed@android.com80e39a72009-04-02 16:59:40 +0000463
reed@android.comed673312009-02-27 16:24:51 +0000464 for (i = 1; i <= 10; i++) {
465 x = SkCubeRootBits(i*i*i, 11);
466 REPORTER_ASSERT(reporter, x == i);
467 }
reed@android.com80e39a72009-04-02 16:59:40 +0000468
reed@android.comed673312009-02-27 16:24:51 +0000469 x = SkFixedSqrt(SK_Fixed1);
470 REPORTER_ASSERT(reporter, x == SK_Fixed1);
471 x = SkFixedSqrt(SK_Fixed1/4);
472 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
473 x = SkFixedSqrt(SK_Fixed1*4);
474 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000475
reed@android.comed673312009-02-27 16:24:51 +0000476 x = SkFractSqrt(SK_Fract1);
477 REPORTER_ASSERT(reporter, x == SK_Fract1);
478 x = SkFractSqrt(SK_Fract1/4);
479 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
480 x = SkFractSqrt(SK_Fract1/16);
481 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000482
reed@android.comed673312009-02-27 16:24:51 +0000483 for (i = 1; i < 100; i++) {
484 x = SkFixedSqrt(SK_Fixed1 * i * i);
485 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
486 }
reed@android.com80e39a72009-04-02 16:59:40 +0000487
reed@android.comed673312009-02-27 16:24:51 +0000488 for (i = 0; i < 1000; i++) {
489 int value = rand.nextS16();
490 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000491
reed@android.comed673312009-02-27 16:24:51 +0000492 int clamp = SkClampMax(value, max);
493 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
494 REPORTER_ASSERT(reporter, clamp == clamp2);
495 }
reed@android.com80e39a72009-04-02 16:59:40 +0000496
reed@android.come72fee52009-11-16 14:52:01 +0000497 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000498 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000499
tomhudson@google.com75589252012-04-10 17:42:21 +0000500 // These random values are being treated as 32-bit-patterns, not as
501 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000502 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000503 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000504 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000505 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000506 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000507 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000508 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000509 check_length(reporter, p, SK_Scalar1);
510 }
reed@android.com80e39a72009-04-02 16:59:40 +0000511
reed@android.comed673312009-02-27 16:24:51 +0000512 {
513 SkFixed result = SkFixedDiv(100, 100);
514 REPORTER_ASSERT(reporter, result == SK_Fixed1);
515 result = SkFixedDiv(1, SK_Fixed1);
516 REPORTER_ASSERT(reporter, result == 1);
517 }
reed@android.com80e39a72009-04-02 16:59:40 +0000518
reed@android.comed673312009-02-27 16:24:51 +0000519 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000520 unittest_isfinite(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000521
reed@android.comed673312009-02-27 16:24:51 +0000522#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000523 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000524 SkFixed numer = rand.nextS();
525 SkFixed denom = rand.nextS();
526 SkFixed result = SkFixedDiv(numer, denom);
527 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000528
reed@android.comed673312009-02-27 16:24:51 +0000529 (void)SkCLZ(numer);
530 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000531
reed@android.comed673312009-02-27 16:24:51 +0000532 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
533 if (check > SK_MaxS32) {
534 check = SK_MaxS32;
535 } else if (check < -SK_MaxS32) {
536 check = SK_MinS32;
537 }
538 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000539
reed@android.comed673312009-02-27 16:24:51 +0000540 result = SkFractDiv(numer, denom);
541 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000542
reed@android.comed673312009-02-27 16:24:51 +0000543 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
544 if (check > SK_MaxS32) {
545 check = SK_MaxS32;
546 } else if (check < -SK_MaxS32) {
547 check = SK_MinS32;
548 }
549 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000550
reed@android.comed673312009-02-27 16:24:51 +0000551 // make them <= 2^24, so we don't overflow in fixmul
552 numer = numer << 8 >> 8;
553 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000554
reed@android.comed673312009-02-27 16:24:51 +0000555 result = SkFixedMul(numer, denom);
556 SkFixed r2 = symmetric_fixmul(numer, denom);
557 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000558
reed@android.comed673312009-02-27 16:24:51 +0000559 result = SkFixedMul(numer, numer);
560 r2 = SkFixedSquare(numer);
561 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000562
reed@android.comed673312009-02-27 16:24:51 +0000563 if (numer >= 0 && denom >= 0) {
564 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000565 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
566 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000567 SkFixed mean2 = SkFloatToFixed(fm);
568 int diff = SkAbs32(mean - mean2);
569 REPORTER_ASSERT(reporter, diff <= 1);
570 }
reed@android.com80e39a72009-04-02 16:59:40 +0000571
reed@android.comed673312009-02-27 16:24:51 +0000572 {
573 SkFixed mod = SkFixedMod(numer, denom);
574 float n = SkFixedToFloat(numer);
575 float d = SkFixedToFloat(denom);
576 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000577 // ensure the same sign
578 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000579 int diff = SkAbs32(mod - SkFloatToFixed(m));
580 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
581 }
reed@android.comed673312009-02-27 16:24:51 +0000582 }
583#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000584
reed@android.come72fee52009-11-16 14:52:01 +0000585 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000586 SkFract x = rand.nextU() >> 1;
587 double xx = (double)x / SK_Fract1;
588 SkFract xr = SkFractSqrt(x);
589 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000590 REPORTER_ASSERT(reporter, xr == check ||
591 xr == check-1 ||
592 xr == check+1);
593
reed@android.comed673312009-02-27 16:24:51 +0000594 xr = SkFixedSqrt(x);
595 xx = (double)x / SK_Fixed1;
596 check = SkFloatToFixed(sqrt(xx));
597 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000598
reed@android.comed673312009-02-27 16:24:51 +0000599 xr = SkSqrt32(x);
600 xx = (double)x;
601 check = (int32_t)sqrt(xx);
602 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
603 }
reed@android.com80e39a72009-04-02 16:59:40 +0000604
reed@google.com7886ad32012-06-11 21:21:26 +0000605#if !defined(SK_SCALAR_IS_FLOAT)
reed@android.comed673312009-02-27 16:24:51 +0000606 {
607 SkFixed s, c;
608 s = SkFixedSinCos(0, &c);
609 REPORTER_ASSERT(reporter, s == 0);
610 REPORTER_ASSERT(reporter, c == SK_Fixed1);
611 }
reed@android.com80e39a72009-04-02 16:59:40 +0000612
reed@android.comed673312009-02-27 16:24:51 +0000613 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000614 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000615 SkFixed rads = rand.nextS() >> 10;
616 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000617
reed@android.comed673312009-02-27 16:24:51 +0000618 SkFixed s, c;
619 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000620
reed@android.comed673312009-02-27 16:24:51 +0000621 double fs = sin(frads);
622 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000623
reed@android.comed673312009-02-27 16:24:51 +0000624 SkFixed is = SkFloatToFixed(fs);
625 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000626
reed@android.comed673312009-02-27 16:24:51 +0000627 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
628 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
629 }
630 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
631#endif
reed@google.com772813a2011-03-30 22:34:45 +0000632
reed@google.com0abb4992011-10-06 20:04:36 +0000633#ifdef SK_SCALAR_IS_FLOAT
634 test_blend(reporter);
635#endif
reed@google.com8d7e39c2011-11-09 17:12:08 +0000636
humper@google.com05af1af2013-01-07 16:47:43 +0000637 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000638
reed@google.com8d7e39c2011-11-09 17:12:08 +0000639 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000640 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000641
642 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000643 test_clz(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000644}
645
reed@android.comd8730ea2009-02-27 22:06:06 +0000646#include "TestClassDef.h"
647DEFINE_TESTCLASS("Math", MathTestClass, TestMath)