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