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