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) { |
| 166 | #ifdef SK_CAN_USE_FLOAT |
| 167 | float x = SkScalarToFloat(p.fX); |
| 168 | float y = SkScalarToFloat(p.fY); |
| 169 | float len = sk_float_sqrt(x*x + y*y); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 170 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 171 | len /= SkScalarToFloat(targetLen); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 172 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 173 | REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f); |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | #if defined(SK_CAN_USE_FLOAT) |
| 178 | |
| 179 | static float nextFloat(SkRandom& rand) { |
| 180 | SkFloatIntUnion data; |
| 181 | data.fSignBitInt = rand.nextU(); |
| 182 | return data.fFloat; |
| 183 | } |
| 184 | |
| 185 | /* returns true if a == b as resulting from (int)x. Since it is undefined |
| 186 | what to do if the float exceeds 2^32-1, we check for that explicitly. |
| 187 | */ |
| 188 | static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) { |
| 189 | if (!(x == x)) { // NAN |
| 190 | return si == SK_MaxS32 || si == SK_MinS32; |
| 191 | } |
| 192 | // for out of range, C is undefined, but skia always should return NaN32 |
| 193 | if (x > SK_MaxS32) { |
| 194 | return si == SK_MaxS32; |
| 195 | } |
| 196 | if (x < -SK_MaxS32) { |
| 197 | return si == SK_MinS32; |
| 198 | } |
| 199 | return si == ni; |
| 200 | } |
| 201 | |
| 202 | static void assert_float_equal(skiatest::Reporter* reporter, const char op[], |
| 203 | float x, uint32_t ni, uint32_t si) { |
| 204 | if (!equal_float_native_skia(x, ni, si)) { |
| 205 | SkString desc; |
| 206 | desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si); |
| 207 | reporter->reportFailed(desc); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | static void test_float_cast(skiatest::Reporter* reporter, float x) { |
| 212 | int ix = (int)x; |
| 213 | int iix = SkFloatToIntCast(x); |
| 214 | assert_float_equal(reporter, "cast", x, ix, iix); |
| 215 | } |
| 216 | |
| 217 | static void test_float_floor(skiatest::Reporter* reporter, float x) { |
| 218 | int ix = (int)floor(x); |
| 219 | int iix = SkFloatToIntFloor(x); |
| 220 | assert_float_equal(reporter, "floor", x, ix, iix); |
| 221 | } |
| 222 | |
| 223 | static void test_float_round(skiatest::Reporter* reporter, float x) { |
| 224 | double xx = x + 0.5; // need intermediate double to avoid temp loss |
| 225 | int ix = (int)floor(xx); |
| 226 | int iix = SkFloatToIntRound(x); |
| 227 | assert_float_equal(reporter, "round", x, ix, iix); |
| 228 | } |
| 229 | |
| 230 | static void test_float_ceil(skiatest::Reporter* reporter, float x) { |
| 231 | int ix = (int)ceil(x); |
| 232 | int iix = SkFloatToIntCeil(x); |
| 233 | assert_float_equal(reporter, "ceil", x, ix, iix); |
| 234 | } |
| 235 | |
| 236 | static void test_float_conversions(skiatest::Reporter* reporter, float x) { |
| 237 | test_float_cast(reporter, x); |
| 238 | test_float_floor(reporter, x); |
| 239 | test_float_round(reporter, x); |
| 240 | test_float_ceil(reporter, x); |
| 241 | } |
| 242 | |
| 243 | static void test_int2float(skiatest::Reporter* reporter, int ival) { |
| 244 | float x0 = (float)ival; |
| 245 | float x1 = SkIntToFloatCast(ival); |
| 246 | float x2 = SkIntToFloatCast_NoOverflowCheck(ival); |
| 247 | REPORTER_ASSERT(reporter, x0 == x1); |
| 248 | REPORTER_ASSERT(reporter, x0 == x2); |
| 249 | } |
| 250 | |
| 251 | static void unittest_fastfloat(skiatest::Reporter* reporter) { |
| 252 | SkRandom rand; |
| 253 | size_t i; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 254 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 255 | static const float gFloats[] = { |
| 256 | 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3, |
| 257 | 0.000000001f, 1000000000.f, // doesn't overflow |
| 258 | 0.0000000001f, 10000000000.f // does overflow |
| 259 | }; |
| 260 | for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 261 | test_float_conversions(reporter, gFloats[i]); |
| 262 | test_float_conversions(reporter, -gFloats[i]); |
| 263 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 264 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 265 | for (int outer = 0; outer < 100; outer++) { |
| 266 | rand.setSeed(outer); |
| 267 | for (i = 0; i < 100000; i++) { |
| 268 | float x = nextFloat(rand); |
| 269 | test_float_conversions(reporter, x); |
| 270 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 271 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 272 | test_int2float(reporter, 0); |
| 273 | test_int2float(reporter, 1); |
| 274 | test_int2float(reporter, -1); |
| 275 | for (i = 0; i < 100000; i++) { |
| 276 | // for now only test ints that are 24bits or less, since we don't |
| 277 | // round (down) large ints the same as IEEE... |
| 278 | int ival = rand.nextU() & 0xFFFFFF; |
| 279 | test_int2float(reporter, ival); |
| 280 | test_int2float(reporter, -ival); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 285 | #ifdef SK_SCALAR_IS_FLOAT |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 286 | static float make_zero() { |
| 287 | return sk_float_sin(0); |
| 288 | } |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 289 | #endif |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 290 | |
| 291 | static void unittest_isfinite(skiatest::Reporter* reporter) { |
| 292 | #ifdef SK_SCALAR_IS_FLOAT |
epoger@google.com | bf083a9 | 2011-06-08 18:26:08 +0000 | [diff] [blame] | 293 | float nan = sk_float_asin(2); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 294 | float inf = 1.0f / make_zero(); |
| 295 | float big = 3.40282e+038f; |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 296 | |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 297 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf)); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 298 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf)); |
| 299 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf)); |
| 300 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf)); |
| 301 | #else |
| 302 | SkFixed nan = SK_FixedNaN; |
| 303 | SkFixed big = SK_FixedMax; |
| 304 | #endif |
| 305 | |
| 306 | REPORTER_ASSERT(reporter, SkScalarIsNaN(nan)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 307 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(big)); |
| 308 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big)); |
| 309 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(0)); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 310 | |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 311 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 312 | REPORTER_ASSERT(reporter, SkScalarIsFinite(big)); |
| 313 | REPORTER_ASSERT(reporter, SkScalarIsFinite(-big)); |
| 314 | REPORTER_ASSERT(reporter, SkScalarIsFinite(0)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 315 | } |
| 316 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 317 | #endif |
| 318 | |
| 319 | static void test_muldiv255(skiatest::Reporter* reporter) { |
| 320 | #ifdef SK_CAN_USE_FLOAT |
| 321 | for (int a = 0; a <= 255; a++) { |
| 322 | for (int b = 0; b <= 255; b++) { |
| 323 | int ab = a * b; |
| 324 | float s = ab / 255.0f; |
| 325 | int round = (int)floorf(s + 0.5f); |
| 326 | int trunc = (int)floorf(s); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 327 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 328 | int iround = SkMulDiv255Round(a, b); |
| 329 | int itrunc = SkMulDiv255Trunc(a, b); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 330 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 331 | REPORTER_ASSERT(reporter, iround == round); |
| 332 | REPORTER_ASSERT(reporter, itrunc == trunc); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 333 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 334 | REPORTER_ASSERT(reporter, itrunc <= iround); |
| 335 | REPORTER_ASSERT(reporter, iround <= a); |
| 336 | REPORTER_ASSERT(reporter, iround <= b); |
| 337 | } |
| 338 | } |
| 339 | #endif |
| 340 | } |
| 341 | |
senorblanco@chromium.org | ec7a30c | 2010-12-07 21:07:56 +0000 | [diff] [blame] | 342 | static void test_muldiv255ceiling(skiatest::Reporter* reporter) { |
| 343 | for (int c = 0; c <= 255; c++) { |
| 344 | for (int a = 0; a <= 255; a++) { |
| 345 | int product = (c * a + 255); |
| 346 | int expected_ceiling = (product + (product >> 8)) >> 8; |
| 347 | int webkit_ceiling = (c * a + 254) / 255; |
| 348 | REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling); |
| 349 | int skia_ceiling = SkMulDiv255Ceiling(c, a); |
| 350 | REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 355 | static void test_copysign(skiatest::Reporter* reporter) { |
| 356 | static const int32_t gTriples[] = { |
| 357 | // x, y, expected result |
| 358 | 0, 0, 0, |
| 359 | 0, 1, 0, |
| 360 | 0, -1, 0, |
| 361 | 1, 0, 1, |
| 362 | 1, 1, 1, |
| 363 | 1, -1, -1, |
| 364 | -1, 0, 1, |
| 365 | -1, 1, 1, |
| 366 | -1, -1, -1, |
| 367 | }; |
| 368 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) { |
| 369 | REPORTER_ASSERT(reporter, |
| 370 | SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]); |
| 371 | #ifdef SK_CAN_USE_FLOAT |
| 372 | float x = (float)gTriples[i]; |
| 373 | float y = (float)gTriples[i+1]; |
| 374 | float expected = (float)gTriples[i+2]; |
| 375 | REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected); |
| 376 | #endif |
| 377 | } |
| 378 | |
| 379 | SkRandom rand; |
| 380 | for (int j = 0; j < 1000; j++) { |
| 381 | int ix = rand.nextS(); |
| 382 | REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix); |
| 383 | REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix); |
| 384 | REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix); |
| 385 | REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix); |
| 386 | |
| 387 | SkScalar sx = rand.nextSScalar1(); |
| 388 | REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx); |
| 389 | REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx); |
| 390 | REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx); |
| 391 | REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx); |
| 392 | } |
| 393 | } |
| 394 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 395 | static void TestMath(skiatest::Reporter* reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 396 | int i; |
| 397 | int32_t x; |
| 398 | SkRandom rand; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 399 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 400 | // these should assert |
| 401 | #if 0 |
| 402 | SkToS8(128); |
| 403 | SkToS8(-129); |
| 404 | SkToU8(256); |
| 405 | SkToU8(-5); |
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 | SkToS16(32768); |
| 408 | SkToS16(-32769); |
| 409 | SkToU16(65536); |
| 410 | SkToU16(-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 | if (sizeof(size_t) > 4) { |
| 413 | SkToS32(4*1024*1024); |
| 414 | SkToS32(-4*1024*1024); |
| 415 | SkToU32(5*1024*1024); |
| 416 | SkToU32(-5); |
| 417 | } |
| 418 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 419 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 420 | test_muldiv255(reporter); |
senorblanco@chromium.org | ec7a30c | 2010-12-07 21:07:56 +0000 | [diff] [blame] | 421 | test_muldiv255ceiling(reporter); |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 422 | test_copysign(reporter); |
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 | { |
| 425 | SkScalar x = SK_ScalarNaN; |
| 426 | REPORTER_ASSERT(reporter, SkScalarIsNaN(x)); |
| 427 | } |
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 | for (i = 1; i <= 10; i++) { |
| 430 | x = SkCubeRootBits(i*i*i, 11); |
| 431 | REPORTER_ASSERT(reporter, x == i); |
| 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 | x = SkFixedSqrt(SK_Fixed1); |
| 435 | REPORTER_ASSERT(reporter, x == SK_Fixed1); |
| 436 | x = SkFixedSqrt(SK_Fixed1/4); |
| 437 | REPORTER_ASSERT(reporter, x == SK_Fixed1/2); |
| 438 | x = SkFixedSqrt(SK_Fixed1*4); |
| 439 | REPORTER_ASSERT(reporter, x == SK_Fixed1*2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 440 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 441 | x = SkFractSqrt(SK_Fract1); |
| 442 | REPORTER_ASSERT(reporter, x == SK_Fract1); |
| 443 | x = SkFractSqrt(SK_Fract1/4); |
| 444 | REPORTER_ASSERT(reporter, x == SK_Fract1/2); |
| 445 | x = SkFractSqrt(SK_Fract1/16); |
| 446 | REPORTER_ASSERT(reporter, x == SK_Fract1/4); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 447 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 448 | for (i = 1; i < 100; i++) { |
| 449 | x = SkFixedSqrt(SK_Fixed1 * i * i); |
| 450 | REPORTER_ASSERT(reporter, x == SK_Fixed1 * i); |
| 451 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 452 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 453 | for (i = 0; i < 1000; i++) { |
| 454 | int value = rand.nextS16(); |
| 455 | int max = rand.nextU16(); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 456 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 457 | int clamp = SkClampMax(value, max); |
| 458 | int clamp2 = value < 0 ? 0 : (value > max ? max : value); |
| 459 | REPORTER_ASSERT(reporter, clamp == clamp2); |
| 460 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 461 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 462 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 463 | SkPoint p; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 464 | |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 465 | // These random values are being treated as 32-bit-patterns, not as |
| 466 | // ints; calling SkIntToScalar() here produces crashes. |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 467 | p.setLength((SkScalar) rand.nextS(), |
| 468 | (SkScalar) rand.nextS(), |
| 469 | SK_Scalar1); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 470 | check_length(reporter, p, SK_Scalar1); |
robertphillips@google.com | 4debcac | 2012-05-14 16:33:36 +0000 | [diff] [blame] | 471 | p.setLength((SkScalar) (rand.nextS() >> 13), |
| 472 | (SkScalar) (rand.nextS() >> 13), |
| 473 | SK_Scalar1); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 474 | check_length(reporter, p, SK_Scalar1); |
| 475 | } |
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 | { |
| 478 | SkFixed result = SkFixedDiv(100, 100); |
| 479 | REPORTER_ASSERT(reporter, result == SK_Fixed1); |
| 480 | result = SkFixedDiv(1, SK_Fixed1); |
| 481 | REPORTER_ASSERT(reporter, result == 1); |
| 482 | } |
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 | #ifdef SK_CAN_USE_FLOAT |
| 485 | unittest_fastfloat(reporter); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 486 | unittest_isfinite(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 487 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 488 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 489 | #ifdef SkLONGLONG |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 490 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 491 | SkFixed numer = rand.nextS(); |
| 492 | SkFixed denom = rand.nextS(); |
| 493 | SkFixed result = SkFixedDiv(numer, denom); |
| 494 | SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 495 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 496 | (void)SkCLZ(numer); |
| 497 | (void)SkCLZ(denom); |
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 | REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); |
| 500 | if (check > SK_MaxS32) { |
| 501 | check = SK_MaxS32; |
| 502 | } else if (check < -SK_MaxS32) { |
| 503 | check = SK_MinS32; |
| 504 | } |
| 505 | REPORTER_ASSERT(reporter, result == (int32_t)check); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 506 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 507 | result = SkFractDiv(numer, denom); |
| 508 | check = ((SkLONGLONG)numer << 30) / denom; |
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 | REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); |
| 511 | if (check > SK_MaxS32) { |
| 512 | check = SK_MaxS32; |
| 513 | } else if (check < -SK_MaxS32) { |
| 514 | check = SK_MinS32; |
| 515 | } |
| 516 | REPORTER_ASSERT(reporter, result == (int32_t)check); |
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 | // make them <= 2^24, so we don't overflow in fixmul |
| 519 | numer = numer << 8 >> 8; |
| 520 | denom = denom << 8 >> 8; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 521 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 522 | result = SkFixedMul(numer, denom); |
| 523 | SkFixed r2 = symmetric_fixmul(numer, denom); |
| 524 | // SkASSERT(result == r2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 525 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 526 | result = SkFixedMul(numer, numer); |
| 527 | r2 = SkFixedSquare(numer); |
| 528 | REPORTER_ASSERT(reporter, result == r2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 529 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 530 | #ifdef SK_CAN_USE_FLOAT |
| 531 | if (numer >= 0 && denom >= 0) { |
| 532 | SkFixed mean = SkFixedMean(numer, denom); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 533 | float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom); |
| 534 | float fm = sk_float_sqrt(sk_float_abs(prod)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 535 | SkFixed mean2 = SkFloatToFixed(fm); |
| 536 | int diff = SkAbs32(mean - mean2); |
| 537 | REPORTER_ASSERT(reporter, diff <= 1); |
| 538 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 539 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 540 | { |
| 541 | SkFixed mod = SkFixedMod(numer, denom); |
| 542 | float n = SkFixedToFloat(numer); |
| 543 | float d = SkFixedToFloat(denom); |
| 544 | float m = sk_float_mod(n, d); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 545 | // ensure the same sign |
| 546 | REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 547 | int diff = SkAbs32(mod - SkFloatToFixed(m)); |
| 548 | REPORTER_ASSERT(reporter, (diff >> 7) == 0); |
| 549 | } |
| 550 | #endif |
| 551 | } |
| 552 | #endif |
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 | #ifdef SK_CAN_USE_FLOAT |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 555 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 556 | SkFract x = rand.nextU() >> 1; |
| 557 | double xx = (double)x / SK_Fract1; |
| 558 | SkFract xr = SkFractSqrt(x); |
| 559 | SkFract check = SkFloatToFract(sqrt(xx)); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 560 | REPORTER_ASSERT(reporter, xr == check || |
| 561 | xr == check-1 || |
| 562 | xr == check+1); |
| 563 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 564 | xr = SkFixedSqrt(x); |
| 565 | xx = (double)x / SK_Fixed1; |
| 566 | check = SkFloatToFixed(sqrt(xx)); |
| 567 | REPORTER_ASSERT(reporter, xr == check || xr == check-1); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 568 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 569 | xr = SkSqrt32(x); |
| 570 | xx = (double)x; |
| 571 | check = (int32_t)sqrt(xx); |
| 572 | REPORTER_ASSERT(reporter, xr == check || xr == check-1); |
| 573 | } |
| 574 | #endif |
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 | #if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT) |
| 577 | { |
| 578 | SkFixed s, c; |
| 579 | s = SkFixedSinCos(0, &c); |
| 580 | REPORTER_ASSERT(reporter, s == 0); |
| 581 | REPORTER_ASSERT(reporter, c == SK_Fixed1); |
| 582 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 583 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 584 | int maxDiff = 0; |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 585 | for (i = 0; i < 1000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 586 | SkFixed rads = rand.nextS() >> 10; |
| 587 | double frads = SkFixedToFloat(rads); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 588 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 589 | SkFixed s, c; |
| 590 | s = SkScalarSinCos(rads, &c); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 591 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 592 | double fs = sin(frads); |
| 593 | double fc = cos(frads); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 594 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 595 | SkFixed is = SkFloatToFixed(fs); |
| 596 | SkFixed ic = SkFloatToFixed(fc); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 597 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 598 | maxDiff = SkMax32(maxDiff, SkAbs32(is - s)); |
| 599 | maxDiff = SkMax32(maxDiff, SkAbs32(ic - c)); |
| 600 | } |
| 601 | SkDebugf("SinCos: maximum error = %d\n", maxDiff); |
| 602 | #endif |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 603 | |
reed@google.com | 0abb499 | 2011-10-06 20:04:36 +0000 | [diff] [blame] | 604 | #ifdef SK_SCALAR_IS_FLOAT |
| 605 | test_blend(reporter); |
| 606 | #endif |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 607 | |
reed@google.com | a7d7461 | 2012-05-30 12:30:09 +0000 | [diff] [blame] | 608 | #ifdef SK_CAN_USE_FLOAT |
| 609 | test_floor(reporter); |
| 610 | #endif |
| 611 | |
reed@google.com | 8d7e39c | 2011-11-09 17:12:08 +0000 | [diff] [blame] | 612 | // disable for now |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame^] | 613 | if (false) test_blend31(); // avoid bit rot, suppress warning |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 614 | } |
| 615 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 616 | #include "TestClassDef.h" |
| 617 | DEFINE_TESTCLASS("Math", MathTestClass, TestMath) |