blob: 1539cd7ccbe93330712b6f2dae68a1bbf155bfe1 [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.com0abb4992011-10-06 20:04:36 +000019static void test_blend(skiatest::Reporter* reporter) {
20 for (int src = 0; src <= 255; src++) {
21 for (int dst = 0; dst <= 255; dst++) {
22 for (int a = 0; a <= 255; a++) {
23 int r0 = SkAlphaBlend255(src, dst, a);
24 float f1 = float_blend(src, dst, a / 255.f);
reed@google.com111c19b2011-10-06 20:13:22 +000025 int r1 = SkScalarRoundToInt(SkFloatToScalar(f1));
reed@google.com772813a2011-03-30 22:34:45 +000026
reed@google.com0abb4992011-10-06 20:04:36 +000027 if (r0 != r1) {
28 float diff = sk_float_abs(f1 - r1);
29 diff = sk_float_abs(diff - 0.5f);
30 if (diff > (1 / 255.f)) {
31#ifdef SK_DEBUG
32 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
33 src, dst, a, r0, f1);
34#endif
35 REPORTER_ASSERT(reporter, false);
36 }
37 }
reed@google.com772813a2011-03-30 22:34:45 +000038 }
39 }
40 }
41}
reed@google.com772813a2011-03-30 22:34:45 +000042
reed@android.comed673312009-02-27 16:24:51 +000043#if defined(SkLONGLONG)
44static int symmetric_fixmul(int a, int b) {
45 int sa = SkExtractSign(a);
46 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +000047
reed@android.comed673312009-02-27 16:24:51 +000048 a = SkApplySign(a, sa);
49 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +000050
reed@android.comed673312009-02-27 16:24:51 +000051#if 1
52 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +000053
reed@android.comed673312009-02-27 16:24:51 +000054 return SkApplySign(c, sa ^ sb);
55#else
56 SkLONGLONG ab = (SkLONGLONG)a * b;
57 if (sa ^ sb) {
58 ab = -ab;
59 }
60 return ab >> 16;
61#endif
62}
63#endif
64
65static void check_length(skiatest::Reporter* reporter,
66 const SkPoint& p, SkScalar targetLen) {
67#ifdef SK_CAN_USE_FLOAT
68 float x = SkScalarToFloat(p.fX);
69 float y = SkScalarToFloat(p.fY);
70 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +000071
reed@android.comed673312009-02-27 16:24:51 +000072 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +000073
reed@android.comed673312009-02-27 16:24:51 +000074 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
75#endif
76}
77
78#if defined(SK_CAN_USE_FLOAT)
79
80static float nextFloat(SkRandom& rand) {
81 SkFloatIntUnion data;
82 data.fSignBitInt = rand.nextU();
83 return data.fFloat;
84}
85
86/* returns true if a == b as resulting from (int)x. Since it is undefined
87 what to do if the float exceeds 2^32-1, we check for that explicitly.
88 */
89static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
90 if (!(x == x)) { // NAN
91 return si == SK_MaxS32 || si == SK_MinS32;
92 }
93 // for out of range, C is undefined, but skia always should return NaN32
94 if (x > SK_MaxS32) {
95 return si == SK_MaxS32;
96 }
97 if (x < -SK_MaxS32) {
98 return si == SK_MinS32;
99 }
100 return si == ni;
101}
102
103static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
104 float x, uint32_t ni, uint32_t si) {
105 if (!equal_float_native_skia(x, ni, si)) {
106 SkString desc;
107 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si);
108 reporter->reportFailed(desc);
109 }
110}
111
112static void test_float_cast(skiatest::Reporter* reporter, float x) {
113 int ix = (int)x;
114 int iix = SkFloatToIntCast(x);
115 assert_float_equal(reporter, "cast", x, ix, iix);
116}
117
118static void test_float_floor(skiatest::Reporter* reporter, float x) {
119 int ix = (int)floor(x);
120 int iix = SkFloatToIntFloor(x);
121 assert_float_equal(reporter, "floor", x, ix, iix);
122}
123
124static void test_float_round(skiatest::Reporter* reporter, float x) {
125 double xx = x + 0.5; // need intermediate double to avoid temp loss
126 int ix = (int)floor(xx);
127 int iix = SkFloatToIntRound(x);
128 assert_float_equal(reporter, "round", x, ix, iix);
129}
130
131static void test_float_ceil(skiatest::Reporter* reporter, float x) {
132 int ix = (int)ceil(x);
133 int iix = SkFloatToIntCeil(x);
134 assert_float_equal(reporter, "ceil", x, ix, iix);
135}
136
137static void test_float_conversions(skiatest::Reporter* reporter, float x) {
138 test_float_cast(reporter, x);
139 test_float_floor(reporter, x);
140 test_float_round(reporter, x);
141 test_float_ceil(reporter, x);
142}
143
144static void test_int2float(skiatest::Reporter* reporter, int ival) {
145 float x0 = (float)ival;
146 float x1 = SkIntToFloatCast(ival);
147 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
148 REPORTER_ASSERT(reporter, x0 == x1);
149 REPORTER_ASSERT(reporter, x0 == x2);
150}
151
152static void unittest_fastfloat(skiatest::Reporter* reporter) {
153 SkRandom rand;
154 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000155
reed@android.comed673312009-02-27 16:24:51 +0000156 static const float gFloats[] = {
157 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
158 0.000000001f, 1000000000.f, // doesn't overflow
159 0.0000000001f, 10000000000.f // does overflow
160 };
161 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000162 test_float_conversions(reporter, gFloats[i]);
163 test_float_conversions(reporter, -gFloats[i]);
164 }
reed@android.com80e39a72009-04-02 16:59:40 +0000165
reed@android.comed673312009-02-27 16:24:51 +0000166 for (int outer = 0; outer < 100; outer++) {
167 rand.setSeed(outer);
168 for (i = 0; i < 100000; i++) {
169 float x = nextFloat(rand);
170 test_float_conversions(reporter, x);
171 }
reed@android.com80e39a72009-04-02 16:59:40 +0000172
reed@android.comed673312009-02-27 16:24:51 +0000173 test_int2float(reporter, 0);
174 test_int2float(reporter, 1);
175 test_int2float(reporter, -1);
176 for (i = 0; i < 100000; i++) {
177 // for now only test ints that are 24bits or less, since we don't
178 // round (down) large ints the same as IEEE...
179 int ival = rand.nextU() & 0xFFFFFF;
180 test_int2float(reporter, ival);
181 test_int2float(reporter, -ival);
182 }
183 }
184}
185
reed@android.comd4134452011-02-09 02:24:26 +0000186#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000187static float make_zero() {
188 return sk_float_sin(0);
189}
reed@android.comd4134452011-02-09 02:24:26 +0000190#endif
reed@google.com077910e2011-02-08 21:56:39 +0000191
192static void unittest_isfinite(skiatest::Reporter* reporter) {
193#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000194 float nan = sk_float_asin(2);
reed@google.com077910e2011-02-08 21:56:39 +0000195 float inf = 1.0 / make_zero();
196 float big = 3.40282e+038;
197
reed@google.com077910e2011-02-08 21:56:39 +0000198 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000199 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
200 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
201 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
202#else
203 SkFixed nan = SK_FixedNaN;
204 SkFixed big = SK_FixedMax;
205#endif
206
207 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000208 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
209 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
210 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
reed@android.comd4134452011-02-09 02:24:26 +0000211
reed@google.com077910e2011-02-08 21:56:39 +0000212 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000213 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
214 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
215 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000216}
217
reed@android.comed673312009-02-27 16:24:51 +0000218#endif
219
220static void test_muldiv255(skiatest::Reporter* reporter) {
221#ifdef SK_CAN_USE_FLOAT
222 for (int a = 0; a <= 255; a++) {
223 for (int b = 0; b <= 255; b++) {
224 int ab = a * b;
225 float s = ab / 255.0f;
226 int round = (int)floorf(s + 0.5f);
227 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000228
reed@android.comed673312009-02-27 16:24:51 +0000229 int iround = SkMulDiv255Round(a, b);
230 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000231
reed@android.comed673312009-02-27 16:24:51 +0000232 REPORTER_ASSERT(reporter, iround == round);
233 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000234
reed@android.comed673312009-02-27 16:24:51 +0000235 REPORTER_ASSERT(reporter, itrunc <= iround);
236 REPORTER_ASSERT(reporter, iround <= a);
237 REPORTER_ASSERT(reporter, iround <= b);
238 }
239 }
240#endif
241}
242
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000243static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
244 for (int c = 0; c <= 255; c++) {
245 for (int a = 0; a <= 255; a++) {
246 int product = (c * a + 255);
247 int expected_ceiling = (product + (product >> 8)) >> 8;
248 int webkit_ceiling = (c * a + 254) / 255;
249 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
250 int skia_ceiling = SkMulDiv255Ceiling(c, a);
251 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
252 }
253 }
254}
255
reed@android.comf0ad0862010-02-09 19:18:38 +0000256static void test_copysign(skiatest::Reporter* reporter) {
257 static const int32_t gTriples[] = {
258 // x, y, expected result
259 0, 0, 0,
260 0, 1, 0,
261 0, -1, 0,
262 1, 0, 1,
263 1, 1, 1,
264 1, -1, -1,
265 -1, 0, 1,
266 -1, 1, 1,
267 -1, -1, -1,
268 };
269 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
270 REPORTER_ASSERT(reporter,
271 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
272#ifdef SK_CAN_USE_FLOAT
273 float x = (float)gTriples[i];
274 float y = (float)gTriples[i+1];
275 float expected = (float)gTriples[i+2];
276 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
277#endif
278 }
279
280 SkRandom rand;
281 for (int j = 0; j < 1000; j++) {
282 int ix = rand.nextS();
283 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
284 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
285 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
286 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
287
288 SkScalar sx = rand.nextSScalar1();
289 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
290 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
291 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
292 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
293 }
294}
295
reed@android.com80e39a72009-04-02 16:59:40 +0000296static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000297 int i;
298 int32_t x;
299 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000300
reed@android.comed673312009-02-27 16:24:51 +0000301 // these should assert
302#if 0
303 SkToS8(128);
304 SkToS8(-129);
305 SkToU8(256);
306 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000307
reed@android.comed673312009-02-27 16:24:51 +0000308 SkToS16(32768);
309 SkToS16(-32769);
310 SkToU16(65536);
311 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000312
reed@android.comed673312009-02-27 16:24:51 +0000313 if (sizeof(size_t) > 4) {
314 SkToS32(4*1024*1024);
315 SkToS32(-4*1024*1024);
316 SkToU32(5*1024*1024);
317 SkToU32(-5);
318 }
319#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000320
reed@android.comed673312009-02-27 16:24:51 +0000321 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000322 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000323 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000324
reed@android.comed673312009-02-27 16:24:51 +0000325 {
326 SkScalar x = SK_ScalarNaN;
327 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
328 }
reed@android.com80e39a72009-04-02 16:59:40 +0000329
reed@android.comed673312009-02-27 16:24:51 +0000330 for (i = 1; i <= 10; i++) {
331 x = SkCubeRootBits(i*i*i, 11);
332 REPORTER_ASSERT(reporter, x == i);
333 }
reed@android.com80e39a72009-04-02 16:59:40 +0000334
reed@android.comed673312009-02-27 16:24:51 +0000335 x = SkFixedSqrt(SK_Fixed1);
336 REPORTER_ASSERT(reporter, x == SK_Fixed1);
337 x = SkFixedSqrt(SK_Fixed1/4);
338 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
339 x = SkFixedSqrt(SK_Fixed1*4);
340 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000341
reed@android.comed673312009-02-27 16:24:51 +0000342 x = SkFractSqrt(SK_Fract1);
343 REPORTER_ASSERT(reporter, x == SK_Fract1);
344 x = SkFractSqrt(SK_Fract1/4);
345 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
346 x = SkFractSqrt(SK_Fract1/16);
347 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000348
reed@android.comed673312009-02-27 16:24:51 +0000349 for (i = 1; i < 100; i++) {
350 x = SkFixedSqrt(SK_Fixed1 * i * i);
351 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
352 }
reed@android.com80e39a72009-04-02 16:59:40 +0000353
reed@android.comed673312009-02-27 16:24:51 +0000354 for (i = 0; i < 1000; i++) {
355 int value = rand.nextS16();
356 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000357
reed@android.comed673312009-02-27 16:24:51 +0000358 int clamp = SkClampMax(value, max);
359 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
360 REPORTER_ASSERT(reporter, clamp == clamp2);
361 }
reed@android.com80e39a72009-04-02 16:59:40 +0000362
reed@android.come72fee52009-11-16 14:52:01 +0000363 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000364 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000365
reed@android.comed673312009-02-27 16:24:51 +0000366 p.setLength(rand.nextS(), rand.nextS(), SK_Scalar1);
367 check_length(reporter, p, SK_Scalar1);
368 p.setLength(rand.nextS() >> 13, rand.nextS() >> 13, SK_Scalar1);
369 check_length(reporter, p, SK_Scalar1);
370 }
reed@android.com80e39a72009-04-02 16:59:40 +0000371
reed@android.comed673312009-02-27 16:24:51 +0000372 {
373 SkFixed result = SkFixedDiv(100, 100);
374 REPORTER_ASSERT(reporter, result == SK_Fixed1);
375 result = SkFixedDiv(1, SK_Fixed1);
376 REPORTER_ASSERT(reporter, result == 1);
377 }
reed@android.com80e39a72009-04-02 16:59:40 +0000378
reed@android.comed673312009-02-27 16:24:51 +0000379#ifdef SK_CAN_USE_FLOAT
380 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000381 unittest_isfinite(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000382#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000383
reed@android.comed673312009-02-27 16:24:51 +0000384#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000385 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000386 SkFixed numer = rand.nextS();
387 SkFixed denom = rand.nextS();
388 SkFixed result = SkFixedDiv(numer, denom);
389 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000390
reed@android.comed673312009-02-27 16:24:51 +0000391 (void)SkCLZ(numer);
392 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000393
reed@android.comed673312009-02-27 16:24:51 +0000394 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
395 if (check > SK_MaxS32) {
396 check = SK_MaxS32;
397 } else if (check < -SK_MaxS32) {
398 check = SK_MinS32;
399 }
400 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000401
reed@android.comed673312009-02-27 16:24:51 +0000402 result = SkFractDiv(numer, denom);
403 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000404
reed@android.comed673312009-02-27 16:24:51 +0000405 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
406 if (check > SK_MaxS32) {
407 check = SK_MaxS32;
408 } else if (check < -SK_MaxS32) {
409 check = SK_MinS32;
410 }
411 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000412
reed@android.comed673312009-02-27 16:24:51 +0000413 // make them <= 2^24, so we don't overflow in fixmul
414 numer = numer << 8 >> 8;
415 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000416
reed@android.comed673312009-02-27 16:24:51 +0000417 result = SkFixedMul(numer, denom);
418 SkFixed r2 = symmetric_fixmul(numer, denom);
419 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000420
reed@android.comed673312009-02-27 16:24:51 +0000421 result = SkFixedMul(numer, numer);
422 r2 = SkFixedSquare(numer);
423 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000424
reed@android.comed673312009-02-27 16:24:51 +0000425#ifdef SK_CAN_USE_FLOAT
426 if (numer >= 0 && denom >= 0) {
427 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000428 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
429 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000430 SkFixed mean2 = SkFloatToFixed(fm);
431 int diff = SkAbs32(mean - mean2);
432 REPORTER_ASSERT(reporter, diff <= 1);
433 }
reed@android.com80e39a72009-04-02 16:59:40 +0000434
reed@android.comed673312009-02-27 16:24:51 +0000435 {
436 SkFixed mod = SkFixedMod(numer, denom);
437 float n = SkFixedToFloat(numer);
438 float d = SkFixedToFloat(denom);
439 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000440 // ensure the same sign
441 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000442 int diff = SkAbs32(mod - SkFloatToFixed(m));
443 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
444 }
445#endif
446 }
447#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000448
reed@android.comed673312009-02-27 16:24:51 +0000449#ifdef SK_CAN_USE_FLOAT
reed@android.come72fee52009-11-16 14:52:01 +0000450 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000451 SkFract x = rand.nextU() >> 1;
452 double xx = (double)x / SK_Fract1;
453 SkFract xr = SkFractSqrt(x);
454 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000455 REPORTER_ASSERT(reporter, xr == check ||
456 xr == check-1 ||
457 xr == check+1);
458
reed@android.comed673312009-02-27 16:24:51 +0000459 xr = SkFixedSqrt(x);
460 xx = (double)x / SK_Fixed1;
461 check = SkFloatToFixed(sqrt(xx));
462 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000463
reed@android.comed673312009-02-27 16:24:51 +0000464 xr = SkSqrt32(x);
465 xx = (double)x;
466 check = (int32_t)sqrt(xx);
467 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
468 }
469#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000470
reed@android.comed673312009-02-27 16:24:51 +0000471#if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT)
472 {
473 SkFixed s, c;
474 s = SkFixedSinCos(0, &c);
475 REPORTER_ASSERT(reporter, s == 0);
476 REPORTER_ASSERT(reporter, c == SK_Fixed1);
477 }
reed@android.com80e39a72009-04-02 16:59:40 +0000478
reed@android.comed673312009-02-27 16:24:51 +0000479 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000480 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000481 SkFixed rads = rand.nextS() >> 10;
482 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000483
reed@android.comed673312009-02-27 16:24:51 +0000484 SkFixed s, c;
485 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000486
reed@android.comed673312009-02-27 16:24:51 +0000487 double fs = sin(frads);
488 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000489
reed@android.comed673312009-02-27 16:24:51 +0000490 SkFixed is = SkFloatToFixed(fs);
491 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000492
reed@android.comed673312009-02-27 16:24:51 +0000493 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
494 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
495 }
496 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
497#endif
reed@google.com772813a2011-03-30 22:34:45 +0000498
reed@google.com0abb4992011-10-06 20:04:36 +0000499#ifdef SK_SCALAR_IS_FLOAT
500 test_blend(reporter);
501#endif
reed@android.comed673312009-02-27 16:24:51 +0000502}
503
reed@android.comd8730ea2009-02-27 22:06:06 +0000504#include "TestClassDef.h"
505DEFINE_TESTCLASS("Math", MathTestClass, TestMath)