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 "SkColorData.h"
9#include "SkEndian.h"
10#include "SkFDot6.h"
11#include "SkFixed.h"
12#include "SkHalf.h"
13#include "SkMathPriv.h"
14#include "SkPoint.h"
15#include "SkRandom.h"
16#include "Test.h"
17
18static 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
36static void test_quick_div(skiatest::Reporter* reporter) {
37    /*
38    The inverse table is generated by turning on SkDebugf in the following test code
39    */
40    SkFixed storage[kInverseTableSize * 2];
41    SkFixed* table = storage + kInverseTableSize;
42
43    // SkDebugf("static const int gFDot6INVERSE[] = {");
44    for (SkFDot6 i=-kInverseTableSize; i<kInverseTableSize; i++) {
45        if (i != 0) {
46            table[i] = SkFDot6Div(SK_FDot6One, i);
47            REPORTER_ASSERT(reporter, table[i] == gFDot6INVERSE[i + kInverseTableSize]);
48        }
49        // SkDebugf("%d, ", table[i]);
50    }
51    // SkDebugf("}\n");
52
53
54    for (SkFDot6 a = -1024; a <= 1024; a++) {
55        for (SkFDot6 b = -1024; b <= 1024; b++) {
56            if (b != 0) {
57                SkFixed ourAnswer = QuickSkFDot6Div(a, b);
58                SkFixed directAnswer = SkFDot6Div(a, b);
59                REPORTER_ASSERT(reporter,
60                    (directAnswer == 0 && ourAnswer == 0) ||
61                    SkFixedDiv(SkAbs32(directAnswer - ourAnswer), SkAbs32(directAnswer)) <= 1 << 10
62                );
63            }
64        }
65    }
66}
67
68///////////////////////////////////////////////////////////////////////////////
69
70static float sk_fsel(float pred, float result_ge, float result_lt) {
71    return pred >= 0 ? result_ge : result_lt;
72}
73
74static float fast_floor(float x) {
75//    float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
76    float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
77    return (float)(x + big) - big;
78}
79
80static float std_floor(float x) {
81    return sk_float_floor(x);
82}
83
84static void test_floor_value(skiatest::Reporter* reporter, float value) {
85    float fast = fast_floor(value);
86    float std = std_floor(value);
87    if (std != fast) {
88        ERRORF(reporter, "fast_floor(%.9g) == %.9g != %.9g == std_floor(%.9g)",
89               value, fast, std, value);
90    }
91}
92
93static void test_floor(skiatest::Reporter* reporter) {
94    static const float gVals[] = {
95        0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
96    };
97
98    for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
99        test_floor_value(reporter, gVals[i]);
100//        test_floor_value(reporter, -gVals[i]);
101    }
102}
103
104///////////////////////////////////////////////////////////////////////////////
105
106// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
107static void test_muldivround(skiatest::Reporter* reporter) {
108#if 0
109    // this "complete" test is too slow, so we test a random sampling of it
110
111    for (int a = 0; a <= 32767; ++a) {
112        for (int b = 0; b <= 32767; ++b) {
113            unsigned prod0 = SkMul16ShiftRound(a, b, 8);
114            unsigned prod1 = SkMulDiv255Round(a, b);
115            SkASSERT(prod0 == prod1);
116        }
117    }
118#endif
119
120    SkRandom rand;
121    for (int i = 0; i < 10000; ++i) {
122        unsigned a = rand.nextU() & 0x7FFF;
123        unsigned b = rand.nextU() & 0x7FFF;
124
125        unsigned prod0 = SkMul16ShiftRound(a, b, 8);
126        unsigned prod1 = SkMulDiv255Round(a, b);
127
128        REPORTER_ASSERT(reporter, prod0 == prod1);
129    }
130}
131
132static float float_blend(int src, int dst, float unit) {
133    return dst + (src - dst) * unit;
134}
135
136static int blend31(int src, int dst, int a31) {
137    return dst + ((src - dst) * a31 * 2114 >> 16);
138    //    return dst + ((src - dst) * a31 * 33 >> 10);
139}
140
141static int blend31_slow(int src, int dst, int a31) {
142    int prod = src * a31 + (31 - a31) * dst + 16;
143    prod = (prod + (prod >> 5)) >> 5;
144    return prod;
145}
146
147static int blend31_round(int src, int dst, int a31) {
148    int prod = (src - dst) * a31 + 16;
149    prod = (prod + (prod >> 5)) >> 5;
150    return dst + prod;
151}
152
153static int blend31_old(int src, int dst, int a31) {
154    a31 += a31 >> 4;
155    return dst + ((src - dst) * a31 >> 5);
156}
157
158// suppress unused code warning
159static int (*blend_functions[])(int, int, int) = {
160    blend31,
161    blend31_slow,
162    blend31_round,
163    blend31_old
164};
165
166static void test_blend31() {
167    int failed = 0;
168    int death = 0;
169    if (false) { // avoid bit rot, suppress warning
170        failed = (*blend_functions[0])(0,0,0);
171    }
172    for (int src = 0; src <= 255; src++) {
173        for (int dst = 0; dst <= 255; dst++) {
174            for (int a = 0; a <= 31; a++) {
175//                int r0 = blend31(src, dst, a);
176//                int r0 = blend31_round(src, dst, a);
177//                int r0 = blend31_old(src, dst, a);
178                int r0 = blend31_slow(src, dst, a);
179
180                float f = float_blend(src, dst, a / 31.f);
181                int r1 = (int)f;
182                int r2 = SkScalarRoundToInt(f);
183
184                if (r0 != r1 && r0 != r2) {
185                    SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
186                                 src,   dst, a,        r0,      f);
187                    failed += 1;
188                }
189                if (r0 > 255) {
190                    death += 1;
191                    SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
192                                        src,   dst, a,        r0,      f);
193                }
194            }
195        }
196    }
197    SkDebugf("---- failed %d death %d\n", failed, death);
198}
199
200static void test_blend(skiatest::Reporter* reporter) {
201    for (int src = 0; src <= 255; src++) {
202        for (int dst = 0; dst <= 255; dst++) {
203            for (int a = 0; a <= 255; a++) {
204                int r0 = SkAlphaBlend255(src, dst, a);
205                float f1 = float_blend(src, dst, a / 255.f);
206                int r1 = SkScalarRoundToInt(f1);
207
208                if (r0 != r1) {
209                    float diff = sk_float_abs(f1 - r1);
210                    diff = sk_float_abs(diff - 0.5f);
211                    if (diff > (1 / 255.f)) {
212                        ERRORF(reporter, "src:%d dst:%d a:%d "
213                               "result:%d float:%g\n", src, dst, a, r0, f1);
214                    }
215                }
216            }
217        }
218    }
219}
220
221static void check_length(skiatest::Reporter* reporter,
222                         const SkPoint& p, SkScalar targetLen) {
223    float x = SkScalarToFloat(p.fX);
224    float y = SkScalarToFloat(p.fY);
225    float len = sk_float_sqrt(x*x + y*y);
226
227    len /= SkScalarToFloat(targetLen);
228
229    REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
230}
231
232static float make_zero() {
233    return sk_float_sin(0);
234}
235
236static void unittest_isfinite(skiatest::Reporter* reporter) {
237    float nan = sk_float_asin(2);
238    float inf = 1.0f / make_zero();
239    float big = 3.40282e+038f;
240
241    REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
242    REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
243    REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
244    REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
245
246    REPORTER_ASSERT(reporter,  SkScalarIsNaN(nan));
247    REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
248    REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
249    REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
250
251    REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
252    REPORTER_ASSERT(reporter,  SkScalarIsFinite(big));
253    REPORTER_ASSERT(reporter,  SkScalarIsFinite(-big));
254    REPORTER_ASSERT(reporter,  SkScalarIsFinite(0));
255}
256
257static void unittest_half(skiatest::Reporter* reporter) {
258    static const float gFloats[] = {
259        0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
260        -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
261    };
262
263    for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
264        SkHalf h = SkFloatToHalf(gFloats[i]);
265        float f = SkHalfToFloat(h);
266        REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
267    }
268
269    // check some special values
270    union FloatUnion {
271        uint32_t fU;
272        float    fF;
273    };
274
275    static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
276    SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
277    float f = SkHalfToFloat(h);
278    REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
279
280    static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
281    h = SkFloatToHalf(largestNegativeHalf.fF);
282    f = SkHalfToFloat(h);
283    REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
284
285    static const FloatUnion smallestPositiveHalf = { 102 << 23 };
286    h = SkFloatToHalf(smallestPositiveHalf.fF);
287    f = SkHalfToFloat(h);
288    REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
289
290    static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
291    h = SkFloatToHalf(overflowHalf.fF);
292    f = SkHalfToFloat(h);
293    REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
294
295    static const FloatUnion underflowHalf = { 101 << 23 };
296    h = SkFloatToHalf(underflowHalf.fF);
297    f = SkHalfToFloat(h);
298    REPORTER_ASSERT(reporter, f == 0.0f );
299
300    static const FloatUnion inf32 = { 255 << 23 };
301    h = SkFloatToHalf(inf32.fF);
302    f = SkHalfToFloat(h);
303    REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
304
305    static const FloatUnion nan32 = { 255 << 23 | 1 };
306    h = SkFloatToHalf(nan32.fF);
307    f = SkHalfToFloat(h);
308    REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
309
310}
311
312template <typename RSqrtFn>
313static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
314    const float maxRelativeError = 6.50196699e-4f;
315
316    // test close to 0 up to 1
317    float input = 0.000001f;
318    for (int i = 0; i < 1000; ++i) {
319        float exact = 1.0f/sk_float_sqrt(input);
320        float estimate = rsqrt(input);
321        float relativeError = sk_float_abs(exact - estimate)/exact;
322        REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
323        input += 0.001f;
324    }
325
326    // test 1 to ~100
327    input = 1.0f;
328    for (int i = 0; i < 1000; ++i) {
329        float exact = 1.0f/sk_float_sqrt(input);
330        float estimate = rsqrt(input);
331        float relativeError = sk_float_abs(exact - estimate)/exact;
332        REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
333        input += 0.01f;
334    }
335
336    // test some big numbers
337    input = 1000000.0f;
338    for (int i = 0; i < 100; ++i) {
339        float exact = 1.0f/sk_float_sqrt(input);
340        float estimate = rsqrt(input);
341        float relativeError = sk_float_abs(exact - estimate)/exact;
342        REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
343        input += 754326.f;
344    }
345}
346
347static void test_muldiv255(skiatest::Reporter* reporter) {
348    for (int a = 0; a <= 255; a++) {
349        for (int b = 0; b <= 255; b++) {
350            int ab = a * b;
351            float s = ab / 255.0f;
352            int round = (int)floorf(s + 0.5f);
353            int trunc = (int)floorf(s);
354
355            int iround = SkMulDiv255Round(a, b);
356            int itrunc = SkMulDiv255Trunc(a, b);
357
358            REPORTER_ASSERT(reporter, iround == round);
359            REPORTER_ASSERT(reporter, itrunc == trunc);
360
361            REPORTER_ASSERT(reporter, itrunc <= iround);
362            REPORTER_ASSERT(reporter, iround <= a);
363            REPORTER_ASSERT(reporter, iround <= b);
364        }
365    }
366}
367
368static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
369    for (int c = 0; c <= 255; c++) {
370        for (int a = 0; a <= 255; a++) {
371            int product = (c * a + 255);
372            int expected_ceiling = (product + (product >> 8)) >> 8;
373            int webkit_ceiling = (c * a + 254) / 255;
374            REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
375            int skia_ceiling = SkMulDiv255Ceiling(c, a);
376            REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
377        }
378    }
379}
380
381static void test_copysign(skiatest::Reporter* reporter) {
382    static const int32_t gTriples[] = {
383        // x, y, expected result
384        0, 0, 0,
385        0, 1, 0,
386        0, -1, 0,
387        1, 0, 1,
388        1, 1, 1,
389        1, -1, -1,
390        -1, 0, 1,
391        -1, 1, 1,
392        -1, -1, -1,
393    };
394    for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
395        REPORTER_ASSERT(reporter,
396                        SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
397        float x = (float)gTriples[i];
398        float y = (float)gTriples[i+1];
399        float expected = (float)gTriples[i+2];
400        REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
401    }
402
403    SkRandom rand;
404    for (int j = 0; j < 1000; j++) {
405        int ix = rand.nextS();
406        REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
407        REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
408        REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
409        REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
410
411        SkScalar sx = rand.nextSScalar1();
412        REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
413        REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
414        REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
415        REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
416    }
417}
418
419DEF_TEST(Math, reporter) {
420    int         i;
421    SkRandom    rand;
422
423    // these should assert
424#if 0
425    SkToS8(128);
426    SkToS8(-129);
427    SkToU8(256);
428    SkToU8(-5);
429
430    SkToS16(32768);
431    SkToS16(-32769);
432    SkToU16(65536);
433    SkToU16(-5);
434
435    if (sizeof(size_t) > 4) {
436        SkToS32(4*1024*1024);
437        SkToS32(-4*1024*1024);
438        SkToU32(5*1024*1024);
439        SkToU32(-5);
440    }
441#endif
442
443    test_muldiv255(reporter);
444    test_muldiv255ceiling(reporter);
445    test_copysign(reporter);
446
447    {
448        SkScalar x = SK_ScalarNaN;
449        REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
450    }
451
452    for (i = 0; i < 1000; i++) {
453        int value = rand.nextS16();
454        int max = rand.nextU16();
455
456        int clamp = SkClampMax(value, max);
457        int clamp2 = value < 0 ? 0 : (value > max ? max : value);
458        REPORTER_ASSERT(reporter, clamp == clamp2);
459    }
460
461    for (i = 0; i < 10000; i++) {
462        SkPoint p;
463
464        // These random values are being treated as 32-bit-patterns, not as
465        // ints; calling SkIntToScalar() here produces crashes.
466        p.setLength((SkScalar) rand.nextS(),
467                    (SkScalar) rand.nextS(),
468                    SK_Scalar1);
469        check_length(reporter, p, SK_Scalar1);
470        p.setLength((SkScalar) (rand.nextS() >> 13),
471                    (SkScalar) (rand.nextS() >> 13),
472                    SK_Scalar1);
473        check_length(reporter, p, SK_Scalar1);
474    }
475
476    {
477        SkFixed result = SkFixedDiv(100, 100);
478        REPORTER_ASSERT(reporter, result == SK_Fixed1);
479        result = SkFixedDiv(1, SK_Fixed1);
480        REPORTER_ASSERT(reporter, result == 1);
481        result = SkFixedDiv(10 - 1, SK_Fixed1 * 3);
482        REPORTER_ASSERT(reporter, result == 3);
483    }
484
485    {
486        REPORTER_ASSERT(reporter, (SkFixedRoundToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
487        REPORTER_ASSERT(reporter, (SkFixedFloorToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
488        REPORTER_ASSERT(reporter, (SkFixedCeilToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
489    }
490
491    unittest_isfinite(reporter);
492    unittest_half(reporter);
493    test_rsqrt(reporter, sk_float_rsqrt);
494    test_rsqrt(reporter, sk_float_rsqrt_portable);
495
496    for (i = 0; i < 10000; i++) {
497        SkFixed numer = rand.nextS();
498        SkFixed denom = rand.nextS();
499        SkFixed result = SkFixedDiv(numer, denom);
500        int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
501
502        (void)SkCLZ(numer);
503        (void)SkCLZ(denom);
504
505        REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
506        if (check > SK_MaxS32) {
507            check = SK_MaxS32;
508        } else if (check < -SK_MaxS32) {
509            check = SK_MinS32;
510        }
511        if (result != (int32_t)check) {
512            ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
513        }
514        REPORTER_ASSERT(reporter, result == (int32_t)check);
515    }
516
517    test_blend(reporter);
518
519    if (false) test_floor(reporter);
520
521    // disable for now
522    if (false) test_blend31();  // avoid bit rot, suppress warning
523
524    test_muldivround(reporter);
525    test_clz(reporter);
526    test_quick_div(reporter);
527}
528
529template <typename T> struct PairRec {
530    T   fYin;
531    T   fYang;
532};
533
534DEF_TEST(TestEndian, reporter) {
535    static const PairRec<uint16_t> g16[] = {
536        { 0x0,      0x0     },
537        { 0xFFFF,   0xFFFF  },
538        { 0x1122,   0x2211  },
539    };
540    static const PairRec<uint32_t> g32[] = {
541        { 0x0,          0x0         },
542        { 0xFFFFFFFF,   0xFFFFFFFF  },
543        { 0x11223344,   0x44332211  },
544    };
545    static const PairRec<uint64_t> g64[] = {
546        { 0x0,      0x0                             },
547        { 0xFFFFFFFFFFFFFFFFULL,  0xFFFFFFFFFFFFFFFFULL  },
548        { 0x1122334455667788ULL,  0x8877665544332211ULL  },
549    };
550
551    REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
552    REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
553    REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
554
555    for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
556        REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
557    }
558    for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
559        REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
560    }
561    for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
562        REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
563    }
564}
565
566template <typename T>
567static void test_divmod(skiatest::Reporter* r) {
568    const struct {
569        T numer;
570        T denom;
571    } kEdgeCases[] = {
572        {(T)17, (T)17},
573        {(T)17, (T)4},
574        {(T)0,  (T)17},
575        // For unsigned T these negatives are just some large numbers.  Doesn't hurt to test them.
576        {(T)-17, (T)-17},
577        {(T)-17, (T)4},
578        {(T)17,  (T)-4},
579        {(T)-17, (T)-4},
580    };
581
582    for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
583        const T numer = kEdgeCases[i].numer;
584        const T denom = kEdgeCases[i].denom;
585        T div, mod;
586        SkTDivMod(numer, denom, &div, &mod);
587        REPORTER_ASSERT(r, numer/denom == div);
588        REPORTER_ASSERT(r, numer%denom == mod);
589    }
590
591    SkRandom rand;
592    for (size_t i = 0; i < 10000; i++) {
593        const T numer = (T)rand.nextS();
594        T denom = 0;
595        while (0 == denom) {
596            denom = (T)rand.nextS();
597        }
598        T div, mod;
599        SkTDivMod(numer, denom, &div, &mod);
600        REPORTER_ASSERT(r, numer/denom == div);
601        REPORTER_ASSERT(r, numer%denom == mod);
602    }
603}
604
605DEF_TEST(divmod_u8, r) {
606    test_divmod<uint8_t>(r);
607}
608
609DEF_TEST(divmod_u16, r) {
610    test_divmod<uint16_t>(r);
611}
612
613DEF_TEST(divmod_u32, r) {
614    test_divmod<uint32_t>(r);
615}
616
617DEF_TEST(divmod_u64, r) {
618    test_divmod<uint64_t>(r);
619}
620
621DEF_TEST(divmod_s8, r) {
622    test_divmod<int8_t>(r);
623}
624
625DEF_TEST(divmod_s16, r) {
626    test_divmod<int16_t>(r);
627}
628
629DEF_TEST(divmod_s32, r) {
630    test_divmod<int32_t>(r);
631}
632
633DEF_TEST(divmod_s64, r) {
634    test_divmod<int64_t>(r);
635}
636
637static void test_nextsizepow2(skiatest::Reporter* r, size_t test, size_t expectedAns) {
638    size_t ans = GrNextSizePow2(test);
639
640    REPORTER_ASSERT(r, ans == expectedAns);
641    //SkDebugf("0x%zx -> 0x%zx (0x%zx)\n", test, ans, expectedAns);
642}
643
644DEF_TEST(GrNextSizePow2, reporter) {
645    constexpr int kNumSizeTBits = 8 * sizeof(size_t);
646
647    size_t test = 0, expectedAns = 1;
648
649    test_nextsizepow2(reporter, test, expectedAns);
650
651    test = 1; expectedAns = 1;
652
653    for (int i = 1; i < kNumSizeTBits; ++i) {
654        test_nextsizepow2(reporter, test, expectedAns);
655
656        test++;
657        expectedAns <<= 1;
658
659        test_nextsizepow2(reporter, test, expectedAns);
660
661        test = expectedAns;
662    }
663
664    // For the remaining three tests there is no higher power (of 2)
665    test = 0x1;
666    test <<= kNumSizeTBits-1;
667    test_nextsizepow2(reporter, test, test);
668
669    test++;
670    test_nextsizepow2(reporter, test, test);
671
672    test_nextsizepow2(reporter, SIZE_MAX, SIZE_MAX);
673}
674
675DEF_TEST(FloatSaturate32, reporter) {
676    const struct {
677        float   fFloat;
678        int     fExpectedInt;
679    } recs[] = {
680        { 0, 0 },
681        { 100.5f, 100 },
682        { (float)SK_MaxS32, SK_MaxS32FitsInFloat },
683        { (float)SK_MinS32, SK_MinS32FitsInFloat },
684        { SK_MaxS32 * 100.0f, SK_MaxS32FitsInFloat },
685        { SK_MinS32 * 100.0f, SK_MinS32FitsInFloat },
686        { SK_ScalarInfinity, SK_MaxS32FitsInFloat },
687        { SK_ScalarNegativeInfinity, SK_MinS32FitsInFloat },
688        { SK_ScalarNaN, SK_MaxS32FitsInFloat },
689    };
690
691    for (auto r : recs) {
692        int i = sk_float_saturate2int(r.fFloat);
693        REPORTER_ASSERT(reporter, r.fExpectedInt == i);
694    }
695}
696
697DEF_TEST(FloatSaturate64, reporter) {
698    const struct {
699        float   fFloat;
700        int64_t fExpected64;
701    } recs[] = {
702        { 0, 0 },
703        { 100.5f, 100 },
704        { (float)SK_MaxS64, SK_MaxS64FitsInFloat },
705        { (float)SK_MinS64, SK_MinS64FitsInFloat },
706        { SK_MaxS64 * 100.0f, SK_MaxS64FitsInFloat },
707        { SK_MinS64 * 100.0f, SK_MinS64FitsInFloat },
708        { SK_ScalarInfinity, SK_MaxS64FitsInFloat },
709        { SK_ScalarNegativeInfinity, SK_MinS64FitsInFloat },
710        { SK_ScalarNaN, SK_MaxS64FitsInFloat },
711    };
712
713    for (auto r : recs) {
714        int64_t i = sk_float_saturate2int64(r.fFloat);
715        REPORTER_ASSERT(reporter, r.fExpected64 == i);
716    }
717}
718
719DEF_TEST(DoubleSaturate32, reporter) {
720    const struct {
721        double  fDouble;
722        int     fExpectedInt;
723    } recs[] = {
724        { 0, 0 },
725        { 100.5, 100 },
726        { SK_MaxS32, SK_MaxS32 },
727        { SK_MinS32, SK_MinS32 },
728        { SK_MaxS32 - 1, SK_MaxS32 - 1 },
729        { SK_MinS32 + 1, SK_MinS32 + 1 },
730        { SK_MaxS32 * 100.0, SK_MaxS32 },
731        { SK_MinS32 * 100.0, SK_MinS32 },
732        { SK_ScalarInfinity, SK_MaxS32 },
733        { SK_ScalarNegativeInfinity, SK_MinS32 },
734        { SK_ScalarNaN, SK_MaxS32 },
735    };
736
737    for (auto r : recs) {
738        int i = sk_double_saturate2int(r.fDouble);
739        REPORTER_ASSERT(reporter, r.fExpectedInt == i);
740    }
741}
742