Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: tests/MathTest.cpp

Issue 760753003: Add float-to-half (binary16) conversion functions. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix unsigned issue on Ubuntu. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkHalf.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "SkFloatingPoint.h" 11 #include "SkFloatingPoint.h"
12 #include "SkHalf.h"
12 #include "SkMathPriv.h" 13 #include "SkMathPriv.h"
13 #include "SkPoint.h" 14 #include "SkPoint.h"
14 #include "SkRandom.h" 15 #include "SkRandom.h"
15 #include "Test.h" 16 #include "Test.h"
16 17
17 static void test_clz(skiatest::Reporter* reporter) { 18 static void test_clz(skiatest::Reporter* reporter) {
18 REPORTER_ASSERT(reporter, 32 == SkCLZ(0)); 19 REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
19 REPORTER_ASSERT(reporter, 31 == SkCLZ(1)); 20 REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
20 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30)); 21 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
21 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U)); 22 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big)); 320 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
320 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big)); 321 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
321 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0)); 322 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
322 323
323 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan)); 324 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
324 REPORTER_ASSERT(reporter, SkScalarIsFinite(big)); 325 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
325 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big)); 326 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
326 REPORTER_ASSERT(reporter, SkScalarIsFinite(0)); 327 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
327 } 328 }
328 329
330 static void unittest_half(skiatest::Reporter* reporter) {
331 static const float gFloats[] = {
332 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
333 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
334 };
335
336 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
337 SkHalf h = SkFloatToHalf(gFloats[i]);
338 float f = SkHalfToFloat(h);
339 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
340 }
341
342 // check some special values
343 union FloatUnion {
344 uint32_t fU;
345 float fF;
346 };
347
348 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
349 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
350 float f = SkHalfToFloat(h);
351 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
352
353 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
354 h = SkFloatToHalf(largestNegativeHalf.fF);
355 f = SkHalfToFloat(h);
356 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
357
358 static const FloatUnion smallestPositiveHalf = { 102 << 23 };
359 h = SkFloatToHalf(smallestPositiveHalf.fF);
360 f = SkHalfToFloat(h);
361 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
362
363 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
364 h = SkFloatToHalf(overflowHalf.fF);
365 f = SkHalfToFloat(h);
366 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
367
368 static const FloatUnion underflowHalf = { 101 << 23 };
369 h = SkFloatToHalf(underflowHalf.fF);
370 f = SkHalfToFloat(h);
371 REPORTER_ASSERT(reporter, f == 0.0f );
372
373 static const FloatUnion inf32 = { 255 << 23 };
374 h = SkFloatToHalf(inf32.fF);
375 f = SkHalfToFloat(h);
376 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
377
378 static const FloatUnion nan32 = { 255 << 23 | 1 };
379 h = SkFloatToHalf(nan32.fF);
380 f = SkHalfToFloat(h);
381 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
382
383 }
384
329 static void test_muldiv255(skiatest::Reporter* reporter) { 385 static void test_muldiv255(skiatest::Reporter* reporter) {
330 for (int a = 0; a <= 255; a++) { 386 for (int a = 0; a <= 255; a++) {
331 for (int b = 0; b <= 255; b++) { 387 for (int b = 0; b <= 255; b++) {
332 int ab = a * b; 388 int ab = a * b;
333 float s = ab / 255.0f; 389 float s = ab / 255.0f;
334 int round = (int)floorf(s + 0.5f); 390 int round = (int)floorf(s + 0.5f);
335 int trunc = (int)floorf(s); 391 int trunc = (int)floorf(s);
336 392
337 int iround = SkMulDiv255Round(a, b); 393 int iround = SkMulDiv255Round(a, b);
338 int itrunc = SkMulDiv255Trunc(a, b); 394 int itrunc = SkMulDiv255Trunc(a, b);
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 513
458 { 514 {
459 SkFixed result = SkFixedDiv(100, 100); 515 SkFixed result = SkFixedDiv(100, 100);
460 REPORTER_ASSERT(reporter, result == SK_Fixed1); 516 REPORTER_ASSERT(reporter, result == SK_Fixed1);
461 result = SkFixedDiv(1, SK_Fixed1); 517 result = SkFixedDiv(1, SK_Fixed1);
462 REPORTER_ASSERT(reporter, result == 1); 518 REPORTER_ASSERT(reporter, result == 1);
463 } 519 }
464 520
465 unittest_fastfloat(reporter); 521 unittest_fastfloat(reporter);
466 unittest_isfinite(reporter); 522 unittest_isfinite(reporter);
523 unittest_half(reporter);
467 524
468 for (i = 0; i < 10000; i++) { 525 for (i = 0; i < 10000; i++) {
469 SkFixed numer = rand.nextS(); 526 SkFixed numer = rand.nextS();
470 SkFixed denom = rand.nextS(); 527 SkFixed denom = rand.nextS();
471 SkFixed result = SkFixedDiv(numer, denom); 528 SkFixed result = SkFixedDiv(numer, denom);
472 int64_t check = ((int64_t)numer << 16) / denom; 529 int64_t check = ((int64_t)numer << 16) / denom;
473 530
474 (void)SkCLZ(numer); 531 (void)SkCLZ(numer);
475 (void)SkCLZ(denom); 532 (void)SkCLZ(denom);
476 533
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 test_divmod<int16_t>(r); 651 test_divmod<int16_t>(r);
595 } 652 }
596 653
597 DEF_TEST(divmod_s32, r) { 654 DEF_TEST(divmod_s32, r) {
598 test_divmod<int32_t>(r); 655 test_divmod<int32_t>(r);
599 } 656 }
600 657
601 DEF_TEST(divmod_s64, r) { 658 DEF_TEST(divmod_s64, r) {
602 test_divmod<int64_t>(r); 659 test_divmod<int64_t>(r);
603 } 660 }
OLDNEW
« no previous file with comments | « src/core/SkHalf.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698