• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "Test.h"
9 #include "TestClassDef.h"
10 #include "SkColorPriv.h"
11 #include "SkEndian.h"
12 #include "SkFloatBits.h"
13 #include "SkFloatingPoint.h"
14 #include "SkMathPriv.h"
15 #include "SkPoint.h"
16 #include "SkRandom.h"
17 
test_clz(skiatest::Reporter * reporter)18 static void test_clz(skiatest::Reporter* reporter) {
19     REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
20     REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
21     REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
22     REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
23 
24     SkRandom rand;
25     for (int i = 0; i < 1000; ++i) {
26         uint32_t mask = rand.nextU();
27         // need to get some zeros for testing, but in some obscure way so the
28         // compiler won't "see" that, and work-around calling the functions.
29         mask >>= (mask & 31);
30         int intri = SkCLZ(mask);
31         int porta = SkCLZ_portable(mask);
32         REPORTER_ASSERT(reporter, intri == porta);
33     }
34 }
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 
sk_fsel(float pred,float result_ge,float result_lt)38 static float sk_fsel(float pred, float result_ge, float result_lt) {
39     return pred >= 0 ? result_ge : result_lt;
40 }
41 
fast_floor(float x)42 static float fast_floor(float x) {
43 //    float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
44     float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
45     return (float)(x + big) - big;
46 }
47 
std_floor(float x)48 static float std_floor(float x) {
49     return sk_float_floor(x);
50 }
51 
test_floor_value(skiatest::Reporter * reporter,float value)52 static void test_floor_value(skiatest::Reporter* reporter, float value) {
53     float fast = fast_floor(value);
54     float std = std_floor(value);
55     REPORTER_ASSERT(reporter, std == fast);
56 //    SkDebugf("value[%1.9f] std[%g] fast[%g] equal[%d]\n",
57 //             value, std, fast, std == fast);
58 }
59 
test_floor(skiatest::Reporter * reporter)60 static void test_floor(skiatest::Reporter* reporter) {
61     static const float gVals[] = {
62         0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
63     };
64 
65     for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
66         test_floor_value(reporter, gVals[i]);
67 //        test_floor_value(reporter, -gVals[i]);
68     }
69 }
70 
71 ///////////////////////////////////////////////////////////////////////////////
72 
73 // test that SkMul16ShiftRound and SkMulDiv255Round return the same result
test_muldivround(skiatest::Reporter * reporter)74 static void test_muldivround(skiatest::Reporter* reporter) {
75 #if 0
76     // this "complete" test is too slow, so we test a random sampling of it
77 
78     for (int a = 0; a <= 32767; ++a) {
79         for (int b = 0; b <= 32767; ++b) {
80             unsigned prod0 = SkMul16ShiftRound(a, b, 8);
81             unsigned prod1 = SkMulDiv255Round(a, b);
82             SkASSERT(prod0 == prod1);
83         }
84     }
85 #endif
86 
87     SkRandom rand;
88     for (int i = 0; i < 10000; ++i) {
89         unsigned a = rand.nextU() & 0x7FFF;
90         unsigned b = rand.nextU() & 0x7FFF;
91 
92         unsigned prod0 = SkMul16ShiftRound(a, b, 8);
93         unsigned prod1 = SkMulDiv255Round(a, b);
94 
95         REPORTER_ASSERT(reporter, prod0 == prod1);
96     }
97 }
98 
float_blend(int src,int dst,float unit)99 static float float_blend(int src, int dst, float unit) {
100     return dst + (src - dst) * unit;
101 }
102 
blend31(int src,int dst,int a31)103 static int blend31(int src, int dst, int a31) {
104     return dst + ((src - dst) * a31 * 2114 >> 16);
105     //    return dst + ((src - dst) * a31 * 33 >> 10);
106 }
107 
blend31_slow(int src,int dst,int a31)108 static int blend31_slow(int src, int dst, int a31) {
109     int prod = src * a31 + (31 - a31) * dst + 16;
110     prod = (prod + (prod >> 5)) >> 5;
111     return prod;
112 }
113 
blend31_round(int src,int dst,int a31)114 static int blend31_round(int src, int dst, int a31) {
115     int prod = (src - dst) * a31 + 16;
116     prod = (prod + (prod >> 5)) >> 5;
117     return dst + prod;
118 }
119 
blend31_old(int src,int dst,int a31)120 static int blend31_old(int src, int dst, int a31) {
121     a31 += a31 >> 4;
122     return dst + ((src - dst) * a31 >> 5);
123 }
124 
125 // suppress unused code warning
126 static int (*blend_functions[])(int, int, int) = {
127     blend31,
128     blend31_slow,
129     blend31_round,
130     blend31_old
131 };
132 
test_blend31()133 static void test_blend31() {
134     int failed = 0;
135     int death = 0;
136     if (false) { // avoid bit rot, suppress warning
137         failed = (*blend_functions[0])(0,0,0);
138     }
139     for (int src = 0; src <= 255; src++) {
140         for (int dst = 0; dst <= 255; dst++) {
141             for (int a = 0; a <= 31; a++) {
142 //                int r0 = blend31(src, dst, a);
143 //                int r0 = blend31_round(src, dst, a);
144 //                int r0 = blend31_old(src, dst, a);
145                 int r0 = blend31_slow(src, dst, a);
146 
147                 float f = float_blend(src, dst, a / 31.f);
148                 int r1 = (int)f;
149                 int r2 = SkScalarRoundToInt(f);
150 
151                 if (r0 != r1 && r0 != r2) {
152                     SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
153                                   src,   dst, a,        r0,      f);
154                     failed += 1;
155                 }
156                 if (r0 > 255) {
157                     death += 1;
158                     SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
159                                         src,   dst, a,        r0,      f);
160                 }
161             }
162         }
163     }
164     SkDebugf("---- failed %d death %d\n", failed, death);
165 }
166 
test_blend(skiatest::Reporter * reporter)167 static void test_blend(skiatest::Reporter* reporter) {
168     for (int src = 0; src <= 255; src++) {
169         for (int dst = 0; dst <= 255; dst++) {
170             for (int a = 0; a <= 255; a++) {
171                 int r0 = SkAlphaBlend255(src, dst, a);
172                 float f1 = float_blend(src, dst, a / 255.f);
173                 int r1 = SkScalarRoundToInt(f1);
174 
175                 if (r0 != r1) {
176                     float diff = sk_float_abs(f1 - r1);
177                     diff = sk_float_abs(diff - 0.5f);
178                     if (diff > (1 / 255.f)) {
179 #ifdef SK_DEBUG
180                         SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
181                                  src, dst, a, r0, f1);
182 #endif
183                         REPORTER_ASSERT(reporter, false);
184                     }
185                 }
186             }
187         }
188     }
189 }
190 
191 #if defined(SkLONGLONG)
symmetric_fixmul(int a,int b)192 static int symmetric_fixmul(int a, int b) {
193     int sa = SkExtractSign(a);
194     int sb = SkExtractSign(b);
195 
196     a = SkApplySign(a, sa);
197     b = SkApplySign(b, sb);
198 
199 #if 1
200     int c = (int)(((SkLONGLONG)a * b) >> 16);
201 
202     return SkApplySign(c, sa ^ sb);
203 #else
204     SkLONGLONG ab = (SkLONGLONG)a * b;
205     if (sa ^ sb) {
206         ab = -ab;
207     }
208     return ab >> 16;
209 #endif
210 }
211 #endif
212 
check_length(skiatest::Reporter * reporter,const SkPoint & p,SkScalar targetLen)213 static void check_length(skiatest::Reporter* reporter,
214                          const SkPoint& p, SkScalar targetLen) {
215     float x = SkScalarToFloat(p.fX);
216     float y = SkScalarToFloat(p.fY);
217     float len = sk_float_sqrt(x*x + y*y);
218 
219     len /= SkScalarToFloat(targetLen);
220 
221     REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
222 }
223 
nextFloat(SkRandom & rand)224 static float nextFloat(SkRandom& rand) {
225     SkFloatIntUnion data;
226     data.fSignBitInt = rand.nextU();
227     return data.fFloat;
228 }
229 
230 /*  returns true if a == b as resulting from (int)x. Since it is undefined
231  what to do if the float exceeds 2^32-1, we check for that explicitly.
232  */
equal_float_native_skia(float x,uint32_t ni,uint32_t si)233 static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
234     if (!(x == x)) {    // NAN
235         return ((int32_t)si) == SK_MaxS32 || ((int32_t)si) == SK_MinS32;
236     }
237     // for out of range, C is undefined, but skia always should return NaN32
238     if (x > SK_MaxS32) {
239         return ((int32_t)si) == SK_MaxS32;
240     }
241     if (x < -SK_MaxS32) {
242         return ((int32_t)si) == SK_MinS32;
243     }
244     return si == ni;
245 }
246 
assert_float_equal(skiatest::Reporter * reporter,const char op[],float x,uint32_t ni,uint32_t si)247 static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
248                                float x, uint32_t ni, uint32_t si) {
249     if (!equal_float_native_skia(x, ni, si)) {
250         SkString desc;
251         uint32_t xi = SkFloat2Bits(x);
252         desc.printf("%s float %g bits %x native %x skia %x\n", op, x, xi, ni, si);
253         reporter->reportFailed(desc);
254     }
255 }
256 
test_float_cast(skiatest::Reporter * reporter,float x)257 static void test_float_cast(skiatest::Reporter* reporter, float x) {
258     int ix = (int)x;
259     int iix = SkFloatToIntCast(x);
260     assert_float_equal(reporter, "cast", x, ix, iix);
261 }
262 
test_float_floor(skiatest::Reporter * reporter,float x)263 static void test_float_floor(skiatest::Reporter* reporter, float x) {
264     int ix = (int)floor(x);
265     int iix = SkFloatToIntFloor(x);
266     assert_float_equal(reporter, "floor", x, ix, iix);
267 }
268 
test_float_round(skiatest::Reporter * reporter,float x)269 static void test_float_round(skiatest::Reporter* reporter, float x) {
270     double xx = x + 0.5;    // need intermediate double to avoid temp loss
271     int ix = (int)floor(xx);
272     int iix = SkFloatToIntRound(x);
273     assert_float_equal(reporter, "round", x, ix, iix);
274 }
275 
test_float_ceil(skiatest::Reporter * reporter,float x)276 static void test_float_ceil(skiatest::Reporter* reporter, float x) {
277     int ix = (int)ceil(x);
278     int iix = SkFloatToIntCeil(x);
279     assert_float_equal(reporter, "ceil", x, ix, iix);
280 }
281 
test_float_conversions(skiatest::Reporter * reporter,float x)282 static void test_float_conversions(skiatest::Reporter* reporter, float x) {
283     test_float_cast(reporter, x);
284     test_float_floor(reporter, x);
285     test_float_round(reporter, x);
286     test_float_ceil(reporter, x);
287 }
288 
test_int2float(skiatest::Reporter * reporter,int ival)289 static void test_int2float(skiatest::Reporter* reporter, int ival) {
290     float x0 = (float)ival;
291     float x1 = SkIntToFloatCast(ival);
292     float x2 = SkIntToFloatCast_NoOverflowCheck(ival);
293     REPORTER_ASSERT(reporter, x0 == x1);
294     REPORTER_ASSERT(reporter, x0 == x2);
295 }
296 
unittest_fastfloat(skiatest::Reporter * reporter)297 static void unittest_fastfloat(skiatest::Reporter* reporter) {
298     SkRandom rand;
299     size_t i;
300 
301     static const float gFloats[] = {
302         0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
303         0.000000001f, 1000000000.f,     // doesn't overflow
304         0.0000000001f, 10000000000.f    // does overflow
305     };
306     for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
307         test_float_conversions(reporter, gFloats[i]);
308         test_float_conversions(reporter, -gFloats[i]);
309     }
310 
311     for (int outer = 0; outer < 100; outer++) {
312         rand.setSeed(outer);
313         for (i = 0; i < 100000; i++) {
314             float x = nextFloat(rand);
315             test_float_conversions(reporter, x);
316         }
317 
318         test_int2float(reporter, 0);
319         test_int2float(reporter, 1);
320         test_int2float(reporter, -1);
321         for (i = 0; i < 100000; i++) {
322             // for now only test ints that are 24bits or less, since we don't
323             // round (down) large ints the same as IEEE...
324             int ival = rand.nextU() & 0xFFFFFF;
325             test_int2float(reporter, ival);
326             test_int2float(reporter, -ival);
327         }
328     }
329 }
330 
331 #ifdef SK_SCALAR_IS_FLOAT
make_zero()332 static float make_zero() {
333     return sk_float_sin(0);
334 }
335 #endif
336 
unittest_isfinite(skiatest::Reporter * reporter)337 static void unittest_isfinite(skiatest::Reporter* reporter) {
338 #ifdef SK_SCALAR_IS_FLOAT
339     float nan = sk_float_asin(2);
340     float inf = 1.0f / make_zero();
341     float big = 3.40282e+038f;
342 
343     REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
344     REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
345     REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
346     REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
347 #else
348     SkFixed nan = SK_FixedNaN;
349     SkFixed big = SK_FixedMax;
350 #endif
351 
352     REPORTER_ASSERT(reporter,  SkScalarIsNaN(nan));
353     REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
354     REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
355     REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
356 
357     REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
358     REPORTER_ASSERT(reporter,  SkScalarIsFinite(big));
359     REPORTER_ASSERT(reporter,  SkScalarIsFinite(-big));
360     REPORTER_ASSERT(reporter,  SkScalarIsFinite(0));
361 }
362 
test_muldiv255(skiatest::Reporter * reporter)363 static void test_muldiv255(skiatest::Reporter* reporter) {
364     for (int a = 0; a <= 255; a++) {
365         for (int b = 0; b <= 255; b++) {
366             int ab = a * b;
367             float s = ab / 255.0f;
368             int round = (int)floorf(s + 0.5f);
369             int trunc = (int)floorf(s);
370 
371             int iround = SkMulDiv255Round(a, b);
372             int itrunc = SkMulDiv255Trunc(a, b);
373 
374             REPORTER_ASSERT(reporter, iround == round);
375             REPORTER_ASSERT(reporter, itrunc == trunc);
376 
377             REPORTER_ASSERT(reporter, itrunc <= iround);
378             REPORTER_ASSERT(reporter, iround <= a);
379             REPORTER_ASSERT(reporter, iround <= b);
380         }
381     }
382 }
383 
test_muldiv255ceiling(skiatest::Reporter * reporter)384 static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
385     for (int c = 0; c <= 255; c++) {
386         for (int a = 0; a <= 255; a++) {
387             int product = (c * a + 255);
388             int expected_ceiling = (product + (product >> 8)) >> 8;
389             int webkit_ceiling = (c * a + 254) / 255;
390             REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
391             int skia_ceiling = SkMulDiv255Ceiling(c, a);
392             REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
393         }
394     }
395 }
396 
test_copysign(skiatest::Reporter * reporter)397 static void test_copysign(skiatest::Reporter* reporter) {
398     static const int32_t gTriples[] = {
399         // x, y, expected result
400         0, 0, 0,
401         0, 1, 0,
402         0, -1, 0,
403         1, 0, 1,
404         1, 1, 1,
405         1, -1, -1,
406         -1, 0, 1,
407         -1, 1, 1,
408         -1, -1, -1,
409     };
410     for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
411         REPORTER_ASSERT(reporter,
412                         SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
413         float x = (float)gTriples[i];
414         float y = (float)gTriples[i+1];
415         float expected = (float)gTriples[i+2];
416         REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
417     }
418 
419     SkRandom rand;
420     for (int j = 0; j < 1000; j++) {
421         int ix = rand.nextS();
422         REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
423         REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
424         REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
425         REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
426 
427         SkScalar sx = rand.nextSScalar1();
428         REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
429         REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
430         REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
431         REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
432     }
433 }
434 
DEF_TEST(Math,reporter)435 DEF_TEST(Math, reporter) {
436     int         i;
437     int32_t     x;
438     SkRandom    rand;
439 
440     // these should assert
441 #if 0
442     SkToS8(128);
443     SkToS8(-129);
444     SkToU8(256);
445     SkToU8(-5);
446 
447     SkToS16(32768);
448     SkToS16(-32769);
449     SkToU16(65536);
450     SkToU16(-5);
451 
452     if (sizeof(size_t) > 4) {
453         SkToS32(4*1024*1024);
454         SkToS32(-4*1024*1024);
455         SkToU32(5*1024*1024);
456         SkToU32(-5);
457     }
458 #endif
459 
460     test_muldiv255(reporter);
461     test_muldiv255ceiling(reporter);
462     test_copysign(reporter);
463 
464     {
465         SkScalar x = SK_ScalarNaN;
466         REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
467     }
468 
469     for (i = 1; i <= 10; i++) {
470         x = SkCubeRootBits(i*i*i, 11);
471         REPORTER_ASSERT(reporter, x == i);
472     }
473 
474     x = SkFixedSqrt(SK_Fixed1);
475     REPORTER_ASSERT(reporter, x == SK_Fixed1);
476     x = SkFixedSqrt(SK_Fixed1/4);
477     REPORTER_ASSERT(reporter, x == SK_Fixed1/2);
478     x = SkFixedSqrt(SK_Fixed1*4);
479     REPORTER_ASSERT(reporter, x == SK_Fixed1*2);
480 
481     x = SkFractSqrt(SK_Fract1);
482     REPORTER_ASSERT(reporter, x == SK_Fract1);
483     x = SkFractSqrt(SK_Fract1/4);
484     REPORTER_ASSERT(reporter, x == SK_Fract1/2);
485     x = SkFractSqrt(SK_Fract1/16);
486     REPORTER_ASSERT(reporter, x == SK_Fract1/4);
487 
488     for (i = 1; i < 100; i++) {
489         x = SkFixedSqrt(SK_Fixed1 * i * i);
490         REPORTER_ASSERT(reporter, x == SK_Fixed1 * i);
491     }
492 
493     for (i = 0; i < 1000; i++) {
494         int value = rand.nextS16();
495         int max = rand.nextU16();
496 
497         int clamp = SkClampMax(value, max);
498         int clamp2 = value < 0 ? 0 : (value > max ? max : value);
499         REPORTER_ASSERT(reporter, clamp == clamp2);
500     }
501 
502     for (i = 0; i < 10000; i++) {
503         SkPoint p;
504 
505         // These random values are being treated as 32-bit-patterns, not as
506         // ints; calling SkIntToScalar() here produces crashes.
507         p.setLength((SkScalar) rand.nextS(),
508                     (SkScalar) rand.nextS(),
509                     SK_Scalar1);
510         check_length(reporter, p, SK_Scalar1);
511         p.setLength((SkScalar) (rand.nextS() >> 13),
512                     (SkScalar) (rand.nextS() >> 13),
513                     SK_Scalar1);
514         check_length(reporter, p, SK_Scalar1);
515     }
516 
517     {
518         SkFixed result = SkFixedDiv(100, 100);
519         REPORTER_ASSERT(reporter, result == SK_Fixed1);
520         result = SkFixedDiv(1, SK_Fixed1);
521         REPORTER_ASSERT(reporter, result == 1);
522     }
523 
524     unittest_fastfloat(reporter);
525     unittest_isfinite(reporter);
526 
527 #ifdef SkLONGLONG
528     for (i = 0; i < 10000; i++) {
529         SkFixed numer = rand.nextS();
530         SkFixed denom = rand.nextS();
531         SkFixed result = SkFixedDiv(numer, denom);
532         SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom;
533 
534         (void)SkCLZ(numer);
535         (void)SkCLZ(denom);
536 
537         REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
538         if (check > SK_MaxS32) {
539             check = SK_MaxS32;
540         } else if (check < -SK_MaxS32) {
541             check = SK_MinS32;
542         }
543         REPORTER_ASSERT(reporter, result == (int32_t)check);
544 
545         result = SkFractDiv(numer, denom);
546         check = ((SkLONGLONG)numer << 30) / denom;
547 
548         REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
549         if (check > SK_MaxS32) {
550             check = SK_MaxS32;
551         } else if (check < -SK_MaxS32) {
552             check = SK_MinS32;
553         }
554         REPORTER_ASSERT(reporter, result == (int32_t)check);
555 
556         // make them <= 2^24, so we don't overflow in fixmul
557         numer = numer << 8 >> 8;
558         denom = denom << 8 >> 8;
559 
560         result = SkFixedMul(numer, denom);
561         SkFixed r2 = symmetric_fixmul(numer, denom);
562         //        SkASSERT(result == r2);
563 
564         result = SkFixedMul(numer, numer);
565         r2 = SkFixedSquare(numer);
566         REPORTER_ASSERT(reporter, result == r2);
567 
568         if (numer >= 0 && denom >= 0) {
569             SkFixed mean = SkFixedMean(numer, denom);
570             float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom);
571             float fm = sk_float_sqrt(sk_float_abs(prod));
572             SkFixed mean2 = SkFloatToFixed(fm);
573             int diff = SkAbs32(mean - mean2);
574             REPORTER_ASSERT(reporter, diff <= 1);
575         }
576 
577         {
578             SkFixed mod = SkFixedMod(numer, denom);
579             float n = SkFixedToFloat(numer);
580             float d = SkFixedToFloat(denom);
581             float m = sk_float_mod(n, d);
582             // ensure the same sign
583             REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0));
584             int diff = SkAbs32(mod - SkFloatToFixed(m));
585             REPORTER_ASSERT(reporter, (diff >> 7) == 0);
586         }
587     }
588 #endif
589 
590     for (i = 0; i < 10000; i++) {
591         SkFract x = rand.nextU() >> 1;
592         double xx = (double)x / SK_Fract1;
593         SkFract xr = SkFractSqrt(x);
594         SkFract check = SkFloatToFract(sqrt(xx));
595         REPORTER_ASSERT(reporter, xr == check ||
596                                   xr == check-1 ||
597                                   xr == check+1);
598 
599         xr = SkFixedSqrt(x);
600         xx = (double)x / SK_Fixed1;
601         check = SkFloatToFixed(sqrt(xx));
602         REPORTER_ASSERT(reporter, xr == check || xr == check-1);
603 
604         xr = SkSqrt32(x);
605         xx = (double)x;
606         check = (int32_t)sqrt(xx);
607         REPORTER_ASSERT(reporter, xr == check || xr == check-1);
608     }
609 
610 #if !defined(SK_SCALAR_IS_FLOAT)
611     {
612         SkFixed s, c;
613         s = SkFixedSinCos(0, &c);
614         REPORTER_ASSERT(reporter, s == 0);
615         REPORTER_ASSERT(reporter, c == SK_Fixed1);
616     }
617 
618     int maxDiff = 0;
619     for (i = 0; i < 1000; i++) {
620         SkFixed rads = rand.nextS() >> 10;
621         double frads = SkFixedToFloat(rads);
622 
623         SkFixed s, c;
624         s = SkScalarSinCos(rads, &c);
625 
626         double fs = sin(frads);
627         double fc = cos(frads);
628 
629         SkFixed is = SkFloatToFixed(fs);
630         SkFixed ic = SkFloatToFixed(fc);
631 
632         maxDiff = SkMax32(maxDiff, SkAbs32(is - s));
633         maxDiff = SkMax32(maxDiff, SkAbs32(ic - c));
634     }
635     SkDebugf("SinCos: maximum error = %d\n", maxDiff);
636 #endif
637 
638 #ifdef SK_SCALAR_IS_FLOAT
639     test_blend(reporter);
640 #endif
641 
642     if (false) test_floor(reporter);
643 
644     // disable for now
645     if (false) test_blend31();  // avoid bit rot, suppress warning
646 
647     test_muldivround(reporter);
648     test_clz(reporter);
649 }
650 
651 template <typename T> struct PairRec {
652     T   fYin;
653     T   fYang;
654 };
655 
DEF_TEST(TestEndian,reporter)656 DEF_TEST(TestEndian, reporter) {
657     static const PairRec<uint16_t> g16[] = {
658         { 0x0,      0x0     },
659         { 0xFFFF,   0xFFFF  },
660         { 0x1122,   0x2211  },
661     };
662     static const PairRec<uint32_t> g32[] = {
663         { 0x0,          0x0         },
664         { 0xFFFFFFFF,   0xFFFFFFFF  },
665         { 0x11223344,   0x44332211  },
666     };
667     static const PairRec<uint64_t> g64[] = {
668         { 0x0,      0x0                             },
669         { 0xFFFFFFFFFFFFFFFFULL,  0xFFFFFFFFFFFFFFFFULL  },
670         { 0x1122334455667788ULL,  0x8877665544332211ULL  },
671     };
672 
673     REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
674     REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
675     REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
676 
677     for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
678         REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
679     }
680     for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
681         REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
682     }
683     for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
684         REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
685     }
686 }
687 
688 template <typename T>
test_divmod(skiatest::Reporter * r)689 static void test_divmod(skiatest::Reporter* r) {
690     const struct {
691         T numer;
692         T denom;
693     } kEdgeCases[] = {
694         {(T)17, (T)17},
695         {(T)17, (T)4},
696         {(T)0,  (T)17},
697         // For unsigned T these negatives are just some large numbers.  Doesn't hurt to test them.
698         {(T)-17, (T)-17},
699         {(T)-17, (T)4},
700         {(T)17,  (T)-4},
701         {(T)-17, (T)-4},
702     };
703 
704     for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
705         const T numer = kEdgeCases[i].numer;
706         const T denom = kEdgeCases[i].denom;
707         T div, mod;
708         SkTDivMod(numer, denom, &div, &mod);
709         REPORTER_ASSERT(r, numer/denom == div);
710         REPORTER_ASSERT(r, numer%denom == mod);
711     }
712 
713     SkRandom rand;
714     for (size_t i = 0; i < 10000; i++) {
715         const T numer = (T)rand.nextS();
716         T denom = 0;
717         while (0 == denom) {
718             denom = (T)rand.nextS();
719         }
720         T div, mod;
721         SkTDivMod(numer, denom, &div, &mod);
722         REPORTER_ASSERT(r, numer/denom == div);
723         REPORTER_ASSERT(r, numer%denom == mod);
724     }
725 }
726 
DEF_TEST(divmod_u8,r)727 DEF_TEST(divmod_u8, r) {
728     test_divmod<uint8_t>(r);
729 }
730 
DEF_TEST(divmod_u16,r)731 DEF_TEST(divmod_u16, r) {
732     test_divmod<uint16_t>(r);
733 }
734 
DEF_TEST(divmod_u32,r)735 DEF_TEST(divmod_u32, r) {
736     test_divmod<uint32_t>(r);
737 }
738 
DEF_TEST(divmod_u64,r)739 DEF_TEST(divmod_u64, r) {
740     test_divmod<uint64_t>(r);
741 }
742 
DEF_TEST(divmod_s8,r)743 DEF_TEST(divmod_s8, r) {
744     test_divmod<int8_t>(r);
745 }
746 
DEF_TEST(divmod_s16,r)747 DEF_TEST(divmod_s16, r) {
748     test_divmod<int16_t>(r);
749 }
750 
DEF_TEST(divmod_s32,r)751 DEF_TEST(divmod_s32, r) {
752     test_divmod<int32_t>(r);
753 }
754 
DEF_TEST(divmod_s64,r)755 DEF_TEST(divmod_s64, r) {
756     test_divmod<int64_t>(r);
757 }
758