blob: b9e21ae1e4abf6dfe495b6d231b79c69185c30f8 [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"
reed@android.comed673312009-02-27 16:24:51 +000010#include "SkPoint.h"
11#include "SkRandom.h"
12
reed@google.com772813a2011-03-30 22:34:45 +000013#if 0
14static U8CPU premul_fast(U8CPU a, U8CPU x) {
15 return a * x * 32897 >> 23;
16}
17
18static U8CPU premul_trunc(U8CPU a, U8CPU x) {
19 double result = a * x;
20 result /= 255.0;
21 return (unsigned)floor(result + 0.0);
22}
23
24static U8CPU premul_round(U8CPU a, U8CPU x) {
25 double result = a * x;
26 result /= 255.0;
27 return (unsigned)floor(result + 0.5);
28}
29
30static void test_premul(skiatest::Reporter* reporter) {
31 for (int a = 0; a <= 255; a++) {
32 for (int x = 0; x <= 255; x++) {
33 unsigned curr_trunc = SkMulDiv255Trunc(a, x);
34 unsigned curr_round = SkMulDiv255Round(a, x);
35 unsigned fast = premul_fast(a, x);
36 unsigned slow_round = premul_round(a, x);
37 unsigned slow_trunc = premul_trunc(a, x);
38 if (fast != slow || curr != fast) {
39 SkDebugf("---- premul(%d %d) curr=%d fast=%d slow=%d\n", a, x,
40 curr, fast, slow);
41 }
42 }
43 }
44}
45#endif
46
reed@android.comed673312009-02-27 16:24:51 +000047#if defined(SkLONGLONG)
48static int symmetric_fixmul(int a, int b) {
49 int sa = SkExtractSign(a);
50 int sb = SkExtractSign(b);
reed@android.com80e39a72009-04-02 16:59:40 +000051
reed@android.comed673312009-02-27 16:24:51 +000052 a = SkApplySign(a, sa);
53 b = SkApplySign(b, sb);
reed@android.com80e39a72009-04-02 16:59:40 +000054
reed@android.comed673312009-02-27 16:24:51 +000055#if 1
56 int c = (int)(((SkLONGLONG)a * b) >> 16);
reed@android.com80e39a72009-04-02 16:59:40 +000057
reed@android.comed673312009-02-27 16:24:51 +000058 return SkApplySign(c, sa ^ sb);
59#else
60 SkLONGLONG ab = (SkLONGLONG)a * b;
61 if (sa ^ sb) {
62 ab = -ab;
63 }
64 return ab >> 16;
65#endif
66}
67#endif
68
69static void check_length(skiatest::Reporter* reporter,
70 const SkPoint& p, SkScalar targetLen) {
71#ifdef SK_CAN_USE_FLOAT
72 float x = SkScalarToFloat(p.fX);
73 float y = SkScalarToFloat(p.fY);
74 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +000075
reed@android.comed673312009-02-27 16:24:51 +000076 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +000077
reed@android.comed673312009-02-27 16:24:51 +000078 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
79#endif
80}
81
82#if defined(SK_CAN_USE_FLOAT)
83
84static float nextFloat(SkRandom& rand) {
85 SkFloatIntUnion data;
86 data.fSignBitInt = rand.nextU();
87 return data.fFloat;
88}
89
90/* returns true if a == b as resulting from (int)x. Since it is undefined
91 what to do if the float exceeds 2^32-1, we check for that explicitly.
92 */
93static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
94 if (!(x == x)) { // NAN
95 return si == SK_MaxS32 || si == SK_MinS32;
96 }
97 // for out of range, C is undefined, but skia always should return NaN32
98 if (x > SK_MaxS32) {
99 return si == SK_MaxS32;
100 }
101 if (x < -SK_MaxS32) {
102 return si == SK_MinS32;
103 }
104 return si == ni;
105}
106
107static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
108 float x, uint32_t ni, uint32_t si) {
109 if (!equal_float_native_skia(x, ni, si)) {
110 SkString desc;
111 desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si);
112 reporter->reportFailed(desc);
113 }
114}
115
116static void test_float_cast(skiatest::Reporter* reporter, float x) {
117 int ix = (int)x;
118 int iix = SkFloatToIntCast(x);
119 assert_float_equal(reporter, "cast", x, ix, iix);
120}
121
122static void test_float_floor(skiatest::Reporter* reporter, float x) {
123 int ix = (int)floor(x);
124 int iix = SkFloatToIntFloor(x);
125 assert_float_equal(reporter, "floor", x, ix, iix);
126}
127
128static void test_float_round(skiatest::Reporter* reporter, float x) {
129 double xx = x + 0.5; // need intermediate double to avoid temp loss
130 int ix = (int)floor(xx);
131 int iix = SkFloatToIntRound(x);
132 assert_float_equal(reporter, "round", x, ix, iix);
133}
134
135static void test_float_ceil(skiatest::Reporter* reporter, float x) {
136 int ix = (int)ceil(x);
137 int iix = SkFloatToIntCeil(x);
138 assert_float_equal(reporter, "ceil", x, ix, iix);
139}
140
141static void test_float_conversions(skiatest::Reporter* reporter, float x) {
142 test_float_cast(reporter, x);
143 test_float_floor(reporter, x);
144 test_float_round(reporter, x);
145 test_float_ceil(reporter, x);
146}
147
148static void test_int2float(skiatest::Reporter* reporter, int ival) {
149 float x0 = (float)ival;
150 float x1 = SkIntToFloatCast(ival);
151 float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
152 REPORTER_ASSERT(reporter, x0 == x1);
153 REPORTER_ASSERT(reporter, x0 == x2);
154}
155
156static void unittest_fastfloat(skiatest::Reporter* reporter) {
157 SkRandom rand;
158 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000159
reed@android.comed673312009-02-27 16:24:51 +0000160 static const float gFloats[] = {
161 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
162 0.000000001f, 1000000000.f, // doesn't overflow
163 0.0000000001f, 10000000000.f // does overflow
164 };
165 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000166 test_float_conversions(reporter, gFloats[i]);
167 test_float_conversions(reporter, -gFloats[i]);
168 }
reed@android.com80e39a72009-04-02 16:59:40 +0000169
reed@android.comed673312009-02-27 16:24:51 +0000170 for (int outer = 0; outer < 100; outer++) {
171 rand.setSeed(outer);
172 for (i = 0; i < 100000; i++) {
173 float x = nextFloat(rand);
174 test_float_conversions(reporter, x);
175 }
reed@android.com80e39a72009-04-02 16:59:40 +0000176
reed@android.comed673312009-02-27 16:24:51 +0000177 test_int2float(reporter, 0);
178 test_int2float(reporter, 1);
179 test_int2float(reporter, -1);
180 for (i = 0; i < 100000; i++) {
181 // for now only test ints that are 24bits or less, since we don't
182 // round (down) large ints the same as IEEE...
183 int ival = rand.nextU() & 0xFFFFFF;
184 test_int2float(reporter, ival);
185 test_int2float(reporter, -ival);
186 }
187 }
188}
189
reed@android.comd4134452011-02-09 02:24:26 +0000190#ifdef SK_SCALAR_IS_FLOAT
reed@google.com077910e2011-02-08 21:56:39 +0000191static float make_zero() {
192 return sk_float_sin(0);
193}
reed@android.comd4134452011-02-09 02:24:26 +0000194#endif
reed@google.com077910e2011-02-08 21:56:39 +0000195
196static void unittest_isfinite(skiatest::Reporter* reporter) {
197#ifdef SK_SCALAR_IS_FLOAT
epoger@google.combf083a92011-06-08 18:26:08 +0000198 float nan = sk_float_asin(2);
reed@google.com077910e2011-02-08 21:56:39 +0000199 float inf = 1.0 / make_zero();
200 float big = 3.40282e+038;
201
reed@google.com077910e2011-02-08 21:56:39 +0000202 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000203 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
204 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
205 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
206#else
207 SkFixed nan = SK_FixedNaN;
208 SkFixed big = SK_FixedMax;
209#endif
210
211 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000212 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
213 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
214 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
reed@android.comd4134452011-02-09 02:24:26 +0000215
reed@google.com077910e2011-02-08 21:56:39 +0000216 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000217 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
218 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
219 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000220}
221
reed@android.comed673312009-02-27 16:24:51 +0000222#endif
223
224static void test_muldiv255(skiatest::Reporter* reporter) {
225#ifdef SK_CAN_USE_FLOAT
226 for (int a = 0; a <= 255; a++) {
227 for (int b = 0; b <= 255; b++) {
228 int ab = a * b;
229 float s = ab / 255.0f;
230 int round = (int)floorf(s + 0.5f);
231 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000232
reed@android.comed673312009-02-27 16:24:51 +0000233 int iround = SkMulDiv255Round(a, b);
234 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000235
reed@android.comed673312009-02-27 16:24:51 +0000236 REPORTER_ASSERT(reporter, iround == round);
237 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000238
reed@android.comed673312009-02-27 16:24:51 +0000239 REPORTER_ASSERT(reporter, itrunc <= iround);
240 REPORTER_ASSERT(reporter, iround <= a);
241 REPORTER_ASSERT(reporter, iround <= b);
242 }
243 }
244#endif
245}
246
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000247static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
248 for (int c = 0; c <= 255; c++) {
249 for (int a = 0; a <= 255; a++) {
250 int product = (c * a + 255);
251 int expected_ceiling = (product + (product >> 8)) >> 8;
252 int webkit_ceiling = (c * a + 254) / 255;
253 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
254 int skia_ceiling = SkMulDiv255Ceiling(c, a);
255 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
256 }
257 }
258}
259
reed@android.comf0ad0862010-02-09 19:18:38 +0000260static void test_copysign(skiatest::Reporter* reporter) {
261 static const int32_t gTriples[] = {
262 // x, y, expected result
263 0, 0, 0,
264 0, 1, 0,
265 0, -1, 0,
266 1, 0, 1,
267 1, 1, 1,
268 1, -1, -1,
269 -1, 0, 1,
270 -1, 1, 1,
271 -1, -1, -1,
272 };
273 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
274 REPORTER_ASSERT(reporter,
275 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
276#ifdef SK_CAN_USE_FLOAT
277 float x = (float)gTriples[i];
278 float y = (float)gTriples[i+1];
279 float expected = (float)gTriples[i+2];
280 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
281#endif
282 }
283
284 SkRandom rand;
285 for (int j = 0; j < 1000; j++) {
286 int ix = rand.nextS();
287 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
288 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
289 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
290 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
291
292 SkScalar sx = rand.nextSScalar1();
293 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
294 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
295 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
296 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
297 }
298}
299
reed@android.com80e39a72009-04-02 16:59:40 +0000300static void TestMath(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000301 int i;
302 int32_t x;
303 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000304
reed@android.comed673312009-02-27 16:24:51 +0000305 // these should assert
306#if 0
307 SkToS8(128);
308 SkToS8(-129);
309 SkToU8(256);
310 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000311
reed@android.comed673312009-02-27 16:24:51 +0000312 SkToS16(32768);
313 SkToS16(-32769);
314 SkToU16(65536);
315 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000316
reed@android.comed673312009-02-27 16:24:51 +0000317 if (sizeof(size_t) > 4) {
318 SkToS32(4*1024*1024);
319 SkToS32(-4*1024*1024);
320 SkToU32(5*1024*1024);
321 SkToU32(-5);
322 }
323#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000324
reed@android.comed673312009-02-27 16:24:51 +0000325 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000326 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000327 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000328
reed@android.comed673312009-02-27 16:24:51 +0000329 {
330 SkScalar x = SK_ScalarNaN;
331 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
332 }
reed@android.com80e39a72009-04-02 16:59:40 +0000333
reed@android.comed673312009-02-27 16:24:51 +0000334 for (i = 1; i <= 10; i++) {
335 x = SkCubeRootBits(i*i*i, 11);
336 REPORTER_ASSERT(reporter, x == i);
337 }
reed@android.com80e39a72009-04-02 16:59:40 +0000338
reed@android.comed673312009-02-27 16:24:51 +0000339 x = SkFixedSqrt(SK_Fixed1);
340 REPORTER_ASSERT(reporter, x == SK_Fixed1);
341 x = SkFixedSqrt(SK_Fixed1/4);
342 REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
343 x = SkFixedSqrt(SK_Fixed1*4);
344 REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
reed@android.com80e39a72009-04-02 16:59:40 +0000345
reed@android.comed673312009-02-27 16:24:51 +0000346 x = SkFractSqrt(SK_Fract1);
347 REPORTER_ASSERT(reporter, x == SK_Fract1);
348 x = SkFractSqrt(SK_Fract1/4);
349 REPORTER_ASSERT(reporter, x == SK_Fract1/2);
350 x = SkFractSqrt(SK_Fract1/16);
351 REPORTER_ASSERT(reporter, x == SK_Fract1/4);
reed@android.com80e39a72009-04-02 16:59:40 +0000352
reed@android.comed673312009-02-27 16:24:51 +0000353 for (i = 1; i < 100; i++) {
354 x = SkFixedSqrt(SK_Fixed1 * i * i);
355 REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
356 }
reed@android.com80e39a72009-04-02 16:59:40 +0000357
reed@android.comed673312009-02-27 16:24:51 +0000358 for (i = 0; i < 1000; i++) {
359 int value = rand.nextS16();
360 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000361
reed@android.comed673312009-02-27 16:24:51 +0000362 int clamp = SkClampMax(value, max);
363 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
364 REPORTER_ASSERT(reporter, clamp == clamp2);
365 }
reed@android.com80e39a72009-04-02 16:59:40 +0000366
reed@android.come72fee52009-11-16 14:52:01 +0000367 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000368 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000369
reed@android.comed673312009-02-27 16:24:51 +0000370 p.setLength(rand.nextS(), rand.nextS(), SK_Scalar1);
371 check_length(reporter, p, SK_Scalar1);
372 p.setLength(rand.nextS() >> 13, rand.nextS() >> 13, SK_Scalar1);
373 check_length(reporter, p, SK_Scalar1);
374 }
reed@android.com80e39a72009-04-02 16:59:40 +0000375
reed@android.comed673312009-02-27 16:24:51 +0000376 {
377 SkFixed result = SkFixedDiv(100, 100);
378 REPORTER_ASSERT(reporter, result == SK_Fixed1);
379 result = SkFixedDiv(1, SK_Fixed1);
380 REPORTER_ASSERT(reporter, result == 1);
381 }
reed@android.com80e39a72009-04-02 16:59:40 +0000382
reed@android.comed673312009-02-27 16:24:51 +0000383#ifdef SK_CAN_USE_FLOAT
384 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000385 unittest_isfinite(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000386#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000387
reed@android.comed673312009-02-27 16:24:51 +0000388#ifdef SkLONGLONG
reed@android.come72fee52009-11-16 14:52:01 +0000389 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000390 SkFixed numer = rand.nextS();
391 SkFixed denom = rand.nextS();
392 SkFixed result = SkFixedDiv(numer, denom);
393 SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000394
reed@android.comed673312009-02-27 16:24:51 +0000395 (void)SkCLZ(numer);
396 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000397
reed@android.comed673312009-02-27 16:24:51 +0000398 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
399 if (check > SK_MaxS32) {
400 check = SK_MaxS32;
401 } else if (check < -SK_MaxS32) {
402 check = SK_MinS32;
403 }
404 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000405
reed@android.comed673312009-02-27 16:24:51 +0000406 result = SkFractDiv(numer, denom);
407 check = ((SkLONGLONG)numer << 30) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000408
reed@android.comed673312009-02-27 16:24:51 +0000409 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
410 if (check > SK_MaxS32) {
411 check = SK_MaxS32;
412 } else if (check < -SK_MaxS32) {
413 check = SK_MinS32;
414 }
415 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.com80e39a72009-04-02 16:59:40 +0000416
reed@android.comed673312009-02-27 16:24:51 +0000417 // make them <= 2^24, so we don't overflow in fixmul
418 numer = numer << 8 >> 8;
419 denom = denom << 8 >> 8;
reed@android.com80e39a72009-04-02 16:59:40 +0000420
reed@android.comed673312009-02-27 16:24:51 +0000421 result = SkFixedMul(numer, denom);
422 SkFixed r2 = symmetric_fixmul(numer, denom);
423 // SkASSERT(result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000424
reed@android.comed673312009-02-27 16:24:51 +0000425 result = SkFixedMul(numer, numer);
426 r2 = SkFixedSquare(numer);
427 REPORTER_ASSERT(reporter, result == r2);
reed@android.com80e39a72009-04-02 16:59:40 +0000428
reed@android.comed673312009-02-27 16:24:51 +0000429#ifdef SK_CAN_USE_FLOAT
430 if (numer >= 0 && denom >= 0) {
431 SkFixed mean = SkFixedMean(numer, denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000432 float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
433 float fm = sk_float_sqrt(sk_float_abs(prod));
reed@android.comed673312009-02-27 16:24:51 +0000434 SkFixed mean2 = SkFloatToFixed(fm);
435 int diff = SkAbs32(mean - mean2);
436 REPORTER_ASSERT(reporter, diff <= 1);
437 }
reed@android.com80e39a72009-04-02 16:59:40 +0000438
reed@android.comed673312009-02-27 16:24:51 +0000439 {
440 SkFixed mod = SkFixedMod(numer, denom);
441 float n = SkFixedToFloat(numer);
442 float d = SkFixedToFloat(denom);
443 float m = sk_float_mod(n, d);
reed@android.com80e39a72009-04-02 16:59:40 +0000444 // ensure the same sign
445 REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
reed@android.comed673312009-02-27 16:24:51 +0000446 int diff = SkAbs32(mod - SkFloatToFixed(m));
447 REPORTER_ASSERT(reporter, (diff >> 7) == 0);
448 }
449#endif
450 }
451#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000452
reed@android.comed673312009-02-27 16:24:51 +0000453#ifdef SK_CAN_USE_FLOAT
reed@android.come72fee52009-11-16 14:52:01 +0000454 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000455 SkFract x = rand.nextU() >> 1;
456 double xx = (double)x / SK_Fract1;
457 SkFract xr = SkFractSqrt(x);
458 SkFract check = SkFloatToFract(sqrt(xx));
reed@android.com80e39a72009-04-02 16:59:40 +0000459 REPORTER_ASSERT(reporter, xr == check ||
460 xr == check-1 ||
461 xr == check+1);
462
reed@android.comed673312009-02-27 16:24:51 +0000463 xr = SkFixedSqrt(x);
464 xx = (double)x / SK_Fixed1;
465 check = SkFloatToFixed(sqrt(xx));
466 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
reed@android.com80e39a72009-04-02 16:59:40 +0000467
reed@android.comed673312009-02-27 16:24:51 +0000468 xr = SkSqrt32(x);
469 xx = (double)x;
470 check = (int32_t)sqrt(xx);
471 REPORTER_ASSERT(reporter, xr == check || xr == check-1);
472 }
473#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000474
reed@android.comed673312009-02-27 16:24:51 +0000475#if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT)
476 {
477 SkFixed s, c;
478 s = SkFixedSinCos(0, &c);
479 REPORTER_ASSERT(reporter, s == 0);
480 REPORTER_ASSERT(reporter, c == SK_Fixed1);
481 }
reed@android.com80e39a72009-04-02 16:59:40 +0000482
reed@android.comed673312009-02-27 16:24:51 +0000483 int maxDiff = 0;
reed@android.come72fee52009-11-16 14:52:01 +0000484 for (i = 0; i < 1000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000485 SkFixed rads = rand.nextS() >> 10;
486 double frads = SkFixedToFloat(rads);
reed@android.com80e39a72009-04-02 16:59:40 +0000487
reed@android.comed673312009-02-27 16:24:51 +0000488 SkFixed s, c;
489 s = SkScalarSinCos(rads, &c);
reed@android.com80e39a72009-04-02 16:59:40 +0000490
reed@android.comed673312009-02-27 16:24:51 +0000491 double fs = sin(frads);
492 double fc = cos(frads);
reed@android.com80e39a72009-04-02 16:59:40 +0000493
reed@android.comed673312009-02-27 16:24:51 +0000494 SkFixed is = SkFloatToFixed(fs);
495 SkFixed ic = SkFloatToFixed(fc);
reed@android.com80e39a72009-04-02 16:59:40 +0000496
reed@android.comed673312009-02-27 16:24:51 +0000497 maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
498 maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
499 }
500 SkDebugf("SinCos: maximum error = %d\n", maxDiff);
501#endif
reed@google.com772813a2011-03-30 22:34:45 +0000502
503// test_premul(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000504}
505
reed@android.comd8730ea2009-02-27 22:06:06 +0000506#include "TestClassDef.h"
507DEFINE_TESTCLASS("Math", MathTestClass, TestMath)