blob: c223c7c1821f504c4a1b354105d77d72bcf55771 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
reed3601f282016-02-05 11:18:39 -080016
Ben Wagner8a1036c2016-11-09 15:00:49 -050017#include <cmath>
18
mtklein8ae991e2016-08-22 13:20:18 -070019static bool is_denorm(uint16_t h) {
20 return (h & 0x7fff) < 0x0400;
mtkleinddb64c82016-02-11 12:48:23 -080021}
22
mtklein8ae991e2016-08-22 13:20:18 -070023static bool is_finite(uint16_t h) {
24 return (h & 0x7c00) != 0x7c00;
25}
26
27DEF_TEST(SkHalfToFloat_finite_ftz, r) {
mtklein58e389b2016-07-15 07:00:11 -070028 for (uint32_t h = 0; h <= 0xffff; h++) {
mtklein8ae991e2016-08-22 13:20:18 -070029 if (!is_finite(h)) {
30 // _finite_ftz() only works for values that can be represented as a finite half float.
31 continue;
mtkleinfff055c2016-02-11 06:30:03 -080032 }
mtklein8ae991e2016-08-22 13:20:18 -070033
mtkleina2d2f382016-08-23 08:58:12 -070034 // _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;
mtklein8ae991e2016-08-22 13:20:18 -070037
mtkleina2d2f382016-08-23 08:58:12 -070038 float actual = SkHalfToFloat_finite_ftz(h)[0];
39
40 REPORTER_ASSERT(r, actual == expected || actual == alternate);
mtkleinfff055c2016-02-11 06:30:03 -080041 }
42}
43
mtklein8ae991e2016-08-22 13:20:18 -070044DEF_TEST(SkFloatToHalf_finite_ftz, r) {
mtkleinfff055c2016-02-11 06:30:03 -080045#if 0
mtklein58e389b2016-07-15 07:00:11 -070046 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) {
mtkleinfff055c2016-02-11 06:30:03 -080047#else
48 SkRandom rand;
49 for (int i = 0; i < 1000000; i++) {
50 uint32_t bits = rand.nextU();
51#endif
52 float f;
53 memcpy(&f, &bits, 4);
mtklein8ae991e2016-08-22 13:20:18 -070054
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.
58 continue;
mtkleinfff055c2016-02-11 06:30:03 -080059 }
mtklein8ae991e2016-08-22 13:20:18 -070060
mtkleina2d2f382016-08-23 08:58:12 -070061 uint16_t alternate = expected;
mtklein8ae991e2016-08-22 13:20:18 -070062 if (is_denorm(expected)) {
mtkleina2d2f382016-08-23 08:58:12 -070063 // _finite_ftz() may flush denorms to zero, and happens to keep the sign bit.
Ben Wagner8a1036c2016-11-09 15:00:49 -050064 alternate = std::signbit(f) ? 0x8000 : 0x0000;
mtklein8ae991e2016-08-22 13:20:18 -070065 }
66
67 uint16_t actual = SkFloatToHalf_finite_ftz(Sk4f{f})[0];
mtkleina2d2f382016-08-23 08:58:12 -070068 // _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);
mtkleinfff055c2016-02-11 06:30:03 -080071 }
72}