blob: 9343277f8d8626b471839a1a054fd24896aa957d [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 */
212static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) {
213 if (!(x == x)) { // NAN
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000214 return ((int32_t)si) == SK_MaxS32 || ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000215 }
216 // for out of range, C is undefined, but skia always should return NaN32
217 if (x > SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000218 return ((int32_t)si) == SK_MaxS32;
reed@android.comed673312009-02-27 16:24:51 +0000219 }
220 if (x < -SK_MaxS32) {
bsalomon@google.com373ebc62012-09-26 13:08:56 +0000221 return ((int32_t)si) == SK_MinS32;
reed@android.comed673312009-02-27 16:24:51 +0000222 }
223 return si == ni;
224}
225
226static void assert_float_equal(skiatest::Reporter* reporter, const char op[],
227 float x, uint32_t ni, uint32_t si) {
228 if (!equal_float_native_skia(x, ni, si)) {
halcanary@google.coma9325fa2014-01-10 14:58:10 +0000229 ERRORF(reporter, "%s float %g bits %x native %x skia %x\n",
230 op, x, SkFloat2Bits(x), ni, si);
reed@android.comed673312009-02-27 16:24:51 +0000231 }
232}
233
234static void test_float_cast(skiatest::Reporter* reporter, float x) {
235 int ix = (int)x;
236 int iix = SkFloatToIntCast(x);
237 assert_float_equal(reporter, "cast", x, ix, iix);
238}
239
240static void test_float_floor(skiatest::Reporter* reporter, float x) {
241 int ix = (int)floor(x);
242 int iix = SkFloatToIntFloor(x);
243 assert_float_equal(reporter, "floor", x, ix, iix);
244}
245
246static void test_float_round(skiatest::Reporter* reporter, float x) {
247 double xx = x + 0.5; // need intermediate double to avoid temp loss
248 int ix = (int)floor(xx);
249 int iix = SkFloatToIntRound(x);
250 assert_float_equal(reporter, "round", x, ix, iix);
251}
252
253static void test_float_ceil(skiatest::Reporter* reporter, float x) {
254 int ix = (int)ceil(x);
255 int iix = SkFloatToIntCeil(x);
256 assert_float_equal(reporter, "ceil", x, ix, iix);
257}
258
259static void test_float_conversions(skiatest::Reporter* reporter, float x) {
260 test_float_cast(reporter, x);
261 test_float_floor(reporter, x);
262 test_float_round(reporter, x);
263 test_float_ceil(reporter, x);
264}
265
266static void test_int2float(skiatest::Reporter* reporter, int ival) {
267 float x0 = (float)ival;
268 float x1 = SkIntToFloatCast(ival);
reed@android.comed673312009-02-27 16:24:51 +0000269 REPORTER_ASSERT(reporter, x0 == x1);
reed@android.comed673312009-02-27 16:24:51 +0000270}
271
272static void unittest_fastfloat(skiatest::Reporter* reporter) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000273 SkRandom rand;
reed@android.comed673312009-02-27 16:24:51 +0000274 size_t i;
reed@android.com80e39a72009-04-02 16:59:40 +0000275
reed@android.comed673312009-02-27 16:24:51 +0000276 static const float gFloats[] = {
277 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
278 0.000000001f, 1000000000.f, // doesn't overflow
279 0.0000000001f, 10000000000.f // does overflow
280 };
281 for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) {
reed@android.comed673312009-02-27 16:24:51 +0000282 test_float_conversions(reporter, gFloats[i]);
283 test_float_conversions(reporter, -gFloats[i]);
284 }
reed@android.com80e39a72009-04-02 16:59:40 +0000285
reed@android.comed673312009-02-27 16:24:51 +0000286 for (int outer = 0; outer < 100; outer++) {
287 rand.setSeed(outer);
288 for (i = 0; i < 100000; i++) {
289 float x = nextFloat(rand);
290 test_float_conversions(reporter, x);
291 }
reed@android.com80e39a72009-04-02 16:59:40 +0000292
reed@android.comed673312009-02-27 16:24:51 +0000293 test_int2float(reporter, 0);
294 test_int2float(reporter, 1);
295 test_int2float(reporter, -1);
296 for (i = 0; i < 100000; i++) {
297 // for now only test ints that are 24bits or less, since we don't
298 // round (down) large ints the same as IEEE...
299 int ival = rand.nextU() & 0xFFFFFF;
300 test_int2float(reporter, ival);
301 test_int2float(reporter, -ival);
302 }
303 }
304}
305
reed@google.com077910e2011-02-08 21:56:39 +0000306static float make_zero() {
307 return sk_float_sin(0);
308}
309
310static void unittest_isfinite(skiatest::Reporter* reporter) {
epoger@google.combf083a92011-06-08 18:26:08 +0000311 float nan = sk_float_asin(2);
tomhudson@google.com75589252012-04-10 17:42:21 +0000312 float inf = 1.0f / make_zero();
313 float big = 3.40282e+038f;
reed@google.com077910e2011-02-08 21:56:39 +0000314
reed@google.com077910e2011-02-08 21:56:39 +0000315 REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
reed@android.comd4134452011-02-09 02:24:26 +0000316 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
317 REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
318 REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
reed@android.comd4134452011-02-09 02:24:26 +0000319
320 REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000321 REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
322 REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
323 REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000324
reed@google.com077910e2011-02-08 21:56:39 +0000325 REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
reed@google.com077910e2011-02-08 21:56:39 +0000326 REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
327 REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
328 REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
reed@google.com077910e2011-02-08 21:56:39 +0000329}
330
jvanverth93679922014-11-26 13:15:59 -0800331static void unittest_half(skiatest::Reporter* reporter) {
332 static const float gFloats[] = {
333 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
334 -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
335 };
336
337 for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
338 SkHalf h = SkFloatToHalf(gFloats[i]);
339 float f = SkHalfToFloat(h);
340 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
341 }
342
343 // check some special values
344 union FloatUnion {
345 uint32_t fU;
346 float fF;
347 };
348
349 static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
350 SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
351 float f = SkHalfToFloat(h);
352 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
353
354 static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
355 h = SkFloatToHalf(largestNegativeHalf.fF);
356 f = SkHalfToFloat(h);
357 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
358
359 static const FloatUnion smallestPositiveHalf = { 102 << 23 };
360 h = SkFloatToHalf(smallestPositiveHalf.fF);
361 f = SkHalfToFloat(h);
362 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
363
364 static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
365 h = SkFloatToHalf(overflowHalf.fF);
366 f = SkHalfToFloat(h);
367 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
368
369 static const FloatUnion underflowHalf = { 101 << 23 };
370 h = SkFloatToHalf(underflowHalf.fF);
371 f = SkHalfToFloat(h);
372 REPORTER_ASSERT(reporter, f == 0.0f );
373
374 static const FloatUnion inf32 = { 255 << 23 };
375 h = SkFloatToHalf(inf32.fF);
376 f = SkHalfToFloat(h);
377 REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
378
379 static const FloatUnion nan32 = { 255 << 23 | 1 };
380 h = SkFloatToHalf(nan32.fF);
381 f = SkHalfToFloat(h);
382 REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
383
384}
385
mtkleina766ca82016-01-26 07:40:30 -0800386template <typename RSqrtFn>
387static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
jvanverth29c69792015-07-23 11:14:29 -0700388 const float maxRelativeError = 6.50196699e-4f;
389
390 // test close to 0 up to 1
391 float input = 0.000001f;
392 for (int i = 0; i < 1000; ++i) {
393 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800394 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700395 float relativeError = sk_float_abs(exact - estimate)/exact;
396 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
397 input += 0.001f;
398 }
399
400 // test 1 to ~100
401 input = 1.0f;
402 for (int i = 0; i < 1000; ++i) {
403 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800404 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700405 float relativeError = sk_float_abs(exact - estimate)/exact;
406 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
407 input += 0.01f;
408 }
409
410 // test some big numbers
411 input = 1000000.0f;
412 for (int i = 0; i < 100; ++i) {
413 float exact = 1.0f/sk_float_sqrt(input);
mtkleina766ca82016-01-26 07:40:30 -0800414 float estimate = rsqrt(input);
jvanverth29c69792015-07-23 11:14:29 -0700415 float relativeError = sk_float_abs(exact - estimate)/exact;
416 REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
417 input += 754326.f;
418 }
419}
420
reed@android.comed673312009-02-27 16:24:51 +0000421static void test_muldiv255(skiatest::Reporter* reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000422 for (int a = 0; a <= 255; a++) {
423 for (int b = 0; b <= 255; b++) {
424 int ab = a * b;
425 float s = ab / 255.0f;
426 int round = (int)floorf(s + 0.5f);
427 int trunc = (int)floorf(s);
reed@android.com80e39a72009-04-02 16:59:40 +0000428
reed@android.comed673312009-02-27 16:24:51 +0000429 int iround = SkMulDiv255Round(a, b);
430 int itrunc = SkMulDiv255Trunc(a, b);
reed@android.com80e39a72009-04-02 16:59:40 +0000431
reed@android.comed673312009-02-27 16:24:51 +0000432 REPORTER_ASSERT(reporter, iround == round);
433 REPORTER_ASSERT(reporter, itrunc == trunc);
reed@android.com80e39a72009-04-02 16:59:40 +0000434
reed@android.comed673312009-02-27 16:24:51 +0000435 REPORTER_ASSERT(reporter, itrunc <= iround);
436 REPORTER_ASSERT(reporter, iround <= a);
437 REPORTER_ASSERT(reporter, iround <= b);
438 }
439 }
reed@android.comed673312009-02-27 16:24:51 +0000440}
441
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000442static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
443 for (int c = 0; c <= 255; c++) {
444 for (int a = 0; a <= 255; a++) {
445 int product = (c * a + 255);
446 int expected_ceiling = (product + (product >> 8)) >> 8;
447 int webkit_ceiling = (c * a + 254) / 255;
448 REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
449 int skia_ceiling = SkMulDiv255Ceiling(c, a);
450 REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
451 }
452 }
453}
454
reed@android.comf0ad0862010-02-09 19:18:38 +0000455static void test_copysign(skiatest::Reporter* reporter) {
456 static const int32_t gTriples[] = {
457 // x, y, expected result
458 0, 0, 0,
459 0, 1, 0,
460 0, -1, 0,
461 1, 0, 1,
462 1, 1, 1,
463 1, -1, -1,
464 -1, 0, 1,
465 -1, 1, 1,
466 -1, -1, -1,
467 };
468 for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
469 REPORTER_ASSERT(reporter,
470 SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
reed@android.comf0ad0862010-02-09 19:18:38 +0000471 float x = (float)gTriples[i];
472 float y = (float)gTriples[i+1];
473 float expected = (float)gTriples[i+2];
474 REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
reed@android.comf0ad0862010-02-09 19:18:38 +0000475 }
476
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000477 SkRandom rand;
reed@android.comf0ad0862010-02-09 19:18:38 +0000478 for (int j = 0; j < 1000; j++) {
479 int ix = rand.nextS();
480 REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
481 REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
482 REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
483 REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
484
485 SkScalar sx = rand.nextSScalar1();
486 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
487 REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
488 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
489 REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
490 }
491}
492
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000493DEF_TEST(Math, reporter) {
reed@android.comed673312009-02-27 16:24:51 +0000494 int i;
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000495 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +0000496
reed@android.comed673312009-02-27 16:24:51 +0000497 // these should assert
498#if 0
499 SkToS8(128);
500 SkToS8(-129);
501 SkToU8(256);
502 SkToU8(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000503
reed@android.comed673312009-02-27 16:24:51 +0000504 SkToS16(32768);
505 SkToS16(-32769);
506 SkToU16(65536);
507 SkToU16(-5);
reed@android.com80e39a72009-04-02 16:59:40 +0000508
reed@android.comed673312009-02-27 16:24:51 +0000509 if (sizeof(size_t) > 4) {
510 SkToS32(4*1024*1024);
511 SkToS32(-4*1024*1024);
512 SkToU32(5*1024*1024);
513 SkToU32(-5);
514 }
515#endif
reed@android.com80e39a72009-04-02 16:59:40 +0000516
reed@android.comed673312009-02-27 16:24:51 +0000517 test_muldiv255(reporter);
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000518 test_muldiv255ceiling(reporter);
reed@android.comf0ad0862010-02-09 19:18:38 +0000519 test_copysign(reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000520
reed@android.comed673312009-02-27 16:24:51 +0000521 {
522 SkScalar x = SK_ScalarNaN;
523 REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
524 }
reed@android.com80e39a72009-04-02 16:59:40 +0000525
reed@android.comed673312009-02-27 16:24:51 +0000526 for (i = 0; i < 1000; i++) {
527 int value = rand.nextS16();
528 int max = rand.nextU16();
reed@android.com80e39a72009-04-02 16:59:40 +0000529
reed@android.comed673312009-02-27 16:24:51 +0000530 int clamp = SkClampMax(value, max);
531 int clamp2 = value < 0 ? 0 : (value > max ? max : value);
532 REPORTER_ASSERT(reporter, clamp == clamp2);
533 }
reed@android.com80e39a72009-04-02 16:59:40 +0000534
reed@android.come72fee52009-11-16 14:52:01 +0000535 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000536 SkPoint p;
reed@android.com80e39a72009-04-02 16:59:40 +0000537
tomhudson@google.com75589252012-04-10 17:42:21 +0000538 // These random values are being treated as 32-bit-patterns, not as
539 // ints; calling SkIntToScalar() here produces crashes.
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000540 p.setLength((SkScalar) rand.nextS(),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000541 (SkScalar) rand.nextS(),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000542 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000543 check_length(reporter, p, SK_Scalar1);
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000544 p.setLength((SkScalar) (rand.nextS() >> 13),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000545 (SkScalar) (rand.nextS() >> 13),
robertphillips@google.com4debcac2012-05-14 16:33:36 +0000546 SK_Scalar1);
reed@android.comed673312009-02-27 16:24:51 +0000547 check_length(reporter, p, SK_Scalar1);
548 }
reed@android.com80e39a72009-04-02 16:59:40 +0000549
reed@android.comed673312009-02-27 16:24:51 +0000550 {
551 SkFixed result = SkFixedDiv(100, 100);
552 REPORTER_ASSERT(reporter, result == SK_Fixed1);
553 result = SkFixedDiv(1, SK_Fixed1);
554 REPORTER_ASSERT(reporter, result == 1);
555 }
reed@android.com80e39a72009-04-02 16:59:40 +0000556
reed@android.comed673312009-02-27 16:24:51 +0000557 unittest_fastfloat(reporter);
reed@google.com077910e2011-02-08 21:56:39 +0000558 unittest_isfinite(reporter);
jvanverth93679922014-11-26 13:15:59 -0800559 unittest_half(reporter);
mtkleina766ca82016-01-26 07:40:30 -0800560 test_rsqrt(reporter, sk_float_rsqrt);
561 test_rsqrt(reporter, sk_float_rsqrt_portable);
reed@android.com80e39a72009-04-02 16:59:40 +0000562
reed@android.come72fee52009-11-16 14:52:01 +0000563 for (i = 0; i < 10000; i++) {
reed@android.comed673312009-02-27 16:24:51 +0000564 SkFixed numer = rand.nextS();
565 SkFixed denom = rand.nextS();
566 SkFixed result = SkFixedDiv(numer, denom);
caryclark3127c992015-12-09 12:02:30 -0800567 int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
reed@android.com80e39a72009-04-02 16:59:40 +0000568
reed@android.comed673312009-02-27 16:24:51 +0000569 (void)SkCLZ(numer);
570 (void)SkCLZ(denom);
reed@android.com80e39a72009-04-02 16:59:40 +0000571
reed@android.comed673312009-02-27 16:24:51 +0000572 REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
573 if (check > SK_MaxS32) {
574 check = SK_MaxS32;
575 } else if (check < -SK_MaxS32) {
576 check = SK_MinS32;
577 }
mtklein97ca98d2015-03-18 11:32:21 -0700578 if (result != (int32_t)check) {
579 ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
580 }
reed@android.comed673312009-02-27 16:24:51 +0000581 REPORTER_ASSERT(reporter, result == (int32_t)check);
reed@android.comed673312009-02-27 16:24:51 +0000582 }
reed@android.com80e39a72009-04-02 16:59:40 +0000583
reed@google.com0abb4992011-10-06 20:04:36 +0000584 test_blend(reporter);
reed@google.com8d7e39c2011-11-09 17:12:08 +0000585
humper@google.com05af1af2013-01-07 16:47:43 +0000586 if (false) test_floor(reporter);
reed@google.coma7d74612012-05-30 12:30:09 +0000587
reed@google.com8d7e39c2011-11-09 17:12:08 +0000588 // disable for now
caryclark@google.com42639cd2012-06-06 12:03:39 +0000589 if (false) test_blend31(); // avoid bit rot, suppress warning
reed@google.comea774d22013-04-22 20:21:56 +0000590
591 test_muldivround(reporter);
reed@google.comc21f86f2013-04-29 14:18:23 +0000592 test_clz(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000593}
594
reed@google.comc9f81662013-05-03 18:06:31 +0000595template <typename T> struct PairRec {
596 T fYin;
597 T fYang;
598};
599
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000600DEF_TEST(TestEndian, reporter) {
reed@google.comc9f81662013-05-03 18:06:31 +0000601 static const PairRec<uint16_t> g16[] = {
602 { 0x0, 0x0 },
603 { 0xFFFF, 0xFFFF },
604 { 0x1122, 0x2211 },
605 };
606 static const PairRec<uint32_t> g32[] = {
607 { 0x0, 0x0 },
608 { 0xFFFFFFFF, 0xFFFFFFFF },
609 { 0x11223344, 0x44332211 },
610 };
611 static const PairRec<uint64_t> g64[] = {
612 { 0x0, 0x0 },
613 { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
614 { 0x1122334455667788ULL, 0x8877665544332211ULL },
615 };
616
617 REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
618 REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
619 REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
620
621 for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
622 REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
623 }
624 for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
625 REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
626 }
627 for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
628 REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
629 }
630}
631
commit-bot@chromium.org2c86fbb2013-09-26 19:22:54 +0000632template <typename T>
633static void test_divmod(skiatest::Reporter* r) {
634 const struct {
635 T numer;
636 T denom;
637 } kEdgeCases[] = {
638 {(T)17, (T)17},
639 {(T)17, (T)4},
640 {(T)0, (T)17},
641 // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
642 {(T)-17, (T)-17},
643 {(T)-17, (T)4},
644 {(T)17, (T)-4},
645 {(T)-17, (T)-4},
646 };
647
648 for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
649 const T numer = kEdgeCases[i].numer;
650 const T denom = kEdgeCases[i].denom;
651 T div, mod;
652 SkTDivMod(numer, denom, &div, &mod);
653 REPORTER_ASSERT(r, numer/denom == div);
654 REPORTER_ASSERT(r, numer%denom == mod);
655 }
656
657 SkRandom rand;
658 for (size_t i = 0; i < 10000; i++) {
659 const T numer = (T)rand.nextS();
660 T denom = 0;
661 while (0 == denom) {
662 denom = (T)rand.nextS();
663 }
664 T div, mod;
665 SkTDivMod(numer, denom, &div, &mod);
666 REPORTER_ASSERT(r, numer/denom == div);
667 REPORTER_ASSERT(r, numer%denom == mod);
668 }
669}
670
671DEF_TEST(divmod_u8, r) {
672 test_divmod<uint8_t>(r);
673}
674
675DEF_TEST(divmod_u16, r) {
676 test_divmod<uint16_t>(r);
677}
678
679DEF_TEST(divmod_u32, r) {
680 test_divmod<uint32_t>(r);
681}
682
683DEF_TEST(divmod_u64, r) {
684 test_divmod<uint64_t>(r);
685}
686
687DEF_TEST(divmod_s8, r) {
688 test_divmod<int8_t>(r);
689}
690
691DEF_TEST(divmod_s16, r) {
692 test_divmod<int16_t>(r);
693}
694
695DEF_TEST(divmod_s32, r) {
696 test_divmod<int32_t>(r);
697}
698
699DEF_TEST(divmod_s64, r) {
700 test_divmod<int64_t>(r);
701}
benjaminwagner70f1a6c2016-04-07 09:23:11 -0700702
703DEF_TEST(PinToFixed, reporter) {
704 // double
705 REPORTER_ASSERT(reporter, 0 == SkDoublePinToFixed(0.0));
706 REPORTER_ASSERT(reporter, 0x10000 == SkDoublePinToFixed(1.0));
707 REPORTER_ASSERT(reporter, 0x7FFFFFFE == SkDoublePinToFixed(32767.999984741));
708 REPORTER_ASSERT(reporter, 0x7FFFFFFF == SkDoublePinToFixed(32767.999984742));
709 REPORTER_ASSERT(reporter, 0x7FFFFFFF == SkDoublePinToFixed(32767.999999999));
710 REPORTER_ASSERT(reporter, 0x7FFFFFFF == SkDoublePinToFixed(32768.0));
711 REPORTER_ASSERT(reporter, 0x7FFFFFFF == SkDoublePinToFixed(5e10));
712 REPORTER_ASSERT(reporter, 0x7FFFFFFF == SkDoublePinToFixed(DBL_MAX));
713 REPORTER_ASSERT(reporter, -0x10000 == SkDoublePinToFixed(-1.0));
714 // SK_FixedMin is defined to be -SK_FixedMax.
715 REPORTER_ASSERT(reporter, -0x7FFFFFFE == SkDoublePinToFixed(-32767.999984741));
716 REPORTER_ASSERT(reporter, -0x7FFFFFFF == SkDoublePinToFixed(-32767.999984742));
717 REPORTER_ASSERT(reporter, -0x7FFFFFFF == SkDoublePinToFixed(-32767.999999999));
718 REPORTER_ASSERT(reporter, -0x7FFFFFFF == SkDoublePinToFixed(-32768.0));
719 REPORTER_ASSERT(reporter, -0x7FFFFFFF == SkDoublePinToFixed(-5e10));
720 REPORTER_ASSERT(reporter, -0x7FFFFFFF == SkDoublePinToFixed(-DBL_MAX));
721
722 // float
723 REPORTER_ASSERT(reporter, 0 == SkFloatPinToFixed(0.0f));
724 REPORTER_ASSERT(reporter, 0x10000 == SkFloatPinToFixed(1.0f));
725 // SkFixed has more precision than float near SK_FixedMax, so SkFloatPinToFixed will never
726 // produce output between 0x7FFFFF80 and 0x7FFFFFFF.
727 REPORTER_ASSERT(reporter, 0x7FFFFF80 == SkFloatPinToFixed(32767.9990f));
728 REPORTER_ASSERT(reporter, 0x7FFFFFFF == SkFloatPinToFixed(32767.9991f));
729 REPORTER_ASSERT(reporter, 0x7FFFFFFF == SkFloatPinToFixed(32768.0f));
730 REPORTER_ASSERT(reporter, 0x7FFFFFFF == SkFloatPinToFixed(5e10f));
731 REPORTER_ASSERT(reporter, 0x7FFFFFFF == SkFloatPinToFixed(FLT_MAX));
732 REPORTER_ASSERT(reporter, -0x10000 == SkFloatPinToFixed(-1.0f));
733 // SK_FixedMin is defined to be -SK_FixedMax.
734 REPORTER_ASSERT(reporter, -0x7FFFFF80 == SkFloatPinToFixed(-32767.9990f));
735 REPORTER_ASSERT(reporter, -0x7FFFFFFF == SkFloatPinToFixed(-32767.9991f));
736 REPORTER_ASSERT(reporter, -0x7FFFFFFF == SkFloatPinToFixed(-32768.0f));
737 REPORTER_ASSERT(reporter, -0x7FFFFFFF == SkFloatPinToFixed(-5e10f));
738 REPORTER_ASSERT(reporter, -0x7FFFFFFF == SkFloatPinToFixed(-FLT_MAX));
739}