blob: 1dd8223091e5914686caf3b6f50f31fff4abdbb2 [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"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070012#include "SkFixed.h"
tomhudson@google.com8afae612012-08-14 15:03:35 +000013#include "SkFloatBits.h"
reed@android.comc846ede2010-04-13 15:29:15 +000014#include "SkFloatingPoint.h"
jvanverth93679922014-11-26 13:15:59 -080015#include "SkHalf.h"
reed@google.com4b163ed2012-08-07 21:35:13 +000016#include "SkMathPriv.h"
reed@android.comed673312009-02-27 16:24:51 +000017#include "SkPoint.h"
18#include "SkRandom.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000019#include "Test.h"
reed@android.comed673312009-02-27 16:24:51 +000020
reed@google.comc21f86f2013-04-29 14:18:23 +000021static void test_clz(skiatest::Reporter* reporter) {
22 REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
23 REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
24 REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
reed@google.com7729534d2013-04-29 14:43:50 +000025 REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
skia.committer@gmail.com81521132013-04-30 07:01:03 +000026
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000027 SkRandom rand;
reed@google.comc21f86f2013-04-29 14:18:23 +000028 for (int i = 0; i < 1000; ++i) {
29 uint32_t mask = rand.nextU();
reed@google.combc57a292013-04-29 15:27:42 +000030 // need to get some zeros for testing, but in some obscure way so the
31 // compiler won't "see" that, and work-around calling the functions.
32 mask >>= (mask & 31);
reed@google.comc21f86f2013-04-29 14:18:23 +000033 int intri = SkCLZ(mask);
34 int porta = SkCLZ_portable(mask);
35 REPORTER_ASSERT(reporter, intri == porta);
36 }
37}
38
39///////////////////////////////////////////////////////////////////////////////
40
reed@google.coma7d74612012-05-30 12:30:09 +000041static float sk_fsel(float pred, float result_ge, float result_lt) {
42 return pred >= 0 ? result_ge : result_lt;
43}
44
45static float fast_floor(float x) {
reed@google.comc20bc252012-05-30 13:48:14 +000046// float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
47 float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
robertphillips@google.com00bf06a2012-05-30 15:19:17 +000048 return (float)(x + big) - big;
reed@google.coma7d74612012-05-30 12:30:09 +000049}
50
51static float std_floor(float x) {
52 return sk_float_floor(x);
53}
54
55static void test_floor_value(skiatest::Reporter* reporter, float value) {
56 float fast = fast_floor(value);
57 float std = std_floor(value);
halcanary7d571242016-02-24 17:59:16 -080058 if (std != fast) {
59 ERRORF(reporter, "fast_floor(%.9g) == %.9g != %.9g == std_floor(%.9g)",
60 value, fast, std, value);
61 }
reed@google.coma7d74612012-05-30 12:30:09 +000062}
63
64static void test_floor(skiatest::Reporter* reporter) {
65 static const float gVals[] = {
66 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
67 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000068
reed@google.coma7d74612012-05-30 12:30:09 +000069 for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
70 test_floor_value(reporter, gVals[i]);
71// test_floor_value(reporter, -gVals[i]);
72 }
73}
74
75///////////////////////////////////////////////////////////////////////////////
76
reed@google.comea774d22013-04-22 20:21:56 +000077// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
78static void test_muldivround(skiatest::Reporter* reporter) {
79#if 0
80 // this "complete" test is too slow, so we test a random sampling of it
81
82 for (int a = 0; a <= 32767; ++a) {
83 for (int b = 0; b <= 32767; ++b) {
84 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
85 unsigned prod1 = SkMulDiv255Round(a, b);
86 SkASSERT(prod0 == prod1);
87 }
88 }
89#endif
90
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000091 SkRandom rand;
reed@google.comea774d22013-04-22 20:21:56 +000092 for (int i = 0; i < 10000; ++i) {
93 unsigned a = rand.nextU() & 0x7FFF;
94 unsigned b = rand.nextU() & 0x7FFF;
95
96 unsigned prod0 = SkMul16ShiftRound(a, b, 8);
97 unsigned prod1 = SkMulDiv255Round(a, b);
98
99 REPORTER_ASSERT(reporter, prod0 == prod1);
100 }
101}
102
reed@google.com0abb4992011-10-06 20:04:36 +0000103static float float_blend(int src, int dst, float unit) {
104 return dst + (src - dst) * unit;
reed@google.com772813a2011-03-30 22:34:45 +0000105}
106
reed@google.com8d7e39c2011-11-09 17:12:08 +0000107static int blend31(int src, int dst, int a31) {
108 return dst + ((src - dst) * a31 * 2114 >> 16);
109 // return dst + ((src - dst) * a31 * 33 >> 10);
110}
111
112static int blend31_slow(int src, int dst, int a31) {
113 int prod = src * a31 + (31 - a31) * dst + 16;
114 prod = (prod + (prod >> 5)) >> 5;
115 return prod;
116}
117
118static int blend31_round(int src, int dst, int a31) {
119 int prod = (src - dst) * a31 + 16;
120 prod = (prod + (prod >> 5)) >> 5;
121 return dst + prod;
122}
123
124static int blend31_old(int src, int dst, int a31) {
125 a31 += a31 >> 4;
126 return dst + ((src - dst) * a31 >> 5);
127}
128
caryclark@google.com42639cd2012-06-06 12:03:39 +0000129// suppress unused code warning
130static int (*blend_functions[])(int, int, int) = {
131 blend31,
132 blend31_slow,
133 blend31_round,
134 blend31_old
135};
136
reed@google.com8d7e39c2011-11-09 17:12:08 +0000137static void test_blend31() {
138 int failed = 0;
139 int death = 0;
caryclark@google.com42639cd2012-06-06 12:03:39 +0000140 if (false) { // avoid bit rot, suppress warning
141 failed = (*blend_functions[0])(0,0,0);
142 }
reed@google.com8d7e39c2011-11-09 17:12:08 +0000143 for (int src = 0; src <= 255; src++) {
144 for (int dst = 0; dst <= 255; dst++) {
145 for (int a = 0; a <= 31; a++) {
146// int r0 = blend31(src, dst, a);
147// int r0 = blend31_round(src, dst, a);
148// int r0 = blend31_old(src, dst, a);
149 int r0 = blend31_slow(src, dst, a);
150
151 float f = float_blend(src, dst, a / 31.f);
152 int r1 = (int)f;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000153 int r2 = SkScalarRoundToInt(f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000154
155 if (r0 != r1 && r0 != r2) {
bungeman@google.comfab44db2013-10-11 18:50:45 +0000156 SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
halcanary7d571242016-02-24 17:59:16 -0800157 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000158 failed += 1;
159 }
160 if (r0 > 255) {
161 death += 1;
bungeman@google.comfab44db2013-10-11 18:50:45 +0000162 SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
163 src, dst, a, r0, f);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000164 }
165 }
166 }
167 }
168 SkDebugf("---- failed %d death %d\n", failed, death);
169}
170
reed@google.com0abb4992011-10-06 20:04:36 +0000171static void test_blend(skiatest::Reporter* reporter) {
172 for (int src = 0; src <= 255; src++) {
173 for (int dst = 0; dst <= 255; dst++) {
174 for (int a = 0; a <= 255; a++) {
175 int r0 = SkAlphaBlend255(src, dst, a);
176 float f1 = float_blend(src, dst, a / 255.f);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000177 int r1 = SkScalarRoundToInt(f1);
reed@google.com772813a2011-03-30 22:34:45 +0000178
reed@google.com0abb4992011-10-06 20:04:36 +0000179 if (r0 != r1) {
180 float diff = sk_float_abs(f1 - r1);
181 diff = sk_float_abs(diff - 0.5f);
182 if (diff > (1 / 255.f)) {
halcanary7d571242016-02-24 17:59:16 -0800183 ERRORF(reporter, "src:%d dst:%d a:%d "
184 "result:%d float:%g\n", src, dst, a, r0, f1);
reed@google.com0abb4992011-10-06 20:04:36 +0000185 }
186 }
reed@google.com772813a2011-03-30 22:34:45 +0000187 }
188 }
189 }
190}
reed@google.com772813a2011-03-30 22:34:45 +0000191
reed@android.comed673312009-02-27 16:24:51 +0000192static void check_length(skiatest::Reporter* reporter,
193 const SkPoint& p, SkScalar targetLen) {
reed@android.comed673312009-02-27 16:24:51 +0000194 float x = SkScalarToFloat(p.fX);
195 float y = SkScalarToFloat(p.fY);
196 float len = sk_float_sqrt(x*x + y*y);
reed@android.com80e39a72009-04-02 16:59:40 +0000197
reed@android.comed673312009-02-27 16:24:51 +0000198 len /= SkScalarToFloat(targetLen);
reed@android.com80e39a72009-04-02 16:59:40 +0000199
reed@android.comed673312009-02-27 16:24:51 +0000200 REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
reed@android.comed673312009-02-27 16:24:51 +0000201}
202
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000203static float nextFloat(SkRandom& rand) {
reed@android.comed673312009-02-27 16:24:51 +0000204 SkFloatIntUnion data;
205 data.fSignBitInt = rand.nextU();
206 return data.fFloat;
207}
208
209/* returns true if a == b as resulting from (int)x. Since it is undefined
210 what to do if the float exceeds 2^32-1, we check for that explicitly.
211 */
mtklein785a5b92016-05-27 10:47:31 -0700212static bool equal_float_native_skia(float x, int32_t ni, int32_t si) {
213 // When the float is out of integer range (NaN, above, below),
214 // the C cast is undefined, but Skia's methods should have clamped.
215 if (!(x == x)) { // NaN
216 return si == SK_MaxS32 || si == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000217 }
reed@android.comed673312009-02-27 16:24:51 +0000218 if (x > SK_MaxS32) {
mtklein785a5b92016-05-27 10:47:31 -0700219 return si == SK_MaxS32;
reed@android.comed673312009-02-27 16:24:51 +0000220 }
mtklein785a5b92016-05-27 10:47:31 -0700221 if (x < SK_MinS32) {
222 return si == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000223 }
224 return si == ni;
225}
226
227static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
mtklein785a5b92016-05-27 10:47:31 -0700228 float x, int32_t ni, int32_t si) {
reed@android.comed673312009-02-27 16:24:51 +0000229 if (!equal_float_native_skia(x, ni, si)) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000230 ERRORF(reporter, "%s float %g bits %x native %x skia %x\n",
231 op, x, SkFloat2Bits(x), ni, si);
reed@android.comed673312009-02-27 16:24:51 +0000232 }
233}
234
reed@android.comed673312009-02-27 16:24:51 +0000235static void test_float_floor(skiatest::Reporter* reporter, float x) {
236 int ix = (int)floor(x);
237 int iix = SkFloatToIntFloor(x);
238 assert_float_equal(reporter, "floor", x, ix, iix);
239}
240
241static void test_float_round(skiatest::Reporter* reporter, float x) {
242 double xx = x + 0.5; // need intermediate double to avoid temp loss
243 int ix = (int)floor(xx);
244 int iix = SkFloatToIntRound(x);
245 assert_float_equal(reporter, "round", x, ix, iix);
246}
247
248static void test_float_ceil(skiatest::Reporter* reporter, float x) {
249 int ix = (int)ceil(x);
250 int iix = SkFloatToIntCeil(x);
251 assert_float_equal(reporter, "ceil", x, ix, iix);
252}
253
254static void test_float_conversions(skiatest::Reporter* reporter, float x) {
reed@android.comed673312009-02-27 16:24:51 +0000255 test_float_floor(reporter, x);
256 test_float_round(reporter, x);
257 test_float_ceil(reporter, x);
258}
259
reed@android.comed673312009-02-27 16:24:51 +0000260static void unittest_fastfloat(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000261 SkRandom rand;
reed@android.comed673312009-02-27 16:24:51 +0000262 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000263
reed@android.comed673312009-02-27 16:24:51 +0000264 static const float gFloats[] = {
mtklein785a5b92016-05-27 10:47:31 -0700265 0.f/0.f, -0.f/0.f, 1.f/0.f, -1.f/0.f,
reed@android.comed673312009-02-27 16:24:51 +0000266 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
267 0.000000001f, 1000000000.f, // doesn't overflow
268 0.0000000001f, 10000000000.f // does overflow
269 };
270 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000271 test_float_conversions(reporter, gFloats[i]);
272 test_float_conversions(reporter, -gFloats[i]);
273 }
reed@android.com80e39a72009-04-02 16:59:40 +0000274
reed@android.comed673312009-02-27 16:24:51 +0000275 for (int outer = 0; outer < 100; outer++) {
276 rand.setSeed(outer);
277 for (i = 0; i < 100000; i++) {
278 float x = nextFloat(rand);
279 test_float_conversions(reporter, x);
280 }
reed@android.comed673312009-02-27 16:24:51 +0000281 }
282}
283
reed@google.com077910e2011-02-08 21:56:39 +0000284static float make_zero() {
285 return sk_float_sin(0);
286}
287
288static void unittest_isfinite(skiatest::Reporter* reporter) {
epoger@google.combf083a92011-06-08 18:26:08 +0000289 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000290 float inf = 1.0f / make_zero();
291 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000292
reed@google.com077910e2011-02-08 21:56:39 +0000293 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000294 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
295 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
296 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
reed@android.comd4134452011-02-09 02:24:26 +0000297
298 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000299 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
300 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
301 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000302
reed@google.com077910e2011-02-08 21:56:39 +0000303 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000304 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
305 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
306 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000307}
308
jvanverth93679922014-11-26 13:15:59 -0800309static void unittest_half(skiatest::Reporter* reporter) {
310 static const float gFloats[] = {
311 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
312 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
313 };
314
315 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
316 SkHalf h = SkFloatToHalf(gFloats[i]);
317 float f = SkHalfToFloat(h);
318 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
319 }
320
321 // check some special values
322 union FloatUnion {
323 uint32_t fU;
324 float fF;
325 };
326
327 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
328 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
329 float f = SkHalfToFloat(h);
330 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
331
332 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
333 h = SkFloatToHalf(largestNegativeHalf.fF);
334 f = SkHalfToFloat(h);
335 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
336
337 static const FloatUnion smallestPositiveHalf = { 102 << 23 };
338 h = SkFloatToHalf(smallestPositiveHalf.fF);
339 f = SkHalfToFloat(h);
340 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
341
342 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
343 h = SkFloatToHalf(overflowHalf.fF);
344 f = SkHalfToFloat(h);
345 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
346
347 static const FloatUnion underflowHalf = { 101 << 23 };
348 h = SkFloatToHalf(underflowHalf.fF);
349 f = SkHalfToFloat(h);
350 REPORTER_ASSERT(reporter, f == 0.0f );
351
352 static const FloatUnion inf32 = { 255 << 23 };
353 h = SkFloatToHalf(inf32.fF);
354 f = SkHalfToFloat(h);
355 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
356
357 static const FloatUnion nan32 = { 255 << 23 | 1 };
358 h = SkFloatToHalf(nan32.fF);
359 f = SkHalfToFloat(h);
360 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
361
362}
363
mtkleina766ca82016-01-26 07:40:30 -0800364template <typename RSqrtFn>
365static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
jvanverth29c69792015-07-23 11:14:29 -0700366 const float maxRelativeError = 6.50196699e-4f;
367
368 // test close to 0 up to 1
369 float input = 0.000001f;
370 for (int i = 0; i < 1000; ++i) {
371 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800372 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700373 float relativeError = sk_float_abs(exact - estimate)/exact;
374 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
375 input += 0.001f;
376 }
377
378 // test 1 to ~100
379 input = 1.0f;
380 for (int i = 0; i < 1000; ++i) {
381 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800382 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700383 float relativeError = sk_float_abs(exact - estimate)/exact;
384 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
385 input += 0.01f;
386 }
387
388 // test some big numbers
389 input = 1000000.0f;
390 for (int i = 0; i < 100; ++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 += 754326.f;
396 }
397}
398
reed@android.comed673312009-02-27 16:24:51 +0000399static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000400 for (int a = 0; a <= 255; a++) {
401 for (int b = 0; b <= 255; b++) {
402 int ab = a * b;
403 float s = ab / 255.0f;
404 int round = (int)floorf(s + 0.5f);
405 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000406
reed@android.comed673312009-02-27 16:24:51 +0000407 int iround = SkMulDiv255Round(a, b);
408 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000409
reed@android.comed673312009-02-27 16:24:51 +0000410 REPORTER_ASSERT(reporter, iround == round);
411 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000412
reed@android.comed673312009-02-27 16:24:51 +0000413 REPORTER_ASSERT(reporter, itrunc <= iround);
414 REPORTER_ASSERT(reporter, iround <= a);
415 REPORTER_ASSERT(reporter, iround <= b);
416 }
417 }
reed@android.comed673312009-02-27 16:24:51 +0000418}
419
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000420static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
421 for (int c = 0; c <= 255; c++) {
422 for (int a = 0; a <= 255; a++) {
423 int product = (c * a + 255);
424 int expected_ceiling = (product + (product >> 8)) >> 8;
425 int webkit_ceiling = (c * a + 254) / 255;
426 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
427 int skia_ceiling = SkMulDiv255Ceiling(c, a);
428 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
429 }
430 }
431}
432
reed@android.comf0ad0862010-02-09 19:18:38 +0000433static void test_copysign(skiatest::Reporter* reporter) {
434 static const int32_t gTriples[] = {
435 // x, y, expected result
436 0, 0, 0,
437 0, 1, 0,
438 0, -1, 0,
439 1, 0, 1,
440 1, 1, 1,
441 1, -1, -1,
442 -1, 0, 1,
443 -1, 1, 1,
444 -1, -1, -1,
445 };
446 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
447 REPORTER_ASSERT(reporter,
448 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000449 float x = (float)gTriples[i];
450 float y = (float)gTriples[i+1];
451 float expected = (float)gTriples[i+2];
452 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000453 }
454
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000455 SkRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000456 for (int j = 0; j < 1000; j++) {
457 int ix = rand.nextS();
458 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
459 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
460 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
461 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
462
463 SkScalar sx = rand.nextSScalar1();
464 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
465 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
466 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
467 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
468 }
469}
470
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000471DEF_TEST(Math, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000472 int i;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000473 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000474
reed@android.comed673312009-02-27 16:24:51 +0000475 // these should assert
476#if 0
477 SkToS8(128);
478 SkToS8(-129);
479 SkToU8(256);
480 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000481
reed@android.comed673312009-02-27 16:24:51 +0000482 SkToS16(32768);
483 SkToS16(-32769);
484 SkToU16(65536);
485 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000486
reed@android.comed673312009-02-27 16:24:51 +0000487 if (sizeof(size_t) > 4) {
488 SkToS32(4*1024*1024);
489 SkToS32(-4*1024*1024);
490 SkToU32(5*1024*1024);
491 SkToU32(-5);
492 }
493#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000494
reed@android.comed673312009-02-27 16:24:51 +0000495 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000496 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000497 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000498
reed@android.comed673312009-02-27 16:24:51 +0000499 {
500 SkScalar x = SK_ScalarNaN;
501 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
502 }
reed@android.com80e39a72009-04-02 16:59:40 +0000503
reed@android.comed673312009-02-27 16:24:51 +0000504 for (i = 0; i < 1000; i++) {
505 int value = rand.nextS16();
506 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000507
reed@android.comed673312009-02-27 16:24:51 +0000508 int clamp = SkClampMax(value, max);
509 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
510 REPORTER_ASSERT(reporter, clamp == clamp2);
511 }
reed@android.com80e39a72009-04-02 16:59:40 +0000512
reed@android.come72fee52009-11-16 14:52:01 +0000513 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000514 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000515
tomhudson@google.com75589252012-04-10 17:42:21 +0000516 // These random values are being treated as 32-bit-patterns, not as
517 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000518 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000519 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000520 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000521 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000522 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000523 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000524 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000525 check_length(reporter, p, SK_Scalar1);
526 }
reed@android.com80e39a72009-04-02 16:59:40 +0000527
reed@android.comed673312009-02-27 16:24:51 +0000528 {
529 SkFixed result = SkFixedDiv(100, 100);
530 REPORTER_ASSERT(reporter, result == SK_Fixed1);
531 result = SkFixedDiv(1, SK_Fixed1);
532 REPORTER_ASSERT(reporter, result == 1);
liyuqian0d2c2342016-07-13 13:34:46 -0700533 result = SkFixedDiv(10 - 1, SK_Fixed1 * 3);
534 REPORTER_ASSERT(reporter, result == 3);
reed@android.comed673312009-02-27 16:24:51 +0000535 }
reed@android.com80e39a72009-04-02 16:59:40 +0000536
reed@android.comed673312009-02-27 16:24:51 +0000537 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000538 unittest_isfinite(reporter);
jvanverth93679922014-11-26 13:15:59 -0800539 unittest_half(reporter);
mtkleina766ca82016-01-26 07:40:30 -0800540 test_rsqrt(reporter, sk_float_rsqrt);
541 test_rsqrt(reporter, sk_float_rsqrt_portable);
reed@android.com80e39a72009-04-02 16:59:40 +0000542
reed@android.come72fee52009-11-16 14:52:01 +0000543 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000544 SkFixed numer = rand.nextS();
545 SkFixed denom = rand.nextS();
546 SkFixed result = SkFixedDiv(numer, denom);
caryclark3127c992015-12-09 12:02:30 -0800547 int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000548
reed@android.comed673312009-02-27 16:24:51 +0000549 (void)SkCLZ(numer);
550 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000551
reed@android.comed673312009-02-27 16:24:51 +0000552 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
553 if (check > SK_MaxS32) {
554 check = SK_MaxS32;
555 } else if (check < -SK_MaxS32) {
556 check = SK_MinS32;
557 }
mtklein97ca98d2015-03-18 11:32:21 -0700558 if (result != (int32_t)check) {
559 ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
560 }
reed@android.comed673312009-02-27 16:24:51 +0000561 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.comed673312009-02-27 16:24:51 +0000562 }
reed@android.com80e39a72009-04-02 16:59:40 +0000563
reed@google.com0abb4992011-10-06 20:04:36 +0000564 test_blend(reporter);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000565
humper@google.com05af1af2013-01-07 16:47:43 +0000566 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000567
reed@google.com8d7e39c2011-11-09 17:12:08 +0000568 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000569 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000570
571 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000572 test_clz(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000573}
574
reed@google.comc9f81662013-05-03 18:06:31 +0000575template <typename T> struct PairRec {
576 T fYin;
577 T fYang;
578};
579
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000580DEF_TEST(TestEndian, reporter) {
reed@google.comc9f81662013-05-03 18:06:31 +0000581 static const PairRec<uint16_t> g16[] = {
582 { 0x0, 0x0 },
583 { 0xFFFF, 0xFFFF },
584 { 0x1122, 0x2211 },
585 };
586 static const PairRec<uint32_t> g32[] = {
587 { 0x0, 0x0 },
588 { 0xFFFFFFFF, 0xFFFFFFFF },
589 { 0x11223344, 0x44332211 },
590 };
591 static const PairRec<uint64_t> g64[] = {
592 { 0x0, 0x0 },
593 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
594 { 0x1122334455667788ULL, 0x8877665544332211ULL },
595 };
596
597 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
598 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
599 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
600
601 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
602 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
603 }
604 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
605 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
606 }
607 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
608 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
609 }
610}
611
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000612template <typename T>
613static void test_divmod(skiatest::Reporter* r) {
614 const struct {
615 T numer;
616 T denom;
617 } kEdgeCases[] = {
618 {(T)17, (T)17},
619 {(T)17, (T)4},
620 {(T)0, (T)17},
621 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
622 {(T)-17, (T)-17},
623 {(T)-17, (T)4},
624 {(T)17, (T)-4},
625 {(T)-17, (T)-4},
626 };
627
628 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
629 const T numer = kEdgeCases[i].numer;
630 const T denom = kEdgeCases[i].denom;
631 T div, mod;
632 SkTDivMod(numer, denom, &div, &mod);
633 REPORTER_ASSERT(r, numer/denom == div);
634 REPORTER_ASSERT(r, numer%denom == mod);
635 }
636
637 SkRandom rand;
638 for (size_t i = 0; i < 10000; i++) {
639 const T numer = (T)rand.nextS();
640 T denom = 0;
641 while (0 == denom) {
642 denom = (T)rand.nextS();
643 }
644 T div, mod;
645 SkTDivMod(numer, denom, &div, &mod);
646 REPORTER_ASSERT(r, numer/denom == div);
647 REPORTER_ASSERT(r, numer%denom == mod);
648 }
649}
650
651DEF_TEST(divmod_u8, r) {
652 test_divmod<uint8_t>(r);
653}
654
655DEF_TEST(divmod_u16, r) {
656 test_divmod<uint16_t>(r);
657}
658
659DEF_TEST(divmod_u32, r) {
660 test_divmod<uint32_t>(r);
661}
662
663DEF_TEST(divmod_u64, r) {
664 test_divmod<uint64_t>(r);
665}
666
667DEF_TEST(divmod_s8, r) {
668 test_divmod<int8_t>(r);
669}
670
671DEF_TEST(divmod_s16, r) {
672 test_divmod<int16_t>(r);
673}
674
675DEF_TEST(divmod_s32, r) {
676 test_divmod<int32_t>(r);
677}
678
679DEF_TEST(divmod_s64, r) {
680 test_divmod<int64_t>(r);
681}