| 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 template <typename RSqrtFn> | 385 static void test_rsqrt(skiatest::Reporter* reporter) { |
| 386 static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) { | |
| 387 const float maxRelativeError = 6.50196699e-4f; | 386 const float maxRelativeError = 6.50196699e-4f; |
| 388 | 387 |
| 389 // test close to 0 up to 1 | 388 // test close to 0 up to 1 |
| 390 float input = 0.000001f; | 389 float input = 0.000001f; |
| 391 for (int i = 0; i < 1000; ++i) { | 390 for (int i = 0; i < 1000; ++i) { |
| 392 float exact = 1.0f/sk_float_sqrt(input); | 391 float exact = 1.0f/sk_float_sqrt(input); |
| 393 float estimate = rsqrt(input); | 392 float estimate = sk_float_rsqrt(input); |
| 394 float relativeError = sk_float_abs(exact - estimate)/exact; | 393 float relativeError = sk_float_abs(exact - estimate)/exact; |
| 395 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); | 394 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); |
| 396 input += 0.001f; | 395 input += 0.001f; |
| 397 } | 396 } |
| 398 | 397 |
| 399 // test 1 to ~100 | 398 // test 1 to ~100 |
| 400 input = 1.0f; | 399 input = 1.0f; |
| 401 for (int i = 0; i < 1000; ++i) { | 400 for (int i = 0; i < 1000; ++i) { |
| 402 float exact = 1.0f/sk_float_sqrt(input); | 401 float exact = 1.0f/sk_float_sqrt(input); |
| 403 float estimate = rsqrt(input); | 402 float estimate = sk_float_rsqrt(input); |
| 404 float relativeError = sk_float_abs(exact - estimate)/exact; | 403 float relativeError = sk_float_abs(exact - estimate)/exact; |
| 405 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); | 404 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); |
| 406 input += 0.01f; | 405 input += 0.01f; |
| 407 } | 406 } |
| 408 | 407 |
| 409 // test some big numbers | 408 // test some big numbers |
| 410 input = 1000000.0f; | 409 input = 1000000.0f; |
| 411 for (int i = 0; i < 100; ++i) { | 410 for (int i = 0; i < 100; ++i) { |
| 412 float exact = 1.0f/sk_float_sqrt(input); | 411 float exact = 1.0f/sk_float_sqrt(input); |
| 413 float estimate = rsqrt(input); | 412 float estimate = sk_float_rsqrt(input); |
| 414 float relativeError = sk_float_abs(exact - estimate)/exact; | 413 float relativeError = sk_float_abs(exact - estimate)/exact; |
| 415 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); | 414 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError); |
| 416 input += 754326.f; | 415 input += 754326.f; |
| 417 } | 416 } |
| 418 } | 417 } |
| 419 | 418 |
| 420 static void test_muldiv255(skiatest::Reporter* reporter) { | 419 static void test_muldiv255(skiatest::Reporter* reporter) { |
| 421 for (int a = 0; a <= 255; a++) { | 420 for (int a = 0; a <= 255; a++) { |
| 422 for (int b = 0; b <= 255; b++) { | 421 for (int b = 0; b <= 255; b++) { |
| 423 int ab = a * b; | 422 int ab = a * b; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 { | 548 { |
| 550 SkFixed result = SkFixedDiv(100, 100); | 549 SkFixed result = SkFixedDiv(100, 100); |
| 551 REPORTER_ASSERT(reporter, result == SK_Fixed1); | 550 REPORTER_ASSERT(reporter, result == SK_Fixed1); |
| 552 result = SkFixedDiv(1, SK_Fixed1); | 551 result = SkFixedDiv(1, SK_Fixed1); |
| 553 REPORTER_ASSERT(reporter, result == 1); | 552 REPORTER_ASSERT(reporter, result == 1); |
| 554 } | 553 } |
| 555 | 554 |
| 556 unittest_fastfloat(reporter); | 555 unittest_fastfloat(reporter); |
| 557 unittest_isfinite(reporter); | 556 unittest_isfinite(reporter); |
| 558 unittest_half(reporter); | 557 unittest_half(reporter); |
| 559 test_rsqrt(reporter, sk_float_rsqrt); | 558 test_rsqrt(reporter); |
| 560 test_rsqrt(reporter, sk_float_rsqrt_portable); | |
| 561 | 559 |
| 562 for (i = 0; i < 10000; i++) { | 560 for (i = 0; i < 10000; i++) { |
| 563 SkFixed numer = rand.nextS(); | 561 SkFixed numer = rand.nextS(); |
| 564 SkFixed denom = rand.nextS(); | 562 SkFixed denom = rand.nextS(); |
| 565 SkFixed result = SkFixedDiv(numer, denom); | 563 SkFixed result = SkFixedDiv(numer, denom); |
| 566 int64_t check = SkLeftShift((int64_t)numer, 16) / denom; | 564 int64_t check = SkLeftShift((int64_t)numer, 16) / denom; |
| 567 | 565 |
| 568 (void)SkCLZ(numer); | 566 (void)SkCLZ(numer); |
| 569 (void)SkCLZ(denom); | 567 (void)SkCLZ(denom); |
| 570 | 568 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 test_divmod<int16_t>(r); | 689 test_divmod<int16_t>(r); |
| 692 } | 690 } |
| 693 | 691 |
| 694 DEF_TEST(divmod_s32, r) { | 692 DEF_TEST(divmod_s32, r) { |
| 695 test_divmod<int32_t>(r); | 693 test_divmod<int32_t>(r); |
| 696 } | 694 } |
| 697 | 695 |
| 698 DEF_TEST(divmod_s64, r) { | 696 DEF_TEST(divmod_s64, r) { |
| 699 test_divmod<int64_t>(r); | 697 test_divmod<int64_t>(r); |
| 700 } | 698 } |
| OLD | NEW |