blob: 580cd941fa926bc215165fb30daed5552995501a [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
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00008#include "SkColorPriv.h"
9#include "SkEndian.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
tomhudson@google.com8afae612012-08-14 15:03:35 +000011#include "SkFloatBits.h"
reed@android.comc846ede2010-04-13 15:29:15 +000012#include "SkFloatingPoint.h"
jvanverth93679922014-11-26 13:15:59 -080013#include "SkHalf.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000014#include "SkMathPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000015#include "SkPoint.h"
16#include "SkRandom.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000017#include "Test.h"
reed@android.comed673312009-02-27 16:24:51 +000018
reed@google.comc21f86f2013-04-29 14:18:23 +000019static void test_clz(skiatest::Reporter* reporter) {
20 REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
21 REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
22 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
reed@google.com7729534d2013-04-29 14:43:50 +000023 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
skia.committer@gmail.com81521132013-04-30 07:01:03 +000024
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000025 SkRandom rand;
reed@google.comc21f86f2013-04-29 14:18:23 +000026 for (int i = 0; i < 1000; ++i) {
27 uint32_t mask = rand.nextU();
reed@google.combc57a292013-04-29 15:27:42 +000028 // need to get some zeros for testing, but in some obscure way so the
29 // compiler won't "see" that, and work-around calling the functions.
30 mask >>= (mask & 31);
reed@google.comc21f86f2013-04-29 14:18:23 +000031 int intri = SkCLZ(mask);
32 int porta = SkCLZ_portable(mask);
33 REPORTER_ASSERT(reporter, intri == porta);
34 }
35}
36
37///////////////////////////////////////////////////////////////////////////////
38
reed@google.coma7d74612012-05-30 12:30:09 +000039static float sk_fsel(float pred, float result_ge, float result_lt) {
40 return pred >= 0 ? result_ge : result_lt;
41}
42
43static float fast_floor(float x) {
reed@google.comc20bc252012-05-30 13:48:14 +000044// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
45 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
robertphillips@google.com00bf06a2012-05-30 15:19:17 +000046 return (float)(x + big) - big;
reed@google.coma7d74612012-05-30 12:30:09 +000047}
48
49static float std_floor(float x) {
50 return sk_float_floor(x);
51}
52
53static void test_floor_value(skiatest::Reporter* reporter, float value) {
54 float fast = fast_floor(value);
55 float std = std_floor(value);
halcanary7d571242016-02-24 17:59:16 -080056 if (std != fast) {
57 ERRORF(reporter, "fast_floor(%.9g) == %.9g != %.9g == std_floor(%.9g)",
58 value, fast, std, value);
59 }
reed@google.coma7d74612012-05-30 12:30:09 +000060}
61
62static void test_floor(skiatest::Reporter* reporter) {
63 static const float gVals[] = {
64 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
65 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000066
reed@google.coma7d74612012-05-30 12:30:09 +000067 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
68 test_floor_value(reporter, gVals[i]);
69// test_floor_value(reporter, -gVals[i]);
70 }
71}
72
73///////////////////////////////////////////////////////////////////////////////
74
reed@google.comea774d22013-04-22 20:21:56 +000075// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
76static void test_muldivround(skiatest::Reporter* reporter) {
77#if 0
78 // this "complete" test is too slow, so we test a random sampling of it
79
80 for (int a = 0; a <= 32767; ++a) {
81 for (int b = 0; b <= 32767; ++b) {
82 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
83 unsigned prod1 = SkMulDiv255Round(a, b);
84 SkASSERT(prod0 == prod1);
85 }
86 }
87#endif
88
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000089 SkRandom rand;
reed@google.comea774d22013-04-22 20:21:56 +000090 for (int i = 0; i < 10000; ++i) {
91 unsigned a = rand.nextU() & 0x7FFF;
92 unsigned b = rand.nextU() & 0x7FFF;
93
94 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
95 unsigned prod1 = SkMulDiv255Round(a, b);
96
97 REPORTER_ASSERT(reporter, prod0 == prod1);
98 }
99}
100
reed@google.com0abb4992011-10-06 20:04:36 +0000101static float float_blend(int src, int dst, float unit) {
102 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +0000103}
104
reed@google.com8d7e39c2011-11-09 17:12:08 +0000105static int blend31(int src, int dst, int a31) {
106 return dst + ((src - dst) * a31 * 2114 >> 16);
107 // return dst + ((src - dst) * a31 * 33 >> 10);
108}
109
110static int blend31_slow(int src, int dst, int a31) {
111 int prod = src * a31 + (31 - a31) * dst + 16;
112 prod = (prod + (prod >> 5)) >> 5;
113 return prod;
114}
115
116static int blend31_round(int src, int dst, int a31) {
117 int prod = (src - dst) * a31 + 16;
118 prod = (prod + (prod >> 5)) >> 5;
119 return dst + prod;
120}
121
122static int blend31_old(int src, int dst, int a31) {
123 a31 += a31 >> 4;
124 return dst + ((src - dst) * a31 >> 5);
125}
126
caryclark@google.com42639cd2012-06-06 12:03:39 +0000127// suppress unused code warning
128static int (*blend_functions[])(int, int, int) = {
129 blend31,
130 blend31_slow,
131 blend31_round,
132 blend31_old
133};
134
reed@google.com8d7e39c2011-11-09 17:12:08 +0000135static void test_blend31() {
136 int failed = 0;
137 int death = 0;
caryclark@google.com42639cd2012-06-06 12:03:39 +0000138 if (false) { // avoid bit rot, suppress warning
139 failed = (*blend_functions[0])(0,0,0);
140 }
reed@google.com8d7e39c2011-11-09 17:12:08 +0000141 for (int src = 0; src <= 255; src++) {
142 for (int dst = 0; dst <= 255; dst++) {
143 for (int a = 0; a <= 31; a++) {
144// int r0 = blend31(src, dst, a);
145// int r0 = blend31_round(src, dst, a);
146// int r0 = blend31_old(src, dst, a);
147 int r0 = blend31_slow(src, dst, a);
148
149 float f = float_blend(src, dst, a / 31.f);
150 int r1 = (int)f;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000151 int r2 = SkScalarRoundToInt(f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000152
153 if (r0 != r1 && r0 != r2) {
bungeman@google.comfab44db2013-10-11 18:50:45 +0000154 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
halcanary7d571242016-02-24 17:59:16 -0800155 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000156 failed += 1;
157 }
158 if (r0 > 255) {
159 death += 1;
bungeman@google.comfab44db2013-10-11 18:50:45 +0000160 SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
161 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000162 }
163 }
164 }
165 }
166 SkDebugf("---- failed %d death %d\n", failed, death);
167}
168
reed@google.com0abb4992011-10-06 20:04:36 +0000169static void test_blend(skiatest::Reporter* reporter) {
170 for (int src = 0; src <= 255; src++) {
171 for (int dst = 0; dst <= 255; dst++) {
172 for (int a = 0; a <= 255; a++) {
173 int r0 = SkAlphaBlend255(src, dst, a);
174 float f1 = float_blend(src, dst, a / 255.f);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000175 int r1 = SkScalarRoundToInt(f1);
reed@google.com772813a2011-03-30 22:34:45 +0000176
reed@google.com0abb4992011-10-06 20:04:36 +0000177 if (r0 != r1) {
178 float diff = sk_float_abs(f1 - r1);
179 diff = sk_float_abs(diff - 0.5f);
180 if (diff > (1 / 255.f)) {
halcanary7d571242016-02-24 17:59:16 -0800181 ERRORF(reporter, "src:%d dst:%d a:%d "
182 "result:%d float:%g\n", src, dst, a, r0, f1);
reed@google.com0abb4992011-10-06 20:04:36 +0000183 }
184 }
reed@google.com772813a2011-03-30 22:34:45 +0000185 }
186 }
187 }
188}
reed@google.com772813a2011-03-30 22:34:45 +0000189
reed@android.comed673312009-02-27 16:24:51 +0000190static void check_length(skiatest::Reporter* reporter,
191 const SkPoint& p, SkScalar targetLen) {
reed@android.comed673312009-02-27 16:24:51 +0000192 float x = SkScalarToFloat(p.fX);
193 float y = SkScalarToFloat(p.fY);
194 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000195
reed@android.comed673312009-02-27 16:24:51 +0000196 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000197
reed@android.comed673312009-02-27 16:24:51 +0000198 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000199}
200
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000201static float nextFloat(SkRandom& rand) {
reed@android.comed673312009-02-27 16:24:51 +0000202 SkFloatIntUnion data;
203 data.fSignBitInt = rand.nextU();
204 return data.fFloat;
205}
206
207/* returns true if a == b as resulting from (int)x. Since it is undefined
208 what to do if the float exceeds 2^32-1, we check for that explicitly.
209 */
210static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
211 if (!(x == x)) { // NAN
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000212 return ((int32_t)si) == SK_MaxS32 || ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000213 }
214 // for out of range, C is undefined, but skia always should return NaN32
215 if (x > SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000216 return ((int32_t)si) == SK_MaxS32;
reed@android.comed673312009-02-27 16:24:51 +0000217 }
218 if (x < -SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000219 return ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000220 }
221 return si == ni;
222}
223
224static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
225 float x, uint32_t ni, uint32_t si) {
226 if (!equal_float_native_skia(x, ni, si)) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000227 ERRORF(reporter, "%s float %g bits %x native %x skia %x\n",
228 op, x, SkFloat2Bits(x), ni, si);
reed@android.comed673312009-02-27 16:24:51 +0000229 }
230}
231
232static void test_float_cast(skiatest::Reporter* reporter, float x) {
233 int ix = (int)x;
234 int iix = SkFloatToIntCast(x);
235 assert_float_equal(reporter, "cast", x, ix, iix);
236}
237
238static void test_float_floor(skiatest::Reporter* reporter, float x) {
239 int ix = (int)floor(x);
240 int iix = SkFloatToIntFloor(x);
241 assert_float_equal(reporter, "floor", x, ix, iix);
242}
243
244static void test_float_round(skiatest::Reporter* reporter, float x) {
245 double xx = x + 0.5; // need intermediate double to avoid temp loss
246 int ix = (int)floor(xx);
247 int iix = SkFloatToIntRound(x);
248 assert_float_equal(reporter, "round", x, ix, iix);
249}
250
251static void test_float_ceil(skiatest::Reporter* reporter, float x) {
252 int ix = (int)ceil(x);
253 int iix = SkFloatToIntCeil(x);
254 assert_float_equal(reporter, "ceil", x, ix, iix);
255}
256
257static void test_float_conversions(skiatest::Reporter* reporter, float x) {
258 test_float_cast(reporter, x);
259 test_float_floor(reporter, x);
260 test_float_round(reporter, x);
261 test_float_ceil(reporter, x);
262}
263
264static void test_int2float(skiatest::Reporter* reporter, int ival) {
265 float x0 = (float)ival;
266 float x1 = SkIntToFloatCast(ival);
reed@android.comed673312009-02-27 16:24:51 +0000267 REPORTER_ASSERT(reporter, x0 == x1);
reed@android.comed673312009-02-27 16:24:51 +0000268}
269
270static void unittest_fastfloat(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000271 SkRandom rand;
reed@android.comed673312009-02-27 16:24:51 +0000272 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000273
reed@android.comed673312009-02-27 16:24:51 +0000274 static const float gFloats[] = {
275 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
276 0.000000001f, 1000000000.f, // doesn't overflow
277 0.0000000001f, 10000000000.f // does overflow
278 };
279 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000280 test_float_conversions(reporter, gFloats[i]);
281 test_float_conversions(reporter, -gFloats[i]);
282 }
reed@android.com80e39a72009-04-02 16:59:40 +0000283
reed@android.comed673312009-02-27 16:24:51 +0000284 for (int outer = 0; outer < 100; outer++) {
285 rand.setSeed(outer);
286 for (i = 0; i < 100000; i++) {
287 float x = nextFloat(rand);
288 test_float_conversions(reporter, x);
289 }
reed@android.com80e39a72009-04-02 16:59:40 +0000290
reed@android.comed673312009-02-27 16:24:51 +0000291 test_int2float(reporter, 0);
292 test_int2float(reporter, 1);
293 test_int2float(reporter, -1);
294 for (i = 0; i < 100000; i++) {
295 // for now only test ints that are 24bits or less, since we don't
296 // round (down) large ints the same as IEEE...
297 int ival = rand.nextU() & 0xFFFFFF;
298 test_int2float(reporter, ival);
299 test_int2float(reporter, -ival);
300 }
301 }
302}
303
reed@google.com077910e2011-02-08 21:56:39 +0000304static float make_zero() {
305 return sk_float_sin(0);
306}
307
308static void unittest_isfinite(skiatest::Reporter* reporter) {
epoger@google.combf083a92011-06-08 18:26:08 +0000309 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000310 float inf = 1.0f / make_zero();
311 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000312
reed@google.com077910e2011-02-08 21:56:39 +0000313 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000314 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
315 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
316 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
reed@android.comd4134452011-02-09 02:24:26 +0000317
318 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000319 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
320 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
321 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000322
reed@google.com077910e2011-02-08 21:56:39 +0000323 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000324 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
325 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
326 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000327}
328
jvanverth93679922014-11-26 13:15:59 -0800329static void unittest_half(skiatest::Reporter* reporter) {
330 static const float gFloats[] = {
331 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
332 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
333 };
334
335 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
336 SkHalf h = SkFloatToHalf(gFloats[i]);
337 float f = SkHalfToFloat(h);
338 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
339 }
340
341 // check some special values
342 union FloatUnion {
343 uint32_t fU;
344 float fF;
345 };
346
347 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
348 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
349 float f = SkHalfToFloat(h);
350 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
351
352 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
353 h = SkFloatToHalf(largestNegativeHalf.fF);
354 f = SkHalfToFloat(h);
355 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
356
357 static const FloatUnion smallestPositiveHalf = { 102 << 23 };
358 h = SkFloatToHalf(smallestPositiveHalf.fF);
359 f = SkHalfToFloat(h);
360 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
361
362 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
363 h = SkFloatToHalf(overflowHalf.fF);
364 f = SkHalfToFloat(h);
365 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
366
367 static const FloatUnion underflowHalf = { 101 << 23 };
368 h = SkFloatToHalf(underflowHalf.fF);
369 f = SkHalfToFloat(h);
370 REPORTER_ASSERT(reporter, f == 0.0f );
371
372 static const FloatUnion inf32 = { 255 << 23 };
373 h = SkFloatToHalf(inf32.fF);
374 f = SkHalfToFloat(h);
375 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
376
377 static const FloatUnion nan32 = { 255 << 23 | 1 };
378 h = SkFloatToHalf(nan32.fF);
379 f = SkHalfToFloat(h);
380 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
381
382}
383
mtkleina766ca82016-01-26 07:40:30 -0800384template <typename RSqrtFn>
385static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
jvanverth29c69792015-07-23 11:14:29 -0700386 const float maxRelativeError = 6.50196699e-4f;
387
388 // test close to 0 up to 1
389 float input = 0.000001f;
390 for (int i = 0; i < 1000; ++i) {
391 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800392 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700393 float relativeError = sk_float_abs(exact - estimate)/exact;
394 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
395 input += 0.001f;
396 }
397
398 // test 1 to ~100
399 input = 1.0f;
400 for (int i = 0; i < 1000; ++i) {
401 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800402 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700403 float relativeError = sk_float_abs(exact - estimate)/exact;
404 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
405 input += 0.01f;
406 }
407
408 // test some big numbers
409 input = 1000000.0f;
410 for (int i = 0; i < 100; ++i) {
411 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800412 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700413 float relativeError = sk_float_abs(exact - estimate)/exact;
414 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
415 input += 754326.f;
416 }
417}
418
reed@android.comed673312009-02-27 16:24:51 +0000419static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000420 for (int a = 0; a <= 255; a++) {
421 for (int b = 0; b <= 255; b++) {
422 int ab = a * b;
423 float s = ab / 255.0f;
424 int round = (int)floorf(s + 0.5f);
425 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000426
reed@android.comed673312009-02-27 16:24:51 +0000427 int iround = SkMulDiv255Round(a, b);
428 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000429
reed@android.comed673312009-02-27 16:24:51 +0000430 REPORTER_ASSERT(reporter, iround == round);
431 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000432
reed@android.comed673312009-02-27 16:24:51 +0000433 REPORTER_ASSERT(reporter, itrunc <= iround);
434 REPORTER_ASSERT(reporter, iround <= a);
435 REPORTER_ASSERT(reporter, iround <= b);
436 }
437 }
reed@android.comed673312009-02-27 16:24:51 +0000438}
439
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000440static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
441 for (int c = 0; c <= 255; c++) {
442 for (int a = 0; a <= 255; a++) {
443 int product = (c * a + 255);
444 int expected_ceiling = (product + (product >> 8)) >> 8;
445 int webkit_ceiling = (c * a + 254) / 255;
446 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
447 int skia_ceiling = SkMulDiv255Ceiling(c, a);
448 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
449 }
450 }
451}
452
reed@android.comf0ad0862010-02-09 19:18:38 +0000453static void test_copysign(skiatest::Reporter* reporter) {
454 static const int32_t gTriples[] = {
455 // x, y, expected result
456 0, 0, 0,
457 0, 1, 0,
458 0, -1, 0,
459 1, 0, 1,
460 1, 1, 1,
461 1, -1, -1,
462 -1, 0, 1,
463 -1, 1, 1,
464 -1, -1, -1,
465 };
466 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
467 REPORTER_ASSERT(reporter,
468 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000469 float x = (float)gTriples[i];
470 float y = (float)gTriples[i+1];
471 float expected = (float)gTriples[i+2];
472 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000473 }
474
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000475 SkRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000476 for (int j = 0; j < 1000; j++) {
477 int ix = rand.nextS();
478 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
479 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
480 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
481 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
482
483 SkScalar sx = rand.nextSScalar1();
484 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
485 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
486 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
487 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
488 }
489}
490
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000491DEF_TEST(Math, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000492 int i;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000493 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000494
reed@android.comed673312009-02-27 16:24:51 +0000495 // these should assert
496#if 0
497 SkToS8(128);
498 SkToS8(-129);
499 SkToU8(256);
500 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000501
reed@android.comed673312009-02-27 16:24:51 +0000502 SkToS16(32768);
503 SkToS16(-32769);
504 SkToU16(65536);
505 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000506
reed@android.comed673312009-02-27 16:24:51 +0000507 if (sizeof(size_t) > 4) {
508 SkToS32(4*1024*1024);
509 SkToS32(-4*1024*1024);
510 SkToU32(5*1024*1024);
511 SkToU32(-5);
512 }
513#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000514
reed@android.comed673312009-02-27 16:24:51 +0000515 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000516 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000517 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000518
reed@android.comed673312009-02-27 16:24:51 +0000519 {
520 SkScalar x = SK_ScalarNaN;
521 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
522 }
reed@android.com80e39a72009-04-02 16:59:40 +0000523
reed@android.comed673312009-02-27 16:24:51 +0000524 for (i = 0; i < 1000; i++) {
525 int value = rand.nextS16();
526 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000527
reed@android.comed673312009-02-27 16:24:51 +0000528 int clamp = SkClampMax(value, max);
529 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
530 REPORTER_ASSERT(reporter, clamp == clamp2);
531 }
reed@android.com80e39a72009-04-02 16:59:40 +0000532
reed@android.come72fee52009-11-16 14:52:01 +0000533 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000534 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000535
tomhudson@google.com75589252012-04-10 17:42:21 +0000536 // These random values are being treated as 32-bit-patterns, not as
537 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000538 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000539 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000540 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000541 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000542 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000543 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000544 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000545 check_length(reporter, p, SK_Scalar1);
546 }
reed@android.com80e39a72009-04-02 16:59:40 +0000547
reed@android.comed673312009-02-27 16:24:51 +0000548 {
549 SkFixed result = SkFixedDiv(100, 100);
550 REPORTER_ASSERT(reporter, result == SK_Fixed1);
551 result = SkFixedDiv(1, SK_Fixed1);
552 REPORTER_ASSERT(reporter, result == 1);
553 }
reed@android.com80e39a72009-04-02 16:59:40 +0000554
reed@android.comed673312009-02-27 16:24:51 +0000555 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000556 unittest_isfinite(reporter);
jvanverth93679922014-11-26 13:15:59 -0800557 unittest_half(reporter);
mtkleina766ca82016-01-26 07:40:30 -0800558 test_rsqrt(reporter, sk_float_rsqrt);
559 test_rsqrt(reporter, sk_float_rsqrt_portable);
reed@android.com80e39a72009-04-02 16:59:40 +0000560
reed@android.come72fee52009-11-16 14:52:01 +0000561 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000562 SkFixed numer = rand.nextS();
563 SkFixed denom = rand.nextS();
564 SkFixed result = SkFixedDiv(numer, denom);
caryclark3127c992015-12-09 12:02:30 -0800565 int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000566
reed@android.comed673312009-02-27 16:24:51 +0000567 (void)SkCLZ(numer);
568 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000569
reed@android.comed673312009-02-27 16:24:51 +0000570 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
571 if (check > SK_MaxS32) {
572 check = SK_MaxS32;
573 } else if (check < -SK_MaxS32) {
574 check = SK_MinS32;
575 }
mtklein97ca98d2015-03-18 11:32:21 -0700576 if (result != (int32_t)check) {
577 ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
578 }
reed@android.comed673312009-02-27 16:24:51 +0000579 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.comed673312009-02-27 16:24:51 +0000580 }
reed@android.com80e39a72009-04-02 16:59:40 +0000581
reed@google.com0abb4992011-10-06 20:04:36 +0000582 test_blend(reporter);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000583
humper@google.com05af1af2013-01-07 16:47:43 +0000584 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000585
reed@google.com8d7e39c2011-11-09 17:12:08 +0000586 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000587 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000588
589 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000590 test_clz(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000591}
592
reed@google.comc9f81662013-05-03 18:06:31 +0000593template <typename T> struct PairRec {
594 T fYin;
595 T fYang;
596};
597
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000598DEF_TEST(TestEndian, reporter) {
reed@google.comc9f81662013-05-03 18:06:31 +0000599 static const PairRec<uint16_t> g16[] = {
600 { 0x0, 0x0 },
601 { 0xFFFF, 0xFFFF },
602 { 0x1122, 0x2211 },
603 };
604 static const PairRec<uint32_t> g32[] = {
605 { 0x0, 0x0 },
606 { 0xFFFFFFFF, 0xFFFFFFFF },
607 { 0x11223344, 0x44332211 },
608 };
609 static const PairRec<uint64_t> g64[] = {
610 { 0x0, 0x0 },
611 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
612 { 0x1122334455667788ULL, 0x8877665544332211ULL },
613 };
614
615 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
616 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
617 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
618
619 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
620 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
621 }
622 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
623 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
624 }
625 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
626 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
627 }
628}
629
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000630template <typename T>
631static void test_divmod(skiatest::Reporter* r) {
632 const struct {
633 T numer;
634 T denom;
635 } kEdgeCases[] = {
636 {(T)17, (T)17},
637 {(T)17, (T)4},
638 {(T)0, (T)17},
639 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
640 {(T)-17, (T)-17},
641 {(T)-17, (T)4},
642 {(T)17, (T)-4},
643 {(T)-17, (T)-4},
644 };
645
646 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
647 const T numer = kEdgeCases[i].numer;
648 const T denom = kEdgeCases[i].denom;
649 T div, mod;
650 SkTDivMod(numer, denom, &div, &mod);
651 REPORTER_ASSERT(r, numer/denom == div);
652 REPORTER_ASSERT(r, numer%denom == mod);
653 }
654
655 SkRandom rand;
656 for (size_t i = 0; i < 10000; i++) {
657 const T numer = (T)rand.nextS();
658 T denom = 0;
659 while (0 == denom) {
660 denom = (T)rand.nextS();
661 }
662 T div, mod;
663 SkTDivMod(numer, denom, &div, &mod);
664 REPORTER_ASSERT(r, numer/denom == div);
665 REPORTER_ASSERT(r, numer%denom == mod);
666 }
667}
668
669DEF_TEST(divmod_u8, r) {
670 test_divmod<uint8_t>(r);
671}
672
673DEF_TEST(divmod_u16, r) {
674 test_divmod<uint16_t>(r);
675}
676
677DEF_TEST(divmod_u32, r) {
678 test_divmod<uint32_t>(r);
679}
680
681DEF_TEST(divmod_u64, r) {
682 test_divmod<uint64_t>(r);
683}
684
685DEF_TEST(divmod_s8, r) {
686 test_divmod<int8_t>(r);
687}
688
689DEF_TEST(divmod_s16, r) {
690 test_divmod<int16_t>(r);
691}
692
693DEF_TEST(divmod_s32, r) {
694 test_divmod<int32_t>(r);
695}
696
697DEF_TEST(divmod_s64, r) {
698 test_divmod<int64_t>(r);
699}