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