Chromium Code Reviews| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 | 57 |
| 58 static uint32_t u(float f) { | 58 static uint32_t u(float f) { |
| 59 uint32_t x; | 59 uint32_t x; |
| 60 memcpy(&x, &f, 4); | 60 memcpy(&x, &f, 4); |
| 61 return x; | 61 return x; |
| 62 } | 62 } |
| 63 | 63 |
| 64 DEF_TEST(HalfToFloat_finite, r) { | 64 static bool is_ordinary(float f) { |
| 65 return f == 0.0f || isfinite(f); | |
|
msarett
2016/08/18 13:19:55
nit: Why not sk_float_is_finite()?
nit: Change fu
| |
| 66 } | |
| 67 | |
| 68 static bool is_denorm(uint16_t h) { | |
| 69 return (h & 0x7fff) < 0x0400; | |
| 70 } | |
| 71 | |
| 72 DEF_TEST(SkHalfToFloat_ordinary, r) { | |
| 65 for (uint32_t h = 0; h <= 0xffff; h++) { | 73 for (uint32_t h = 0; h <= 0xffff; h++) { |
| 74 if (is_denorm(h)) { | |
| 75 continue; | |
|
msarett
2016/08/18 13:19:55
Can we assert that this converts to zero here?
| |
| 76 } | |
| 66 float f = SkHalfToFloat(h); | 77 float f = SkHalfToFloat(h); |
| 67 if (isfinite(f)) { | 78 if (is_ordinary(f)) { |
| 68 float got = SkHalfToFloat_finite(h)[0]; | 79 float got = SkHalfToFloat_ordinary(h)[0]; |
| 69 if (got != f) { | 80 if (got != f) { |
| 70 SkDebugf("0x%04x -> 0x%08x (%g), want 0x%08x (%g)\n", | 81 SkDebugf("0x%04x -> 0x%08x (%g), want 0x%08x (%g)\n", |
| 71 h, | 82 h, |
| 72 u(got), got, | 83 u(got), got, |
| 73 u(f), f); | 84 u(f), f); |
| 74 } | 85 } |
| 75 REPORTER_ASSERT(r, SkHalfToFloat_finite(h)[0] == f); | 86 REPORTER_ASSERT(r, SkHalfToFloat_ordinary(h)[0] == f); |
| 76 uint64_t result; | 87 uint64_t result; |
| 77 SkFloatToHalf_finite(SkHalfToFloat_finite(h)).store(&result); | 88 SkFloatToHalf_ordinary(SkHalfToFloat_ordinary(h)).store(&result); |
| 78 REPORTER_ASSERT(r, result == h); | 89 REPORTER_ASSERT(r, result == h); |
| 79 } | 90 } |
| 80 } | 91 } |
| 81 } | 92 } |
| 82 | 93 |
| 83 DEF_TEST(FloatToHalf_finite, r) { | 94 DEF_TEST(SkFloatToHalf_ordinary, r) { |
| 84 #if 0 | 95 #if 0 |
| 85 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { | 96 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { |
| 86 #else | 97 #else |
| 87 SkRandom rand; | 98 SkRandom rand; |
| 88 for (int i = 0; i < 1000000; i++) { | 99 for (int i = 0; i < 1000000; i++) { |
| 89 uint32_t bits = rand.nextU(); | 100 uint32_t bits = rand.nextU(); |
| 90 #endif | 101 #endif |
| 91 float f; | 102 float f; |
| 92 memcpy(&f, &bits, 4); | 103 memcpy(&f, &bits, 4); |
| 93 if (isfinite(f) && isfinite(SkHalfToFloat(SkFloatToHalf(f)))) { | 104 if (is_ordinary(f) && is_ordinary(SkHalfToFloat(SkFloatToHalf(f)))) { |
|
msarett
2016/08/18 13:19:55
Will this case ever not be true?
is_ordinary(SkHal
| |
| 94 uint16_t h1 = SkFloatToHalf_finite(Sk4f(f,0,0,0))[0], | 105 uint16_t h1 = SkFloatToHalf_ordinary(Sk4f(f,0,0,0))[0], |
| 95 h2 = SkFloatToHalf(f); | 106 h2 = SkFloatToHalf(f); |
| 107 if (is_denorm(h2)) { | |
| 108 continue; | |
|
msarett
2016/08/18 13:19:55
Can we assert that h1 is zero in this case?
| |
| 109 } | |
| 96 bool ok = (h1 == h2 || h1 == h2-1); | 110 bool ok = (h1 == h2 || h1 == h2-1); |
|
msarett
2016/08/18 13:19:55
Realize this is not new... But do we allow a tole
| |
| 97 REPORTER_ASSERT(r, ok); | 111 REPORTER_ASSERT(r, ok); |
| 98 if (!ok) { | 112 if (!ok) { |
| 99 SkDebugf("%08x (%g) -> %04x, want %04x (%g)\n", | 113 SkDebugf("%08x (%g) -> %04x, want %04x (%g)\n", |
| 100 bits, f, h1, h2, SkHalfToFloat(h2)); | 114 bits, f, h1, h2, SkHalfToFloat(h2)); |
| 101 break; | 115 break; |
| 102 } | 116 } |
| 103 } | 117 } |
| 104 } | 118 } |
| 105 } | 119 } |
| OLD | NEW |