2011-07-28 14:26:00 +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.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2017-09-15 15:59:23 +00:00
|
|
|
#include "SkColorData.h"
|
2013-12-12 21:11:12 +00:00
|
|
|
#include "SkEndian.h"
|
2016-11-18 15:18:15 +00:00
|
|
|
#include "SkFDot6.h"
|
2016-04-07 15:49:31 +00:00
|
|
|
#include "SkFixed.h"
|
2014-11-26 21:15:59 +00:00
|
|
|
#include "SkHalf.h"
|
2012-08-07 21:35:13 +00:00
|
|
|
#include "SkMathPriv.h"
|
2009-02-27 16:24:51 +00:00
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRandom.h"
|
2018-06-13 13:59:02 +00:00
|
|
|
#include "SkTo.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2009-02-27 16:24:51 +00:00
|
|
|
|
2013-04-29 14:18:23 +00:00
|
|
|
static void test_clz(skiatest::Reporter* reporter) {
|
|
|
|
REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
|
|
|
|
REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
|
|
|
|
REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
|
2013-04-29 14:43:50 +00:00
|
|
|
REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
|
2013-04-30 07:01:03 +00:00
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-04-29 14:18:23 +00:00
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
uint32_t mask = rand.nextU();
|
2013-04-29 15:27:42 +00:00
|
|
|
// need to get some zeros for testing, but in some obscure way so the
|
|
|
|
// compiler won't "see" that, and work-around calling the functions.
|
|
|
|
mask >>= (mask & 31);
|
2013-04-29 14:18:23 +00:00
|
|
|
int intri = SkCLZ(mask);
|
|
|
|
int porta = SkCLZ_portable(mask);
|
|
|
|
REPORTER_ASSERT(reporter, intri == porta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 15:18:15 +00:00
|
|
|
static void test_quick_div(skiatest::Reporter* reporter) {
|
|
|
|
/*
|
|
|
|
The inverse table is generated by turning on SkDebugf in the following test code
|
|
|
|
*/
|
|
|
|
SkFixed storage[kInverseTableSize * 2];
|
|
|
|
SkFixed* table = storage + kInverseTableSize;
|
|
|
|
|
|
|
|
// SkDebugf("static const int gFDot6INVERSE[] = {");
|
|
|
|
for (SkFDot6 i=-kInverseTableSize; i<kInverseTableSize; i++) {
|
|
|
|
if (i != 0) {
|
|
|
|
table[i] = SkFDot6Div(SK_FDot6One, i);
|
|
|
|
REPORTER_ASSERT(reporter, table[i] == gFDot6INVERSE[i + kInverseTableSize]);
|
|
|
|
}
|
|
|
|
// SkDebugf("%d, ", table[i]);
|
|
|
|
}
|
|
|
|
// SkDebugf("}\n");
|
|
|
|
|
|
|
|
|
|
|
|
for (SkFDot6 a = -1024; a <= 1024; a++) {
|
|
|
|
for (SkFDot6 b = -1024; b <= 1024; b++) {
|
|
|
|
if (b != 0) {
|
|
|
|
SkFixed ourAnswer = QuickSkFDot6Div(a, b);
|
|
|
|
SkFixed directAnswer = SkFDot6Div(a, b);
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
(directAnswer == 0 && ourAnswer == 0) ||
|
|
|
|
SkFixedDiv(SkAbs32(directAnswer - ourAnswer), SkAbs32(directAnswer)) <= 1 << 10
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-29 14:18:23 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-05-30 12:30:09 +00:00
|
|
|
static float sk_fsel(float pred, float result_ge, float result_lt) {
|
|
|
|
return pred >= 0 ? result_ge : result_lt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float fast_floor(float x) {
|
2012-05-30 13:48:14 +00:00
|
|
|
// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
|
|
|
|
float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
|
2012-05-30 15:19:17 +00:00
|
|
|
return (float)(x + big) - big;
|
2012-05-30 12:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static float std_floor(float x) {
|
|
|
|
return sk_float_floor(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_floor_value(skiatest::Reporter* reporter, float value) {
|
|
|
|
float fast = fast_floor(value);
|
|
|
|
float std = std_floor(value);
|
2016-02-25 01:59:16 +00:00
|
|
|
if (std != fast) {
|
|
|
|
ERRORF(reporter, "fast_floor(%.9g) == %.9g != %.9g == std_floor(%.9g)",
|
|
|
|
value, fast, std, value);
|
|
|
|
}
|
2012-05-30 12:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_floor(skiatest::Reporter* reporter) {
|
|
|
|
static const float gVals[] = {
|
|
|
|
0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-05-30 12:30:09 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
|
|
|
|
test_floor_value(reporter, gVals[i]);
|
|
|
|
// test_floor_value(reporter, -gVals[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-04-22 20:21:56 +00:00
|
|
|
// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
|
|
|
|
static void test_muldivround(skiatest::Reporter* reporter) {
|
|
|
|
#if 0
|
|
|
|
// this "complete" test is too slow, so we test a random sampling of it
|
|
|
|
|
|
|
|
for (int a = 0; a <= 32767; ++a) {
|
|
|
|
for (int b = 0; b <= 32767; ++b) {
|
|
|
|
unsigned prod0 = SkMul16ShiftRound(a, b, 8);
|
|
|
|
unsigned prod1 = SkMulDiv255Round(a, b);
|
|
|
|
SkASSERT(prod0 == prod1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-04-22 20:21:56 +00:00
|
|
|
for (int i = 0; i < 10000; ++i) {
|
|
|
|
unsigned a = rand.nextU() & 0x7FFF;
|
|
|
|
unsigned b = rand.nextU() & 0x7FFF;
|
|
|
|
|
|
|
|
unsigned prod0 = SkMul16ShiftRound(a, b, 8);
|
|
|
|
unsigned prod1 = SkMulDiv255Round(a, b);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, prod0 == prod1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-06 20:04:36 +00:00
|
|
|
static float float_blend(int src, int dst, float unit) {
|
|
|
|
return dst + (src - dst) * unit;
|
2011-03-30 22:34:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-09 17:12:08 +00:00
|
|
|
static int blend31(int src, int dst, int a31) {
|
|
|
|
return dst + ((src - dst) * a31 * 2114 >> 16);
|
|
|
|
// return dst + ((src - dst) * a31 * 33 >> 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int blend31_slow(int src, int dst, int a31) {
|
|
|
|
int prod = src * a31 + (31 - a31) * dst + 16;
|
|
|
|
prod = (prod + (prod >> 5)) >> 5;
|
|
|
|
return prod;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int blend31_round(int src, int dst, int a31) {
|
|
|
|
int prod = (src - dst) * a31 + 16;
|
|
|
|
prod = (prod + (prod >> 5)) >> 5;
|
|
|
|
return dst + prod;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int blend31_old(int src, int dst, int a31) {
|
|
|
|
a31 += a31 >> 4;
|
|
|
|
return dst + ((src - dst) * a31 >> 5);
|
|
|
|
}
|
|
|
|
|
2012-06-06 12:03:39 +00:00
|
|
|
// suppress unused code warning
|
|
|
|
static int (*blend_functions[])(int, int, int) = {
|
|
|
|
blend31,
|
|
|
|
blend31_slow,
|
|
|
|
blend31_round,
|
|
|
|
blend31_old
|
|
|
|
};
|
|
|
|
|
2011-11-09 17:12:08 +00:00
|
|
|
static void test_blend31() {
|
|
|
|
int failed = 0;
|
|
|
|
int death = 0;
|
2012-06-06 12:03:39 +00:00
|
|
|
if (false) { // avoid bit rot, suppress warning
|
|
|
|
failed = (*blend_functions[0])(0,0,0);
|
|
|
|
}
|
2011-11-09 17:12:08 +00:00
|
|
|
for (int src = 0; src <= 255; src++) {
|
|
|
|
for (int dst = 0; dst <= 255; dst++) {
|
|
|
|
for (int a = 0; a <= 31; a++) {
|
|
|
|
// int r0 = blend31(src, dst, a);
|
|
|
|
// int r0 = blend31_round(src, dst, a);
|
|
|
|
// int r0 = blend31_old(src, dst, a);
|
|
|
|
int r0 = blend31_slow(src, dst, a);
|
|
|
|
|
|
|
|
float f = float_blend(src, dst, a / 31.f);
|
|
|
|
int r1 = (int)f;
|
2013-11-25 19:44:07 +00:00
|
|
|
int r2 = SkScalarRoundToInt(f);
|
2011-11-09 17:12:08 +00:00
|
|
|
|
|
|
|
if (r0 != r1 && r0 != r2) {
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
|
2016-02-25 01:59:16 +00:00
|
|
|
src, dst, a, r0, f);
|
2011-11-09 17:12:08 +00:00
|
|
|
failed += 1;
|
|
|
|
}
|
|
|
|
if (r0 > 255) {
|
|
|
|
death += 1;
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
|
|
|
|
src, dst, a, r0, f);
|
2011-11-09 17:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkDebugf("---- failed %d death %d\n", failed, death);
|
|
|
|
}
|
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
static void check_length(skiatest::Reporter* reporter,
|
|
|
|
const SkPoint& p, SkScalar targetLen) {
|
|
|
|
float x = SkScalarToFloat(p.fX);
|
|
|
|
float y = SkScalarToFloat(p.fY);
|
|
|
|
float len = sk_float_sqrt(x*x + y*y);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
len /= SkScalarToFloat(targetLen);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
|
|
|
|
}
|
|
|
|
|
2011-02-08 21:56:39 +00:00
|
|
|
static void unittest_isfinite(skiatest::Reporter* reporter) {
|
2011-06-08 18:26:08 +00:00
|
|
|
float nan = sk_float_asin(2);
|
2018-05-14 20:51:32 +00:00
|
|
|
float inf = SK_ScalarInfinity;
|
2012-04-10 17:42:21 +00:00
|
|
|
float big = 3.40282e+038f;
|
2011-02-08 21:56:39 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
|
2011-02-09 02:24:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
|
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
|
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
|
2011-02-08 21:56:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
|
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
|
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-08 21:56:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
|
|
|
|
}
|
|
|
|
|
2014-11-26 21:15:59 +00:00
|
|
|
static void unittest_half(skiatest::Reporter* reporter) {
|
|
|
|
static const float gFloats[] = {
|
|
|
|
0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
|
|
|
|
-0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
|
|
|
|
SkHalf h = SkFloatToHalf(gFloats[i]);
|
|
|
|
float f = SkHalfToFloat(h);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// check some special values
|
|
|
|
union FloatUnion {
|
|
|
|
uint32_t fU;
|
|
|
|
float fF;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
|
|
|
|
SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
|
|
|
|
float f = SkHalfToFloat(h);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
|
|
|
|
|
|
|
|
static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
|
|
|
|
h = SkFloatToHalf(largestNegativeHalf.fF);
|
|
|
|
f = SkHalfToFloat(h);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
|
|
|
|
|
|
|
|
static const FloatUnion smallestPositiveHalf = { 102 << 23 };
|
|
|
|
h = SkFloatToHalf(smallestPositiveHalf.fF);
|
|
|
|
f = SkHalfToFloat(h);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
|
|
|
|
|
|
|
|
static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
|
|
|
|
h = SkFloatToHalf(overflowHalf.fF);
|
|
|
|
f = SkHalfToFloat(h);
|
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
|
|
|
|
|
|
|
|
static const FloatUnion underflowHalf = { 101 << 23 };
|
|
|
|
h = SkFloatToHalf(underflowHalf.fF);
|
|
|
|
f = SkHalfToFloat(h);
|
|
|
|
REPORTER_ASSERT(reporter, f == 0.0f );
|
|
|
|
|
|
|
|
static const FloatUnion inf32 = { 255 << 23 };
|
|
|
|
h = SkFloatToHalf(inf32.fF);
|
|
|
|
f = SkHalfToFloat(h);
|
|
|
|
REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
|
|
|
|
|
|
|
|
static const FloatUnion nan32 = { 255 << 23 | 1 };
|
|
|
|
h = SkFloatToHalf(nan32.fF);
|
|
|
|
f = SkHalfToFloat(h);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-26 15:40:30 +00:00
|
|
|
template <typename RSqrtFn>
|
|
|
|
static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
|
2015-07-23 18:14:29 +00:00
|
|
|
const float maxRelativeError = 6.50196699e-4f;
|
|
|
|
|
|
|
|
// test close to 0 up to 1
|
|
|
|
float input = 0.000001f;
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
float exact = 1.0f/sk_float_sqrt(input);
|
2016-01-26 15:40:30 +00:00
|
|
|
float estimate = rsqrt(input);
|
2015-07-23 18:14:29 +00:00
|
|
|
float relativeError = sk_float_abs(exact - estimate)/exact;
|
|
|
|
REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
|
|
|
|
input += 0.001f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 1 to ~100
|
|
|
|
input = 1.0f;
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
float exact = 1.0f/sk_float_sqrt(input);
|
2016-01-26 15:40:30 +00:00
|
|
|
float estimate = rsqrt(input);
|
2015-07-23 18:14:29 +00:00
|
|
|
float relativeError = sk_float_abs(exact - estimate)/exact;
|
|
|
|
REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
|
|
|
|
input += 0.01f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// test some big numbers
|
|
|
|
input = 1000000.0f;
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
float exact = 1.0f/sk_float_sqrt(input);
|
2016-01-26 15:40:30 +00:00
|
|
|
float estimate = rsqrt(input);
|
2015-07-23 18:14:29 +00:00
|
|
|
float relativeError = sk_float_abs(exact - estimate)/exact;
|
|
|
|
REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
|
|
|
|
input += 754326.f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
static void test_muldiv255(skiatest::Reporter* reporter) {
|
|
|
|
for (int a = 0; a <= 255; a++) {
|
|
|
|
for (int b = 0; b <= 255; b++) {
|
|
|
|
int ab = a * b;
|
|
|
|
float s = ab / 255.0f;
|
|
|
|
int round = (int)floorf(s + 0.5f);
|
|
|
|
int trunc = (int)floorf(s);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
int iround = SkMulDiv255Round(a, b);
|
|
|
|
int itrunc = SkMulDiv255Trunc(a, b);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, iround == round);
|
|
|
|
REPORTER_ASSERT(reporter, itrunc == trunc);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, itrunc <= iround);
|
|
|
|
REPORTER_ASSERT(reporter, iround <= a);
|
|
|
|
REPORTER_ASSERT(reporter, iround <= b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-07 21:07:56 +00:00
|
|
|
static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
|
|
|
|
for (int c = 0; c <= 255; c++) {
|
|
|
|
for (int a = 0; a <= 255; a++) {
|
|
|
|
int product = (c * a + 255);
|
|
|
|
int expected_ceiling = (product + (product >> 8)) >> 8;
|
|
|
|
int webkit_ceiling = (c * a + 254) / 255;
|
|
|
|
REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
|
|
|
|
int skia_ceiling = SkMulDiv255Ceiling(c, a);
|
|
|
|
REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-09 19:18:38 +00:00
|
|
|
static void test_copysign(skiatest::Reporter* reporter) {
|
|
|
|
static const int32_t gTriples[] = {
|
|
|
|
// x, y, expected result
|
|
|
|
0, 0, 0,
|
|
|
|
0, 1, 0,
|
|
|
|
0, -1, 0,
|
|
|
|
1, 0, 1,
|
|
|
|
1, 1, 1,
|
|
|
|
1, -1, -1,
|
|
|
|
-1, 0, 1,
|
|
|
|
-1, 1, 1,
|
|
|
|
-1, -1, -1,
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
|
|
|
|
float x = (float)gTriples[i];
|
|
|
|
float y = (float)gTriples[i+1];
|
|
|
|
float expected = (float)gTriples[i+2];
|
|
|
|
REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2010-02-09 19:18:38 +00:00
|
|
|
for (int j = 0; j < 1000; j++) {
|
|
|
|
int ix = rand.nextS();
|
|
|
|
REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
|
|
|
|
REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
|
|
|
|
REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
|
|
|
|
REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
|
|
|
|
|
|
|
|
SkScalar sx = rand.nextSScalar1();
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:51:32 +00:00
|
|
|
static void huge_vector_normalize(skiatest::Reporter* reporter) {
|
|
|
|
// these values should fail (overflow/underflow) trying to normalize
|
|
|
|
const SkVector fail[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ SK_ScalarInfinity, 0 }, { 0, SK_ScalarInfinity },
|
|
|
|
{ 0, SK_ScalarNaN }, { SK_ScalarNaN, 0 },
|
|
|
|
};
|
|
|
|
for (SkVector v : fail) {
|
|
|
|
SkVector v2 = v;
|
|
|
|
if (v2.setLength(1.0f)) {
|
|
|
|
REPORTER_ASSERT(reporter, !v.setLength(1.0f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Math, reporter) {
|
2009-02-27 16:24:51 +00:00
|
|
|
int i;
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
// these should assert
|
|
|
|
#if 0
|
|
|
|
SkToS8(128);
|
|
|
|
SkToS8(-129);
|
|
|
|
SkToU8(256);
|
|
|
|
SkToU8(-5);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
SkToS16(32768);
|
|
|
|
SkToS16(-32769);
|
|
|
|
SkToU16(65536);
|
|
|
|
SkToU16(-5);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
if (sizeof(size_t) > 4) {
|
|
|
|
SkToS32(4*1024*1024);
|
|
|
|
SkToS32(-4*1024*1024);
|
|
|
|
SkToU32(5*1024*1024);
|
|
|
|
SkToU32(-5);
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
test_muldiv255(reporter);
|
2010-12-07 21:07:56 +00:00
|
|
|
test_muldiv255ceiling(reporter);
|
2010-02-09 19:18:38 +00:00
|
|
|
test_copysign(reporter);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
{
|
|
|
|
SkScalar x = SK_ScalarNaN;
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
|
|
|
|
}
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
for (i = 0; i < 1000; i++) {
|
2018-09-05 14:51:00 +00:00
|
|
|
int value = rand.nextS() >> 16;
|
|
|
|
int max = rand.nextU() >> 16;
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
int clamp = SkClampMax(value, max);
|
|
|
|
int clamp2 = value < 0 ? 0 : (value > max ? max : value);
|
|
|
|
REPORTER_ASSERT(reporter, clamp == clamp2);
|
|
|
|
}
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-11-16 14:52:01 +00:00
|
|
|
for (i = 0; i < 10000; i++) {
|
2009-02-27 16:24:51 +00:00
|
|
|
SkPoint p;
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2012-04-10 17:42:21 +00:00
|
|
|
// These random values are being treated as 32-bit-patterns, not as
|
|
|
|
// ints; calling SkIntToScalar() here produces crashes.
|
2012-05-14 16:33:36 +00:00
|
|
|
p.setLength((SkScalar) rand.nextS(),
|
2012-08-23 18:14:13 +00:00
|
|
|
(SkScalar) rand.nextS(),
|
2012-05-14 16:33:36 +00:00
|
|
|
SK_Scalar1);
|
2009-02-27 16:24:51 +00:00
|
|
|
check_length(reporter, p, SK_Scalar1);
|
2012-05-14 16:33:36 +00:00
|
|
|
p.setLength((SkScalar) (rand.nextS() >> 13),
|
2012-08-23 18:14:13 +00:00
|
|
|
(SkScalar) (rand.nextS() >> 13),
|
2012-05-14 16:33:36 +00:00
|
|
|
SK_Scalar1);
|
2009-02-27 16:24:51 +00:00
|
|
|
check_length(reporter, p, SK_Scalar1);
|
|
|
|
}
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
{
|
|
|
|
SkFixed result = SkFixedDiv(100, 100);
|
|
|
|
REPORTER_ASSERT(reporter, result == SK_Fixed1);
|
|
|
|
result = SkFixedDiv(1, SK_Fixed1);
|
|
|
|
REPORTER_ASSERT(reporter, result == 1);
|
2016-07-13 20:34:46 +00:00
|
|
|
result = SkFixedDiv(10 - 1, SK_Fixed1 * 3);
|
|
|
|
REPORTER_ASSERT(reporter, result == 3);
|
2009-02-27 16:24:51 +00:00
|
|
|
}
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2016-10-20 18:23:09 +00:00
|
|
|
{
|
|
|
|
REPORTER_ASSERT(reporter, (SkFixedRoundToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
|
|
|
|
REPORTER_ASSERT(reporter, (SkFixedFloorToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
|
|
|
|
REPORTER_ASSERT(reporter, (SkFixedCeilToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:51:32 +00:00
|
|
|
huge_vector_normalize(reporter);
|
2011-02-08 21:56:39 +00:00
|
|
|
unittest_isfinite(reporter);
|
2014-11-26 21:15:59 +00:00
|
|
|
unittest_half(reporter);
|
2016-01-26 15:40:30 +00:00
|
|
|
test_rsqrt(reporter, sk_float_rsqrt);
|
|
|
|
test_rsqrt(reporter, sk_float_rsqrt_portable);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-11-16 14:52:01 +00:00
|
|
|
for (i = 0; i < 10000; i++) {
|
2009-02-27 16:24:51 +00:00
|
|
|
SkFixed numer = rand.nextS();
|
|
|
|
SkFixed denom = rand.nextS();
|
|
|
|
SkFixed result = SkFixedDiv(numer, denom);
|
2015-12-09 20:02:30 +00:00
|
|
|
int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
(void)SkCLZ(numer);
|
|
|
|
(void)SkCLZ(denom);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
|
|
|
|
if (check > SK_MaxS32) {
|
|
|
|
check = SK_MaxS32;
|
|
|
|
} else if (check < -SK_MaxS32) {
|
|
|
|
check = SK_MinS32;
|
|
|
|
}
|
2015-03-18 18:32:21 +00:00
|
|
|
if (result != (int32_t)check) {
|
|
|
|
ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
|
|
|
|
}
|
2009-02-27 16:24:51 +00:00
|
|
|
REPORTER_ASSERT(reporter, result == (int32_t)check);
|
|
|
|
}
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2013-01-07 16:47:43 +00:00
|
|
|
if (false) test_floor(reporter);
|
2012-05-30 12:30:09 +00:00
|
|
|
|
2011-11-09 17:12:08 +00:00
|
|
|
// disable for now
|
2012-06-06 12:03:39 +00:00
|
|
|
if (false) test_blend31(); // avoid bit rot, suppress warning
|
2013-04-22 20:21:56 +00:00
|
|
|
|
|
|
|
test_muldivround(reporter);
|
2013-04-29 14:18:23 +00:00
|
|
|
test_clz(reporter);
|
2016-11-18 15:18:15 +00:00
|
|
|
test_quick_div(reporter);
|
2009-02-27 16:24:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 18:06:31 +00:00
|
|
|
template <typename T> struct PairRec {
|
|
|
|
T fYin;
|
|
|
|
T fYang;
|
|
|
|
};
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(TestEndian, reporter) {
|
2013-05-03 18:06:31 +00:00
|
|
|
static const PairRec<uint16_t> g16[] = {
|
|
|
|
{ 0x0, 0x0 },
|
|
|
|
{ 0xFFFF, 0xFFFF },
|
|
|
|
{ 0x1122, 0x2211 },
|
|
|
|
};
|
|
|
|
static const PairRec<uint32_t> g32[] = {
|
|
|
|
{ 0x0, 0x0 },
|
|
|
|
{ 0xFFFFFFFF, 0xFFFFFFFF },
|
|
|
|
{ 0x11223344, 0x44332211 },
|
|
|
|
};
|
|
|
|
static const PairRec<uint64_t> g64[] = {
|
|
|
|
{ 0x0, 0x0 },
|
|
|
|
{ 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
|
|
|
|
{ 0x1122334455667788ULL, 0x8877665544332211ULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
|
|
|
|
REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
|
|
|
|
REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 19:22:54 +00:00
|
|
|
template <typename T>
|
|
|
|
static void test_divmod(skiatest::Reporter* r) {
|
|
|
|
const struct {
|
|
|
|
T numer;
|
|
|
|
T denom;
|
|
|
|
} kEdgeCases[] = {
|
|
|
|
{(T)17, (T)17},
|
|
|
|
{(T)17, (T)4},
|
|
|
|
{(T)0, (T)17},
|
|
|
|
// For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
|
|
|
|
{(T)-17, (T)-17},
|
|
|
|
{(T)-17, (T)4},
|
|
|
|
{(T)17, (T)-4},
|
|
|
|
{(T)-17, (T)-4},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
|
|
|
|
const T numer = kEdgeCases[i].numer;
|
|
|
|
const T denom = kEdgeCases[i].denom;
|
|
|
|
T div, mod;
|
|
|
|
SkTDivMod(numer, denom, &div, &mod);
|
|
|
|
REPORTER_ASSERT(r, numer/denom == div);
|
|
|
|
REPORTER_ASSERT(r, numer%denom == mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRandom rand;
|
|
|
|
for (size_t i = 0; i < 10000; i++) {
|
|
|
|
const T numer = (T)rand.nextS();
|
|
|
|
T denom = 0;
|
|
|
|
while (0 == denom) {
|
|
|
|
denom = (T)rand.nextS();
|
|
|
|
}
|
|
|
|
T div, mod;
|
|
|
|
SkTDivMod(numer, denom, &div, &mod);
|
|
|
|
REPORTER_ASSERT(r, numer/denom == div);
|
|
|
|
REPORTER_ASSERT(r, numer%denom == mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(divmod_u8, r) {
|
|
|
|
test_divmod<uint8_t>(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(divmod_u16, r) {
|
|
|
|
test_divmod<uint16_t>(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(divmod_u32, r) {
|
|
|
|
test_divmod<uint32_t>(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(divmod_u64, r) {
|
|
|
|
test_divmod<uint64_t>(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(divmod_s8, r) {
|
|
|
|
test_divmod<int8_t>(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(divmod_s16, r) {
|
|
|
|
test_divmod<int16_t>(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(divmod_s32, r) {
|
|
|
|
test_divmod<int32_t>(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(divmod_s64, r) {
|
|
|
|
test_divmod<int64_t>(r);
|
|
|
|
}
|
2016-10-28 16:15:03 +00:00
|
|
|
|
|
|
|
static void test_nextsizepow2(skiatest::Reporter* r, size_t test, size_t expectedAns) {
|
|
|
|
size_t ans = GrNextSizePow2(test);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, ans == expectedAns);
|
|
|
|
//SkDebugf("0x%zx -> 0x%zx (0x%zx)\n", test, ans, expectedAns);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(GrNextSizePow2, reporter) {
|
|
|
|
constexpr int kNumSizeTBits = 8 * sizeof(size_t);
|
|
|
|
|
|
|
|
size_t test = 0, expectedAns = 1;
|
|
|
|
|
|
|
|
test_nextsizepow2(reporter, test, expectedAns);
|
|
|
|
|
|
|
|
test = 1; expectedAns = 1;
|
|
|
|
|
|
|
|
for (int i = 1; i < kNumSizeTBits; ++i) {
|
|
|
|
test_nextsizepow2(reporter, test, expectedAns);
|
|
|
|
|
|
|
|
test++;
|
|
|
|
expectedAns <<= 1;
|
|
|
|
|
|
|
|
test_nextsizepow2(reporter, test, expectedAns);
|
|
|
|
|
|
|
|
test = expectedAns;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the remaining three tests there is no higher power (of 2)
|
|
|
|
test = 0x1;
|
|
|
|
test <<= kNumSizeTBits-1;
|
|
|
|
test_nextsizepow2(reporter, test, test);
|
|
|
|
|
|
|
|
test++;
|
|
|
|
test_nextsizepow2(reporter, test, test);
|
|
|
|
|
|
|
|
test_nextsizepow2(reporter, SIZE_MAX, SIZE_MAX);
|
|
|
|
}
|
2017-08-09 15:06:53 +00:00
|
|
|
|
2018-01-31 20:55:47 +00:00
|
|
|
DEF_TEST(FloatSaturate32, reporter) {
|
2017-08-09 15:06:53 +00:00
|
|
|
const struct {
|
|
|
|
float fFloat;
|
|
|
|
int fExpectedInt;
|
|
|
|
} recs[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 100.5f, 100 },
|
|
|
|
{ (float)SK_MaxS32, SK_MaxS32FitsInFloat },
|
|
|
|
{ (float)SK_MinS32, SK_MinS32FitsInFloat },
|
|
|
|
{ SK_MaxS32 * 100.0f, SK_MaxS32FitsInFloat },
|
|
|
|
{ SK_MinS32 * 100.0f, SK_MinS32FitsInFloat },
|
|
|
|
{ SK_ScalarInfinity, SK_MaxS32FitsInFloat },
|
|
|
|
{ SK_ScalarNegativeInfinity, SK_MinS32FitsInFloat },
|
|
|
|
{ SK_ScalarNaN, SK_MaxS32FitsInFloat },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto r : recs) {
|
|
|
|
int i = sk_float_saturate2int(r.fFloat);
|
|
|
|
REPORTER_ASSERT(reporter, r.fExpectedInt == i);
|
2018-03-05 16:49:51 +00:00
|
|
|
|
|
|
|
// ensure that these bound even non-finite values (including NaN)
|
|
|
|
|
|
|
|
SkScalar mx = SkTMax<SkScalar>(r.fFloat, 50);
|
|
|
|
REPORTER_ASSERT(reporter, mx >= 50);
|
|
|
|
|
|
|
|
SkScalar mn = SkTMin<SkScalar>(r.fFloat, 50);
|
|
|
|
REPORTER_ASSERT(reporter, mn <= 50);
|
|
|
|
|
|
|
|
SkScalar p = SkTPin<SkScalar>(r.fFloat, 0, 100);
|
|
|
|
REPORTER_ASSERT(reporter, p >= 0 && p <= 100);
|
2017-08-09 15:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 18:49:35 +00:00
|
|
|
|
2018-01-31 20:55:47 +00:00
|
|
|
DEF_TEST(FloatSaturate64, reporter) {
|
|
|
|
const struct {
|
|
|
|
float fFloat;
|
|
|
|
int64_t fExpected64;
|
|
|
|
} recs[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 100.5f, 100 },
|
|
|
|
{ (float)SK_MaxS64, SK_MaxS64FitsInFloat },
|
|
|
|
{ (float)SK_MinS64, SK_MinS64FitsInFloat },
|
|
|
|
{ SK_MaxS64 * 100.0f, SK_MaxS64FitsInFloat },
|
|
|
|
{ SK_MinS64 * 100.0f, SK_MinS64FitsInFloat },
|
|
|
|
{ SK_ScalarInfinity, SK_MaxS64FitsInFloat },
|
|
|
|
{ SK_ScalarNegativeInfinity, SK_MinS64FitsInFloat },
|
|
|
|
{ SK_ScalarNaN, SK_MaxS64FitsInFloat },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto r : recs) {
|
|
|
|
int64_t i = sk_float_saturate2int64(r.fFloat);
|
|
|
|
REPORTER_ASSERT(reporter, r.fExpected64 == i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(DoubleSaturate32, reporter) {
|
2017-10-10 18:49:35 +00:00
|
|
|
const struct {
|
|
|
|
double fDouble;
|
|
|
|
int fExpectedInt;
|
|
|
|
} recs[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 100.5, 100 },
|
|
|
|
{ SK_MaxS32, SK_MaxS32 },
|
|
|
|
{ SK_MinS32, SK_MinS32 },
|
|
|
|
{ SK_MaxS32 - 1, SK_MaxS32 - 1 },
|
|
|
|
{ SK_MinS32 + 1, SK_MinS32 + 1 },
|
|
|
|
{ SK_MaxS32 * 100.0, SK_MaxS32 },
|
|
|
|
{ SK_MinS32 * 100.0, SK_MinS32 },
|
|
|
|
{ SK_ScalarInfinity, SK_MaxS32 },
|
|
|
|
{ SK_ScalarNegativeInfinity, SK_MinS32 },
|
|
|
|
{ SK_ScalarNaN, SK_MaxS32 },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto r : recs) {
|
|
|
|
int i = sk_double_saturate2int(r.fDouble);
|
|
|
|
REPORTER_ASSERT(reporter, r.fExpectedInt == i);
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 16:34:53 +00:00
|
|
|
|
|
|
|
#if defined(__ARM_NEON)
|
|
|
|
#include <arm_neon.h>
|
|
|
|
|
|
|
|
DEF_TEST(NeonU16Div255, r) {
|
|
|
|
|
|
|
|
for (int v = 0; v <= 255*255; v++) {
|
|
|
|
int want = (v + 127)/255;
|
|
|
|
|
|
|
|
uint16x8_t V = vdupq_n_u16(v);
|
|
|
|
int got = vrshrq_n_u16(vrsraq_n_u16(V, V, 8), 8)[0];
|
|
|
|
|
|
|
|
if (got != want) {
|
|
|
|
SkDebugf("%d -> %d, want %d\n", v, got, want);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, got == want);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|