reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 16 | |
Ben Wagner | 8a1036c | 2016-11-09 15:00:49 -0500 | [diff] [blame] | 17 | #include <cmath> |
| 18 | |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 19 | static bool is_denorm(uint16_t h) { |
| 20 | return (h & 0x7fff) < 0x0400; |
mtklein | ddb64c8 | 2016-02-11 12:48:23 -0800 | [diff] [blame] | 21 | } |
| 22 | |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 23 | static bool is_finite(uint16_t h) { |
| 24 | return (h & 0x7c00) != 0x7c00; |
| 25 | } |
| 26 | |
| 27 | DEF_TEST(SkHalfToFloat_finite_ftz, r) { |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 28 | for (uint32_t h = 0; h <= 0xffff; h++) { |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 29 | if (!is_finite(h)) { |
| 30 | // _finite_ftz() only works for values that can be represented as a finite half float. |
| 31 | continue; |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 32 | } |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 33 | |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 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; |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 37 | |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 38 | float actual = SkHalfToFloat_finite_ftz(h)[0]; |
| 39 | |
| 40 | REPORTER_ASSERT(r, actual == expected || actual == alternate); |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 44 | DEF_TEST(SkFloatToHalf_finite_ftz, r) { |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 45 | #if 0 |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 46 | for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 47 | #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); |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 54 | |
| 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; |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 59 | } |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 60 | |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 61 | uint16_t alternate = expected; |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 62 | if (is_denorm(expected)) { |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 63 | // _finite_ftz() may flush denorms to zero, and happens to keep the sign bit. |
Ben Wagner | 8a1036c | 2016-11-09 15:00:49 -0500 | [diff] [blame] | 64 | alternate = std::signbit(f) ? 0x8000 : 0x0000; |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | uint16_t actual = SkFloatToHalf_finite_ftz(Sk4f{f})[0]; |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 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); |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 71 | } |
| 72 | } |