reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 8 | #include "SkFloatingPoint.h" |
| 9 | #include "SkMath.h" |
| 10 | #include "SkPoint.h" |
| 11 | #include "SkRandom.h" |
reed@google.com | 30d90eb | 2012-05-15 14:17:36 +0000 | [diff] [blame] | 12 | #include "SkRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "Test.h" |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | 4e332f8 | 2014-05-05 16:04:42 +0000 | [diff] [blame] | 15 | static void test_roundtoint(skiatest::Reporter* reporter) { |
reed@google.com | 3253f83 | 2014-05-05 16:32:16 +0000 | [diff] [blame] | 16 | SkScalar x = 0.49999997f; |
commit-bot@chromium.org | 4e332f8 | 2014-05-05 16:04:42 +0000 | [diff] [blame] | 17 | int ix = SkScalarRoundToInt(x); |
| 18 | // We "should" get 0, since x < 0.5, but we don't due to float addition rounding up the low |
| 19 | // bit after adding 0.5. |
| 20 | REPORTER_ASSERT(reporter, 1 == ix); |
| 21 | |
| 22 | // This version explicitly performs the +0.5 step using double, which should avoid losing the |
| 23 | // low bits. |
| 24 | ix = SkDScalarRoundToInt(x); |
| 25 | REPORTER_ASSERT(reporter, 0 == ix); |
| 26 | } |
| 27 | |
reed@google.com | 30d90eb | 2012-05-15 14:17:36 +0000 | [diff] [blame] | 28 | struct PointSet { |
| 29 | const SkPoint* fPts; |
| 30 | size_t fCount; |
| 31 | bool fIsFinite; |
| 32 | }; |
| 33 | |
| 34 | static void test_isRectFinite(skiatest::Reporter* reporter) { |
| 35 | static const SkPoint gF0[] = { |
| 36 | { 0, 0 }, { 1, 1 } |
| 37 | }; |
| 38 | static const SkPoint gF1[] = { |
| 39 | { 0, 0 }, { 1, 1 }, { 99.234f, -42342 } |
| 40 | }; |
| 41 | |
| 42 | static const SkPoint gI0[] = { |
| 43 | { 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { SK_ScalarNaN, 3 }, { 2, 3 }, |
| 44 | }; |
| 45 | static const SkPoint gI1[] = { |
| 46 | { 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { 3, SK_ScalarNaN }, { 2, 3 }, |
| 47 | }; |
| 48 | static const SkPoint gI2[] = { |
| 49 | { 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { SK_ScalarInfinity, 3 }, { 2, 3 }, |
| 50 | }; |
| 51 | static const SkPoint gI3[] = { |
| 52 | { 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { 3, SK_ScalarInfinity }, { 2, 3 }, |
| 53 | }; |
| 54 | |
| 55 | static const struct { |
| 56 | const SkPoint* fPts; |
| 57 | size_t fCount; |
| 58 | bool fIsFinite; |
| 59 | } gSets[] = { |
| 60 | { gF0, SK_ARRAY_COUNT(gF0), true }, |
| 61 | { gF1, SK_ARRAY_COUNT(gF1), true }, |
| 62 | |
| 63 | { gI0, SK_ARRAY_COUNT(gI0), false }, |
| 64 | { gI1, SK_ARRAY_COUNT(gI1), false }, |
| 65 | { gI2, SK_ARRAY_COUNT(gI2), false }, |
| 66 | { gI3, SK_ARRAY_COUNT(gI3), false }, |
| 67 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 68 | |
reed@google.com | 30d90eb | 2012-05-15 14:17:36 +0000 | [diff] [blame] | 69 | for (size_t i = 0; i < SK_ARRAY_COUNT(gSets); ++i) { |
| 70 | SkRect r; |
| 71 | r.set(gSets[i].fPts, gSets[i].fCount); |
| 72 | bool rectIsFinite = !r.isEmpty(); |
| 73 | REPORTER_ASSERT(reporter, gSets[i].fIsFinite == rectIsFinite); |
| 74 | } |
| 75 | } |
| 76 | |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 77 | static bool isFinite_int(float x) { |
| 78 | uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts |
| 79 | int exponent = bits << 1 >> 24; |
| 80 | return exponent != 0xFF; |
| 81 | } |
| 82 | |
| 83 | static bool isFinite_float(float x) { |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 84 | return SkToBool(sk_float_isfinite(x)); |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static bool isFinite_mulzero(float x) { |
| 88 | float y = x * 0; |
| 89 | return y == y; |
| 90 | } |
| 91 | |
| 92 | // return true if the float is finite |
| 93 | typedef bool (*IsFiniteProc1)(float); |
| 94 | |
| 95 | static bool isFinite2_and(float x, float y, IsFiniteProc1 proc) { |
| 96 | return proc(x) && proc(y); |
| 97 | } |
| 98 | |
| 99 | static bool isFinite2_mulzeroadd(float x, float y, IsFiniteProc1 proc) { |
| 100 | return proc(x * 0 + y * 0); |
| 101 | } |
| 102 | |
| 103 | // return true if both floats are finite |
| 104 | typedef bool (*IsFiniteProc2)(float, float, IsFiniteProc1); |
| 105 | |
reed@google.com | 5ae777d | 2011-12-06 20:18:05 +0000 | [diff] [blame] | 106 | enum FloatClass { |
| 107 | kFinite, |
| 108 | kInfinite, |
| 109 | kNaN |
| 110 | }; |
| 111 | |
| 112 | static void test_floatclass(skiatest::Reporter* reporter, float value, FloatClass fc) { |
| 113 | // our sk_float_is... function may return int instead of bool, |
| 114 | // hence the double ! to turn it into a bool |
| 115 | REPORTER_ASSERT(reporter, !!sk_float_isfinite(value) == (fc == kFinite)); |
| 116 | REPORTER_ASSERT(reporter, !!sk_float_isinf(value) == (fc == kInfinite)); |
| 117 | REPORTER_ASSERT(reporter, !!sk_float_isnan(value) == (fc == kNaN)); |
| 118 | } |
| 119 | |
robertphillips@google.com | 706f621 | 2012-05-14 17:51:23 +0000 | [diff] [blame] | 120 | #if defined _WIN32 |
| 121 | #pragma warning ( push ) |
| 122 | // we are intentionally causing an overflow here |
| 123 | // (warning C4756: overflow in constant arithmetic) |
| 124 | #pragma warning ( disable : 4756 ) |
| 125 | #endif |
| 126 | |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 127 | static void test_isfinite(skiatest::Reporter* reporter) { |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 128 | struct Rec { |
| 129 | float fValue; |
| 130 | bool fIsFinite; |
| 131 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 132 | |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 133 | float max = 3.402823466e+38f; |
| 134 | float inf = max * max; |
reed@google.com | 5ae777d | 2011-12-06 20:18:05 +0000 | [diff] [blame] | 135 | float nan = inf * 0; |
| 136 | |
| 137 | test_floatclass(reporter, 0, kFinite); |
| 138 | test_floatclass(reporter, max, kFinite); |
| 139 | test_floatclass(reporter, -max, kFinite); |
| 140 | test_floatclass(reporter, inf, kInfinite); |
| 141 | test_floatclass(reporter, -inf, kInfinite); |
| 142 | test_floatclass(reporter, nan, kNaN); |
| 143 | test_floatclass(reporter, -nan, kNaN); |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 144 | |
| 145 | const Rec data[] = { |
bungeman@google.com | f8aa18c | 2012-03-19 21:04:52 +0000 | [diff] [blame] | 146 | { 0, true }, |
| 147 | { 1, true }, |
| 148 | { -1, true }, |
| 149 | { max * 0.75f, true }, |
| 150 | { max, true }, |
| 151 | { -max * 0.75f, true }, |
| 152 | { -max, true }, |
| 153 | { inf, false }, |
| 154 | { -inf, false }, |
| 155 | { nan, false }, |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | const IsFiniteProc1 gProc1[] = { |
| 159 | isFinite_int, |
| 160 | isFinite_float, |
| 161 | isFinite_mulzero |
| 162 | }; |
| 163 | const IsFiniteProc2 gProc2[] = { |
| 164 | isFinite2_and, |
| 165 | isFinite2_mulzeroadd |
| 166 | }; |
| 167 | |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 168 | size_t i, n = SK_ARRAY_COUNT(data); |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 169 | |
| 170 | for (i = 0; i < n; ++i) { |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 171 | for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) { |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 172 | const Rec& rec = data[i]; |
| 173 | bool finite = gProc1[k](rec.fValue); |
| 174 | REPORTER_ASSERT(reporter, rec.fIsFinite == finite); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | for (i = 0; i < n; ++i) { |
| 179 | const Rec& rec0 = data[i]; |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 180 | for (size_t j = 0; j < n; ++j) { |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 181 | const Rec& rec1 = data[j]; |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 182 | for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) { |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 183 | IsFiniteProc1 proc1 = gProc1[k]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 184 | |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 185 | for (size_t m = 0; m < SK_ARRAY_COUNT(gProc2); ++m) { |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 186 | bool finite = gProc2[m](rec0.fValue, rec1.fValue, proc1); |
| 187 | bool finite2 = rec0.fIsFinite && rec1.fIsFinite; |
| 188 | REPORTER_ASSERT(reporter, finite2 == finite); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 193 | |
reed@google.com | 30d90eb | 2012-05-15 14:17:36 +0000 | [diff] [blame] | 194 | test_isRectFinite(reporter); |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 195 | } |
| 196 | |
robertphillips@google.com | 706f621 | 2012-05-14 17:51:23 +0000 | [diff] [blame] | 197 | #if defined _WIN32 |
| 198 | #pragma warning ( pop ) |
| 199 | #endif |
| 200 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 201 | DEF_TEST(Scalar, reporter) { |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 202 | test_isfinite(reporter); |
commit-bot@chromium.org | 4e332f8 | 2014-05-05 16:04:42 +0000 | [diff] [blame] | 203 | test_roundtoint(reporter); |
reed@google.com | d230e3e | 2011-12-05 20:49:37 +0000 | [diff] [blame] | 204 | } |