blob: 85f0ad59a6896bef0167848eb2995b4bec70cce4 [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
robertphillipsc5035e72016-03-17 06:58:39 -07008#include "SkAutoPixmapStorage.h"
reed3601f282016-02-05 11:18:39 -08009#include "SkColor.h"
10#include "SkHalf.h"
mtkleina525cb12016-02-09 08:18:10 -080011#include "SkOpts.h"
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040012#include "SkPM4f.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040013#include "SkPixmap.h"
mtkleinfff055c2016-02-11 06:30:03 -080014#include "SkRandom.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040015#include "SkTo.h"
16#include "Test.h"
reed3601f282016-02-05 11:18:39 -080017
Ben Wagner8a1036c2016-11-09 15:00:49 -050018#include <cmath>
19
reed3601f282016-02-05 11:18:39 -080020static bool eq_within_half_float(float a, float b) {
21 const float kTolerance = 1.0f / (1 << (8 + 10));
22
23 SkHalf ha = SkFloatToHalf(a);
24 SkHalf hb = SkFloatToHalf(b);
25 float a2 = SkHalfToFloat(ha);
26 float b2 = SkHalfToFloat(hb);
27 return fabsf(a2 - b2) <= kTolerance;
28}
29
30static bool eq_within_half_float(const SkPM4f& a, const SkPM4f& b) {
31 for (int i = 0; i < 4; ++i) {
32 if (!eq_within_half_float(a.fVec[i], b.fVec[i])) {
33 return false;
34 }
35 }
36 return true;
37}
38
39DEF_TEST(color_half_float, reporter) {
40 const int w = 100;
41 const int h = 100;
42
43 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
44
45 SkAutoPixmapStorage pm;
46 pm.alloc(info);
Mike Reedf0ffb892017-10-03 14:47:21 -040047 REPORTER_ASSERT(reporter, pm.computeByteSize() == SkToSizeT(w * h * sizeof(uint64_t)));
reed3601f282016-02-05 11:18:39 -080048
brianosmane074d1f2016-06-24 06:31:47 -070049 SkColor4f c4 { 1, 0.5f, 0.25f, 0.5f };
reed3601f282016-02-05 11:18:39 -080050 pm.erase(c4);
51
52 SkPM4f origpm4 = c4.premul();
53 for (int y = 0; y < pm.height(); ++y) {
54 for (int x = 0; x < pm.width(); ++x) {
55 SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y));
56 REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4));
57 }
58 }
59}
mtkleina525cb12016-02-09 08:18:10 -080060
mtklein8ae991e2016-08-22 13:20:18 -070061static bool is_denorm(uint16_t h) {
62 return (h & 0x7fff) < 0x0400;
mtkleinddb64c82016-02-11 12:48:23 -080063}
64
mtklein8ae991e2016-08-22 13:20:18 -070065static bool is_finite(uint16_t h) {
66 return (h & 0x7c00) != 0x7c00;
67}
68
69DEF_TEST(SkHalfToFloat_finite_ftz, r) {
mtklein58e389b2016-07-15 07:00:11 -070070 for (uint32_t h = 0; h <= 0xffff; h++) {
mtklein8ae991e2016-08-22 13:20:18 -070071 if (!is_finite(h)) {
72 // _finite_ftz() only works for values that can be represented as a finite half float.
73 continue;
mtkleinfff055c2016-02-11 06:30:03 -080074 }
mtklein8ae991e2016-08-22 13:20:18 -070075
mtkleina2d2f382016-08-23 08:58:12 -070076 // _finite_ftz() may flush denorms to zero. 0.0f will compare == with both +0.0f and -0.0f.
77 float expected = SkHalfToFloat(h),
78 alternate = is_denorm(h) ? 0.0f : expected;
mtklein8ae991e2016-08-22 13:20:18 -070079
mtkleina2d2f382016-08-23 08:58:12 -070080 float actual = SkHalfToFloat_finite_ftz(h)[0];
81
82 REPORTER_ASSERT(r, actual == expected || actual == alternate);
mtkleinfff055c2016-02-11 06:30:03 -080083 }
84}
85
mtklein8ae991e2016-08-22 13:20:18 -070086DEF_TEST(SkFloatToHalf_finite_ftz, r) {
mtkleinfff055c2016-02-11 06:30:03 -080087#if 0
mtklein58e389b2016-07-15 07:00:11 -070088 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) {
mtkleinfff055c2016-02-11 06:30:03 -080089#else
90 SkRandom rand;
91 for (int i = 0; i < 1000000; i++) {
92 uint32_t bits = rand.nextU();
93#endif
94 float f;
95 memcpy(&f, &bits, 4);
mtklein8ae991e2016-08-22 13:20:18 -070096
97 uint16_t expected = SkFloatToHalf(f);
98 if (!is_finite(expected)) {
99 // _finite_ftz() only works for values that can be represented as a finite half float.
100 continue;
mtkleinfff055c2016-02-11 06:30:03 -0800101 }
mtklein8ae991e2016-08-22 13:20:18 -0700102
mtkleina2d2f382016-08-23 08:58:12 -0700103 uint16_t alternate = expected;
mtklein8ae991e2016-08-22 13:20:18 -0700104 if (is_denorm(expected)) {
mtkleina2d2f382016-08-23 08:58:12 -0700105 // _finite_ftz() may flush denorms to zero, and happens to keep the sign bit.
Ben Wagner8a1036c2016-11-09 15:00:49 -0500106 alternate = std::signbit(f) ? 0x8000 : 0x0000;
mtklein8ae991e2016-08-22 13:20:18 -0700107 }
108
109 uint16_t actual = SkFloatToHalf_finite_ftz(Sk4f{f})[0];
mtkleina2d2f382016-08-23 08:58:12 -0700110 // _finite_ftz() may truncate instead of rounding, so it may be one too small.
111 REPORTER_ASSERT(r, actual == expected || actual == expected - 1 ||
112 actual == alternate || actual == alternate - 1);
mtkleinfff055c2016-02-11 06:30:03 -0800113 }
114}