blob: 4bfbd94283f3ec8eacd78bdf71c5a78d3f4a9341 [file] [log] [blame]
reed@android.comed673312009-02-27 16:24:51 +00001#include "Test.h"
reed@android.comc846ede2010-04-13 15:29:15 +00002#include "SkFloatingPoint.h"
reed@android.comed673312009-02-27 16:24:51 +00003#include "SkPoint.h"
4#include "SkRandom.h"
5
6#if defined(SkLONGLONG)
7static int symmetric_fixmul(int a, int b) {
8 int sa = SkExtractSign(a);
9 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +000010
reed@android.comed673312009-02-27 16:24:51 +000011 a = SkApplySign(a, sa);
12 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +000013
reed@android.comed673312009-02-27 16:24:51 +000014#if 1
15 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +000016
reed@android.comed673312009-02-27 16:24:51 +000017 return SkApplySign(c, sa ^ sb);
18#else
19 SkLONGLONG ab = (SkLONGLONG)a * b;
20 if (sa ^ sb) {
21 ab = -ab;
22 }
23 return ab >> 16;
24#endif
25}
26#endif
27
28static void check_length(skiatest::Reporter* reporter,
29 const SkPoint& p, SkScalar targetLen) {
30#ifdef SK_CAN_USE_FLOAT
31 float x = SkScalarToFloat(p.fX);
32 float y = SkScalarToFloat(p.fY);
33 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +000034
reed@android.comed673312009-02-27 16:24:51 +000035 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +000036
reed@android.comed673312009-02-27 16:24:51 +000037 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
38#endif
39}
40
41#if defined(SK_CAN_USE_FLOAT)
42
43static float nextFloat(SkRandom& rand) {
44 SkFloatIntUnion data;
45 data.fSignBitInt = rand.nextU();
46 return data.fFloat;
47}
48
49/* returns true if a == b as resulting from (int)x. Since it is undefined
50 what to do if the float exceeds 2^32-1, we check for that explicitly.
51 */
52static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
53 if (!(x == x)) { // NAN
54 return si == SK_MaxS32 || si == SK_MinS32;
55 }
56 // for out of range, C is undefined, but skia always should return NaN32
57 if (x > SK_MaxS32) {
58 return si == SK_MaxS32;
59 }
60 if (x < -SK_MaxS32) {
61 return si == SK_MinS32;
62 }
63 return si == ni;
64}
65
66static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
67 float x, uint32_t ni, uint32_t si) {
68 if (!equal_float_native_skia(x, ni, si)) {
69 SkString desc;
70 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si);
71 reporter->reportFailed(desc);
72 }
73}
74
75static void test_float_cast(skiatest::Reporter* reporter, float x) {
76 int ix = (int)x;
77 int iix = SkFloatToIntCast(x);
78 assert_float_equal(reporter, "cast", x, ix, iix);
79}
80
81static void test_float_floor(skiatest::Reporter* reporter, float x) {
82 int ix = (int)floor(x);
83 int iix = SkFloatToIntFloor(x);
84 assert_float_equal(reporter, "floor", x, ix, iix);
85}
86
87static void test_float_round(skiatest::Reporter* reporter, float x) {
88 double xx = x + 0.5; // need intermediate double to avoid temp loss
89 int ix = (int)floor(xx);
90 int iix = SkFloatToIntRound(x);
91 assert_float_equal(reporter, "round", x, ix, iix);
92}
93
94static void test_float_ceil(skiatest::Reporter* reporter, float x) {
95 int ix = (int)ceil(x);
96 int iix = SkFloatToIntCeil(x);
97 assert_float_equal(reporter, "ceil", x, ix, iix);
98}
99
100static void test_float_conversions(skiatest::Reporter* reporter, float x) {
101 test_float_cast(reporter, x);
102 test_float_floor(reporter, x);
103 test_float_round(reporter, x);
104 test_float_ceil(reporter, x);
105}
106
107static void test_int2float(skiatest::Reporter* reporter, int ival) {
108 float x0 = (float)ival;
109 float x1 = SkIntToFloatCast(ival);
110 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
111 REPORTER_ASSERT(reporter, x0 == x1);
112 REPORTER_ASSERT(reporter, x0 == x2);
113}
114
115static void unittest_fastfloat(skiatest::Reporter* reporter) {
116 SkRandom rand;
117 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000118
reed@android.comed673312009-02-27 16:24:51 +0000119 static const float gFloats[] = {
120 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
121 0.000000001f, 1000000000.f, // doesn't overflow
122 0.0000000001f, 10000000000.f // does overflow
123 };
124 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000125 test_float_conversions(reporter, gFloats[i]);
126 test_float_conversions(reporter, -gFloats[i]);
127 }
reed@android.com80e39a72009-04-02 16:59:40 +0000128
reed@android.comed673312009-02-27 16:24:51 +0000129 for (int outer = 0; outer < 100; outer++) {
130 rand.setSeed(outer);
131 for (i = 0; i < 100000; i++) {
132 float x = nextFloat(rand);
133 test_float_conversions(reporter, x);
134 }
reed@android.com80e39a72009-04-02 16:59:40 +0000135
reed@android.comed673312009-02-27 16:24:51 +0000136 test_int2float(reporter, 0);
137 test_int2float(reporter, 1);
138 test_int2float(reporter, -1);
139 for (i = 0; i < 100000; i++) {
140 // for now only test ints that are 24bits or less, since we don't
141 // round (down) large ints the same as IEEE...
142 int ival = rand.nextU() & 0xFFFFFF;
143 test_int2float(reporter, ival);
144 test_int2float(reporter, -ival);
145 }
146 }
147}
148
149#endif
150
151static void test_muldiv255(skiatest::Reporter* reporter) {
152#ifdef SK_CAN_USE_FLOAT
153 for (int a = 0; a <= 255; a++) {
154 for (int b = 0; b <= 255; b++) {
155 int ab = a * b;
156 float s = ab / 255.0f;
157 int round = (int)floorf(s + 0.5f);
158 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000159
reed@android.comed673312009-02-27 16:24:51 +0000160 int iround = SkMulDiv255Round(a, b);
161 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000162
reed@android.comed673312009-02-27 16:24:51 +0000163 REPORTER_ASSERT(reporter, iround == round);
164 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000165
reed@android.comed673312009-02-27 16:24:51 +0000166 REPORTER_ASSERT(reporter, itrunc <= iround);
167 REPORTER_ASSERT(reporter, iround <= a);
168 REPORTER_ASSERT(reporter, iround <= b);
169 }
170 }
171#endif
172}
173
reed@android.comf0ad0862010-02-09 19:18:38 +0000174static void test_copysign(skiatest::Reporter* reporter) {
175 static const int32_t gTriples[] = {
176 // x, y, expected result
177 0, 0, 0,
178 0, 1, 0,
179 0, -1, 0,
180 1, 0, 1,
181 1, 1, 1,
182 1, -1, -1,
183 -1, 0, 1,
184 -1, 1, 1,
185 -1, -1, -1,
186 };
187 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
188 REPORTER_ASSERT(reporter,
189 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
190#ifdef SK_CAN_USE_FLOAT
191 float x = (float)gTriples[i];
192 float y = (float)gTriples[i+1];
193 float expected = (float)gTriples[i+2];
194 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
195#endif
196 }
197
198 SkRandom rand;
199 for (int j = 0; j < 1000; j++) {
200 int ix = rand.nextS();
201 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
202 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
203 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
204 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
205
206 SkScalar sx = rand.nextSScalar1();
207 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
208 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
209 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
210 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
211 }
212}
213
reed@android.com80e39a72009-04-02 16:59:40 +0000214static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000215 int i;
216 int32_t x;
217 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000218
reed@android.comed673312009-02-27 16:24:51 +0000219 // these should assert
220#if 0
221 SkToS8(128);
222 SkToS8(-129);
223 SkToU8(256);
224 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000225
reed@android.comed673312009-02-27 16:24:51 +0000226 SkToS16(32768);
227 SkToS16(-32769);
228 SkToU16(65536);
229 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000230
reed@android.comed673312009-02-27 16:24:51 +0000231 if (sizeof(size_t) > 4) {
232 SkToS32(4*1024*1024);
233 SkToS32(-4*1024*1024);
234 SkToU32(5*1024*1024);
235 SkToU32(-5);
236 }
237#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000238
reed@android.comed673312009-02-27 16:24:51 +0000239 test_muldiv255(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000240 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000241
reed@android.comed673312009-02-27 16:24:51 +0000242 {
243 SkScalar x = SK_ScalarNaN;
244 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
245 }
reed@android.com80e39a72009-04-02 16:59:40 +0000246
reed@android.comed673312009-02-27 16:24:51 +0000247 for (i = 1; i <= 10; i++) {
248 x = SkCubeRootBits(i*i*i, 11);
249 REPORTER_ASSERT(reporter, x == i);
250 }
reed@android.com80e39a72009-04-02 16:59:40 +0000251
reed@android.comed673312009-02-27 16:24:51 +0000252 x = SkFixedSqrt(SK_Fixed1);
253 REPORTER_ASSERT(reporter, x == SK_Fixed1);
254 x = SkFixedSqrt(SK_Fixed1/4);
255 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
256 x = SkFixedSqrt(SK_Fixed1*4);
257 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000258
reed@android.comed673312009-02-27 16:24:51 +0000259 x = SkFractSqrt(SK_Fract1);
260 REPORTER_ASSERT(reporter, x == SK_Fract1);
261 x = SkFractSqrt(SK_Fract1/4);
262 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
263 x = SkFractSqrt(SK_Fract1/16);
264 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000265
reed@android.comed673312009-02-27 16:24:51 +0000266 for (i = 1; i < 100; i++) {
267 x = SkFixedSqrt(SK_Fixed1 * i * i);
268 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
269 }
reed@android.com80e39a72009-04-02 16:59:40 +0000270
reed@android.comed673312009-02-27 16:24:51 +0000271 for (i = 0; i < 1000; i++) {
272 int value = rand.nextS16();
273 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000274
reed@android.comed673312009-02-27 16:24:51 +0000275 int clamp = SkClampMax(value, max);
276 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
277 REPORTER_ASSERT(reporter, clamp == clamp2);
278 }
reed@android.com80e39a72009-04-02 16:59:40 +0000279
reed@android.come72fee52009-11-16 14:52:01 +0000280 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000281 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000282
reed@android.comed673312009-02-27 16:24:51 +0000283 p.setLength(rand.nextS(), rand.nextS(), SK_Scalar1);
284 check_length(reporter, p, SK_Scalar1);
285 p.setLength(rand.nextS() >> 13, rand.nextS() >> 13, SK_Scalar1);
286 check_length(reporter, p, SK_Scalar1);
287 }
reed@android.com80e39a72009-04-02 16:59:40 +0000288
reed@android.comed673312009-02-27 16:24:51 +0000289 {
290 SkFixed result = SkFixedDiv(100, 100);
291 REPORTER_ASSERT(reporter, result == SK_Fixed1);
292 result = SkFixedDiv(1, SK_Fixed1);
293 REPORTER_ASSERT(reporter, result == 1);
294 }
reed@android.com80e39a72009-04-02 16:59:40 +0000295
reed@android.comed673312009-02-27 16:24:51 +0000296#ifdef SK_CAN_USE_FLOAT
297 unittest_fastfloat(reporter);
298#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000299
reed@android.comed673312009-02-27 16:24:51 +0000300#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000301 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000302 SkFixed numer = rand.nextS();
303 SkFixed denom = rand.nextS();
304 SkFixed result = SkFixedDiv(numer, denom);
305 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000306
reed@android.comed673312009-02-27 16:24:51 +0000307 (void)SkCLZ(numer);
308 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000309
reed@android.comed673312009-02-27 16:24:51 +0000310 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
311 if (check > SK_MaxS32) {
312 check = SK_MaxS32;
313 } else if (check < -SK_MaxS32) {
314 check = SK_MinS32;
315 }
316 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000317
reed@android.comed673312009-02-27 16:24:51 +0000318 result = SkFractDiv(numer, denom);
319 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000320
reed@android.comed673312009-02-27 16:24:51 +0000321 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
322 if (check > SK_MaxS32) {
323 check = SK_MaxS32;
324 } else if (check < -SK_MaxS32) {
325 check = SK_MinS32;
326 }
327 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000328
reed@android.comed673312009-02-27 16:24:51 +0000329 // make them <= 2^24, so we don't overflow in fixmul
330 numer = numer << 8 >> 8;
331 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000332
reed@android.comed673312009-02-27 16:24:51 +0000333 result = SkFixedMul(numer, denom);
334 SkFixed r2 = symmetric_fixmul(numer, denom);
335 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000336
reed@android.comed673312009-02-27 16:24:51 +0000337 result = SkFixedMul(numer, numer);
338 r2 = SkFixedSquare(numer);
339 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000340
reed@android.comed673312009-02-27 16:24:51 +0000341#ifdef SK_CAN_USE_FLOAT
342 if (numer >= 0 && denom >= 0) {
343 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000344 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
345 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000346 SkFixed mean2 = SkFloatToFixed(fm);
347 int diff = SkAbs32(mean - mean2);
348 REPORTER_ASSERT(reporter, diff <= 1);
349 }
reed@android.com80e39a72009-04-02 16:59:40 +0000350
reed@android.comed673312009-02-27 16:24:51 +0000351 {
352 SkFixed mod = SkFixedMod(numer, denom);
353 float n = SkFixedToFloat(numer);
354 float d = SkFixedToFloat(denom);
355 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000356 // ensure the same sign
357 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000358 int diff = SkAbs32(mod - SkFloatToFixed(m));
359 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
360 }
361#endif
362 }
363#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000364
reed@android.comed673312009-02-27 16:24:51 +0000365#ifdef SK_CAN_USE_FLOAT
reed@android.come72fee52009-11-16 14:52:01 +0000366 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000367 SkFract x = rand.nextU() >> 1;
368 double xx = (double)x / SK_Fract1;
369 SkFract xr = SkFractSqrt(x);
370 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000371 REPORTER_ASSERT(reporter, xr == check ||
372 xr == check-1 ||
373 xr == check+1);
374
reed@android.comed673312009-02-27 16:24:51 +0000375 xr = SkFixedSqrt(x);
376 xx = (double)x / SK_Fixed1;
377 check = SkFloatToFixed(sqrt(xx));
378 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000379
reed@android.comed673312009-02-27 16:24:51 +0000380 xr = SkSqrt32(x);
381 xx = (double)x;
382 check = (int32_t)sqrt(xx);
383 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
384 }
385#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000386
reed@android.comed673312009-02-27 16:24:51 +0000387#if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT)
388 {
389 SkFixed s, c;
390 s = SkFixedSinCos(0, &c);
391 REPORTER_ASSERT(reporter, s == 0);
392 REPORTER_ASSERT(reporter, c == SK_Fixed1);
393 }
reed@android.com80e39a72009-04-02 16:59:40 +0000394
reed@android.comed673312009-02-27 16:24:51 +0000395 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000396 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000397 SkFixed rads = rand.nextS() >> 10;
398 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000399
reed@android.comed673312009-02-27 16:24:51 +0000400 SkFixed s, c;
401 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000402
reed@android.comed673312009-02-27 16:24:51 +0000403 double fs = sin(frads);
404 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000405
reed@android.comed673312009-02-27 16:24:51 +0000406 SkFixed is = SkFloatToFixed(fs);
407 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000408
reed@android.comed673312009-02-27 16:24:51 +0000409 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
410 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
411 }
412 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
413#endif
414}
415
reed@android.comd8730ea2009-02-27 22:06:06 +0000416#include "TestClassDef.h"
417DEFINE_TESTCLASS("Math", MathTestClass, TestMath)