OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 "Test.h" | 8 #include "Test.h" |
9 #include "SkAutoPixmapStorage.h" | 9 #include "SkAutoPixmapStorage.h" |
10 #include "SkColor.h" | 10 #include "SkColor.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 SkPM4f origpm4 = c4.premul(); | 49 SkPM4f origpm4 = c4.premul(); |
50 for (int y = 0; y < pm.height(); ++y) { | 50 for (int y = 0; y < pm.height(); ++y) { |
51 for (int x = 0; x < pm.width(); ++x) { | 51 for (int x = 0; x < pm.width(); ++x) { |
52 SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y)); | 52 SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y)); |
53 REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4)); | 53 REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4)); |
54 } | 54 } |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 static uint32_t u(float f) { | 58 static bool is_denorm(uint16_t h) { |
59 uint32_t x; | 59 return (h & 0x7fff) < 0x0400; |
60 memcpy(&x, &f, 4); | |
61 return x; | |
62 } | 60 } |
63 | 61 |
64 DEF_TEST(HalfToFloat_finite, r) { | 62 static bool is_finite(uint16_t h) { |
| 63 return (h & 0x7c00) != 0x7c00; |
| 64 } |
| 65 |
| 66 DEF_TEST(SkHalfToFloat_finite_ftz, r) { |
65 for (uint32_t h = 0; h <= 0xffff; h++) { | 67 for (uint32_t h = 0; h <= 0xffff; h++) { |
66 float f = SkHalfToFloat(h); | 68 if (!is_finite(h)) { |
67 if (isfinite(f)) { | 69 // _finite_ftz() only works for values that can be represented as a
finite half float. |
68 float got = SkHalfToFloat_finite(h)[0]; | 70 continue; |
69 if (got != f) { | |
70 SkDebugf("0x%04x -> 0x%08x (%g), want 0x%08x (%g)\n", | |
71 h, | |
72 u(got), got, | |
73 u(f), f); | |
74 } | |
75 REPORTER_ASSERT(r, SkHalfToFloat_finite(h)[0] == f); | |
76 uint64_t result; | |
77 SkFloatToHalf_finite(SkHalfToFloat_finite(h)).store(&result); | |
78 REPORTER_ASSERT(r, result == h); | |
79 } | 71 } |
| 72 |
| 73 // _finite_ftz() flushes denorms to zero. 0.0f will compare == with bot
h +0.0f and -0.0f. |
| 74 float expected = is_denorm(h) ? 0.0f : SkHalfToFloat(h); |
| 75 |
| 76 REPORTER_ASSERT(r, SkHalfToFloat_finite_ftz(h)[0] == expected); |
80 } | 77 } |
81 } | 78 } |
82 | 79 |
83 DEF_TEST(FloatToHalf_finite, r) { | 80 DEF_TEST(SkFloatToHalf_finite_ftz, r) { |
84 #if 0 | 81 #if 0 |
85 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { | 82 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { |
86 #else | 83 #else |
87 SkRandom rand; | 84 SkRandom rand; |
88 for (int i = 0; i < 1000000; i++) { | 85 for (int i = 0; i < 1000000; i++) { |
89 uint32_t bits = rand.nextU(); | 86 uint32_t bits = rand.nextU(); |
90 #endif | 87 #endif |
91 float f; | 88 float f; |
92 memcpy(&f, &bits, 4); | 89 memcpy(&f, &bits, 4); |
93 if (isfinite(f) && isfinite(SkHalfToFloat(SkFloatToHalf(f)))) { | 90 |
94 uint16_t h1 = SkFloatToHalf_finite(Sk4f(f,0,0,0))[0], | 91 uint16_t expected = SkFloatToHalf(f); |
95 h2 = SkFloatToHalf(f); | 92 if (!is_finite(expected)) { |
96 bool ok = (h1 == h2 || h1 == h2-1); | 93 // _finite_ftz() only works for values that can be represented as a
finite half float. |
97 REPORTER_ASSERT(r, ok); | 94 continue; |
98 if (!ok) { | |
99 SkDebugf("%08x (%g) -> %04x, want %04x (%g)\n", | |
100 bits, f, h1, h2, SkHalfToFloat(h2)); | |
101 break; | |
102 } | |
103 } | 95 } |
| 96 |
| 97 if (is_denorm(expected)) { |
| 98 // _finite_ftz() flushes denorms to zero, and happens to keep the si
gn bit. |
| 99 expected = signbit(f) ? 0x8000 : 0x0000; |
| 100 } |
| 101 |
| 102 uint16_t actual = SkFloatToHalf_finite_ftz(Sk4f{f})[0]; |
| 103 // _finite_ftz() truncates instead of rounding, so it may be one too sma
ll. |
| 104 REPORTER_ASSERT(r, actual == expected || actual == expected - 1); |
104 } | 105 } |
105 } | 106 } |
OLD | NEW |