epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 8 | #include "Test.h" |
reed@android.com | c846ede | 2010-04-13 15:29:15 +0000 | [diff] [blame] | 9 | #include "SkFloatingPoint.h" |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 10 | #include "SkMath.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 11 | #include "SkPoint.h" |
| 12 | #include "SkRandom.h" |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 14 | |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 15 | static float sk_fsel(float pred, float result_ge, float result_lt) { |
| 16 | return pred >= 0 ? result_ge : result_lt; |
| 17 | } |
| 18 | |
| 19 | static float fast_floor(float x) { |
reed@google.com | c20bc25 | 2012-05-30 13:48:14 +0000 | [diff] [blame] | 20 | // float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23); |
| 21 | float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23)); |
robertphillips@google.com | 00bf06a | 2012-05-30 15:19:17 +0000 | [diff] [blame] | 22 | return (float)(x + big) - big; |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | static float std_floor(float x) { |
| 26 | return sk_float_floor(x); |
| 27 | } |
| 28 | |
| 29 | static void test_floor_value(skiatest::Reporter* reporter, float value) { |
| 30 | float fast = fast_floor(value); |
| 31 | float std = std_floor(value); |
| 32 | REPORTER_ASSERT(reporter, std == fast); |
| 33 | // SkDebugf("value[%1.9f] std[%g] fast[%g] equal[%d]\n", |
| 34 | // value, std, fast, std == fast); |
| 35 | } |
| 36 | |
| 37 | static void test_floor(skiatest::Reporter* reporter) { |
| 38 | static const float gVals[] = { |
| 39 | 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f |
| 40 | }; |
| 41 | |
| 42 | for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) { |
| 43 | test_floor_value(reporter, gVals[i]); |
| 44 | // test_floor_value(reporter, -gVals[i]); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 50 | static float float_blend(int src, int dst, float unit) { |
| 51 | return dst + (src - dst) * unit; |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 52 | } |
| 53 | |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 54 | static int blend31(int src, int dst, int a31) { |
| 55 | return dst + ((src - dst) * a31 * 2114 >> 16); |
| 56 | // return dst + ((src - dst) * a31 * 33 >> 10); |
| 57 | } |
| 58 | |
| 59 | static int blend31_slow(int src, int dst, int a31) { |
| 60 | int prod = src * a31 + (31 - a31) * dst + 16; |
| 61 | prod = (prod + (prod >> 5)) >> 5; |
| 62 | return prod; |
| 63 | } |
| 64 | |
| 65 | static int blend31_round(int src, int dst, int a31) { |
| 66 | int prod = (src - dst) * a31 + 16; |
| 67 | prod = (prod + (prod >> 5)) >> 5; |
| 68 | return dst + prod; |
| 69 | } |
| 70 | |
| 71 | static int blend31_old(int src, int dst, int a31) { |
| 72 | a31 += a31 >> 4; |
| 73 | return dst + ((src - dst) * a31 >> 5); |
| 74 | } |
| 75 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 76 | // suppress unused code warning |
| 77 | static int (*blend_functions[])(int, int, int) = { |
| 78 | blend31, |
| 79 | blend31_slow, |
| 80 | blend31_round, |
| 81 | blend31_old |
| 82 | }; |
| 83 | |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 84 | static void test_blend31() { |
| 85 | int failed = 0; |
| 86 | int death = 0; |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 87 | if (false) { // avoid bit rot, suppress warning |
| 88 | failed = (*blend_functions[0])(0,0,0); |
| 89 | } |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 90 | for (int src = 0; src <= 255; src++) { |
| 91 | for (int dst = 0; dst <= 255; dst++) { |
| 92 | for (int a = 0; a <= 31; a++) { |
| 93 | // int r0 = blend31(src, dst, a); |
| 94 | // int r0 = blend31_round(src, dst, a); |
| 95 | // int r0 = blend31_old(src, dst, a); |
| 96 | int r0 = blend31_slow(src, dst, a); |
| 97 | |
| 98 | float f = float_blend(src, dst, a / 31.f); |
| 99 | int r1 = (int)f; |
| 100 | int r2 = SkScalarRoundToInt(SkFloatToScalar(f)); |
| 101 | |
| 102 | if (r0 != r1 && r0 != r2) { |
| 103 | printf("src:%d dst:%d a:%d result:%d float:%g\n", |
| 104 | src, dst, a, r0, f); |
| 105 | failed += 1; |
| 106 | } |
| 107 | if (r0 > 255) { |
| 108 | death += 1; |
| 109 | printf("death src:%d dst:%d a:%d result:%d float:%g\n", |
| 110 | src, dst, a, r0, f); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | SkDebugf("---- failed %d death %d\n", failed, death); |
| 116 | } |
| 117 | |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 118 | static void test_blend(skiatest::Reporter* reporter) { |
| 119 | for (int src = 0; src <= 255; src++) { |
| 120 | for (int dst = 0; dst <= 255; dst++) { |
| 121 | for (int a = 0; a <= 255; a++) { |
| 122 | int r0 = SkAlphaBlend255(src, dst, a); |
| 123 | float f1 = float_blend(src, dst, a / 255.f); |
reed@google.com | 111c19b | 2011-10-06 20:13:22 +0000 | [diff] [blame] | 124 | int r1 = SkScalarRoundToInt(SkFloatToScalar(f1)); |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 125 | |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 126 | if (r0 != r1) { |
| 127 | float diff = sk_float_abs(f1 - r1); |
| 128 | diff = sk_float_abs(diff - 0.5f); |
| 129 | if (diff > (1 / 255.f)) { |
| 130 | #ifdef SK_DEBUG |
| 131 | SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n", |
| 132 | src, dst, a, r0, f1); |
| 133 | #endif |
| 134 | REPORTER_ASSERT(reporter, false); |
| 135 | } |
| 136 | } |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 141 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 142 | #if defined(SkLONGLONG) |
| 143 | static int symmetric_fixmul(int a, int b) { |
| 144 | int sa = SkExtractSign(a); |
| 145 | int sb = SkExtractSign(b); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 146 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 147 | a = SkApplySign(a, sa); |
| 148 | b = SkApplySign(b, sb); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 149 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 150 | #if 1 |
| 151 | int c = (int)(((SkLONGLONG)a * b) >> 16); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 152 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 153 | return SkApplySign(c, sa ^ sb); |
| 154 | #else |
| 155 | SkLONGLONG ab = (SkLONGLONG)a * b; |
| 156 | if (sa ^ sb) { |
| 157 | ab = -ab; |
| 158 | } |
| 159 | return ab >> 16; |
| 160 | #endif |
| 161 | } |
| 162 | #endif |
| 163 | |
| 164 | static void check_length(skiatest::Reporter* reporter, |
| 165 | const SkPoint& p, SkScalar targetLen) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 166 | float x = SkScalarToFloat(p.fX); |
| 167 | float y = SkScalarToFloat(p.fY); |
| 168 | float len = sk_float_sqrt(x*x + y*y); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 169 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 170 | len /= SkScalarToFloat(targetLen); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 171 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 172 | REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 173 | } |
| 174 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 175 | static float nextFloat(SkRandom& rand) { |
| 176 | SkFloatIntUnion data; |
| 177 | data.fSignBitInt = rand.nextU(); |
| 178 | return data.fFloat; |
| 179 | } |
| 180 | |
| 181 | /* returns true if a == b as resulting from (int)x. Since it is undefined |
| 182 | what to do if the float exceeds 2^32-1, we check for that explicitly. |
| 183 | */ |
| 184 | static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) { |
| 185 | if (!(x == x)) { // NAN |
| 186 | return si == SK_MaxS32 || si == SK_MinS32; |
| 187 | } |
| 188 | // for out of range, C is undefined, but skia always should return NaN32 |
| 189 | if (x > SK_MaxS32) { |
| 190 | return si == SK_MaxS32; |
| 191 | } |
| 192 | if (x < -SK_MaxS32) { |
| 193 | return si == SK_MinS32; |
| 194 | } |
| 195 | return si == ni; |
| 196 | } |
| 197 | |
| 198 | static void assert_float_equal(skiatest::Reporter* reporter, const char op[], |
| 199 | float x, uint32_t ni, uint32_t si) { |
| 200 | if (!equal_float_native_skia(x, ni, si)) { |
| 201 | SkString desc; |
| 202 | desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si); |
| 203 | reporter->reportFailed(desc); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static void test_float_cast(skiatest::Reporter* reporter, float x) { |
| 208 | int ix = (int)x; |
| 209 | int iix = SkFloatToIntCast(x); |
| 210 | assert_float_equal(reporter, "cast", x, ix, iix); |
| 211 | } |
| 212 | |
| 213 | static void test_float_floor(skiatest::Reporter* reporter, float x) { |
| 214 | int ix = (int)floor(x); |
| 215 | int iix = SkFloatToIntFloor(x); |
| 216 | assert_float_equal(reporter, "floor", x, ix, iix); |
| 217 | } |
| 218 | |
| 219 | static void test_float_round(skiatest::Reporter* reporter, float x) { |
| 220 | double xx = x + 0.5; // need intermediate double to avoid temp loss |
| 221 | int ix = (int)floor(xx); |
| 222 | int iix = SkFloatToIntRound(x); |
| 223 | assert_float_equal(reporter, "round", x, ix, iix); |
| 224 | } |
| 225 | |
| 226 | static void test_float_ceil(skiatest::Reporter* reporter, float x) { |
| 227 | int ix = (int)ceil(x); |
| 228 | int iix = SkFloatToIntCeil(x); |
| 229 | assert_float_equal(reporter, "ceil", x, ix, iix); |
| 230 | } |
| 231 | |
| 232 | static void test_float_conversions(skiatest::Reporter* reporter, float x) { |
| 233 | test_float_cast(reporter, x); |
| 234 | test_float_floor(reporter, x); |
| 235 | test_float_round(reporter, x); |
| 236 | test_float_ceil(reporter, x); |
| 237 | } |
| 238 | |
| 239 | static void test_int2float(skiatest::Reporter* reporter, int ival) { |
| 240 | float x0 = (float)ival; |
| 241 | float x1 = SkIntToFloatCast(ival); |
| 242 | float x2 = SkIntToFloatCast_NoOverflowCheck(ival); |
| 243 | REPORTER_ASSERT(reporter, x0 == x1); |
| 244 | REPORTER_ASSERT(reporter, x0 == x2); |
| 245 | } |
| 246 | |
| 247 | static void unittest_fastfloat(skiatest::Reporter* reporter) { |
| 248 | SkRandom rand; |
| 249 | size_t i; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 250 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 251 | static const float gFloats[] = { |
| 252 | 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3, |
| 253 | 0.000000001f, 1000000000.f, // doesn't overflow |
| 254 | 0.0000000001f, 10000000000.f // does overflow |
| 255 | }; |
| 256 | for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 257 | test_float_conversions(reporter, gFloats[i]); |
| 258 | test_float_conversions(reporter, -gFloats[i]); |
| 259 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 260 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 261 | for (int outer = 0; outer < 100; outer++) { |
| 262 | rand.setSeed(outer); |
| 263 | for (i = 0; i < 100000; i++) { |
| 264 | float x = nextFloat(rand); |
| 265 | test_float_conversions(reporter, x); |
| 266 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 267 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 268 | test_int2float(reporter, 0); |
| 269 | test_int2float(reporter, 1); |
| 270 | test_int2float(reporter, -1); |
| 271 | for (i = 0; i < 100000; i++) { |
| 272 | // for now only test ints that are 24bits or less, since we don't |
| 273 | // round (down) large ints the same as IEEE... |
| 274 | int ival = rand.nextU() & 0xFFFFFF; |
| 275 | test_int2float(reporter, ival); |
| 276 | test_int2float(reporter, -ival); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 281 | #ifdef SK_SCALAR_IS_FLOAT |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 282 | static float make_zero() { |
| 283 | return sk_float_sin(0); |
| 284 | } |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 285 | #endif |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 286 | |
| 287 | static void unittest_isfinite(skiatest::Reporter* reporter) { |
| 288 | #ifdef SK_SCALAR_IS_FLOAT |
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)); |
| 297 | #else |
| 298 | SkFixed nan = SK_FixedNaN; |
| 299 | SkFixed big = SK_FixedMax; |
| 300 | #endif |
| 301 | |
| 302 | REPORTER_ASSERT(reporter, SkScalarIsNaN(nan)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 303 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(big)); |
| 304 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big)); |
| 305 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(0)); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 306 | |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 307 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 308 | REPORTER_ASSERT(reporter, SkScalarIsFinite(big)); |
| 309 | REPORTER_ASSERT(reporter, SkScalarIsFinite(-big)); |
| 310 | REPORTER_ASSERT(reporter, SkScalarIsFinite(0)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 311 | } |
| 312 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 313 | static void test_muldiv255(skiatest::Reporter* reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 314 | for (int a = 0; a <= 255; a++) { |
| 315 | for (int b = 0; b <= 255; b++) { |
| 316 | int ab = a * b; |
| 317 | float s = ab / 255.0f; |
| 318 | int round = (int)floorf(s + 0.5f); |
| 319 | int trunc = (int)floorf(s); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 320 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 321 | int iround = SkMulDiv255Round(a, b); |
| 322 | int itrunc = SkMulDiv255Trunc(a, b); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 323 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 324 | REPORTER_ASSERT(reporter, iround == round); |
| 325 | REPORTER_ASSERT(reporter, itrunc == trunc); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 326 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 327 | REPORTER_ASSERT(reporter, itrunc <= iround); |
| 328 | REPORTER_ASSERT(reporter, iround <= a); |
| 329 | REPORTER_ASSERT(reporter, iround <= b); |
| 330 | } |
| 331 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 332 | } |
| 333 | |
senorblanco@chromium.org | ec7a30c | 2010-12-07 21:07:56 +0000 | [diff] [blame] | 334 | static void test_muldiv255ceiling(skiatest::Reporter* reporter) { |
| 335 | for (int c = 0; c <= 255; c++) { |
| 336 | for (int a = 0; a <= 255; a++) { |
| 337 | int product = (c * a + 255); |
| 338 | int expected_ceiling = (product + (product >> 8)) >> 8; |
| 339 | int webkit_ceiling = (c * a + 254) / 255; |
| 340 | REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling); |
| 341 | int skia_ceiling = SkMulDiv255Ceiling(c, a); |
| 342 | REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 347 | static void test_copysign(skiatest::Reporter* reporter) { |
| 348 | static const int32_t gTriples[] = { |
| 349 | // x, y, expected result |
| 350 | 0, 0, 0, |
| 351 | 0, 1, 0, |
| 352 | 0, -1, 0, |
| 353 | 1, 0, 1, |
| 354 | 1, 1, 1, |
| 355 | 1, -1, -1, |
| 356 | -1, 0, 1, |
| 357 | -1, 1, 1, |
| 358 | -1, -1, -1, |
| 359 | }; |
| 360 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) { |
| 361 | REPORTER_ASSERT(reporter, |
| 362 | SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]); |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 363 | float x = (float)gTriples[i]; |
| 364 | float y = (float)gTriples[i+1]; |
| 365 | float expected = (float)gTriples[i+2]; |
| 366 | REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected); |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | SkRandom rand; |
| 370 | for (int j = 0; j < 1000; j++) { |
| 371 | int ix = rand.nextS(); |
| 372 | REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix); |
| 373 | REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix); |
| 374 | REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix); |
| 375 | REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix); |
| 376 | |
| 377 | SkScalar sx = rand.nextSScalar1(); |
| 378 | REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx); |
| 379 | REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx); |
| 380 | REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx); |
| 381 | REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx); |
| 382 | } |
| 383 | } |
| 384 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 385 | static void TestMath(skiatest::Reporter* reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 386 | int i; |
| 387 | int32_t x; |
| 388 | SkRandom rand; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 389 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 390 | // these should assert |
| 391 | #if 0 |
| 392 | SkToS8(128); |
| 393 | SkToS8(-129); |
| 394 | SkToU8(256); |
| 395 | SkToU8(-5); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 396 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 397 | SkToS16(32768); |
| 398 | SkToS16(-32769); |
| 399 | SkToU16(65536); |
| 400 | SkToU16(-5); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 401 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 402 | if (sizeof(size_t) > 4) { |
| 403 | SkToS32(4*1024*1024); |
| 404 | SkToS32(-4*1024*1024); |
| 405 | SkToU32(5*1024*1024); |
| 406 | SkToU32(-5); |
| 407 | } |
| 408 | #endif |
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 | test_muldiv255(reporter); |
senorblanco@chromium.org | ec7a30c | 2010-12-07 21:07:56 +0000 | [diff] [blame] | 411 | test_muldiv255ceiling(reporter); |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 412 | test_copysign(reporter); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 413 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 414 | { |
| 415 | SkScalar x = SK_ScalarNaN; |
| 416 | REPORTER_ASSERT(reporter, SkScalarIsNaN(x)); |
| 417 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 418 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 419 | for (i = 1; i <= 10; i++) { |
| 420 | x = SkCubeRootBits(i*i*i, 11); |
| 421 | REPORTER_ASSERT(reporter, x == i); |
| 422 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 423 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 424 | x = SkFixedSqrt(SK_Fixed1); |
| 425 | REPORTER_ASSERT(reporter, x == SK_Fixed1); |
| 426 | x = SkFixedSqrt(SK_Fixed1/4); |
| 427 | REPORTER_ASSERT(reporter, x == SK_Fixed1/2); |
| 428 | x = SkFixedSqrt(SK_Fixed1*4); |
| 429 | REPORTER_ASSERT(reporter, x == SK_Fixed1*2); |
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 | x = SkFractSqrt(SK_Fract1); |
| 432 | REPORTER_ASSERT(reporter, x == SK_Fract1); |
| 433 | x = SkFractSqrt(SK_Fract1/4); |
| 434 | REPORTER_ASSERT(reporter, x == SK_Fract1/2); |
| 435 | x = SkFractSqrt(SK_Fract1/16); |
| 436 | REPORTER_ASSERT(reporter, x == SK_Fract1/4); |
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 | for (i = 1; i < 100; i++) { |
| 439 | x = SkFixedSqrt(SK_Fixed1 * i * i); |
| 440 | REPORTER_ASSERT(reporter, x == SK_Fixed1 * i); |
| 441 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 442 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 443 | for (i = 0; i < 1000; i++) { |
| 444 | int value = rand.nextS16(); |
| 445 | int max = rand.nextU16(); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 446 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 447 | int clamp = SkClampMax(value, max); |
| 448 | int clamp2 = value < 0 ? 0 : (value > max ? max : value); |
| 449 | REPORTER_ASSERT(reporter, clamp == clamp2); |
| 450 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 451 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 452 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 453 | SkPoint p; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 454 | |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 455 | // These random values are being treated as 32-bit-patterns, not as |
| 456 | // ints; calling SkIntToScalar() here produces crashes. |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 457 | p.setLength((SkScalar) rand.nextS(), |
| 458 | (SkScalar) rand.nextS(), |
| 459 | SK_Scalar1); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 460 | check_length(reporter, p, SK_Scalar1); |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 461 | p.setLength((SkScalar) (rand.nextS() >> 13), |
| 462 | (SkScalar) (rand.nextS() >> 13), |
| 463 | SK_Scalar1); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 464 | check_length(reporter, p, SK_Scalar1); |
| 465 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 466 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 467 | { |
| 468 | SkFixed result = SkFixedDiv(100, 100); |
| 469 | REPORTER_ASSERT(reporter, result == SK_Fixed1); |
| 470 | result = SkFixedDiv(1, SK_Fixed1); |
| 471 | REPORTER_ASSERT(reporter, result == 1); |
| 472 | } |
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 | unittest_fastfloat(reporter); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 475 | unittest_isfinite(reporter); |
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 | #ifdef SkLONGLONG |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 478 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 479 | SkFixed numer = rand.nextS(); |
| 480 | SkFixed denom = rand.nextS(); |
| 481 | SkFixed result = SkFixedDiv(numer, denom); |
| 482 | SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 483 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 484 | (void)SkCLZ(numer); |
| 485 | (void)SkCLZ(denom); |
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 | REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); |
| 488 | if (check > SK_MaxS32) { |
| 489 | check = SK_MaxS32; |
| 490 | } else if (check < -SK_MaxS32) { |
| 491 | check = SK_MinS32; |
| 492 | } |
| 493 | REPORTER_ASSERT(reporter, result == (int32_t)check); |
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 | result = SkFractDiv(numer, denom); |
| 496 | check = ((SkLONGLONG)numer << 30) / denom; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 497 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 498 | REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); |
| 499 | if (check > SK_MaxS32) { |
| 500 | check = SK_MaxS32; |
| 501 | } else if (check < -SK_MaxS32) { |
| 502 | check = SK_MinS32; |
| 503 | } |
| 504 | REPORTER_ASSERT(reporter, result == (int32_t)check); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 505 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 506 | // make them <= 2^24, so we don't overflow in fixmul |
| 507 | numer = numer << 8 >> 8; |
| 508 | denom = denom << 8 >> 8; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 509 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 510 | result = SkFixedMul(numer, denom); |
| 511 | SkFixed r2 = symmetric_fixmul(numer, denom); |
| 512 | // SkASSERT(result == r2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 513 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 514 | result = SkFixedMul(numer, numer); |
| 515 | r2 = SkFixedSquare(numer); |
| 516 | REPORTER_ASSERT(reporter, result == r2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 517 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 518 | if (numer >= 0 && denom >= 0) { |
| 519 | SkFixed mean = SkFixedMean(numer, denom); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 520 | float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom); |
| 521 | float fm = sk_float_sqrt(sk_float_abs(prod)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 522 | SkFixed mean2 = SkFloatToFixed(fm); |
| 523 | int diff = SkAbs32(mean - mean2); |
| 524 | REPORTER_ASSERT(reporter, diff <= 1); |
| 525 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 526 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 527 | { |
| 528 | SkFixed mod = SkFixedMod(numer, denom); |
| 529 | float n = SkFixedToFloat(numer); |
| 530 | float d = SkFixedToFloat(denom); |
| 531 | float m = sk_float_mod(n, d); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 532 | // ensure the same sign |
| 533 | REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 534 | int diff = SkAbs32(mod - SkFloatToFixed(m)); |
| 535 | REPORTER_ASSERT(reporter, (diff >> 7) == 0); |
| 536 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 537 | } |
| 538 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 539 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 540 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 541 | SkFract x = rand.nextU() >> 1; |
| 542 | double xx = (double)x / SK_Fract1; |
| 543 | SkFract xr = SkFractSqrt(x); |
| 544 | SkFract check = SkFloatToFract(sqrt(xx)); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 545 | REPORTER_ASSERT(reporter, xr == check || |
| 546 | xr == check-1 || |
| 547 | xr == check+1); |
| 548 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 549 | xr = SkFixedSqrt(x); |
| 550 | xx = (double)x / SK_Fixed1; |
| 551 | check = SkFloatToFixed(sqrt(xx)); |
| 552 | REPORTER_ASSERT(reporter, xr == check || xr == check-1); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 553 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 554 | xr = SkSqrt32(x); |
| 555 | xx = (double)x; |
| 556 | check = (int32_t)sqrt(xx); |
| 557 | REPORTER_ASSERT(reporter, xr == check || xr == check-1); |
| 558 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 559 | |
reed@google.com | 7886ad3 | 2012-06-11 21:21:26 +0000 | [diff] [blame] | 560 | #if !defined(SK_SCALAR_IS_FLOAT) |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 561 | { |
| 562 | SkFixed s, c; |
| 563 | s = SkFixedSinCos(0, &c); |
| 564 | REPORTER_ASSERT(reporter, s == 0); |
| 565 | REPORTER_ASSERT(reporter, c == SK_Fixed1); |
| 566 | } |
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 | int maxDiff = 0; |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 569 | for (i = 0; i < 1000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 570 | SkFixed rads = rand.nextS() >> 10; |
| 571 | double frads = SkFixedToFloat(rads); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 572 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 573 | SkFixed s, c; |
| 574 | s = SkScalarSinCos(rads, &c); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 575 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 576 | double fs = sin(frads); |
| 577 | double fc = cos(frads); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 578 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 579 | SkFixed is = SkFloatToFixed(fs); |
| 580 | SkFixed ic = SkFloatToFixed(fc); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 581 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 582 | maxDiff = SkMax32(maxDiff, SkAbs32(is - s)); |
| 583 | maxDiff = SkMax32(maxDiff, SkAbs32(ic - c)); |
| 584 | } |
| 585 | SkDebugf("SinCos: maximum error = %d\n", maxDiff); |
| 586 | #endif |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 587 | |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 588 | #ifdef SK_SCALAR_IS_FLOAT |
| 589 | test_blend(reporter); |
| 590 | #endif |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 591 | |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 592 | test_floor(reporter); |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 593 | |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 594 | // disable for now |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 595 | if (false) test_blend31(); // avoid bit rot, suppress warning |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 596 | } |
| 597 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 598 | #include "TestClassDef.h" |
| 599 | DEFINE_TESTCLASS("Math", MathTestClass, TestMath) |