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