OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkColorPriv.h" | 8 #include "SkColorPriv.h" |
9 #include "SkEndian.h" | 9 #include "SkEndian.h" |
10 #include "SkFloatBits.h" | 10 #include "SkFloatBits.h" |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 f = SkHalfToFloat(h); | 375 f = SkHalfToFloat(h); |
376 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) ); | 376 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) ); |
377 | 377 |
378 static const FloatUnion nan32 = { 255 << 23 | 1 }; | 378 static const FloatUnion nan32 = { 255 << 23 | 1 }; |
379 h = SkFloatToHalf(nan32.fF); | 379 h = SkFloatToHalf(nan32.fF); |
380 f = SkHalfToFloat(h); | 380 f = SkHalfToFloat(h); |
381 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) ); | 381 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) ); |
382 | 382 |
383 } | 383 } |
384 | 384 |
| 385 static void test_rsqrt(skiatest::Reporter* reporter) { |
| 386 const float maxRelativeError = 6.50196699e-4f; |
| 387 |
| 388 // test close to 0 up to 1 |
| 389 float input = 0.000001f; |
| 390 for (int i = 0; i < 1000; ++i) { |
| 391 float exact = 1.0f/sk_float_sqrt(input); |
| 392 float estimate = sk_float_rsqrt(input); |
| 393 float relativeError = sk_float_abs(exact - estimate)/exact; |
| 394 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); |
| 395 input += 0.001f; |
| 396 } |
| 397 |
| 398 // test 1 to ~100 |
| 399 input = 1.0f; |
| 400 for (int i = 0; i < 1000; ++i) { |
| 401 float exact = 1.0f/sk_float_sqrt(input); |
| 402 float estimate = sk_float_rsqrt(input); |
| 403 float relativeError = sk_float_abs(exact - estimate)/exact; |
| 404 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); |
| 405 input += 0.01f; |
| 406 } |
| 407 |
| 408 // test some big numbers |
| 409 input = 1000000.0f; |
| 410 for (int i = 0; i < 100; ++i) { |
| 411 float exact = 1.0f/sk_float_sqrt(input); |
| 412 float estimate = sk_float_rsqrt(input); |
| 413 float relativeError = sk_float_abs(exact - estimate)/exact; |
| 414 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); |
| 415 input += 754326.f; |
| 416 } |
| 417 } |
| 418 |
385 static void test_muldiv255(skiatest::Reporter* reporter) { | 419 static void test_muldiv255(skiatest::Reporter* reporter) { |
386 for (int a = 0; a <= 255; a++) { | 420 for (int a = 0; a <= 255; a++) { |
387 for (int b = 0; b <= 255; b++) { | 421 for (int b = 0; b <= 255; b++) { |
388 int ab = a * b; | 422 int ab = a * b; |
389 float s = ab / 255.0f; | 423 float s = ab / 255.0f; |
390 int round = (int)floorf(s + 0.5f); | 424 int round = (int)floorf(s + 0.5f); |
391 int trunc = (int)floorf(s); | 425 int trunc = (int)floorf(s); |
392 | 426 |
393 int iround = SkMulDiv255Round(a, b); | 427 int iround = SkMulDiv255Round(a, b); |
394 int itrunc = SkMulDiv255Trunc(a, b); | 428 int itrunc = SkMulDiv255Trunc(a, b); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 { | 548 { |
515 SkFixed result = SkFixedDiv(100, 100); | 549 SkFixed result = SkFixedDiv(100, 100); |
516 REPORTER_ASSERT(reporter, result == SK_Fixed1); | 550 REPORTER_ASSERT(reporter, result == SK_Fixed1); |
517 result = SkFixedDiv(1, SK_Fixed1); | 551 result = SkFixedDiv(1, SK_Fixed1); |
518 REPORTER_ASSERT(reporter, result == 1); | 552 REPORTER_ASSERT(reporter, result == 1); |
519 } | 553 } |
520 | 554 |
521 unittest_fastfloat(reporter); | 555 unittest_fastfloat(reporter); |
522 unittest_isfinite(reporter); | 556 unittest_isfinite(reporter); |
523 unittest_half(reporter); | 557 unittest_half(reporter); |
| 558 test_rsqrt(reporter); |
524 | 559 |
525 for (i = 0; i < 10000; i++) { | 560 for (i = 0; i < 10000; i++) { |
526 SkFixed numer = rand.nextS(); | 561 SkFixed numer = rand.nextS(); |
527 SkFixed denom = rand.nextS(); | 562 SkFixed denom = rand.nextS(); |
528 SkFixed result = SkFixedDiv(numer, denom); | 563 SkFixed result = SkFixedDiv(numer, denom); |
529 int64_t check = ((int64_t)numer << 16) / denom; | 564 int64_t check = ((int64_t)numer << 16) / denom; |
530 | 565 |
531 (void)SkCLZ(numer); | 566 (void)SkCLZ(numer); |
532 (void)SkCLZ(denom); | 567 (void)SkCLZ(denom); |
533 | 568 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 test_divmod<int16_t>(r); | 689 test_divmod<int16_t>(r); |
655 } | 690 } |
656 | 691 |
657 DEF_TEST(divmod_s32, r) { | 692 DEF_TEST(divmod_s32, r) { |
658 test_divmod<int32_t>(r); | 693 test_divmod<int32_t>(r); |
659 } | 694 } |
660 | 695 |
661 DEF_TEST(divmod_s64, r) { | 696 DEF_TEST(divmod_s64, r) { |
662 test_divmod<int64_t>(r); | 697 test_divmod<int64_t>(r); |
663 } | 698 } |
OLD | NEW |