MathTest.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  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. */
  7. #include "include/core/SkPoint.h"
  8. #include "include/private/SkColorData.h"
  9. #include "include/private/SkFixed.h"
  10. #include "include/private/SkHalf.h"
  11. #include "include/private/SkTo.h"
  12. #include "include/utils/SkRandom.h"
  13. #include "src/core/SkEndian.h"
  14. #include "src/core/SkFDot6.h"
  15. #include "src/core/SkMathPriv.h"
  16. #include "tests/Test.h"
  17. static void test_clz(skiatest::Reporter* reporter) {
  18. REPORTER_ASSERT(reporter, 32 == SkCLZ(0));
  19. REPORTER_ASSERT(reporter, 31 == SkCLZ(1));
  20. REPORTER_ASSERT(reporter, 1 == SkCLZ(1 << 30));
  21. REPORTER_ASSERT(reporter, 0 == SkCLZ(~0U));
  22. SkRandom rand;
  23. for (int i = 0; i < 1000; ++i) {
  24. uint32_t mask = rand.nextU();
  25. // need to get some zeros for testing, but in some obscure way so the
  26. // compiler won't "see" that, and work-around calling the functions.
  27. mask >>= (mask & 31);
  28. int intri = SkCLZ(mask);
  29. int porta = SkCLZ_portable(mask);
  30. REPORTER_ASSERT(reporter, intri == porta);
  31. }
  32. }
  33. ///////////////////////////////////////////////////////////////////////////////
  34. static float sk_fsel(float pred, float result_ge, float result_lt) {
  35. return pred >= 0 ? result_ge : result_lt;
  36. }
  37. static float fast_floor(float x) {
  38. // float big = sk_fsel(x, 0x1.0p+23, -0x1.0p+23);
  39. float big = sk_fsel(x, (float)(1 << 23), -(float)(1 << 23));
  40. return (float)(x + big) - big;
  41. }
  42. static float std_floor(float x) {
  43. return sk_float_floor(x);
  44. }
  45. static void test_floor_value(skiatest::Reporter* reporter, float value) {
  46. float fast = fast_floor(value);
  47. float std = std_floor(value);
  48. if (std != fast) {
  49. ERRORF(reporter, "fast_floor(%.9g) == %.9g != %.9g == std_floor(%.9g)",
  50. value, fast, std, value);
  51. }
  52. }
  53. static void test_floor(skiatest::Reporter* reporter) {
  54. static const float gVals[] = {
  55. 0, 1, 1.1f, 1.01f, 1.001f, 1.0001f, 1.00001f, 1.000001f, 1.0000001f
  56. };
  57. for (size_t i = 0; i < SK_ARRAY_COUNT(gVals); ++i) {
  58. test_floor_value(reporter, gVals[i]);
  59. // test_floor_value(reporter, -gVals[i]);
  60. }
  61. }
  62. ///////////////////////////////////////////////////////////////////////////////
  63. // test that SkMul16ShiftRound and SkMulDiv255Round return the same result
  64. static void test_muldivround(skiatest::Reporter* reporter) {
  65. #if 0
  66. // this "complete" test is too slow, so we test a random sampling of it
  67. for (int a = 0; a <= 32767; ++a) {
  68. for (int b = 0; b <= 32767; ++b) {
  69. unsigned prod0 = SkMul16ShiftRound(a, b, 8);
  70. unsigned prod1 = SkMulDiv255Round(a, b);
  71. SkASSERT(prod0 == prod1);
  72. }
  73. }
  74. #endif
  75. SkRandom rand;
  76. for (int i = 0; i < 10000; ++i) {
  77. unsigned a = rand.nextU() & 0x7FFF;
  78. unsigned b = rand.nextU() & 0x7FFF;
  79. unsigned prod0 = SkMul16ShiftRound(a, b, 8);
  80. unsigned prod1 = SkMulDiv255Round(a, b);
  81. REPORTER_ASSERT(reporter, prod0 == prod1);
  82. }
  83. }
  84. static float float_blend(int src, int dst, float unit) {
  85. return dst + (src - dst) * unit;
  86. }
  87. static int blend31(int src, int dst, int a31) {
  88. return dst + ((src - dst) * a31 * 2114 >> 16);
  89. // return dst + ((src - dst) * a31 * 33 >> 10);
  90. }
  91. static int blend31_slow(int src, int dst, int a31) {
  92. int prod = src * a31 + (31 - a31) * dst + 16;
  93. prod = (prod + (prod >> 5)) >> 5;
  94. return prod;
  95. }
  96. static int blend31_round(int src, int dst, int a31) {
  97. int prod = (src - dst) * a31 + 16;
  98. prod = (prod + (prod >> 5)) >> 5;
  99. return dst + prod;
  100. }
  101. static int blend31_old(int src, int dst, int a31) {
  102. a31 += a31 >> 4;
  103. return dst + ((src - dst) * a31 >> 5);
  104. }
  105. // suppress unused code warning
  106. static int (*blend_functions[])(int, int, int) = {
  107. blend31,
  108. blend31_slow,
  109. blend31_round,
  110. blend31_old
  111. };
  112. static void test_blend31() {
  113. int failed = 0;
  114. int death = 0;
  115. if (false) { // avoid bit rot, suppress warning
  116. failed = (*blend_functions[0])(0,0,0);
  117. }
  118. for (int src = 0; src <= 255; src++) {
  119. for (int dst = 0; dst <= 255; dst++) {
  120. for (int a = 0; a <= 31; a++) {
  121. // int r0 = blend31(src, dst, a);
  122. // int r0 = blend31_round(src, dst, a);
  123. // int r0 = blend31_old(src, dst, a);
  124. int r0 = blend31_slow(src, dst, a);
  125. float f = float_blend(src, dst, a / 31.f);
  126. int r1 = (int)f;
  127. int r2 = SkScalarRoundToInt(f);
  128. if (r0 != r1 && r0 != r2) {
  129. SkDebugf("src:%d dst:%d a:%d result:%d float:%g\n",
  130. src, dst, a, r0, f);
  131. failed += 1;
  132. }
  133. if (r0 > 255) {
  134. death += 1;
  135. SkDebugf("death src:%d dst:%d a:%d result:%d float:%g\n",
  136. src, dst, a, r0, f);
  137. }
  138. }
  139. }
  140. }
  141. SkDebugf("---- failed %d death %d\n", failed, death);
  142. }
  143. static void check_length(skiatest::Reporter* reporter,
  144. const SkPoint& p, SkScalar targetLen) {
  145. float x = SkScalarToFloat(p.fX);
  146. float y = SkScalarToFloat(p.fY);
  147. float len = sk_float_sqrt(x*x + y*y);
  148. len /= SkScalarToFloat(targetLen);
  149. REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f);
  150. }
  151. static void unittest_isfinite(skiatest::Reporter* reporter) {
  152. float nan = sk_float_asin(2);
  153. float inf = SK_ScalarInfinity;
  154. float big = 3.40282e+038f;
  155. REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf));
  156. REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf));
  157. REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf));
  158. REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf));
  159. REPORTER_ASSERT(reporter, SkScalarIsNaN(nan));
  160. REPORTER_ASSERT(reporter, !SkScalarIsNaN(big));
  161. REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big));
  162. REPORTER_ASSERT(reporter, !SkScalarIsNaN(0));
  163. REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan));
  164. REPORTER_ASSERT(reporter, SkScalarIsFinite(big));
  165. REPORTER_ASSERT(reporter, SkScalarIsFinite(-big));
  166. REPORTER_ASSERT(reporter, SkScalarIsFinite(0));
  167. }
  168. static void unittest_half(skiatest::Reporter* reporter) {
  169. static const float gFloats[] = {
  170. 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3,
  171. -0.f, -1.f, -0.5f, -0.499999f, -0.5000001f, -1.f/3
  172. };
  173. for (size_t i = 0; i < SK_ARRAY_COUNT(gFloats); ++i) {
  174. SkHalf h = SkFloatToHalf(gFloats[i]);
  175. float f = SkHalfToFloat(h);
  176. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, gFloats[i]));
  177. }
  178. // check some special values
  179. union FloatUnion {
  180. uint32_t fU;
  181. float fF;
  182. };
  183. static const FloatUnion largestPositiveHalf = { ((142 << 23) | (1023 << 13)) };
  184. SkHalf h = SkFloatToHalf(largestPositiveHalf.fF);
  185. float f = SkHalfToFloat(h);
  186. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestPositiveHalf.fF));
  187. static const FloatUnion largestNegativeHalf = { (1u << 31) | (142u << 23) | (1023u << 13) };
  188. h = SkFloatToHalf(largestNegativeHalf.fF);
  189. f = SkHalfToFloat(h);
  190. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, largestNegativeHalf.fF));
  191. static const FloatUnion smallestPositiveHalf = { 102 << 23 };
  192. h = SkFloatToHalf(smallestPositiveHalf.fF);
  193. f = SkHalfToFloat(h);
  194. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f, smallestPositiveHalf.fF));
  195. static const FloatUnion overflowHalf = { ((143 << 23) | (1023 << 13)) };
  196. h = SkFloatToHalf(overflowHalf.fF);
  197. f = SkHalfToFloat(h);
  198. REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
  199. static const FloatUnion underflowHalf = { 101 << 23 };
  200. h = SkFloatToHalf(underflowHalf.fF);
  201. f = SkHalfToFloat(h);
  202. REPORTER_ASSERT(reporter, f == 0.0f );
  203. static const FloatUnion inf32 = { 255 << 23 };
  204. h = SkFloatToHalf(inf32.fF);
  205. f = SkHalfToFloat(h);
  206. REPORTER_ASSERT(reporter, !SkScalarIsFinite(f) );
  207. static const FloatUnion nan32 = { 255 << 23 | 1 };
  208. h = SkFloatToHalf(nan32.fF);
  209. f = SkHalfToFloat(h);
  210. REPORTER_ASSERT(reporter, SkScalarIsNaN(f) );
  211. }
  212. template <typename RSqrtFn>
  213. static void test_rsqrt(skiatest::Reporter* reporter, RSqrtFn rsqrt) {
  214. const float maxRelativeError = 6.50196699e-4f;
  215. // test close to 0 up to 1
  216. float input = 0.000001f;
  217. for (int i = 0; i < 1000; ++i) {
  218. float exact = 1.0f/sk_float_sqrt(input);
  219. float estimate = rsqrt(input);
  220. float relativeError = sk_float_abs(exact - estimate)/exact;
  221. REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
  222. input += 0.001f;
  223. }
  224. // test 1 to ~100
  225. input = 1.0f;
  226. for (int i = 0; i < 1000; ++i) {
  227. float exact = 1.0f/sk_float_sqrt(input);
  228. float estimate = rsqrt(input);
  229. float relativeError = sk_float_abs(exact - estimate)/exact;
  230. REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
  231. input += 0.01f;
  232. }
  233. // test some big numbers
  234. input = 1000000.0f;
  235. for (int i = 0; i < 100; ++i) {
  236. float exact = 1.0f/sk_float_sqrt(input);
  237. float estimate = rsqrt(input);
  238. float relativeError = sk_float_abs(exact - estimate)/exact;
  239. REPORTER_ASSERT(reporter, relativeError <= maxRelativeError);
  240. input += 754326.f;
  241. }
  242. }
  243. static void test_muldiv255(skiatest::Reporter* reporter) {
  244. for (int a = 0; a <= 255; a++) {
  245. for (int b = 0; b <= 255; b++) {
  246. int ab = a * b;
  247. float s = ab / 255.0f;
  248. int round = (int)floorf(s + 0.5f);
  249. int trunc = (int)floorf(s);
  250. int iround = SkMulDiv255Round(a, b);
  251. int itrunc = SkMulDiv255Trunc(a, b);
  252. REPORTER_ASSERT(reporter, iround == round);
  253. REPORTER_ASSERT(reporter, itrunc == trunc);
  254. REPORTER_ASSERT(reporter, itrunc <= iround);
  255. REPORTER_ASSERT(reporter, iround <= a);
  256. REPORTER_ASSERT(reporter, iround <= b);
  257. }
  258. }
  259. }
  260. static void test_muldiv255ceiling(skiatest::Reporter* reporter) {
  261. for (int c = 0; c <= 255; c++) {
  262. for (int a = 0; a <= 255; a++) {
  263. int product = (c * a + 255);
  264. int expected_ceiling = (product + (product >> 8)) >> 8;
  265. int webkit_ceiling = (c * a + 254) / 255;
  266. REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling);
  267. int skia_ceiling = SkMulDiv255Ceiling(c, a);
  268. REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling);
  269. }
  270. }
  271. }
  272. static void test_copysign(skiatest::Reporter* reporter) {
  273. static const int32_t gTriples[] = {
  274. // x, y, expected result
  275. 0, 0, 0,
  276. 0, 1, 0,
  277. 0, -1, 0,
  278. 1, 0, 1,
  279. 1, 1, 1,
  280. 1, -1, -1,
  281. -1, 0, 1,
  282. -1, 1, 1,
  283. -1, -1, -1,
  284. };
  285. for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) {
  286. REPORTER_ASSERT(reporter,
  287. SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]);
  288. float x = (float)gTriples[i];
  289. float y = (float)gTriples[i+1];
  290. float expected = (float)gTriples[i+2];
  291. REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected);
  292. }
  293. SkRandom rand;
  294. for (int j = 0; j < 1000; j++) {
  295. int ix = rand.nextS();
  296. REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix);
  297. REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix);
  298. REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix);
  299. REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix);
  300. SkScalar sx = rand.nextSScalar1();
  301. REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx);
  302. REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx);
  303. REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx);
  304. REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx);
  305. }
  306. }
  307. static void huge_vector_normalize(skiatest::Reporter* reporter) {
  308. // these values should fail (overflow/underflow) trying to normalize
  309. const SkVector fail[] = {
  310. { 0, 0 },
  311. { SK_ScalarInfinity, 0 }, { 0, SK_ScalarInfinity },
  312. { 0, SK_ScalarNaN }, { SK_ScalarNaN, 0 },
  313. };
  314. for (SkVector v : fail) {
  315. SkVector v2 = v;
  316. if (v2.setLength(1.0f)) {
  317. REPORTER_ASSERT(reporter, !v.setLength(1.0f));
  318. }
  319. }
  320. }
  321. DEF_TEST(Math, reporter) {
  322. int i;
  323. SkRandom rand;
  324. // these should assert
  325. #if 0
  326. SkToS8(128);
  327. SkToS8(-129);
  328. SkToU8(256);
  329. SkToU8(-5);
  330. SkToS16(32768);
  331. SkToS16(-32769);
  332. SkToU16(65536);
  333. SkToU16(-5);
  334. if (sizeof(size_t) > 4) {
  335. SkToS32(4*1024*1024);
  336. SkToS32(-4*1024*1024);
  337. SkToU32(5*1024*1024);
  338. SkToU32(-5);
  339. }
  340. #endif
  341. test_muldiv255(reporter);
  342. test_muldiv255ceiling(reporter);
  343. test_copysign(reporter);
  344. {
  345. SkScalar x = SK_ScalarNaN;
  346. REPORTER_ASSERT(reporter, SkScalarIsNaN(x));
  347. }
  348. for (i = 0; i < 1000; i++) {
  349. int value = rand.nextS() >> 16;
  350. int max = rand.nextU() >> 16;
  351. int clamp = SkClampMax(value, max);
  352. int clamp2 = value < 0 ? 0 : (value > max ? max : value);
  353. REPORTER_ASSERT(reporter, clamp == clamp2);
  354. }
  355. for (i = 0; i < 10000; i++) {
  356. SkPoint p;
  357. // These random values are being treated as 32-bit-patterns, not as
  358. // ints; calling SkIntToScalar() here produces crashes.
  359. p.setLength((SkScalar) rand.nextS(),
  360. (SkScalar) rand.nextS(),
  361. SK_Scalar1);
  362. check_length(reporter, p, SK_Scalar1);
  363. p.setLength((SkScalar) (rand.nextS() >> 13),
  364. (SkScalar) (rand.nextS() >> 13),
  365. SK_Scalar1);
  366. check_length(reporter, p, SK_Scalar1);
  367. }
  368. {
  369. SkFixed result = SkFixedDiv(100, 100);
  370. REPORTER_ASSERT(reporter, result == SK_Fixed1);
  371. result = SkFixedDiv(1, SK_Fixed1);
  372. REPORTER_ASSERT(reporter, result == 1);
  373. result = SkFixedDiv(10 - 1, SK_Fixed1 * 3);
  374. REPORTER_ASSERT(reporter, result == 3);
  375. }
  376. {
  377. REPORTER_ASSERT(reporter, (SkFixedRoundToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
  378. REPORTER_ASSERT(reporter, (SkFixedFloorToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
  379. REPORTER_ASSERT(reporter, (SkFixedCeilToFixed(-SK_Fixed1 * 10) >> 1) == -SK_Fixed1 * 5);
  380. }
  381. huge_vector_normalize(reporter);
  382. unittest_isfinite(reporter);
  383. unittest_half(reporter);
  384. test_rsqrt(reporter, sk_float_rsqrt);
  385. test_rsqrt(reporter, sk_float_rsqrt_portable);
  386. for (i = 0; i < 10000; i++) {
  387. SkFixed numer = rand.nextS();
  388. SkFixed denom = rand.nextS();
  389. SkFixed result = SkFixedDiv(numer, denom);
  390. int64_t check = SkLeftShift((int64_t)numer, 16) / denom;
  391. (void)SkCLZ(numer);
  392. (void)SkCLZ(denom);
  393. REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32);
  394. if (check > SK_MaxS32) {
  395. check = SK_MaxS32;
  396. } else if (check < -SK_MaxS32) {
  397. check = SK_MinS32;
  398. }
  399. if (result != (int32_t)check) {
  400. ERRORF(reporter, "\nFixed Divide: %8x / %8x -> %8x %8x\n", numer, denom, result, check);
  401. }
  402. REPORTER_ASSERT(reporter, result == (int32_t)check);
  403. }
  404. if (false) test_floor(reporter);
  405. // disable for now
  406. if (false) test_blend31(); // avoid bit rot, suppress warning
  407. test_muldivround(reporter);
  408. test_clz(reporter);
  409. }
  410. template <typename T> struct PairRec {
  411. T fYin;
  412. T fYang;
  413. };
  414. DEF_TEST(TestEndian, reporter) {
  415. static const PairRec<uint16_t> g16[] = {
  416. { 0x0, 0x0 },
  417. { 0xFFFF, 0xFFFF },
  418. { 0x1122, 0x2211 },
  419. };
  420. static const PairRec<uint32_t> g32[] = {
  421. { 0x0, 0x0 },
  422. { 0xFFFFFFFF, 0xFFFFFFFF },
  423. { 0x11223344, 0x44332211 },
  424. };
  425. static const PairRec<uint64_t> g64[] = {
  426. { 0x0, 0x0 },
  427. { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFFFFFFFFFULL },
  428. { 0x1122334455667788ULL, 0x8877665544332211ULL },
  429. };
  430. REPORTER_ASSERT(reporter, 0x1122 == SkTEndianSwap16<0x2211>::value);
  431. REPORTER_ASSERT(reporter, 0x11223344 == SkTEndianSwap32<0x44332211>::value);
  432. REPORTER_ASSERT(reporter, 0x1122334455667788ULL == SkTEndianSwap64<0x8877665544332211ULL>::value);
  433. for (size_t i = 0; i < SK_ARRAY_COUNT(g16); ++i) {
  434. REPORTER_ASSERT(reporter, g16[i].fYang == SkEndianSwap16(g16[i].fYin));
  435. }
  436. for (size_t i = 0; i < SK_ARRAY_COUNT(g32); ++i) {
  437. REPORTER_ASSERT(reporter, g32[i].fYang == SkEndianSwap32(g32[i].fYin));
  438. }
  439. for (size_t i = 0; i < SK_ARRAY_COUNT(g64); ++i) {
  440. REPORTER_ASSERT(reporter, g64[i].fYang == SkEndianSwap64(g64[i].fYin));
  441. }
  442. }
  443. template <typename T>
  444. static void test_divmod(skiatest::Reporter* r) {
  445. #if !defined(__MSVC_RUNTIME_CHECKS)
  446. const struct {
  447. T numer;
  448. T denom;
  449. } kEdgeCases[] = {
  450. {(T)17, (T)17},
  451. {(T)17, (T)4},
  452. {(T)0, (T)17},
  453. // For unsigned T these negatives are just some large numbers. Doesn't hurt to test them.
  454. {(T)-17, (T)-17},
  455. {(T)-17, (T)4},
  456. {(T)17, (T)-4},
  457. {(T)-17, (T)-4},
  458. };
  459. for (size_t i = 0; i < SK_ARRAY_COUNT(kEdgeCases); i++) {
  460. const T numer = kEdgeCases[i].numer;
  461. const T denom = kEdgeCases[i].denom;
  462. T div, mod;
  463. SkTDivMod(numer, denom, &div, &mod);
  464. REPORTER_ASSERT(r, numer/denom == div);
  465. REPORTER_ASSERT(r, numer%denom == mod);
  466. }
  467. SkRandom rand;
  468. for (size_t i = 0; i < 10000; i++) {
  469. const T numer = (T)rand.nextS();
  470. T denom = 0;
  471. while (0 == denom) {
  472. denom = (T)rand.nextS();
  473. }
  474. T div, mod;
  475. SkTDivMod(numer, denom, &div, &mod);
  476. REPORTER_ASSERT(r, numer/denom == div);
  477. REPORTER_ASSERT(r, numer%denom == mod);
  478. }
  479. #endif
  480. }
  481. DEF_TEST(divmod_u8, r) {
  482. test_divmod<uint8_t>(r);
  483. }
  484. DEF_TEST(divmod_u16, r) {
  485. test_divmod<uint16_t>(r);
  486. }
  487. DEF_TEST(divmod_u32, r) {
  488. test_divmod<uint32_t>(r);
  489. }
  490. DEF_TEST(divmod_u64, r) {
  491. test_divmod<uint64_t>(r);
  492. }
  493. DEF_TEST(divmod_s8, r) {
  494. test_divmod<int8_t>(r);
  495. }
  496. DEF_TEST(divmod_s16, r) {
  497. test_divmod<int16_t>(r);
  498. }
  499. DEF_TEST(divmod_s32, r) {
  500. test_divmod<int32_t>(r);
  501. }
  502. DEF_TEST(divmod_s64, r) {
  503. test_divmod<int64_t>(r);
  504. }
  505. static void test_nextsizepow2(skiatest::Reporter* r, size_t test, size_t expectedAns) {
  506. size_t ans = GrNextSizePow2(test);
  507. REPORTER_ASSERT(r, ans == expectedAns);
  508. //SkDebugf("0x%zx -> 0x%zx (0x%zx)\n", test, ans, expectedAns);
  509. }
  510. DEF_TEST(GrNextSizePow2, reporter) {
  511. constexpr int kNumSizeTBits = 8 * sizeof(size_t);
  512. size_t test = 0, expectedAns = 1;
  513. test_nextsizepow2(reporter, test, expectedAns);
  514. test = 1; expectedAns = 1;
  515. for (int i = 1; i < kNumSizeTBits; ++i) {
  516. test_nextsizepow2(reporter, test, expectedAns);
  517. test++;
  518. expectedAns <<= 1;
  519. test_nextsizepow2(reporter, test, expectedAns);
  520. test = expectedAns;
  521. }
  522. // For the remaining three tests there is no higher power (of 2)
  523. test = 0x1;
  524. test <<= kNumSizeTBits-1;
  525. test_nextsizepow2(reporter, test, test);
  526. test++;
  527. test_nextsizepow2(reporter, test, test);
  528. test_nextsizepow2(reporter, SIZE_MAX, SIZE_MAX);
  529. }
  530. DEF_TEST(FloatSaturate32, reporter) {
  531. const struct {
  532. float fFloat;
  533. int fExpectedInt;
  534. } recs[] = {
  535. { 0, 0 },
  536. { 100.5f, 100 },
  537. { (float)SK_MaxS32, SK_MaxS32FitsInFloat },
  538. { (float)SK_MinS32, SK_MinS32FitsInFloat },
  539. { SK_MaxS32 * 100.0f, SK_MaxS32FitsInFloat },
  540. { SK_MinS32 * 100.0f, SK_MinS32FitsInFloat },
  541. { SK_ScalarInfinity, SK_MaxS32FitsInFloat },
  542. { SK_ScalarNegativeInfinity, SK_MinS32FitsInFloat },
  543. { SK_ScalarNaN, SK_MaxS32FitsInFloat },
  544. };
  545. for (auto r : recs) {
  546. int i = sk_float_saturate2int(r.fFloat);
  547. REPORTER_ASSERT(reporter, r.fExpectedInt == i);
  548. // ensure that these bound even non-finite values (including NaN)
  549. SkScalar mx = SkTMax<SkScalar>(r.fFloat, 50);
  550. REPORTER_ASSERT(reporter, mx >= 50);
  551. SkScalar mn = SkTMin<SkScalar>(r.fFloat, 50);
  552. REPORTER_ASSERT(reporter, mn <= 50);
  553. SkScalar p = SkTPin<SkScalar>(r.fFloat, 0, 100);
  554. REPORTER_ASSERT(reporter, p >= 0 && p <= 100);
  555. }
  556. }
  557. DEF_TEST(FloatSaturate64, reporter) {
  558. const struct {
  559. float fFloat;
  560. int64_t fExpected64;
  561. } recs[] = {
  562. { 0, 0 },
  563. { 100.5f, 100 },
  564. { (float)SK_MaxS64, SK_MaxS64FitsInFloat },
  565. { (float)SK_MinS64, SK_MinS64FitsInFloat },
  566. { SK_MaxS64 * 100.0f, SK_MaxS64FitsInFloat },
  567. { SK_MinS64 * 100.0f, SK_MinS64FitsInFloat },
  568. { SK_ScalarInfinity, SK_MaxS64FitsInFloat },
  569. { SK_ScalarNegativeInfinity, SK_MinS64FitsInFloat },
  570. { SK_ScalarNaN, SK_MaxS64FitsInFloat },
  571. };
  572. for (auto r : recs) {
  573. int64_t i = sk_float_saturate2int64(r.fFloat);
  574. REPORTER_ASSERT(reporter, r.fExpected64 == i);
  575. }
  576. }
  577. DEF_TEST(DoubleSaturate32, reporter) {
  578. const struct {
  579. double fDouble;
  580. int fExpectedInt;
  581. } recs[] = {
  582. { 0, 0 },
  583. { 100.5, 100 },
  584. { SK_MaxS32, SK_MaxS32 },
  585. { SK_MinS32, SK_MinS32 },
  586. { SK_MaxS32 - 1, SK_MaxS32 - 1 },
  587. { SK_MinS32 + 1, SK_MinS32 + 1 },
  588. { SK_MaxS32 * 100.0, SK_MaxS32 },
  589. { SK_MinS32 * 100.0, SK_MinS32 },
  590. { SK_ScalarInfinity, SK_MaxS32 },
  591. { SK_ScalarNegativeInfinity, SK_MinS32 },
  592. { SK_ScalarNaN, SK_MaxS32 },
  593. };
  594. for (auto r : recs) {
  595. int i = sk_double_saturate2int(r.fDouble);
  596. REPORTER_ASSERT(reporter, r.fExpectedInt == i);
  597. }
  598. }
  599. #if defined(__ARM_NEON)
  600. #include <arm_neon.h>
  601. DEF_TEST(NeonU16Div255, r) {
  602. for (int v = 0; v <= 255*255; v++) {
  603. int want = (v + 127)/255;
  604. uint16x8_t V = vdupq_n_u16(v);
  605. int got = vrshrq_n_u16(vrsraq_n_u16(V, V, 8), 8)[0];
  606. if (got != want) {
  607. SkDebugf("%d -> %d, want %d\n", v, got, want);
  608. }
  609. REPORTER_ASSERT(r, got == want);
  610. }
  611. }
  612. #endif
  613. DEF_TEST(unit_floats, r) {
  614. // pick a non-trivial, non-pow-2 value, to test the loop
  615. float v[13];
  616. constexpr int N = SK_ARRAY_COUNT(v);
  617. // empty array reports true
  618. REPORTER_ASSERT(r, sk_floats_are_unit(v, 0));
  619. SkRandom rand;
  620. for (int outer = 0; outer < 1000; ++outer) {
  621. // check some good values
  622. for (int i = 0; i < N; ++i) {
  623. v[i] = rand.nextUScalar1();
  624. }
  625. const int index = rand.nextU() % N;
  626. REPORTER_ASSERT(r, sk_floats_are_unit(v, N));
  627. v[index] = -0.f;
  628. REPORTER_ASSERT(r, sk_floats_are_unit(v, N));
  629. v[index] = 1.0f;
  630. REPORTER_ASSERT(r, sk_floats_are_unit(v, N));
  631. // check some bad values
  632. const float non_norms[] = {
  633. 1.0000001f, 2, SK_ScalarInfinity, SK_ScalarNaN
  634. };
  635. for (float bad : non_norms) {
  636. v[index] = bad;
  637. REPORTER_ASSERT(r, !sk_floats_are_unit(v, N));
  638. v[index] = -bad;
  639. REPORTER_ASSERT(r, !sk_floats_are_unit(v, N));
  640. }
  641. }
  642. }