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