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

Unified 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, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkHalf.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/MathTest.cpp
diff --git a/tests/MathTest.cpp b/tests/MathTest.cpp
index 20539369b81c58bd56fa5bad344a85d833231f31..4347096ea54b62cdba8bc47a6d35fb1a7f8a1249 100644
--- a/tests/MathTest.cpp
+++ b/tests/MathTest.cpp
@@ -9,6 +9,7 @@
#include "SkEndian.h"
#include "SkFloatBits.h"
#include "SkFloatingPoint.h"
+#include "SkHalf.h"
#include "SkMathPriv.h"
#include "SkPoint.h"
#include "SkRandom.h"
@@ -326,6 +327,61 @@ static void unittest_isfinite(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
}
+static void unittest_half(skiatest::Reporter* reporter) {
+ static const float gFloats[] = {
+ 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
+ -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
+ };
+
+ for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
+ SkHalf h = SkFloatToHalf(gFloats[i]);
+ float f = SkHalfToFloat(h);
+ REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
+ }
+
+ // check some special values
+ union FloatUnion {
+ uint32_t fU;
+ float fF;
+ };
+
+ static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
+ SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
+ float f = SkHalfToFloat(h);
+ REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
+
+ static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
+ h = SkFloatToHalf(largestNegativeHalf.fF);
+ f = SkHalfToFloat(h);
+ REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
+
+ static const FloatUnion smallestPositiveHalf = { 102 << 23 };
+ h = SkFloatToHalf(smallestPositiveHalf.fF);
+ f = SkHalfToFloat(h);
+ REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
+
+ static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
+ h = SkFloatToHalf(overflowHalf.fF);
+ f = SkHalfToFloat(h);
+ REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
+
+ static const FloatUnion underflowHalf = { 101 << 23 };
+ h = SkFloatToHalf(underflowHalf.fF);
+ f = SkHalfToFloat(h);
+ REPORTER_ASSERT(reporter, f == 0.0f );
+
+ static const FloatUnion inf32 = { 255 << 23 };
+ h = SkFloatToHalf(inf32.fF);
+ f = SkHalfToFloat(h);
+ REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
+
+ static const FloatUnion nan32 = { 255 << 23 | 1 };
+ h = SkFloatToHalf(nan32.fF);
+ f = SkHalfToFloat(h);
+ REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
+
+}
+
static void test_muldiv255(skiatest::Reporter* reporter) {
for (int a = 0; a <= 255; a++) {
for (int b = 0; b <= 255; b++) {
@@ -464,6 +520,7 @@ DEF_TEST(Math, reporter) {
unittest_fastfloat(reporter);
unittest_isfinite(reporter);
+ unittest_half(reporter);
for (i = 0; i < 10000; i++) {
SkFixed numer = rand.nextS();
« 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