static bool eq_within_half_float(float a, float b) { const float kTolerance = 1.0f / (1 << (8 + 10)); SkHalf ha = SkFloatToHalf(a); SkHalf hb = SkFloatToHalf(b); float a2 = SkHalfToFloat(ha); float b2 = SkHalfToFloat(hb); return fabsf(a2 - b2) <= kTolerance; }
DEF_TEST(SkFloatToHalf_finite_ftz, r) { #if 0 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { #else SkRandom rand; for (int i = 0; i < 1000000; i++) { uint32_t bits = rand.nextU(); #endif float f; memcpy(&f, &bits, 4); uint16_t expected = SkFloatToHalf(f); if (!is_finite(expected)) { // _finite_ftz() only works for values that can be represented as a finite half float. continue; } uint16_t alternate = expected; if (is_denorm(expected)) { // _finite_ftz() may flush denorms to zero, and happens to keep the sign bit. alternate = signbit(f) ? 0x8000 : 0x0000; } uint16_t actual = SkFloatToHalf_finite_ftz(Sk4f{f})[0]; // _finite_ftz() may truncate instead of rounding, so it may be one too small. REPORTER_ASSERT(r, actual == expected || actual == expected - 1 || actual == alternate || actual == alternate - 1); } }
DEF_TEST(FloatToHalf_finite, r) { #if 0 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { #else SkRandom rand; for (int i = 0; i < 1000000; i++) { uint32_t bits = rand.nextU(); #endif float f; memcpy(&f, &bits, 4); if (isfinite(f) && isfinite(SkHalfToFloat(SkFloatToHalf(f)))) { uint16_t h1 = SkFloatToHalf_finite(Sk4f(f,0,0,0))[0], h2 = SkFloatToHalf(f); bool ok = (h1 == h2 || h1 == h2-1); REPORTER_ASSERT(r, ok); if (!ok) { SkDebugf("%08x (%g) -> %04x, want %04x (%g)\n", bits, f, h1, h2, SkHalfToFloat(h2)); break; } } } }
DEF_TEST(FloatToHalf_01, r) { #if 0 for (uint32_t bits = 0; bits < 0x80000000; bits++) { #else SkRandom rand; for (int i = 0; i < 1000000; i++) { uint32_t bits = rand.nextU(); #endif float f; memcpy(&f, &bits, 4); if (f >= 0 && f <= 1) { uint16_t h1 = (uint16_t)SkFloatToHalf_01(Sk4f(f,0,0,0)), h2 = SkFloatToHalf(f); bool ok = (h1 == h2 || h1 == h2-1); REPORTER_ASSERT(r, ok); if (!ok) { SkDebugf("%08x (%d) -> %04x (%d), want %04x (%d)\n", bits, bits>>23, h1, h1>>10, h2, h2>>10); break; } } }
void SkPM4f::toF16(uint16_t half[4]) const { for (int i = 0; i < 4; ++i) { half[i] = SkFloatToHalf(fVec[i]); } }
static void float_to_half(uint16_t dst[], const float src[], int n) { while (n-->0) { *dst++ = SkFloatToHalf(*src++); } }