2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkColor.h"
9 #include "include/core/SkPixmap.h"
10 #include "include/private/SkHalf.h"
11 #include "include/private/SkTo.h"
12 #include "include/utils/SkRandom.h"
13 #include "src/core/SkAutoPixmapStorage.h"
14 #include "src/core/SkOpts.h"
15 #include "tests/Test.h"
19 static bool is_denorm(uint16_t h) {
20 return (h & 0x7fff) < 0x0400;
23 static bool is_finite(uint16_t h) {
24 return (h & 0x7c00) != 0x7c00;
27 DEF_TEST(SkHalfToFloat_finite_ftz, r) {
28 for (uint32_t h = 0; h <= 0xffff; h++) {
30 // _finite_ftz() only works for values that can be represented as a finite half float.
34 // _finite_ftz() may flush denorms to zero. 0.0f will compare == with both +0.0f and -0.0f.
35 float expected = SkHalfToFloat(h),
36 alternate = is_denorm(h) ? 0.0f : expected;
38 float actual = SkHalfToFloat_finite_ftz(h)[0];
40 REPORTER_ASSERT(r, actual == expected || actual == alternate);
44 DEF_TEST(SkFloatToHalf_finite_ftz, r) {
46 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) {
49 for (int i = 0; i < 1000000; i++) {
50 uint32_t bits = rand.nextU();
55 uint16_t expected = SkFloatToHalf(f);
56 if (!is_finite(expected)) {
57 // _finite_ftz() only works for values that can be represented as a finite half float.
61 uint16_t alternate = expected;
62 if (is_denorm(expected)) {
63 // _finite_ftz() may flush denorms to zero, and happens to keep the sign bit.
64 alternate = std::signbit(f) ? 0x8000 : 0x0000;
67 uint16_t actual = SkFloatToHalf_finite_ftz(skvx::float4{f})[0];
68 // _finite_ftz() may truncate instead of rounding, so it may be one too small.
69 REPORTER_ASSERT(r, actual == expected || actual == expected - 1 ||
70 actual == alternate || actual == alternate - 1);