blob: b6f71d19bca35e465afcb4de6bb963e5c8312f0e [file] [log] [blame]
reed@google.comd230e3e2011-12-05 20:49:37 +00001/*
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.comd230e3e2011-12-05 20:49:37 +00008#include "SkFloatingPoint.h"
9#include "SkMath.h"
10#include "SkPoint.h"
11#include "SkRandom.h"
reed@google.com30d90eb2012-05-15 14:17:36 +000012#include "SkRect.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "Test.h"
reed@google.comd230e3e2011-12-05 20:49:37 +000014
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000015static void test_roundtoint(skiatest::Reporter* reporter) {
reed@google.com3253f832014-05-05 16:32:16 +000016 SkScalar x = 0.49999997f;
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +000017 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.com30d90eb2012-05-15 14:17:36 +000028struct PointSet {
29 const SkPoint* fPts;
30 size_t fCount;
31 bool fIsFinite;
32};
33
34static 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.comd6176b02012-08-23 18:14:13 +000068
reed@google.com30d90eb2012-05-15 14:17:36 +000069 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.comd230e3e2011-12-05 20:49:37 +000077static 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
83static bool isFinite_float(float x) {
robertphillips@google.com6853e802012-04-16 15:50:18 +000084 return SkToBool(sk_float_isfinite(x));
reed@google.comd230e3e2011-12-05 20:49:37 +000085}
86
87static bool isFinite_mulzero(float x) {
88 float y = x * 0;
89 return y == y;
90}
91
92// return true if the float is finite
93typedef bool (*IsFiniteProc1)(float);
94
95static bool isFinite2_and(float x, float y, IsFiniteProc1 proc) {
96 return proc(x) && proc(y);
97}
98
99static 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
104typedef bool (*IsFiniteProc2)(float, float, IsFiniteProc1);
105
reed@google.com5ae777d2011-12-06 20:18:05 +0000106enum FloatClass {
107 kFinite,
108 kInfinite,
109 kNaN
110};
111
112static 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.com706f6212012-05-14 17:51:23 +0000120#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.comd230e3e2011-12-05 20:49:37 +0000127static void test_isfinite(skiatest::Reporter* reporter) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000128 struct Rec {
129 float fValue;
130 bool fIsFinite;
131 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132
reed@google.comd230e3e2011-12-05 20:49:37 +0000133 float max = 3.402823466e+38f;
134 float inf = max * max;
reed@google.com5ae777d2011-12-06 20:18:05 +0000135 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.comd230e3e2011-12-05 20:49:37 +0000144
145 const Rec data[] = {
bungeman@google.comf8aa18c2012-03-19 21:04:52 +0000146 { 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.comd230e3e2011-12-05 20:49:37 +0000156 };
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.comcadbcb82012-01-06 19:22:11 +0000168 size_t i, n = SK_ARRAY_COUNT(data);
reed@google.comd230e3e2011-12-05 20:49:37 +0000169
170 for (i = 0; i < n; ++i) {
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000171 for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000172 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.comcadbcb82012-01-06 19:22:11 +0000180 for (size_t j = 0; j < n; ++j) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000181 const Rec& rec1 = data[j];
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000182 for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000183 IsFiniteProc1 proc1 = gProc1[k];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000184
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000185 for (size_t m = 0; m < SK_ARRAY_COUNT(gProc2); ++m) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000186 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.comd6176b02012-08-23 18:14:13 +0000193
reed@google.com30d90eb2012-05-15 14:17:36 +0000194 test_isRectFinite(reporter);
reed@google.comd230e3e2011-12-05 20:49:37 +0000195}
196
robertphillips@google.com706f6212012-05-14 17:51:23 +0000197#if defined _WIN32
198#pragma warning ( pop )
199#endif
200
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000201DEF_TEST(Scalar, reporter) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000202 test_isfinite(reporter);
commit-bot@chromium.org4e332f82014-05-05 16:04:42 +0000203 test_roundtoint(reporter);
reed@google.comd230e3e2011-12-05 20:49:37 +0000204}