blob: e67d8f1e79a1c9ed76b2af418d6179b243a4128b [file] [log] [blame]
reed3601f282016-02-05 11:18:39 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Test.h"
robertphillipsc5035e72016-03-17 06:58:39 -07009#include "SkAutoPixmapStorage.h"
reed3601f282016-02-05 11:18:39 -080010#include "SkColor.h"
11#include "SkHalf.h"
mtkleina525cb12016-02-09 08:18:10 -080012#include "SkOpts.h"
reed3601f282016-02-05 11:18:39 -080013#include "SkPixmap.h"
reeddd9ffea2016-02-18 12:39:14 -080014#include "SkPM4f.h"
mtkleinfff055c2016-02-11 06:30:03 -080015#include "SkRandom.h"
reed3601f282016-02-05 11:18:39 -080016
Ben Wagner8a1036c2016-11-09 15:00:49 -050017#include <cmath>
18
reed3601f282016-02-05 11:18:39 -080019static bool eq_within_half_float(float a, float b) {
20 const float kTolerance = 1.0f / (1 << (8 + 10));
21
22 SkHalf ha = SkFloatToHalf(a);
23 SkHalf hb = SkFloatToHalf(b);
24 float a2 = SkHalfToFloat(ha);
25 float b2 = SkHalfToFloat(hb);
26 return fabsf(a2 - b2) <= kTolerance;
27}
28
29static bool eq_within_half_float(const SkPM4f& a, const SkPM4f& b) {
30 for (int i = 0; i < 4; ++i) {
31 if (!eq_within_half_float(a.fVec[i], b.fVec[i])) {
32 return false;
33 }
34 }
35 return true;
36}
37
38DEF_TEST(color_half_float, reporter) {
39 const int w = 100;
40 const int h = 100;
41
42 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
43
44 SkAutoPixmapStorage pm;
45 pm.alloc(info);
Mike Reedf0ffb892017-10-03 14:47:21 -040046 REPORTER_ASSERT(reporter, pm.computeByteSize() == SkToSizeT(w * h * sizeof(uint64_t)));
reed3601f282016-02-05 11:18:39 -080047
brianosmane074d1f2016-06-24 06:31:47 -070048 SkColor4f c4 { 1, 0.5f, 0.25f, 0.5f };
reed3601f282016-02-05 11:18:39 -080049 pm.erase(c4);
50
51 SkPM4f origpm4 = c4.premul();
52 for (int y = 0; y < pm.height(); ++y) {
53 for (int x = 0; x < pm.width(); ++x) {
54 SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y));
55 REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4));
56 }
57 }
58}
mtkleina525cb12016-02-09 08:18:10 -080059
mtklein8ae991e2016-08-22 13:20:18 -070060static bool is_denorm(uint16_t h) {
61 return (h & 0x7fff) < 0x0400;
mtkleinddb64c82016-02-11 12:48:23 -080062}
63
mtklein8ae991e2016-08-22 13:20:18 -070064static bool is_finite(uint16_t h) {
65 return (h & 0x7c00) != 0x7c00;
66}
67
68DEF_TEST(SkHalfToFloat_finite_ftz, r) {
mtklein58e389b2016-07-15 07:00:11 -070069 for (uint32_t h = 0; h <= 0xffff; h++) {
mtklein8ae991e2016-08-22 13:20:18 -070070 if (!is_finite(h)) {
71 // _finite_ftz() only works for values that can be represented as a finite half float.
72 continue;
mtkleinfff055c2016-02-11 06:30:03 -080073 }
mtklein8ae991e2016-08-22 13:20:18 -070074
mtkleina2d2f382016-08-23 08:58:12 -070075 // _finite_ftz() may flush denorms to zero. 0.0f will compare == with both +0.0f and -0.0f.
76 float expected = SkHalfToFloat(h),
77 alternate = is_denorm(h) ? 0.0f : expected;
mtklein8ae991e2016-08-22 13:20:18 -070078
mtkleina2d2f382016-08-23 08:58:12 -070079 float actual = SkHalfToFloat_finite_ftz(h)[0];
80
81 REPORTER_ASSERT(r, actual == expected || actual == alternate);
mtkleinfff055c2016-02-11 06:30:03 -080082 }
83}
84
mtklein8ae991e2016-08-22 13:20:18 -070085DEF_TEST(SkFloatToHalf_finite_ftz, r) {
mtkleinfff055c2016-02-11 06:30:03 -080086#if 0
mtklein58e389b2016-07-15 07:00:11 -070087 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) {
mtkleinfff055c2016-02-11 06:30:03 -080088#else
89 SkRandom rand;
90 for (int i = 0; i < 1000000; i++) {
91 uint32_t bits = rand.nextU();
92#endif
93 float f;
94 memcpy(&f, &bits, 4);
mtklein8ae991e2016-08-22 13:20:18 -070095
96 uint16_t expected = SkFloatToHalf(f);
97 if (!is_finite(expected)) {
98 // _finite_ftz() only works for values that can be represented as a finite half float.
99 continue;
mtkleinfff055c2016-02-11 06:30:03 -0800100 }
mtklein8ae991e2016-08-22 13:20:18 -0700101
mtkleina2d2f382016-08-23 08:58:12 -0700102 uint16_t alternate = expected;
mtklein8ae991e2016-08-22 13:20:18 -0700103 if (is_denorm(expected)) {
mtkleina2d2f382016-08-23 08:58:12 -0700104 // _finite_ftz() may flush denorms to zero, and happens to keep the sign bit.
Ben Wagner8a1036c2016-11-09 15:00:49 -0500105 alternate = std::signbit(f) ? 0x8000 : 0x0000;
mtklein8ae991e2016-08-22 13:20:18 -0700106 }
107
108 uint16_t actual = SkFloatToHalf_finite_ftz(Sk4f{f})[0];
mtkleina2d2f382016-08-23 08:58:12 -0700109 // _finite_ftz() may truncate instead of rounding, so it may be one too small.
110 REPORTER_ASSERT(r, actual == expected || actual == expected - 1 ||
111 actual == alternate || actual == alternate - 1);
mtkleinfff055c2016-02-11 06:30:03 -0800112 }
113}