epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 8 | #include "SkColorData.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "SkEndian.h" |
Yuqian Li | ce1d293 | 2016-11-18 10:18:15 -0500 | [diff] [blame] | 10 | #include "SkFDot6.h" |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame] | 11 | #include "SkFixed.h" |
jvanverth | 9367992 | 2014-11-26 13:15:59 -0800 | [diff] [blame] | 12 | #include "SkHalf.h" |
reed@google.com | 4b163ed | 2012-08-07 21:35:13 +0000 | [diff] [blame] | 13 | #include "SkMathPriv.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 14 | #include "SkPoint.h" |
| 15 | #include "SkRandom.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 16 | #include "Test.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 17 | |
reed@google.com | c21f86f | 2013-04-29 14:18:23 +0000 | [diff] [blame] | 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)); |
reed@google.com | 7729534d | 2013-04-29 14:43:50 +0000 | [diff] [blame] | 22 | REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U)); |
skia.committer@gmail.com | 8152113 | 2013-04-30 07:01:03 +0000 | [diff] [blame] | 23 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 24 | SkRandom rand; |
reed@google.com | c21f86f | 2013-04-29 14:18:23 +0000 | [diff] [blame] | 25 | for (int i = 0; i < 1000; ++i) { |
| 26 | uint32_t mask = rand.nextU(); |
reed@google.com | bc57a29 | 2013-04-29 15:27:42 +0000 | [diff] [blame] | 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); |
reed@google.com | c21f86f | 2013-04-29 14:18:23 +0000 | [diff] [blame] | 30 | int intri = SkCLZ(mask); |
| 31 | int porta = SkCLZ_portable(mask); |
| 32 | REPORTER_ASSERT(reporter, intri == porta); |
| 33 | } |
| 34 | } |
| 35 | |
Yuqian Li | ce1d293 | 2016-11-18 10:18:15 -0500 | [diff] [blame] | 36 | static 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 | |
reed@google.com | c21f86f | 2013-04-29 14:18:23 +0000 | [diff] [blame] | 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 70 | static float sk_fsel(float pred, float result_ge, float result_lt) { |
| 71 | return pred >= 0 ? result_ge : result_lt; |
| 72 | } |
| 73 | |
| 74 | static float fast_floor(float x) { |
reed@google.com | c20bc25 | 2012-05-30 13:48:14 +0000 | [diff] [blame] | 75 | // float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23); |
| 76 | float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23)); |
robertphillips@google.com | 00bf06a | 2012-05-30 15:19:17 +0000 | [diff] [blame] | 77 | return (float)(x + big) - big; |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static float std_floor(float x) { |
| 81 | return sk_float_floor(x); |
| 82 | } |
| 83 | |
| 84 | static void test_floor_value(skiatest::Reporter* reporter, float value) { |
| 85 | float fast = fast_floor(value); |
| 86 | float std = std_floor(value); |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 87 | if (std != fast) { |
| 88 | ERRORF(reporter, "fast_floor(%.9g) == %.9g != %.9g == std_floor(%.9g)", |
| 89 | value, fast, std, value); |
| 90 | } |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static 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 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 97 | |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 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 | |
reed@google.com | ea774d2 | 2013-04-22 20:21:56 +0000 | [diff] [blame] | 106 | // test that SkMul16ShiftRound and SkMulDiv255Round return the same result |
| 107 | static 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 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 120 | SkRandom rand; |
reed@google.com | ea774d2 | 2013-04-22 20:21:56 +0000 | [diff] [blame] | 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 | |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 132 | static float float_blend(int src, int dst, float unit) { |
| 133 | return dst + (src - dst) * unit; |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 134 | } |
| 135 | |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 136 | static 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 | |
| 141 | static 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 | |
| 147 | static 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 | |
| 153 | static int blend31_old(int src, int dst, int a31) { |
| 154 | a31 += a31 >> 4; |
| 155 | return dst + ((src - dst) * a31 >> 5); |
| 156 | } |
| 157 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 158 | // suppress unused code warning |
| 159 | static int (*blend_functions[])(int, int, int) = { |
| 160 | blend31, |
| 161 | blend31_slow, |
| 162 | blend31_round, |
| 163 | blend31_old |
| 164 | }; |
| 165 | |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 166 | static void test_blend31() { |
| 167 | int failed = 0; |
| 168 | int death = 0; |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 169 | if (false) { // avoid bit rot, suppress warning |
| 170 | failed = (*blend_functions[0])(0,0,0); |
| 171 | } |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 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; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 182 | int r2 = SkScalarRoundToInt(f); |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 183 | |
| 184 | if (r0 != r1 && r0 != r2) { |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 185 | SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n", |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 186 | src, dst, a, r0, f); |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 187 | failed += 1; |
| 188 | } |
| 189 | if (r0 > 255) { |
| 190 | death += 1; |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 191 | SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n", |
| 192 | src, dst, a, r0, f); |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | SkDebugf("---- failed %d death %d\n", failed, death); |
| 198 | } |
| 199 | |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 200 | static 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); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 206 | int r1 = SkScalarRoundToInt(f1); |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 207 | |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 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)) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 212 | ERRORF(reporter, "src:%d dst:%d a:%d " |
| 213 | "result:%d float:%g\n", src, dst, a, r0, f1); |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 220 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 221 | static void check_length(skiatest::Reporter* reporter, |
| 222 | const SkPoint& p, SkScalar targetLen) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 223 | float x = SkScalarToFloat(p.fX); |
| 224 | float y = SkScalarToFloat(p.fY); |
| 225 | float len = sk_float_sqrt(x*x + y*y); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 226 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 227 | len /= SkScalarToFloat(targetLen); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 228 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 229 | REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 230 | } |
| 231 | |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 232 | static float make_zero() { |
| 233 | return sk_float_sin(0); |
| 234 | } |
| 235 | |
| 236 | static void unittest_isfinite(skiatest::Reporter* reporter) { |
epoger@google.com | bf083a9 | 2011-06-08 18:26:08 +0000 | [diff] [blame] | 237 | float nan = sk_float_asin(2); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 238 | float inf = 1.0f / make_zero(); |
| 239 | float big = 3.40282e+038f; |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 240 | |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 241 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf)); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 242 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf)); |
| 243 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf)); |
| 244 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf)); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 245 | |
| 246 | REPORTER_ASSERT(reporter, SkScalarIsNaN(nan)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 247 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(big)); |
| 248 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big)); |
| 249 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(0)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 250 | |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 251 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 252 | REPORTER_ASSERT(reporter, SkScalarIsFinite(big)); |
| 253 | REPORTER_ASSERT(reporter, SkScalarIsFinite(-big)); |
| 254 | REPORTER_ASSERT(reporter, SkScalarIsFinite(0)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 255 | } |
| 256 | |
jvanverth | 9367992 | 2014-11-26 13:15:59 -0800 | [diff] [blame] | 257 | static 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 | |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 312 | template <typename RSqrtFn> |
| 313 | static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) { |
jvanverth | 29c6979 | 2015-07-23 11:14:29 -0700 | [diff] [blame] | 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); |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 320 | float estimate = rsqrt(input); |
jvanverth | 29c6979 | 2015-07-23 11:14:29 -0700 | [diff] [blame] | 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); |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 330 | float estimate = rsqrt(input); |
jvanverth | 29c6979 | 2015-07-23 11:14:29 -0700 | [diff] [blame] | 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); |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 340 | float estimate = rsqrt(input); |
jvanverth | 29c6979 | 2015-07-23 11:14:29 -0700 | [diff] [blame] | 341 | float relativeError = sk_float_abs(exact - estimate)/exact; |
| 342 | REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); |
| 343 | input += 754326.f; |
| 344 | } |
| 345 | } |
| 346 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 347 | static void test_muldiv255(skiatest::Reporter* reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 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); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 354 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 355 | int iround = SkMulDiv255Round(a, b); |
| 356 | int itrunc = SkMulDiv255Trunc(a, b); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 357 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 358 | REPORTER_ASSERT(reporter, iround == round); |
| 359 | REPORTER_ASSERT(reporter, itrunc == trunc); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 360 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 361 | REPORTER_ASSERT(reporter, itrunc <= iround); |
| 362 | REPORTER_ASSERT(reporter, iround <= a); |
| 363 | REPORTER_ASSERT(reporter, iround <= b); |
| 364 | } |
| 365 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 366 | } |
| 367 | |
senorblanco@chromium.org | ec7a30c | 2010-12-07 21:07:56 +0000 | [diff] [blame] | 368 | static 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 | |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 381 | static 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]); |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 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); |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 401 | } |
| 402 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 403 | SkRandom rand; |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 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 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 419 | DEF_TEST(Math, reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 420 | int i; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 421 | SkRandom rand; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 422 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 423 | // these should assert |
| 424 | #if 0 |
| 425 | SkToS8(128); |
| 426 | SkToS8(-129); |
| 427 | SkToU8(256); |
| 428 | SkToU8(-5); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 429 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 430 | SkToS16(32768); |
| 431 | SkToS16(-32769); |
| 432 | SkToU16(65536); |
| 433 | SkToU16(-5); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 434 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 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 |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 442 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 443 | test_muldiv255(reporter); |
senorblanco@chromium.org | ec7a30c | 2010-12-07 21:07:56 +0000 | [diff] [blame] | 444 | test_muldiv255ceiling(reporter); |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 445 | test_copysign(reporter); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 446 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 447 | { |
| 448 | SkScalar x = SK_ScalarNaN; |
| 449 | REPORTER_ASSERT(reporter, SkScalarIsNaN(x)); |
| 450 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 451 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 452 | for (i = 0; i < 1000; i++) { |
| 453 | int value = rand.nextS16(); |
| 454 | int max = rand.nextU16(); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 455 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 456 | int clamp = SkClampMax(value, max); |
| 457 | int clamp2 = value < 0 ? 0 : (value > max ? max : value); |
| 458 | REPORTER_ASSERT(reporter, clamp == clamp2); |
| 459 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 460 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 461 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 462 | SkPoint p; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 463 | |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 464 | // These random values are being treated as 32-bit-patterns, not as |
| 465 | // ints; calling SkIntToScalar() here produces crashes. |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 466 | p.setLength((SkScalar) rand.nextS(), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 467 | (SkScalar) rand.nextS(), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 468 | SK_Scalar1); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 469 | check_length(reporter, p, SK_Scalar1); |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 470 | p.setLength((SkScalar) (rand.nextS() >> 13), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 471 | (SkScalar) (rand.nextS() >> 13), |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 472 | SK_Scalar1); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 473 | check_length(reporter, p, SK_Scalar1); |
| 474 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 475 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 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); |
liyuqian | 0d2c234 | 2016-07-13 13:34:46 -0700 | [diff] [blame] | 481 | result = SkFixedDiv(10 - 1, SK_Fixed1 * 3); |
| 482 | REPORTER_ASSERT(reporter, result == 3); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 483 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 484 | |
liyuqian | 3f490cc | 2016-10-20 11:23:09 -0700 | [diff] [blame] | 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 | |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 491 | unittest_isfinite(reporter); |
jvanverth | 9367992 | 2014-11-26 13:15:59 -0800 | [diff] [blame] | 492 | unittest_half(reporter); |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 493 | test_rsqrt(reporter, sk_float_rsqrt); |
| 494 | test_rsqrt(reporter, sk_float_rsqrt_portable); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 495 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 496 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 497 | SkFixed numer = rand.nextS(); |
| 498 | SkFixed denom = rand.nextS(); |
| 499 | SkFixed result = SkFixedDiv(numer, denom); |
caryclark | 3127c99 | 2015-12-09 12:02:30 -0800 | [diff] [blame] | 500 | int64_t check = SkLeftShift((int64_t)numer, 16) / denom; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 501 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 502 | (void)SkCLZ(numer); |
| 503 | (void)SkCLZ(denom); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 504 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 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 | } |
mtklein | 97ca98d | 2015-03-18 11:32:21 -0700 | [diff] [blame] | 511 | if (result != (int32_t)check) { |
| 512 | ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check); |
| 513 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 514 | REPORTER_ASSERT(reporter, result == (int32_t)check); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 515 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 516 | |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 517 | test_blend(reporter); |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 518 | |
humper@google.com | 05af1af | 2013-01-07 16:47:43 +0000 | [diff] [blame] | 519 | if (false) test_floor(reporter); |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 520 | |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 521 | // disable for now |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 522 | if (false) test_blend31(); // avoid bit rot, suppress warning |
reed@google.com | ea774d2 | 2013-04-22 20:21:56 +0000 | [diff] [blame] | 523 | |
| 524 | test_muldivround(reporter); |
reed@google.com | c21f86f | 2013-04-29 14:18:23 +0000 | [diff] [blame] | 525 | test_clz(reporter); |
Yuqian Li | ce1d293 | 2016-11-18 10:18:15 -0500 | [diff] [blame] | 526 | test_quick_div(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 527 | } |
| 528 | |
reed@google.com | c9f8166 | 2013-05-03 18:06:31 +0000 | [diff] [blame] | 529 | template <typename T> struct PairRec { |
| 530 | T fYin; |
| 531 | T fYang; |
| 532 | }; |
| 533 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 534 | DEF_TEST(TestEndian, reporter) { |
reed@google.com | c9f8166 | 2013-05-03 18:06:31 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 2c86fbb | 2013-09-26 19:22:54 +0000 | [diff] [blame] | 566 | template <typename T> |
| 567 | static 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 | |
| 605 | DEF_TEST(divmod_u8, r) { |
| 606 | test_divmod<uint8_t>(r); |
| 607 | } |
| 608 | |
| 609 | DEF_TEST(divmod_u16, r) { |
| 610 | test_divmod<uint16_t>(r); |
| 611 | } |
| 612 | |
| 613 | DEF_TEST(divmod_u32, r) { |
| 614 | test_divmod<uint32_t>(r); |
| 615 | } |
| 616 | |
| 617 | DEF_TEST(divmod_u64, r) { |
| 618 | test_divmod<uint64_t>(r); |
| 619 | } |
| 620 | |
| 621 | DEF_TEST(divmod_s8, r) { |
| 622 | test_divmod<int8_t>(r); |
| 623 | } |
| 624 | |
| 625 | DEF_TEST(divmod_s16, r) { |
| 626 | test_divmod<int16_t>(r); |
| 627 | } |
| 628 | |
| 629 | DEF_TEST(divmod_s32, r) { |
| 630 | test_divmod<int32_t>(r); |
| 631 | } |
| 632 | |
| 633 | DEF_TEST(divmod_s64, r) { |
| 634 | test_divmod<int64_t>(r); |
| 635 | } |
Robert Phillips | 9e38047 | 2016-10-28 12:15:03 -0400 | [diff] [blame] | 636 | |
| 637 | static 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 | |
| 644 | DEF_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 | } |
Mike Reed | 828f1d5 | 2017-08-09 11:06:53 -0400 | [diff] [blame] | 674 | |
| 675 | DEF_TEST(FloatSaturate, 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 | } |
Mike Reed | e076223 | 2017-10-10 14:49:35 -0400 | [diff] [blame] | 696 | |
| 697 | DEF_TEST(DoubleSaturate, reporter) { |
| 698 | const struct { |
| 699 | double fDouble; |
| 700 | int fExpectedInt; |
| 701 | } recs[] = { |
| 702 | { 0, 0 }, |
| 703 | { 100.5, 100 }, |
| 704 | { SK_MaxS32, SK_MaxS32 }, |
| 705 | { SK_MinS32, SK_MinS32 }, |
| 706 | { SK_MaxS32 - 1, SK_MaxS32 - 1 }, |
| 707 | { SK_MinS32 + 1, SK_MinS32 + 1 }, |
| 708 | { SK_MaxS32 * 100.0, SK_MaxS32 }, |
| 709 | { SK_MinS32 * 100.0, SK_MinS32 }, |
| 710 | { SK_ScalarInfinity, SK_MaxS32 }, |
| 711 | { SK_ScalarNegativeInfinity, SK_MinS32 }, |
| 712 | { SK_ScalarNaN, SK_MaxS32 }, |
| 713 | }; |
| 714 | |
| 715 | for (auto r : recs) { |
| 716 | int i = sk_double_saturate2int(r.fDouble); |
| 717 | REPORTER_ASSERT(reporter, r.fExpectedInt == i); |
| 718 | } |
| 719 | } |