blob: 524d7755493560f1df5971f1e051a5e20fa124a1 [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
8#include "Test.h"
9#include "SkFloatingPoint.h"
10#include "SkMath.h"
11#include "SkPoint.h"
12#include "SkRandom.h"
reed@google.com30d90eb2012-05-15 14:17:36 +000013#include "SkRect.h"
reed@google.comd230e3e2011-12-05 20:49:37 +000014
15#ifdef SK_CAN_USE_FLOAT
16
reed@google.com30d90eb2012-05-15 14:17:36 +000017struct PointSet {
18 const SkPoint* fPts;
19 size_t fCount;
20 bool fIsFinite;
21};
22
23static void test_isRectFinite(skiatest::Reporter* reporter) {
24 static const SkPoint gF0[] = {
25 { 0, 0 }, { 1, 1 }
26 };
27 static const SkPoint gF1[] = {
28 { 0, 0 }, { 1, 1 }, { 99.234f, -42342 }
29 };
30
31 static const SkPoint gI0[] = {
32 { 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { SK_ScalarNaN, 3 }, { 2, 3 },
33 };
34 static const SkPoint gI1[] = {
35 { 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { 3, SK_ScalarNaN }, { 2, 3 },
36 };
37 static const SkPoint gI2[] = {
38 { 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { SK_ScalarInfinity, 3 }, { 2, 3 },
39 };
40 static const SkPoint gI3[] = {
41 { 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { 3, SK_ScalarInfinity }, { 2, 3 },
42 };
43
44 static const struct {
45 const SkPoint* fPts;
46 size_t fCount;
47 bool fIsFinite;
48 } gSets[] = {
49 { gF0, SK_ARRAY_COUNT(gF0), true },
50 { gF1, SK_ARRAY_COUNT(gF1), true },
51
52 { gI0, SK_ARRAY_COUNT(gI0), false },
53 { gI1, SK_ARRAY_COUNT(gI1), false },
54 { gI2, SK_ARRAY_COUNT(gI2), false },
55 { gI3, SK_ARRAY_COUNT(gI3), false },
56 };
57
58 for (size_t i = 0; i < SK_ARRAY_COUNT(gSets); ++i) {
59 SkRect r;
60 r.set(gSets[i].fPts, gSets[i].fCount);
61 bool rectIsFinite = !r.isEmpty();
62 REPORTER_ASSERT(reporter, gSets[i].fIsFinite == rectIsFinite);
63 }
64}
65
reed@google.comd230e3e2011-12-05 20:49:37 +000066static bool isFinite_int(float x) {
67 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
68 int exponent = bits << 1 >> 24;
69 return exponent != 0xFF;
70}
71
72static bool isFinite_float(float x) {
robertphillips@google.com6853e802012-04-16 15:50:18 +000073 return SkToBool(sk_float_isfinite(x));
reed@google.comd230e3e2011-12-05 20:49:37 +000074}
75
76static bool isFinite_mulzero(float x) {
77 float y = x * 0;
78 return y == y;
79}
80
81// return true if the float is finite
82typedef bool (*IsFiniteProc1)(float);
83
84static bool isFinite2_and(float x, float y, IsFiniteProc1 proc) {
85 return proc(x) && proc(y);
86}
87
88static bool isFinite2_mulzeroadd(float x, float y, IsFiniteProc1 proc) {
89 return proc(x * 0 + y * 0);
90}
91
92// return true if both floats are finite
93typedef bool (*IsFiniteProc2)(float, float, IsFiniteProc1);
94
95#endif
96
reed@google.com5ae777d2011-12-06 20:18:05 +000097enum FloatClass {
98 kFinite,
99 kInfinite,
100 kNaN
101};
102
103static void test_floatclass(skiatest::Reporter* reporter, float value, FloatClass fc) {
104 // our sk_float_is... function may return int instead of bool,
105 // hence the double ! to turn it into a bool
106 REPORTER_ASSERT(reporter, !!sk_float_isfinite(value) == (fc == kFinite));
107 REPORTER_ASSERT(reporter, !!sk_float_isinf(value) == (fc == kInfinite));
108 REPORTER_ASSERT(reporter, !!sk_float_isnan(value) == (fc == kNaN));
109}
110
robertphillips@google.com706f6212012-05-14 17:51:23 +0000111#if defined _WIN32
112#pragma warning ( push )
113// we are intentionally causing an overflow here
114// (warning C4756: overflow in constant arithmetic)
115#pragma warning ( disable : 4756 )
116#endif
117
reed@google.comd230e3e2011-12-05 20:49:37 +0000118static void test_isfinite(skiatest::Reporter* reporter) {
119#ifdef SK_CAN_USE_FLOAT
120 struct Rec {
121 float fValue;
122 bool fIsFinite;
123 };
124
125 float max = 3.402823466e+38f;
126 float inf = max * max;
reed@google.com5ae777d2011-12-06 20:18:05 +0000127 float nan = inf * 0;
128
129 test_floatclass(reporter, 0, kFinite);
130 test_floatclass(reporter, max, kFinite);
131 test_floatclass(reporter, -max, kFinite);
132 test_floatclass(reporter, inf, kInfinite);
133 test_floatclass(reporter, -inf, kInfinite);
134 test_floatclass(reporter, nan, kNaN);
135 test_floatclass(reporter, -nan, kNaN);
reed@google.comd230e3e2011-12-05 20:49:37 +0000136
137 const Rec data[] = {
bungeman@google.comf8aa18c2012-03-19 21:04:52 +0000138 { 0, true },
139 { 1, true },
140 { -1, true },
141 { max * 0.75f, true },
142 { max, true },
143 { -max * 0.75f, true },
144 { -max, true },
145 { inf, false },
146 { -inf, false },
147 { nan, false },
reed@google.comd230e3e2011-12-05 20:49:37 +0000148 };
149
150 const IsFiniteProc1 gProc1[] = {
151 isFinite_int,
152 isFinite_float,
153 isFinite_mulzero
154 };
155 const IsFiniteProc2 gProc2[] = {
156 isFinite2_and,
157 isFinite2_mulzeroadd
158 };
159
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000160 size_t i, n = SK_ARRAY_COUNT(data);
reed@google.comd230e3e2011-12-05 20:49:37 +0000161
162 for (i = 0; i < n; ++i) {
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000163 for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000164 const Rec& rec = data[i];
165 bool finite = gProc1[k](rec.fValue);
166 REPORTER_ASSERT(reporter, rec.fIsFinite == finite);
167 }
168 }
169
170 for (i = 0; i < n; ++i) {
171 const Rec& rec0 = data[i];
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000172 for (size_t j = 0; j < n; ++j) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000173 const Rec& rec1 = data[j];
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000174 for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000175 IsFiniteProc1 proc1 = gProc1[k];
176
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000177 for (size_t m = 0; m < SK_ARRAY_COUNT(gProc2); ++m) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000178 bool finite = gProc2[m](rec0.fValue, rec1.fValue, proc1);
179 bool finite2 = rec0.fIsFinite && rec1.fIsFinite;
180 REPORTER_ASSERT(reporter, finite2 == finite);
181 }
182 }
183 }
184 }
reed@google.com30d90eb2012-05-15 14:17:36 +0000185
186 test_isRectFinite(reporter);
reed@google.comd230e3e2011-12-05 20:49:37 +0000187#endif
188}
189
robertphillips@google.com706f6212012-05-14 17:51:23 +0000190#if defined _WIN32
191#pragma warning ( pop )
192#endif
193
reed@google.comd230e3e2011-12-05 20:49:37 +0000194static void TestScalar(skiatest::Reporter* reporter) {
195 test_isfinite(reporter);
196}
197
198#include "TestClassDef.h"
199DEFINE_TESTCLASS("Scalar", TestScalarClass, TestScalar)
200