| 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 "SkColor.h" | 9 #include "SkColor.h" |
| 10 #include "SkHalf.h" | 10 #include "SkHalf.h" |
| 11 #include "SkOpts.h" | 11 #include "SkOpts.h" |
| 12 #include "SkPixmap.h" | 12 #include "SkPixmap.h" |
| 13 #include "SkRandom.h" | |
| 14 | 13 |
| 15 static bool eq_within_half_float(float a, float b) { | 14 static bool eq_within_half_float(float a, float b) { |
| 16 const float kTolerance = 1.0f / (1 << (8 + 10)); | 15 const float kTolerance = 1.0f / (1 << (8 + 10)); |
| 17 | 16 |
| 18 SkHalf ha = SkFloatToHalf(a); | 17 SkHalf ha = SkFloatToHalf(a); |
| 19 SkHalf hb = SkFloatToHalf(b); | 18 SkHalf hb = SkFloatToHalf(b); |
| 20 float a2 = SkHalfToFloat(ha); | 19 float a2 = SkHalfToFloat(ha); |
| 21 float b2 = SkHalfToFloat(hb); | 20 float b2 = SkHalfToFloat(hb); |
| 22 return fabsf(a2 - b2) <= kTolerance; | 21 return fabsf(a2 - b2) <= kTolerance; |
| 23 } | 22 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 const uint16_t hs[] = { 0x3c00, 0x4000, 0x4200, 0x4400, 0x4500, 0x4600, 0x47
00 }; | 57 const uint16_t hs[] = { 0x3c00, 0x4000, 0x4200, 0x4400, 0x4500, 0x4600, 0x47
00 }; |
| 59 | 58 |
| 60 uint16_t hscratch[7]; | 59 uint16_t hscratch[7]; |
| 61 SkOpts::float_to_half(hscratch, fs, 7); | 60 SkOpts::float_to_half(hscratch, fs, 7); |
| 62 REPORTER_ASSERT(reporter, 0 == memcmp(hscratch, hs, sizeof(hs))); | 61 REPORTER_ASSERT(reporter, 0 == memcmp(hscratch, hs, sizeof(hs))); |
| 63 | 62 |
| 64 float fscratch[7]; | 63 float fscratch[7]; |
| 65 SkOpts::half_to_float(fscratch, hs, 7); | 64 SkOpts::half_to_float(fscratch, hs, 7); |
| 66 REPORTER_ASSERT(reporter, 0 == memcmp(fscratch, fs, sizeof(fs))); | 65 REPORTER_ASSERT(reporter, 0 == memcmp(fscratch, fs, sizeof(fs))); |
| 67 } | 66 } |
| 68 | |
| 69 DEF_TEST(HalfToFloat_01, r) { | |
| 70 for (uint16_t h = 0; h < 0x8000; h++) { | |
| 71 float f = SkHalfToFloat(h); | |
| 72 if (f >= 0 && f <= 1) { | |
| 73 REPORTER_ASSERT(r, SkHalfToFloat_01(h)[0] == f); | |
| 74 REPORTER_ASSERT(r, SkFloatToHalf_01(SkHalfToFloat_01(h)) == h); | |
| 75 } | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 DEF_TEST(FloatToHalf_01, r) { | |
| 80 #if 0 | |
| 81 for (uint32_t bits = 0; bits < 0x80000000; bits++) { | |
| 82 #else | |
| 83 SkRandom rand; | |
| 84 for (int i = 0; i < 1000000; i++) { | |
| 85 uint32_t bits = rand.nextU(); | |
| 86 #endif | |
| 87 float f; | |
| 88 memcpy(&f, &bits, 4); | |
| 89 if (f >= 0 && f <= 1) { | |
| 90 uint16_t h1 = (uint16_t)SkFloatToHalf_01(Sk4f(f,0,0,0)), | |
| 91 h2 = SkFloatToHalf(f); | |
| 92 bool ok = (h1 == h2 || h1 == h2-1); | |
| 93 REPORTER_ASSERT(r, ok); | |
| 94 if (!ok) { | |
| 95 SkDebugf("%08x (%d) -> %04x (%d), want %04x (%d)\n", | |
| 96 bits, bits>>23, h1, h1>>10, h2, h2>>10); | |
| 97 break; | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 } | |
| OLD | NEW |