blob: 0bd3cf5da557e65a49d658f379493bc94769b4a3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
benjaminwagner70f1a6c2016-04-07 09:23:11 -07008#include "float.h"
9
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000010#include "SkColorPriv.h"
11#include "SkEndian.h"
Yuqian Lice1d2932016-11-18 10:18:15 -050012#include "SkFDot6.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070013#include "SkFixed.h"
tomhudson@google.com8afae612012-08-14 15:03:35 +000014#include "SkFloatBits.h"
reed@android.comc846ede2010-04-13 15:29:15 +000015#include "SkFloatingPoint.h"
jvanverth93679922014-11-26 13:15:59 -080016#include "SkHalf.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000017#include "SkMathPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000018#include "SkPoint.h"
19#include "SkRandom.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000020#include "Test.h"
reed@android.comed673312009-02-27 16:24:51 +000021
reed@google.comc21f86f2013-04-29 14:18:23 +000022static void test_clz(skiatest::Reporter* reporter) {
23 REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
24 REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
25 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
reed@google.com7729534d2013-04-29 14:43:50 +000026 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
skia.committer@gmail.com81521132013-04-30 07:01:03 +000027
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000028 SkRandom rand;
reed@google.comc21f86f2013-04-29 14:18:23 +000029 for (int i = 0; i < 1000; ++i) {
30 uint32_t mask = rand.nextU();
reed@google.combc57a292013-04-29 15:27:42 +000031 // need to get some zeros for testing, but in some obscure way so the
32 // compiler won't "see" that, and work-around calling the functions.
33 mask >>= (mask & 31);
reed@google.comc21f86f2013-04-29 14:18:23 +000034 int intri = SkCLZ(mask);
35 int porta = SkCLZ_portable(mask);
36 REPORTER_ASSERT(reporter, intri == porta);
37 }
38}
39
Yuqian Lice1d2932016-11-18 10:18:15 -050040static void test_quick_div(skiatest::Reporter* reporter) {
41 /*
42 The inverse table is generated by turning on SkDebugf in the following test code
43 */
44 SkFixed storage[kInverseTableSize * 2];
45 SkFixed* table = storage + kInverseTableSize;
46
47 // SkDebugf("static const int gFDot6INVERSE[] = {");
48 for (SkFDot6 i=-kInverseTableSize; i<kInverseTableSize; i++) {
49 if (i != 0) {
50 table[i] = SkFDot6Div(SK_FDot6One, i);
51 REPORTER_ASSERT(reporter, table[i] == gFDot6INVERSE[i + kInverseTableSize]);
52 }
53 // SkDebugf("%d, ", table[i]);
54 }
55 // SkDebugf("}\n");
56
57
58 for (SkFDot6 a = -1024; a <= 1024; a++) {
59 for (SkFDot6 b = -1024; b <= 1024; b++) {
60 if (b != 0) {
61 SkFixed ourAnswer = QuickSkFDot6Div(a, b);
62 SkFixed directAnswer = SkFDot6Div(a, b);
63 REPORTER_ASSERT(reporter,
64 (directAnswer == 0 && ourAnswer == 0) ||
65 SkFixedDiv(SkAbs32(directAnswer - ourAnswer), SkAbs32(directAnswer)) <= 1 << 10
66 );
67 }
68 }
69 }
70}
71
reed@google.comc21f86f2013-04-29 14:18:23 +000072///////////////////////////////////////////////////////////////////////////////
73
reed@google.coma7d74612012-05-30 12:30:09 +000074static float sk_fsel(float pred, float result_ge, float result_lt) {
75 return pred >= 0 ? result_ge : result_lt;
76}
77
78static float fast_floor(float x) {
reed@google.comc20bc252012-05-30 13:48:14 +000079// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
80 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
robertphillips@google.com00bf06a2012-05-30 15:19:17 +000081 return (float)(x + big) - big;
reed@google.coma7d74612012-05-30 12:30:09 +000082}
83
84static float std_floor(float x) {
85 return sk_float_floor(x);
86}
87
88static void test_floor_value(skiatest::Reporter* reporter, float value) {
89 float fast = fast_floor(value);
90 float std = std_floor(value);
halcanary7d571242016-02-24 17:59:16 -080091 if (std != fast) {
92 ERRORF(reporter, "fast_floor(%.9g) == %.9g != %.9g == std_floor(%.9g)",
93 value, fast, std, value);
94 }
reed@google.coma7d74612012-05-30 12:30:09 +000095}
96
97static void test_floor(skiatest::Reporter* reporter) {
98 static const float gVals[] = {
99 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
100 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
reed@google.coma7d74612012-05-30 12:30:09 +0000102 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
103 test_floor_value(reporter, gVals[i]);
104// test_floor_value(reporter, -gVals[i]);
105 }
106}
107
108///////////////////////////////////////////////////////////////////////////////
109
reed@google.comea774d22013-04-22 20:21:56 +0000110// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
111static void test_muldivround(skiatest::Reporter* reporter) {
112#if 0
113 // this "complete" test is too slow, so we test a random sampling of it
114
115 for (int a = 0; a <= 32767; ++a) {
116 for (int b = 0; b <= 32767; ++b) {
117 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
118 unsigned prod1 = SkMulDiv255Round(a, b);
119 SkASSERT(prod0 == prod1);
120 }
121 }
122#endif
123
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000124 SkRandom rand;
reed@google.comea774d22013-04-22 20:21:56 +0000125 for (int i = 0; i < 10000; ++i) {
126 unsigned a = rand.nextU() & 0x7FFF;
127 unsigned b = rand.nextU() & 0x7FFF;
128
129 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
130 unsigned prod1 = SkMulDiv255Round(a, b);
131
132 REPORTER_ASSERT(reporter, prod0 == prod1);
133 }
134}
135
reed@google.com0abb4992011-10-06 20:04:36 +0000136static float float_blend(int src, int dst, float unit) {
137 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +0000138}
139
reed@google.com8d7e39c2011-11-09 17:12:08 +0000140static int blend31(int src, int dst, int a31) {
141 return dst + ((src - dst) * a31 * 2114 >> 16);
142 // return dst + ((src - dst) * a31 * 33 >> 10);
143}
144
145static int blend31_slow(int src, int dst, int a31) {
146 int prod = src * a31 + (31 - a31) * dst + 16;
147 prod = (prod + (prod >> 5)) >> 5;
148 return prod;
149}
150
151static int blend31_round(int src, int dst, int a31) {
152 int prod = (src - dst) * a31 + 16;
153 prod = (prod + (prod >> 5)) >> 5;
154 return dst + prod;
155}
156
157static int blend31_old(int src, int dst, int a31) {
158 a31 += a31 >> 4;
159 return dst + ((src - dst) * a31 >> 5);
160}
161
caryclark@google.com42639cd2012-06-06 12:03:39 +0000162// suppress unused code warning
163static int (*blend_functions[])(int, int, int) = {
164 blend31,
165 blend31_slow,
166 blend31_round,
167 blend31_old
168};
169
reed@google.com8d7e39c2011-11-09 17:12:08 +0000170static void test_blend31() {
171 int failed = 0;
172 int death = 0;
caryclark@google.com42639cd2012-06-06 12:03:39 +0000173 if (false) { // avoid bit rot, suppress warning
174 failed = (*blend_functions[0])(0,0,0);
175 }
reed@google.com8d7e39c2011-11-09 17:12:08 +0000176 for (int src = 0; src <= 255; src++) {
177 for (int dst = 0; dst <= 255; dst++) {
178 for (int a = 0; a <= 31; a++) {
179// int r0 = blend31(src, dst, a);
180// int r0 = blend31_round(src, dst, a);
181// int r0 = blend31_old(src, dst, a);
182 int r0 = blend31_slow(src, dst, a);
183
184 float f = float_blend(src, dst, a / 31.f);
185 int r1 = (int)f;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000186 int r2 = SkScalarRoundToInt(f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000187
188 if (r0 != r1 && r0 != r2) {
bungeman@google.comfab44db2013-10-11 18:50:45 +0000189 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
halcanary7d571242016-02-24 17:59:16 -0800190 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000191 failed += 1;
192 }
193 if (r0 > 255) {
194 death += 1;
bungeman@google.comfab44db2013-10-11 18:50:45 +0000195 SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
196 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000197 }
198 }
199 }
200 }
201 SkDebugf("---- failed %d death %d\n", failed, death);
202}
203
reed@google.com0abb4992011-10-06 20:04:36 +0000204static void test_blend(skiatest::Reporter* reporter) {
205 for (int src = 0; src <= 255; src++) {
206 for (int dst = 0; dst <= 255; dst++) {
207 for (int a = 0; a <= 255; a++) {
208 int r0 = SkAlphaBlend255(src, dst, a);
209 float f1 = float_blend(src, dst, a / 255.f);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000210 int r1 = SkScalarRoundToInt(f1);
reed@google.com772813a2011-03-30 22:34:45 +0000211
reed@google.com0abb4992011-10-06 20:04:36 +0000212 if (r0 != r1) {
213 float diff = sk_float_abs(f1 - r1);
214 diff = sk_float_abs(diff - 0.5f);
215 if (diff > (1 / 255.f)) {
halcanary7d571242016-02-24 17:59:16 -0800216 ERRORF(reporter, "src:%d dst:%d a:%d "
217 "result:%d float:%g\n", src, dst, a, r0, f1);
reed@google.com0abb4992011-10-06 20:04:36 +0000218 }
219 }
reed@google.com772813a2011-03-30 22:34:45 +0000220 }
221 }
222 }
223}
reed@google.com772813a2011-03-30 22:34:45 +0000224
reed@android.comed673312009-02-27 16:24:51 +0000225static void check_length(skiatest::Reporter* reporter,
226 const SkPoint& p, SkScalar targetLen) {
reed@android.comed673312009-02-27 16:24:51 +0000227 float x = SkScalarToFloat(p.fX);
228 float y = SkScalarToFloat(p.fY);
229 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000230
reed@android.comed673312009-02-27 16:24:51 +0000231 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000232
reed@android.comed673312009-02-27 16:24:51 +0000233 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000234}
235
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000236static float nextFloat(SkRandom& rand) {
reed@android.comed673312009-02-27 16:24:51 +0000237 SkFloatIntUnion data;
238 data.fSignBitInt = rand.nextU();
239 return data.fFloat;
240}
241
242/* returns true if a == b as resulting from (int)x. Since it is undefined
243 what to do if the float exceeds 2^32-1, we check for that explicitly.
244 */
mtklein785a5b92016-05-27 10:47:31 -0700245static bool equal_float_native_skia(float x, int32_t ni, int32_t si) {
246 // When the float is out of integer range (NaN, above, below),
247 // the C cast is undefined, but Skia's methods should have clamped.
248 if (!(x == x)) { // NaN
249 return si == SK_MaxS32 || si == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000250 }
reed@android.comed673312009-02-27 16:24:51 +0000251 if (x > SK_MaxS32) {
mtklein785a5b92016-05-27 10:47:31 -0700252 return si == SK_MaxS32;
reed@android.comed673312009-02-27 16:24:51 +0000253 }
mtklein785a5b92016-05-27 10:47:31 -0700254 if (x < SK_MinS32) {
255 return si == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000256 }
257 return si == ni;
258}
259
260static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
mtklein785a5b92016-05-27 10:47:31 -0700261 float x, int32_t ni, int32_t si) {
reed@android.comed673312009-02-27 16:24:51 +0000262 if (!equal_float_native_skia(x, ni, si)) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000263 ERRORF(reporter, "%s float %g bits %x native %x skia %x\n",
264 op, x, SkFloat2Bits(x), ni, si);
reed@android.comed673312009-02-27 16:24:51 +0000265 }
266}
267
reed@android.comed673312009-02-27 16:24:51 +0000268static void test_float_floor(skiatest::Reporter* reporter, float x) {
269 int ix = (int)floor(x);
270 int iix = SkFloatToIntFloor(x);
271 assert_float_equal(reporter, "floor", x, ix, iix);
272}
273
274static void test_float_round(skiatest::Reporter* reporter, float x) {
275 double xx = x + 0.5; // need intermediate double to avoid temp loss
276 int ix = (int)floor(xx);
277 int iix = SkFloatToIntRound(x);
278 assert_float_equal(reporter, "round", x, ix, iix);
279}
280
281static void test_float_ceil(skiatest::Reporter* reporter, float x) {
282 int ix = (int)ceil(x);
283 int iix = SkFloatToIntCeil(x);
284 assert_float_equal(reporter, "ceil", x, ix, iix);
285}
286
287static void test_float_conversions(skiatest::Reporter* reporter, float x) {
reed@android.comed673312009-02-27 16:24:51 +0000288 test_float_floor(reporter, x);
289 test_float_round(reporter, x);
290 test_float_ceil(reporter, x);
291}
292
reed@android.comed673312009-02-27 16:24:51 +0000293static void unittest_fastfloat(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000294 SkRandom rand;
reed@android.comed673312009-02-27 16:24:51 +0000295 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000296
reed@android.comed673312009-02-27 16:24:51 +0000297 static const float gFloats[] = {
mtklein785a5b92016-05-27 10:47:31 -0700298 0.f/0.f, -0.f/0.f, 1.f/0.f, -1.f/0.f,
reed@android.comed673312009-02-27 16:24:51 +0000299 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
300 0.000000001f, 1000000000.f, // doesn't overflow
301 0.0000000001f, 10000000000.f // does overflow
302 };
303 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000304 test_float_conversions(reporter, gFloats[i]);
305 test_float_conversions(reporter, -gFloats[i]);
306 }
reed@android.com80e39a72009-04-02 16:59:40 +0000307
reed@android.comed673312009-02-27 16:24:51 +0000308 for (int outer = 0; outer < 100; outer++) {
309 rand.setSeed(outer);
310 for (i = 0; i < 100000; i++) {
311 float x = nextFloat(rand);
312 test_float_conversions(reporter, x);
313 }
reed@android.comed673312009-02-27 16:24:51 +0000314 }
315}
316
reed@google.com077910e2011-02-08 21:56:39 +0000317static float make_zero() {
318 return sk_float_sin(0);
319}
320
321static void unittest_isfinite(skiatest::Reporter* reporter) {
epoger@google.combf083a92011-06-08 18:26:08 +0000322 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000323 float inf = 1.0f / make_zero();
324 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000325
reed@google.com077910e2011-02-08 21:56:39 +0000326 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000327 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
328 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
329 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
reed@android.comd4134452011-02-09 02:24:26 +0000330
331 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000332 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
333 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
334 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000335
reed@google.com077910e2011-02-08 21:56:39 +0000336 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000337 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
338 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
339 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000340}
341
jvanverth93679922014-11-26 13:15:59 -0800342static void unittest_half(skiatest::Reporter* reporter) {
343 static const float gFloats[] = {
344 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
345 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
346 };
347
348 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
349 SkHalf h = SkFloatToHalf(gFloats[i]);
350 float f = SkHalfToFloat(h);
351 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
352 }
353
354 // check some special values
355 union FloatUnion {
356 uint32_t fU;
357 float fF;
358 };
359
360 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
361 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
362 float f = SkHalfToFloat(h);
363 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
364
365 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
366 h = SkFloatToHalf(largestNegativeHalf.fF);
367 f = SkHalfToFloat(h);
368 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
369
370 static const FloatUnion smallestPositiveHalf = { 102 << 23 };
371 h = SkFloatToHalf(smallestPositiveHalf.fF);
372 f = SkHalfToFloat(h);
373 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
374
375 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
376 h = SkFloatToHalf(overflowHalf.fF);
377 f = SkHalfToFloat(h);
378 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
379
380 static const FloatUnion underflowHalf = { 101 << 23 };
381 h = SkFloatToHalf(underflowHalf.fF);
382 f = SkHalfToFloat(h);
383 REPORTER_ASSERT(reporter, f == 0.0f );
384
385 static const FloatUnion inf32 = { 255 << 23 };
386 h = SkFloatToHalf(inf32.fF);
387 f = SkHalfToFloat(h);
388 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
389
390 static const FloatUnion nan32 = { 255 << 23 | 1 };
391 h = SkFloatToHalf(nan32.fF);
392 f = SkHalfToFloat(h);
393 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
394
395}
396
mtkleina766ca82016-01-26 07:40:30 -0800397template <typename RSqrtFn>
398static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
jvanverth29c69792015-07-23 11:14:29 -0700399 const float maxRelativeError = 6.50196699e-4f;
400
401 // test close to 0 up to 1
402 float input = 0.000001f;
403 for (int i = 0; i < 1000; ++i) {
404 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800405 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700406 float relativeError = sk_float_abs(exact - estimate)/exact;
407 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
408 input += 0.001f;
409 }
410
411 // test 1 to ~100
412 input = 1.0f;
413 for (int i = 0; i < 1000; ++i) {
414 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800415 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700416 float relativeError = sk_float_abs(exact - estimate)/exact;
417 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
418 input += 0.01f;
419 }
420
421 // test some big numbers
422 input = 1000000.0f;
423 for (int i = 0; i < 100; ++i) {
424 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800425 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700426 float relativeError = sk_float_abs(exact - estimate)/exact;
427 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
428 input += 754326.f;
429 }
430}
431
reed@android.comed673312009-02-27 16:24:51 +0000432static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000433 for (int a = 0; a <= 255; a++) {
434 for (int b = 0; b <= 255; b++) {
435 int ab = a * b;
436 float s = ab / 255.0f;
437 int round = (int)floorf(s + 0.5f);
438 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000439
reed@android.comed673312009-02-27 16:24:51 +0000440 int iround = SkMulDiv255Round(a, b);
441 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000442
reed@android.comed673312009-02-27 16:24:51 +0000443 REPORTER_ASSERT(reporter, iround == round);
444 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000445
reed@android.comed673312009-02-27 16:24:51 +0000446 REPORTER_ASSERT(reporter, itrunc <= iround);
447 REPORTER_ASSERT(reporter, iround <= a);
448 REPORTER_ASSERT(reporter, iround <= b);
449 }
450 }
reed@android.comed673312009-02-27 16:24:51 +0000451}
452
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000453static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
454 for (int c = 0; c <= 255; c++) {
455 for (int a = 0; a <= 255; a++) {
456 int product = (c * a + 255);
457 int expected_ceiling = (product + (product >> 8)) >> 8;
458 int webkit_ceiling = (c * a + 254) / 255;
459 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
460 int skia_ceiling = SkMulDiv255Ceiling(c, a);
461 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
462 }
463 }
464}
465
reed@android.comf0ad0862010-02-09 19:18:38 +0000466static void test_copysign(skiatest::Reporter* reporter) {
467 static const int32_t gTriples[] = {
468 // x, y, expected result
469 0, 0, 0,
470 0, 1, 0,
471 0, -1, 0,
472 1, 0, 1,
473 1, 1, 1,
474 1, -1, -1,
475 -1, 0, 1,
476 -1, 1, 1,
477 -1, -1, -1,
478 };
479 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
480 REPORTER_ASSERT(reporter,
481 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000482 float x = (float)gTriples[i];
483 float y = (float)gTriples[i+1];
484 float expected = (float)gTriples[i+2];
485 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000486 }
487
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000488 SkRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000489 for (int j = 0; j < 1000; j++) {
490 int ix = rand.nextS();
491 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
492 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
493 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
494 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
495
496 SkScalar sx = rand.nextSScalar1();
497 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
498 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
499 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
500 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
501 }
502}
503
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000504DEF_TEST(Math, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000505 int i;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000506 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000507
reed@android.comed673312009-02-27 16:24:51 +0000508 // these should assert
509#if 0
510 SkToS8(128);
511 SkToS8(-129);
512 SkToU8(256);
513 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000514
reed@android.comed673312009-02-27 16:24:51 +0000515 SkToS16(32768);
516 SkToS16(-32769);
517 SkToU16(65536);
518 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000519
reed@android.comed673312009-02-27 16:24:51 +0000520 if (sizeof(size_t) > 4) {
521 SkToS32(4*1024*1024);
522 SkToS32(-4*1024*1024);
523 SkToU32(5*1024*1024);
524 SkToU32(-5);
525 }
526#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000527
reed@android.comed673312009-02-27 16:24:51 +0000528 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000529 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000530 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000531
reed@android.comed673312009-02-27 16:24:51 +0000532 {
533 SkScalar x = SK_ScalarNaN;
534 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
535 }
reed@android.com80e39a72009-04-02 16:59:40 +0000536
reed@android.comed673312009-02-27 16:24:51 +0000537 for (i = 0; i < 1000; i++) {
538 int value = rand.nextS16();
539 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000540
reed@android.comed673312009-02-27 16:24:51 +0000541 int clamp = SkClampMax(value, max);
542 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
543 REPORTER_ASSERT(reporter, clamp == clamp2);
544 }
reed@android.com80e39a72009-04-02 16:59:40 +0000545
reed@android.come72fee52009-11-16 14:52:01 +0000546 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000547 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000548
tomhudson@google.com75589252012-04-10 17:42:21 +0000549 // These random values are being treated as 32-bit-patterns, not as
550 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000551 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000552 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000553 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000554 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000555 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000556 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000557 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000558 check_length(reporter, p, SK_Scalar1);
559 }
reed@android.com80e39a72009-04-02 16:59:40 +0000560
reed@android.comed673312009-02-27 16:24:51 +0000561 {
562 SkFixed result = SkFixedDiv(100, 100);
563 REPORTER_ASSERT(reporter, result == SK_Fixed1);
564 result = SkFixedDiv(1, SK_Fixed1);
565 REPORTER_ASSERT(reporter, result == 1);
liyuqian0d2c2342016-07-13 13:34:46 -0700566 result = SkFixedDiv(10 - 1, SK_Fixed1 * 3);
567 REPORTER_ASSERT(reporter, result == 3);
reed@android.comed673312009-02-27 16:24:51 +0000568 }
reed@android.com80e39a72009-04-02 16:59:40 +0000569
liyuqian3f490cc2016-10-20 11:23:09 -0700570 {
571 REPORTER_ASSERT(reporter, (SkFixedRoundToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
572 REPORTER_ASSERT(reporter, (SkFixedFloorToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
573 REPORTER_ASSERT(reporter, (SkFixedCeilToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
574 }
575
reed@android.comed673312009-02-27 16:24:51 +0000576 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000577 unittest_isfinite(reporter);
jvanverth93679922014-11-26 13:15:59 -0800578 unittest_half(reporter);
mtkleina766ca82016-01-26 07:40:30 -0800579 test_rsqrt(reporter, sk_float_rsqrt);
580 test_rsqrt(reporter, sk_float_rsqrt_portable);
reed@android.com80e39a72009-04-02 16:59:40 +0000581
reed@android.come72fee52009-11-16 14:52:01 +0000582 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000583 SkFixed numer = rand.nextS();
584 SkFixed denom = rand.nextS();
585 SkFixed result = SkFixedDiv(numer, denom);
caryclark3127c992015-12-09 12:02:30 -0800586 int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000587
reed@android.comed673312009-02-27 16:24:51 +0000588 (void)SkCLZ(numer);
589 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000590
reed@android.comed673312009-02-27 16:24:51 +0000591 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
592 if (check > SK_MaxS32) {
593 check = SK_MaxS32;
594 } else if (check < -SK_MaxS32) {
595 check = SK_MinS32;
596 }
mtklein97ca98d2015-03-18 11:32:21 -0700597 if (result != (int32_t)check) {
598 ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
599 }
reed@android.comed673312009-02-27 16:24:51 +0000600 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.comed673312009-02-27 16:24:51 +0000601 }
reed@android.com80e39a72009-04-02 16:59:40 +0000602
reed@google.com0abb4992011-10-06 20:04:36 +0000603 test_blend(reporter);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000604
humper@google.com05af1af2013-01-07 16:47:43 +0000605 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000606
reed@google.com8d7e39c2011-11-09 17:12:08 +0000607 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000608 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000609
610 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000611 test_clz(reporter);
Yuqian Lice1d2932016-11-18 10:18:15 -0500612 test_quick_div(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000613}
614
reed@google.comc9f81662013-05-03 18:06:31 +0000615template <typename T> struct PairRec {
616 T fYin;
617 T fYang;
618};
619
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000620DEF_TEST(TestEndian, reporter) {
reed@google.comc9f81662013-05-03 18:06:31 +0000621 static const PairRec<uint16_t> g16[] = {
622 { 0x0, 0x0 },
623 { 0xFFFF, 0xFFFF },
624 { 0x1122, 0x2211 },
625 };
626 static const PairRec<uint32_t> g32[] = {
627 { 0x0, 0x0 },
628 { 0xFFFFFFFF, 0xFFFFFFFF },
629 { 0x11223344, 0x44332211 },
630 };
631 static const PairRec<uint64_t> g64[] = {
632 { 0x0, 0x0 },
633 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
634 { 0x1122334455667788ULL, 0x8877665544332211ULL },
635 };
636
637 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
638 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
639 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
640
641 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
642 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
643 }
644 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
645 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
646 }
647 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
648 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
649 }
650}
651
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000652template <typename T>
653static void test_divmod(skiatest::Reporter* r) {
654 const struct {
655 T numer;
656 T denom;
657 } kEdgeCases[] = {
658 {(T)17, (T)17},
659 {(T)17, (T)4},
660 {(T)0, (T)17},
661 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
662 {(T)-17, (T)-17},
663 {(T)-17, (T)4},
664 {(T)17, (T)-4},
665 {(T)-17, (T)-4},
666 };
667
668 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
669 const T numer = kEdgeCases[i].numer;
670 const T denom = kEdgeCases[i].denom;
671 T div, mod;
672 SkTDivMod(numer, denom, &div, &mod);
673 REPORTER_ASSERT(r, numer/denom == div);
674 REPORTER_ASSERT(r, numer%denom == mod);
675 }
676
677 SkRandom rand;
678 for (size_t i = 0; i < 10000; i++) {
679 const T numer = (T)rand.nextS();
680 T denom = 0;
681 while (0 == denom) {
682 denom = (T)rand.nextS();
683 }
684 T div, mod;
685 SkTDivMod(numer, denom, &div, &mod);
686 REPORTER_ASSERT(r, numer/denom == div);
687 REPORTER_ASSERT(r, numer%denom == mod);
688 }
689}
690
691DEF_TEST(divmod_u8, r) {
692 test_divmod<uint8_t>(r);
693}
694
695DEF_TEST(divmod_u16, r) {
696 test_divmod<uint16_t>(r);
697}
698
699DEF_TEST(divmod_u32, r) {
700 test_divmod<uint32_t>(r);
701}
702
703DEF_TEST(divmod_u64, r) {
704 test_divmod<uint64_t>(r);
705}
706
707DEF_TEST(divmod_s8, r) {
708 test_divmod<int8_t>(r);
709}
710
711DEF_TEST(divmod_s16, r) {
712 test_divmod<int16_t>(r);
713}
714
715DEF_TEST(divmod_s32, r) {
716 test_divmod<int32_t>(r);
717}
718
719DEF_TEST(divmod_s64, r) {
720 test_divmod<int64_t>(r);
721}
Robert Phillips9e380472016-10-28 12:15:03 -0400722
723static void test_nextsizepow2(skiatest::Reporter* r, size_t test, size_t expectedAns) {
724 size_t ans = GrNextSizePow2(test);
725
726 REPORTER_ASSERT(r, ans == expectedAns);
727 //SkDebugf("0x%zx -> 0x%zx (0x%zx)\n", test, ans, expectedAns);
728}
729
730DEF_TEST(GrNextSizePow2, reporter) {
731 constexpr int kNumSizeTBits = 8 * sizeof(size_t);
732
733 size_t test = 0, expectedAns = 1;
734
735 test_nextsizepow2(reporter, test, expectedAns);
736
737 test = 1; expectedAns = 1;
738
739 for (int i = 1; i < kNumSizeTBits; ++i) {
740 test_nextsizepow2(reporter, test, expectedAns);
741
742 test++;
743 expectedAns <<= 1;
744
745 test_nextsizepow2(reporter, test, expectedAns);
746
747 test = expectedAns;
748 }
749
750 // For the remaining three tests there is no higher power (of 2)
751 test = 0x1;
752 test <<= kNumSizeTBits-1;
753 test_nextsizepow2(reporter, test, test);
754
755 test++;
756 test_nextsizepow2(reporter, test, test);
757
758 test_nextsizepow2(reporter, SIZE_MAX, SIZE_MAX);
759}