2011-12-05 20:49:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMath.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/private/SkFloatingPoint.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "tests/Test.h"
|
2011-12-05 20:49:37 +00:00
|
|
|
|
2014-05-05 16:04:42 +00:00
|
|
|
static void test_roundtoint(skiatest::Reporter* reporter) {
|
2014-05-05 16:32:16 +00:00
|
|
|
SkScalar x = 0.49999997f;
|
2014-05-05 16:04:42 +00:00
|
|
|
int ix = SkScalarRoundToInt(x);
|
|
|
|
// We "should" get 0, since x < 0.5, but we don't due to float addition rounding up the low
|
|
|
|
// bit after adding 0.5.
|
|
|
|
REPORTER_ASSERT(reporter, 1 == ix);
|
|
|
|
|
|
|
|
// This version explicitly performs the +0.5 step using double, which should avoid losing the
|
|
|
|
// low bits.
|
|
|
|
ix = SkDScalarRoundToInt(x);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == ix);
|
|
|
|
}
|
|
|
|
|
2012-05-15 14:17:36 +00:00
|
|
|
struct PointSet {
|
|
|
|
const SkPoint* fPts;
|
|
|
|
size_t fCount;
|
|
|
|
bool fIsFinite;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_isRectFinite(skiatest::Reporter* reporter) {
|
|
|
|
static const SkPoint gF0[] = {
|
|
|
|
{ 0, 0 }, { 1, 1 }
|
|
|
|
};
|
|
|
|
static const SkPoint gF1[] = {
|
|
|
|
{ 0, 0 }, { 1, 1 }, { 99.234f, -42342 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SkPoint gI0[] = {
|
|
|
|
{ 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { SK_ScalarNaN, 3 }, { 2, 3 },
|
|
|
|
};
|
|
|
|
static const SkPoint gI1[] = {
|
|
|
|
{ 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { 3, SK_ScalarNaN }, { 2, 3 },
|
|
|
|
};
|
|
|
|
static const SkPoint gI2[] = {
|
|
|
|
{ 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { SK_ScalarInfinity, 3 }, { 2, 3 },
|
|
|
|
};
|
|
|
|
static const SkPoint gI3[] = {
|
|
|
|
{ 0, 0 }, { 1, 1 }, { 99.234f, -42342 }, { 3, SK_ScalarInfinity }, { 2, 3 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const SkPoint* fPts;
|
2014-12-12 23:11:17 +00:00
|
|
|
int fCount;
|
2012-05-15 14:17:36 +00:00
|
|
|
bool fIsFinite;
|
|
|
|
} gSets[] = {
|
|
|
|
{ gF0, SK_ARRAY_COUNT(gF0), true },
|
|
|
|
{ gF1, SK_ARRAY_COUNT(gF1), true },
|
|
|
|
|
|
|
|
{ gI0, SK_ARRAY_COUNT(gI0), false },
|
|
|
|
{ gI1, SK_ARRAY_COUNT(gI1), false },
|
|
|
|
{ gI2, SK_ARRAY_COUNT(gI2), false },
|
|
|
|
{ gI3, SK_ARRAY_COUNT(gI3), false },
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-15 14:17:36 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gSets); ++i) {
|
|
|
|
SkRect r;
|
|
|
|
r.set(gSets[i].fPts, gSets[i].fCount);
|
|
|
|
bool rectIsFinite = !r.isEmpty();
|
|
|
|
REPORTER_ASSERT(reporter, gSets[i].fIsFinite == rectIsFinite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-05 20:49:37 +00:00
|
|
|
static bool isFinite_int(float x) {
|
|
|
|
uint32_t bits = SkFloat2Bits(x); // need unsigned for our shifts
|
|
|
|
int exponent = bits << 1 >> 24;
|
|
|
|
return exponent != 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isFinite_float(float x) {
|
2012-04-16 15:50:18 +00:00
|
|
|
return SkToBool(sk_float_isfinite(x));
|
2011-12-05 20:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isFinite_mulzero(float x) {
|
|
|
|
float y = x * 0;
|
|
|
|
return y == y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if the float is finite
|
|
|
|
typedef bool (*IsFiniteProc1)(float);
|
|
|
|
|
|
|
|
static bool isFinite2_and(float x, float y, IsFiniteProc1 proc) {
|
|
|
|
return proc(x) && proc(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isFinite2_mulzeroadd(float x, float y, IsFiniteProc1 proc) {
|
|
|
|
return proc(x * 0 + y * 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if both floats are finite
|
|
|
|
typedef bool (*IsFiniteProc2)(float, float, IsFiniteProc1);
|
|
|
|
|
2011-12-06 20:18:05 +00:00
|
|
|
enum FloatClass {
|
|
|
|
kFinite,
|
|
|
|
kInfinite,
|
|
|
|
kNaN
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_floatclass(skiatest::Reporter* reporter, float value, FloatClass fc) {
|
|
|
|
// our sk_float_is... function may return int instead of bool,
|
|
|
|
// hence the double ! to turn it into a bool
|
|
|
|
REPORTER_ASSERT(reporter, !!sk_float_isfinite(value) == (fc == kFinite));
|
|
|
|
REPORTER_ASSERT(reporter, !!sk_float_isinf(value) == (fc == kInfinite));
|
|
|
|
REPORTER_ASSERT(reporter, !!sk_float_isnan(value) == (fc == kNaN));
|
|
|
|
}
|
|
|
|
|
2012-05-14 17:51:23 +00:00
|
|
|
#if defined _WIN32
|
|
|
|
#pragma warning ( push )
|
|
|
|
// we are intentionally causing an overflow here
|
|
|
|
// (warning C4756: overflow in constant arithmetic)
|
|
|
|
#pragma warning ( disable : 4756 )
|
|
|
|
#endif
|
|
|
|
|
2011-12-05 20:49:37 +00:00
|
|
|
static void test_isfinite(skiatest::Reporter* reporter) {
|
|
|
|
struct Rec {
|
|
|
|
float fValue;
|
|
|
|
bool fIsFinite;
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-12-05 20:49:37 +00:00
|
|
|
float max = 3.402823466e+38f;
|
|
|
|
float inf = max * max;
|
2011-12-06 20:18:05 +00:00
|
|
|
float nan = inf * 0;
|
|
|
|
|
|
|
|
test_floatclass(reporter, 0, kFinite);
|
|
|
|
test_floatclass(reporter, max, kFinite);
|
|
|
|
test_floatclass(reporter, -max, kFinite);
|
|
|
|
test_floatclass(reporter, inf, kInfinite);
|
|
|
|
test_floatclass(reporter, -inf, kInfinite);
|
|
|
|
test_floatclass(reporter, nan, kNaN);
|
|
|
|
test_floatclass(reporter, -nan, kNaN);
|
2011-12-05 20:49:37 +00:00
|
|
|
|
|
|
|
const Rec data[] = {
|
2012-03-19 21:04:52 +00:00
|
|
|
{ 0, true },
|
|
|
|
{ 1, true },
|
|
|
|
{ -1, true },
|
|
|
|
{ max * 0.75f, true },
|
|
|
|
{ max, true },
|
|
|
|
{ -max * 0.75f, true },
|
|
|
|
{ -max, true },
|
|
|
|
{ inf, false },
|
|
|
|
{ -inf, false },
|
|
|
|
{ nan, false },
|
2011-12-05 20:49:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const IsFiniteProc1 gProc1[] = {
|
|
|
|
isFinite_int,
|
|
|
|
isFinite_float,
|
|
|
|
isFinite_mulzero
|
|
|
|
};
|
|
|
|
const IsFiniteProc2 gProc2[] = {
|
|
|
|
isFinite2_and,
|
|
|
|
isFinite2_mulzeroadd
|
|
|
|
};
|
|
|
|
|
2012-01-06 19:22:11 +00:00
|
|
|
size_t i, n = SK_ARRAY_COUNT(data);
|
2011-12-05 20:49:37 +00:00
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
2012-01-06 19:22:11 +00:00
|
|
|
for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) {
|
2011-12-05 20:49:37 +00:00
|
|
|
const Rec& rec = data[i];
|
|
|
|
bool finite = gProc1[k](rec.fValue);
|
|
|
|
REPORTER_ASSERT(reporter, rec.fIsFinite == finite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
const Rec& rec0 = data[i];
|
2012-01-06 19:22:11 +00:00
|
|
|
for (size_t j = 0; j < n; ++j) {
|
2011-12-05 20:49:37 +00:00
|
|
|
const Rec& rec1 = data[j];
|
2012-01-06 19:22:11 +00:00
|
|
|
for (size_t k = 0; k < SK_ARRAY_COUNT(gProc1); ++k) {
|
2011-12-05 20:49:37 +00:00
|
|
|
IsFiniteProc1 proc1 = gProc1[k];
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-01-06 19:22:11 +00:00
|
|
|
for (size_t m = 0; m < SK_ARRAY_COUNT(gProc2); ++m) {
|
2011-12-05 20:49:37 +00:00
|
|
|
bool finite = gProc2[m](rec0.fValue, rec1.fValue, proc1);
|
|
|
|
bool finite2 = rec0.fIsFinite && rec1.fIsFinite;
|
|
|
|
REPORTER_ASSERT(reporter, finite2 == finite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-15 14:17:36 +00:00
|
|
|
test_isRectFinite(reporter);
|
2011-12-05 20:49:37 +00:00
|
|
|
}
|
|
|
|
|
2012-05-14 17:51:23 +00:00
|
|
|
#if defined _WIN32
|
|
|
|
#pragma warning ( pop )
|
|
|
|
#endif
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Scalar, reporter) {
|
2011-12-05 20:49:37 +00:00
|
|
|
test_isfinite(reporter);
|
2014-05-05 16:04:42 +00:00
|
|
|
test_roundtoint(reporter);
|
2011-12-05 20:49:37 +00:00
|
|
|
}
|