blob: efdad3aca24616920f0b78d9349ffd7b6c0534e5 [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
reed@google.com772813a2011-03-30 22:34:45 +00006#if 0
7static U8CPU premul_fast(U8CPU a, U8CPU x) {
8 return a * x * 32897 >> 23;
9}
10
11static U8CPU premul_trunc(U8CPU a, U8CPU x) {
12 double result = a * x;
13 result /= 255.0;
14 return (unsigned)floor(result + 0.0);
15}
16
17static U8CPU premul_round(U8CPU a, U8CPU x) {
18 double result = a * x;
19 result /= 255.0;
20 return (unsigned)floor(result + 0.5);
21}
22
23static void test_premul(skiatest::Reporter* reporter) {
24 for (int a = 0; a <= 255; a++) {
25 for (int x = 0; x <= 255; x++) {
26 unsigned curr_trunc = SkMulDiv255Trunc(a, x);
27 unsigned curr_round = SkMulDiv255Round(a, x);
28 unsigned fast = premul_fast(a, x);
29 unsigned slow_round = premul_round(a, x);
30 unsigned slow_trunc = premul_trunc(a, x);
31 if (fast != slow || curr != fast) {
32 SkDebugf("---- premul(%d %d) curr=%d fast=%d slow=%d\n", a, x,
33 curr, fast, slow);
34 }
35 }
36 }
37}
38#endif
39
reed@android.comed673312009-02-27 16:24:51 +000040#if defined(SkLONGLONG)
41static int symmetric_fixmul(int a, int b) {
42 int sa = SkExtractSign(a);
43 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +000044
reed@android.comed673312009-02-27 16:24:51 +000045 a = SkApplySign(a, sa);
46 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +000047
reed@android.comed673312009-02-27 16:24:51 +000048#if 1
49 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +000050
reed@android.comed673312009-02-27 16:24:51 +000051 return SkApplySign(c, sa ^ sb);
52#else
53 SkLONGLONG ab = (SkLONGLONG)a * b;
54 if (sa ^ sb) {
55 ab = -ab;
56 }
57 return ab >> 16;
58#endif
59}
60#endif
61
62static void check_length(skiatest::Reporter* reporter,
63 const SkPoint& p, SkScalar targetLen) {
64#ifdef SK_CAN_USE_FLOAT
65 float x = SkScalarToFloat(p.fX);
66 float y = SkScalarToFloat(p.fY);
67 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +000068
reed@android.comed673312009-02-27 16:24:51 +000069 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +000070
reed@android.comed673312009-02-27 16:24:51 +000071 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
72#endif
73}
74
75#if defined(SK_CAN_USE_FLOAT)
76
77static float nextFloat(SkRandom& rand) {
78 SkFloatIntUnion data;
79 data.fSignBitInt = rand.nextU();
80 return data.fFloat;
81}
82
83/* returns true if a == b as resulting from (int)x. Since it is undefined
84 what to do if the float exceeds 2^32-1, we check for that explicitly.
85 */
86static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
87 if (!(x == x)) { // NAN
88 return si == SK_MaxS32 || si == SK_MinS32;
89 }
90 // for out of range, C is undefined, but skia always should return NaN32
91 if (x > SK_MaxS32) {
92 return si == SK_MaxS32;
93 }
94 if (x < -SK_MaxS32) {
95 return si == SK_MinS32;
96 }
97 return si == ni;
98}
99
100static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
101 float x, uint32_t ni, uint32_t si) {
102 if (!equal_float_native_skia(x, ni, si)) {
103 SkString desc;
104 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si);
105 reporter->reportFailed(desc);
106 }
107}
108
109static void test_float_cast(skiatest::Reporter* reporter, float x) {
110 int ix = (int)x;
111 int iix = SkFloatToIntCast(x);
112 assert_float_equal(reporter, "cast", x, ix, iix);
113}
114
115static void test_float_floor(skiatest::Reporter* reporter, float x) {
116 int ix = (int)floor(x);
117 int iix = SkFloatToIntFloor(x);
118 assert_float_equal(reporter, "floor", x, ix, iix);
119}
120
121static void test_float_round(skiatest::Reporter* reporter, float x) {
122 double xx = x + 0.5; // need intermediate double to avoid temp loss
123 int ix = (int)floor(xx);
124 int iix = SkFloatToIntRound(x);
125 assert_float_equal(reporter, "round", x, ix, iix);
126}
127
128static void test_float_ceil(skiatest::Reporter* reporter, float x) {
129 int ix = (int)ceil(x);
130 int iix = SkFloatToIntCeil(x);
131 assert_float_equal(reporter, "ceil", x, ix, iix);
132}
133
134static void test_float_conversions(skiatest::Reporter* reporter, float x) {
135 test_float_cast(reporter, x);
136 test_float_floor(reporter, x);
137 test_float_round(reporter, x);
138 test_float_ceil(reporter, x);
139}
140
141static void test_int2float(skiatest::Reporter* reporter, int ival) {
142 float x0 = (float)ival;
143 float x1 = SkIntToFloatCast(ival);
144 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
145 REPORTER_ASSERT(reporter, x0 == x1);
146 REPORTER_ASSERT(reporter, x0 == x2);
147}
148
149static void unittest_fastfloat(skiatest::Reporter* reporter) {
150 SkRandom rand;
151 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000152
reed@android.comed673312009-02-27 16:24:51 +0000153 static const float gFloats[] = {
154 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
155 0.000000001f, 1000000000.f, // doesn't overflow
156 0.0000000001f, 10000000000.f // does overflow
157 };
158 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000159 test_float_conversions(reporter, gFloats[i]);
160 test_float_conversions(reporter, -gFloats[i]);
161 }
reed@android.com80e39a72009-04-02 16:59:40 +0000162
reed@android.comed673312009-02-27 16:24:51 +0000163 for (int outer = 0; outer < 100; outer++) {
164 rand.setSeed(outer);
165 for (i = 0; i < 100000; i++) {
166 float x = nextFloat(rand);
167 test_float_conversions(reporter, x);
168 }
reed@android.com80e39a72009-04-02 16:59:40 +0000169
reed@android.comed673312009-02-27 16:24:51 +0000170 test_int2float(reporter, 0);
171 test_int2float(reporter, 1);
172 test_int2float(reporter, -1);
173 for (i = 0; i < 100000; i++) {
174 // for now only test ints that are 24bits or less, since we don't
175 // round (down) large ints the same as IEEE...
176 int ival = rand.nextU() & 0xFFFFFF;
177 test_int2float(reporter, ival);
178 test_int2float(reporter, -ival);
179 }
180 }
181}
182
reed@android.comd4134452011-02-09 02:24:26 +0000183#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000184static float make_zero() {
185 return sk_float_sin(0);
186}
reed@android.comd4134452011-02-09 02:24:26 +0000187#endif
reed@google.com077910e2011-02-08 21:56:39 +0000188
189static void unittest_isfinite(skiatest::Reporter* reporter) {
190#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000191 float nan = sk_float_asin(2);
reed@google.com077910e2011-02-08 21:56:39 +0000192 float inf = 1.0 / make_zero();
193 float big = 3.40282e+038;
194
reed@google.com077910e2011-02-08 21:56:39 +0000195 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000196 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
197 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
198 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
199#else
200 SkFixed nan = SK_FixedNaN;
201 SkFixed big = SK_FixedMax;
202#endif
203
204 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000205 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
206 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
207 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
reed@android.comd4134452011-02-09 02:24:26 +0000208
reed@google.com077910e2011-02-08 21:56:39 +0000209 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000210 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
211 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
212 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000213}
214
reed@android.comed673312009-02-27 16:24:51 +0000215#endif
216
217static void test_muldiv255(skiatest::Reporter* reporter) {
218#ifdef SK_CAN_USE_FLOAT
219 for (int a = 0; a <= 255; a++) {
220 for (int b = 0; b <= 255; b++) {
221 int ab = a * b;
222 float s = ab / 255.0f;
223 int round = (int)floorf(s + 0.5f);
224 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000225
reed@android.comed673312009-02-27 16:24:51 +0000226 int iround = SkMulDiv255Round(a, b);
227 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000228
reed@android.comed673312009-02-27 16:24:51 +0000229 REPORTER_ASSERT(reporter, iround == round);
230 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000231
reed@android.comed673312009-02-27 16:24:51 +0000232 REPORTER_ASSERT(reporter, itrunc <= iround);
233 REPORTER_ASSERT(reporter, iround <= a);
234 REPORTER_ASSERT(reporter, iround <= b);
235 }
236 }
237#endif
238}
239
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000240static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
241 for (int c = 0; c <= 255; c++) {
242 for (int a = 0; a <= 255; a++) {
243 int product = (c * a + 255);
244 int expected_ceiling = (product + (product >> 8)) >> 8;
245 int webkit_ceiling = (c * a + 254) / 255;
246 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
247 int skia_ceiling = SkMulDiv255Ceiling(c, a);
248 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
249 }
250 }
251}
252
reed@android.comf0ad0862010-02-09 19:18:38 +0000253static void test_copysign(skiatest::Reporter* reporter) {
254 static const int32_t gTriples[] = {
255 // x, y, expected result
256 0, 0, 0,
257 0, 1, 0,
258 0, -1, 0,
259 1, 0, 1,
260 1, 1, 1,
261 1, -1, -1,
262 -1, 0, 1,
263 -1, 1, 1,
264 -1, -1, -1,
265 };
266 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
267 REPORTER_ASSERT(reporter,
268 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
269#ifdef SK_CAN_USE_FLOAT
270 float x = (float)gTriples[i];
271 float y = (float)gTriples[i+1];
272 float expected = (float)gTriples[i+2];
273 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
274#endif
275 }
276
277 SkRandom rand;
278 for (int j = 0; j < 1000; j++) {
279 int ix = rand.nextS();
280 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
281 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
282 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
283 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
284
285 SkScalar sx = rand.nextSScalar1();
286 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
287 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
288 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
289 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
290 }
291}
292
reed@android.com80e39a72009-04-02 16:59:40 +0000293static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000294 int i;
295 int32_t x;
296 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000297
reed@android.comed673312009-02-27 16:24:51 +0000298 // these should assert
299#if 0
300 SkToS8(128);
301 SkToS8(-129);
302 SkToU8(256);
303 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000304
reed@android.comed673312009-02-27 16:24:51 +0000305 SkToS16(32768);
306 SkToS16(-32769);
307 SkToU16(65536);
308 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000309
reed@android.comed673312009-02-27 16:24:51 +0000310 if (sizeof(size_t) > 4) {
311 SkToS32(4*1024*1024);
312 SkToS32(-4*1024*1024);
313 SkToU32(5*1024*1024);
314 SkToU32(-5);
315 }
316#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000317
reed@android.comed673312009-02-27 16:24:51 +0000318 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000319 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000320 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000321
reed@android.comed673312009-02-27 16:24:51 +0000322 {
323 SkScalar x = SK_ScalarNaN;
324 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
325 }
reed@android.com80e39a72009-04-02 16:59:40 +0000326
reed@android.comed673312009-02-27 16:24:51 +0000327 for (i = 1; i <= 10; i++) {
328 x = SkCubeRootBits(i*i*i, 11);
329 REPORTER_ASSERT(reporter, x == i);
330 }
reed@android.com80e39a72009-04-02 16:59:40 +0000331
reed@android.comed673312009-02-27 16:24:51 +0000332 x = SkFixedSqrt(SK_Fixed1);
333 REPORTER_ASSERT(reporter, x == SK_Fixed1);
334 x = SkFixedSqrt(SK_Fixed1/4);
335 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
336 x = SkFixedSqrt(SK_Fixed1*4);
337 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000338
reed@android.comed673312009-02-27 16:24:51 +0000339 x = SkFractSqrt(SK_Fract1);
340 REPORTER_ASSERT(reporter, x == SK_Fract1);
341 x = SkFractSqrt(SK_Fract1/4);
342 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
343 x = SkFractSqrt(SK_Fract1/16);
344 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000345
reed@android.comed673312009-02-27 16:24:51 +0000346 for (i = 1; i < 100; i++) {
347 x = SkFixedSqrt(SK_Fixed1 * i * i);
348 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
349 }
reed@android.com80e39a72009-04-02 16:59:40 +0000350
reed@android.comed673312009-02-27 16:24:51 +0000351 for (i = 0; i < 1000; i++) {
352 int value = rand.nextS16();
353 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000354
reed@android.comed673312009-02-27 16:24:51 +0000355 int clamp = SkClampMax(value, max);
356 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
357 REPORTER_ASSERT(reporter, clamp == clamp2);
358 }
reed@android.com80e39a72009-04-02 16:59:40 +0000359
reed@android.come72fee52009-11-16 14:52:01 +0000360 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000361 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000362
reed@android.comed673312009-02-27 16:24:51 +0000363 p.setLength(rand.nextS(), rand.nextS(), SK_Scalar1);
364 check_length(reporter, p, SK_Scalar1);
365 p.setLength(rand.nextS() >> 13, rand.nextS() >> 13, SK_Scalar1);
366 check_length(reporter, p, SK_Scalar1);
367 }
reed@android.com80e39a72009-04-02 16:59:40 +0000368
reed@android.comed673312009-02-27 16:24:51 +0000369 {
370 SkFixed result = SkFixedDiv(100, 100);
371 REPORTER_ASSERT(reporter, result == SK_Fixed1);
372 result = SkFixedDiv(1, SK_Fixed1);
373 REPORTER_ASSERT(reporter, result == 1);
374 }
reed@android.com80e39a72009-04-02 16:59:40 +0000375
reed@android.comed673312009-02-27 16:24:51 +0000376#ifdef SK_CAN_USE_FLOAT
377 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000378 unittest_isfinite(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000379#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000380
reed@android.comed673312009-02-27 16:24:51 +0000381#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000382 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000383 SkFixed numer = rand.nextS();
384 SkFixed denom = rand.nextS();
385 SkFixed result = SkFixedDiv(numer, denom);
386 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000387
reed@android.comed673312009-02-27 16:24:51 +0000388 (void)SkCLZ(numer);
389 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000390
reed@android.comed673312009-02-27 16:24:51 +0000391 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
392 if (check > SK_MaxS32) {
393 check = SK_MaxS32;
394 } else if (check < -SK_MaxS32) {
395 check = SK_MinS32;
396 }
397 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000398
reed@android.comed673312009-02-27 16:24:51 +0000399 result = SkFractDiv(numer, denom);
400 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000401
reed@android.comed673312009-02-27 16:24:51 +0000402 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
403 if (check > SK_MaxS32) {
404 check = SK_MaxS32;
405 } else if (check < -SK_MaxS32) {
406 check = SK_MinS32;
407 }
408 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000409
reed@android.comed673312009-02-27 16:24:51 +0000410 // make them <= 2^24, so we don't overflow in fixmul
411 numer = numer << 8 >> 8;
412 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000413
reed@android.comed673312009-02-27 16:24:51 +0000414 result = SkFixedMul(numer, denom);
415 SkFixed r2 = symmetric_fixmul(numer, denom);
416 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000417
reed@android.comed673312009-02-27 16:24:51 +0000418 result = SkFixedMul(numer, numer);
419 r2 = SkFixedSquare(numer);
420 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000421
reed@android.comed673312009-02-27 16:24:51 +0000422#ifdef SK_CAN_USE_FLOAT
423 if (numer >= 0 && denom >= 0) {
424 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000425 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
426 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000427 SkFixed mean2 = SkFloatToFixed(fm);
428 int diff = SkAbs32(mean - mean2);
429 REPORTER_ASSERT(reporter, diff <= 1);
430 }
reed@android.com80e39a72009-04-02 16:59:40 +0000431
reed@android.comed673312009-02-27 16:24:51 +0000432 {
433 SkFixed mod = SkFixedMod(numer, denom);
434 float n = SkFixedToFloat(numer);
435 float d = SkFixedToFloat(denom);
436 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000437 // ensure the same sign
438 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000439 int diff = SkAbs32(mod - SkFloatToFixed(m));
440 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
441 }
442#endif
443 }
444#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000445
reed@android.comed673312009-02-27 16:24:51 +0000446#ifdef SK_CAN_USE_FLOAT
reed@android.come72fee52009-11-16 14:52:01 +0000447 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000448 SkFract x = rand.nextU() >> 1;
449 double xx = (double)x / SK_Fract1;
450 SkFract xr = SkFractSqrt(x);
451 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000452 REPORTER_ASSERT(reporter, xr == check ||
453 xr == check-1 ||
454 xr == check+1);
455
reed@android.comed673312009-02-27 16:24:51 +0000456 xr = SkFixedSqrt(x);
457 xx = (double)x / SK_Fixed1;
458 check = SkFloatToFixed(sqrt(xx));
459 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000460
reed@android.comed673312009-02-27 16:24:51 +0000461 xr = SkSqrt32(x);
462 xx = (double)x;
463 check = (int32_t)sqrt(xx);
464 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
465 }
466#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000467
reed@android.comed673312009-02-27 16:24:51 +0000468#if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT)
469 {
470 SkFixed s, c;
471 s = SkFixedSinCos(0, &c);
472 REPORTER_ASSERT(reporter, s == 0);
473 REPORTER_ASSERT(reporter, c == SK_Fixed1);
474 }
reed@android.com80e39a72009-04-02 16:59:40 +0000475
reed@android.comed673312009-02-27 16:24:51 +0000476 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000477 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000478 SkFixed rads = rand.nextS() >> 10;
479 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000480
reed@android.comed673312009-02-27 16:24:51 +0000481 SkFixed s, c;
482 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000483
reed@android.comed673312009-02-27 16:24:51 +0000484 double fs = sin(frads);
485 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000486
reed@android.comed673312009-02-27 16:24:51 +0000487 SkFixed is = SkFloatToFixed(fs);
488 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000489
reed@android.comed673312009-02-27 16:24:51 +0000490 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
491 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
492 }
493 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
494#endif
reed@google.com772813a2011-03-30 22:34:45 +0000495
496// test_premul(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000497}
498
reed@android.comd8730ea2009-02-27 22:06:06 +0000499#include "TestClassDef.h"
500DEFINE_TESTCLASS("Math", MathTestClass, TestMath)