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