2016-02-05 19:18:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
2016-03-17 13:58:39 +00:00
|
|
|
#include "SkAutoPixmapStorage.h"
|
2016-02-05 19:18:39 +00:00
|
|
|
#include "SkColor.h"
|
|
|
|
#include "SkHalf.h"
|
2016-02-09 16:18:10 +00:00
|
|
|
#include "SkOpts.h"
|
2016-02-05 19:18:39 +00:00
|
|
|
#include "SkPixmap.h"
|
2016-02-18 20:39:14 +00:00
|
|
|
#include "SkPM4f.h"
|
2016-02-11 14:30:03 +00:00
|
|
|
#include "SkRandom.h"
|
2016-02-05 19:18:39 +00:00
|
|
|
|
2016-11-09 20:00:49 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2016-02-05 19:18:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool eq_within_half_float(const SkPM4f& a, const SkPM4f& b) {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!eq_within_half_float(a.fVec[i], b.fVec[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(color_half_float, reporter) {
|
|
|
|
const int w = 100;
|
|
|
|
const int h = 100;
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
|
|
|
|
|
|
|
|
SkAutoPixmapStorage pm;
|
|
|
|
pm.alloc(info);
|
2017-10-03 18:47:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, pm.computeByteSize() == SkToSizeT(w * h * sizeof(uint64_t)));
|
2016-02-05 19:18:39 +00:00
|
|
|
|
2016-06-24 13:31:47 +00:00
|
|
|
SkColor4f c4 { 1, 0.5f, 0.25f, 0.5f };
|
2016-02-05 19:18:39 +00:00
|
|
|
pm.erase(c4);
|
|
|
|
|
|
|
|
SkPM4f origpm4 = c4.premul();
|
|
|
|
for (int y = 0; y < pm.height(); ++y) {
|
|
|
|
for (int x = 0; x < pm.width(); ++x) {
|
|
|
|
SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y));
|
|
|
|
REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 16:18:10 +00:00
|
|
|
|
2016-08-22 20:20:18 +00:00
|
|
|
static bool is_denorm(uint16_t h) {
|
|
|
|
return (h & 0x7fff) < 0x0400;
|
2016-02-11 20:48:23 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 20:20:18 +00:00
|
|
|
static bool is_finite(uint16_t h) {
|
|
|
|
return (h & 0x7c00) != 0x7c00;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkHalfToFloat_finite_ftz, r) {
|
2016-07-15 14:00:11 +00:00
|
|
|
for (uint32_t h = 0; h <= 0xffff; h++) {
|
2016-08-22 20:20:18 +00:00
|
|
|
if (!is_finite(h)) {
|
|
|
|
// _finite_ftz() only works for values that can be represented as a finite half float.
|
|
|
|
continue;
|
2016-02-11 14:30:03 +00:00
|
|
|
}
|
2016-08-22 20:20:18 +00:00
|
|
|
|
2016-08-23 15:58:12 +00:00
|
|
|
// _finite_ftz() may flush denorms to zero. 0.0f will compare == with both +0.0f and -0.0f.
|
|
|
|
float expected = SkHalfToFloat(h),
|
|
|
|
alternate = is_denorm(h) ? 0.0f : expected;
|
2016-08-22 20:20:18 +00:00
|
|
|
|
2016-08-23 15:58:12 +00:00
|
|
|
float actual = SkHalfToFloat_finite_ftz(h)[0];
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, actual == expected || actual == alternate);
|
2016-02-11 14:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 20:20:18 +00:00
|
|
|
DEF_TEST(SkFloatToHalf_finite_ftz, r) {
|
2016-02-11 14:30:03 +00:00
|
|
|
#if 0
|
2016-07-15 14:00:11 +00:00
|
|
|
for (uint64_t bits = 0; bits <= 0xffffffff; bits++) {
|
2016-02-11 14:30:03 +00:00
|
|
|
#else
|
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < 1000000; i++) {
|
|
|
|
uint32_t bits = rand.nextU();
|
|
|
|
#endif
|
|
|
|
float f;
|
|
|
|
memcpy(&f, &bits, 4);
|
2016-08-22 20:20:18 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-23 15:58:12 +00:00
|
|
|
uint16_t alternate = expected;
|
2016-08-22 20:20:18 +00:00
|
|
|
if (is_denorm(expected)) {
|
2016-08-23 15:58:12 +00:00
|
|
|
// _finite_ftz() may flush denorms to zero, and happens to keep the sign bit.
|
2016-11-09 20:00:49 +00:00
|
|
|
alternate = std::signbit(f) ? 0x8000 : 0x0000;
|
2016-02-11 14:30:03 +00:00
|
|
|
}
|
2016-08-22 20:20:18 +00:00
|
|
|
|
|
|
|
uint16_t actual = SkFloatToHalf_finite_ftz(Sk4f{f})[0];
|
2016-08-23 15:58:12 +00:00
|
|
|
// _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);
|
2016-02-11 14:30:03 +00:00
|
|
|
}
|
|
|
|
}
|