blob: 47d3226343d5de165466f56e2c06f6669635551a [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"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@google.comd230e3e2011-12-05 20:49:37 +000010#include "SkFloatingPoint.h"
11#include "SkMath.h"
12#include "SkPoint.h"
13#include "SkRandom.h"
reed@google.com30d90eb2012-05-15 14:17:36 +000014#include "SkRect.h"
reed@google.comd230e3e2011-12-05 20:49:37 +000015
reed@google.com30d90eb2012-05-15 14:17:36 +000016struct PointSet {
17 const SkPoint* fPts;
18 size_t fCount;
19 bool fIsFinite;
20};
21
22static void test_isRectFinite(skiatest::Reporter* reporter) {
reed@google.com415e76a2012-05-15 14:32:42 +000023#ifdef SK_SCALAR_IS_FLOAT
reed@google.com30d90eb2012-05-15 14:17:36 +000024 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 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000057
reed@google.com30d90eb2012-05-15 14:17:36 +000058 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 }
reed@google.com415e76a2012-05-15 14:32:42 +000064#endif
reed@google.com30d90eb2012-05-15 14:17:36 +000065}
66
reed@google.comd230e3e2011-12-05 20:49:37 +000067static bool isFinite_int(float x) {
68 uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
69 int exponent = bits << 1 >> 24;
70 return exponent != 0xFF;
71}
72
73static bool isFinite_float(float x) {
robertphillips@google.com6853e802012-04-16 15:50:18 +000074 return SkToBool(sk_float_isfinite(x));
reed@google.comd230e3e2011-12-05 20:49:37 +000075}
76
77static bool isFinite_mulzero(float x) {
78 float y = x * 0;
79 return y == y;
80}
81
82// return true if the float is finite
83typedef bool (*IsFiniteProc1)(float);
84
85static bool isFinite2_and(float x, float y, IsFiniteProc1 proc) {
86 return proc(x) && proc(y);
87}
88
89static bool isFinite2_mulzeroadd(float x, float y, IsFiniteProc1 proc) {
90 return proc(x * 0 + y * 0);
91}
92
93// return true if both floats are finite
94typedef bool (*IsFiniteProc2)(float, float, IsFiniteProc1);
95
reed@google.com5ae777d2011-12-06 20:18:05 +000096enum FloatClass {
97 kFinite,
98 kInfinite,
99 kNaN
100};
101
102static void test_floatclass(skiatest::Reporter* reporter, float value, FloatClass fc) {
103 // our sk_float_is... function may return int instead of bool,
104 // hence the double ! to turn it into a bool
105 REPORTER_ASSERT(reporter, !!sk_float_isfinite(value) == (fc == kFinite));
106 REPORTER_ASSERT(reporter, !!sk_float_isinf(value) == (fc == kInfinite));
107 REPORTER_ASSERT(reporter, !!sk_float_isnan(value) == (fc == kNaN));
108}
109
robertphillips@google.com706f6212012-05-14 17:51:23 +0000110#if defined _WIN32
111#pragma warning ( push )
112// we are intentionally causing an overflow here
113// (warning C4756: overflow in constant arithmetic)
114#pragma warning ( disable : 4756 )
115#endif
116
reed@google.comd230e3e2011-12-05 20:49:37 +0000117static void test_isfinite(skiatest::Reporter* reporter) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000118 struct Rec {
119 float fValue;
120 bool fIsFinite;
121 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122
reed@google.comd230e3e2011-12-05 20:49:37 +0000123 float max = 3.402823466e+38f;
124 float inf = max * max;
reed@google.com5ae777d2011-12-06 20:18:05 +0000125 float nan = inf * 0;
126
127 test_floatclass(reporter, 0, kFinite);
128 test_floatclass(reporter, max, kFinite);
129 test_floatclass(reporter, -max, kFinite);
130 test_floatclass(reporter, inf, kInfinite);
131 test_floatclass(reporter, -inf, kInfinite);
132 test_floatclass(reporter, nan, kNaN);
133 test_floatclass(reporter, -nan, kNaN);
reed@google.comd230e3e2011-12-05 20:49:37 +0000134
135 const Rec data[] = {
bungeman@google.comf8aa18c2012-03-19 21:04:52 +0000136 { 0, true },
137 { 1, true },
138 { -1, true },
139 { max * 0.75f, true },
140 { max, true },
141 { -max * 0.75f, true },
142 { -max, true },
143 { inf, false },
144 { -inf, false },
145 { nan, false },
reed@google.comd230e3e2011-12-05 20:49:37 +0000146 };
147
148 const IsFiniteProc1 gProc1[] = {
149 isFinite_int,
150 isFinite_float,
151 isFinite_mulzero
152 };
153 const IsFiniteProc2 gProc2[] = {
154 isFinite2_and,
155 isFinite2_mulzeroadd
156 };
157
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000158 size_t i, n = SK_ARRAY_COUNT(data);
reed@google.comd230e3e2011-12-05 20:49:37 +0000159
160 for (i = 0; i < n; ++i) {
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000161 for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000162 const Rec& rec = data[i];
163 bool finite = gProc1[k](rec.fValue);
164 REPORTER_ASSERT(reporter, rec.fIsFinite == finite);
165 }
166 }
167
168 for (i = 0; i < n; ++i) {
169 const Rec& rec0 = data[i];
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000170 for (size_t j = 0; j < n; ++j) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000171 const Rec& rec1 = data[j];
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000172 for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000173 IsFiniteProc1 proc1 = gProc1[k];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000174
bsalomon@google.comcadbcb82012-01-06 19:22:11 +0000175 for (size_t m = 0; m < SK_ARRAY_COUNT(gProc2); ++m) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000176 bool finite = gProc2[m](rec0.fValue, rec1.fValue, proc1);
177 bool finite2 = rec0.fIsFinite && rec1.fIsFinite;
178 REPORTER_ASSERT(reporter, finite2 == finite);
179 }
180 }
181 }
182 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000183
reed@google.com30d90eb2012-05-15 14:17:36 +0000184 test_isRectFinite(reporter);
reed@google.comd230e3e2011-12-05 20:49:37 +0000185}
186
robertphillips@google.com706f6212012-05-14 17:51:23 +0000187#if defined _WIN32
188#pragma warning ( pop )
189#endif
190
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000191DEF_TEST(Scalar, reporter) {
reed@google.comd230e3e2011-12-05 20:49:37 +0000192 test_isfinite(reporter);
193}