tarith.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /* tarith.c
  2. *
  3. * Copyright (c) 2011-2013 John Cunningham Bowler
  4. *
  5. * Last changed in libpng 1.6.0 [February 14, 2013]
  6. *
  7. * This code is released under the libpng license.
  8. * For conditions of distribution and use, see the disclaimer
  9. * and license in png.h
  10. *
  11. * Test internal arithmetic functions of libpng.
  12. *
  13. * This code must be linked against a math library (-lm), but does not require
  14. * libpng or zlib to work. Because it includes the complete source of 'png.c'
  15. * it tests the code with whatever compiler options are used to build it.
  16. * Changing these options can substantially change the errors in the
  17. * calculations that the compiler chooses!
  18. */
  19. #define _POSIX_SOURCE 1
  20. #define _ISOC99_SOURCE 1
  21. /* Obtain a copy of the code to be tested (plus other things), disabling
  22. * stuff that is not required.
  23. */
  24. #include <math.h>
  25. #include <stdlib.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include "../../pngpriv.h"
  30. #define png_error png_warning
  31. void png_warning(png_const_structrp png_ptr, png_const_charp msg)
  32. {
  33. fprintf(stderr, "validation: %s\n", msg);
  34. }
  35. #define png_fixed_error png_fixed_warning
  36. void png_fixed_warning(png_const_structrp png_ptr, png_const_charp msg)
  37. {
  38. fprintf(stderr, "overflow in: %s\n", msg);
  39. }
  40. #define png_set_error_fn(pp, ep, efp, wfp) ((void)0)
  41. #define png_malloc(pp, s) malloc(s)
  42. #define png_malloc_warn(pp, s) malloc(s)
  43. #define png_malloc_base(pp, s) malloc(s)
  44. #define png_calloc(pp, s) calloc(1, (s))
  45. #define png_free(pp, s) free(s)
  46. #define png_safecat(b, sb, pos, str) (pos)
  47. #define png_format_number(start, end, format, number) (start)
  48. #define crc32(crc, pp, s) (crc)
  49. #define inflateReset(zs) Z_OK
  50. #define png_create_struct(type) (0)
  51. #define png_destroy_struct(pp) ((void)0)
  52. #define png_create_struct_2(type, m, mm) (0)
  53. #define png_destroy_struct_2(pp, f, mm) ((void)0)
  54. #undef PNG_SIMPLIFIED_READ_SUPPORTED
  55. #undef PNG_SIMPLIFIED_WRITE_SUPPORTED
  56. #undef PNG_USER_MEM_SUPPORTED
  57. #include "../../png.c"
  58. /* Validate ASCII to fp routines. */
  59. static int verbose = 0;
  60. int validation_ascii_to_fp(int count, int argc, char **argv)
  61. {
  62. int showall = 0;
  63. double max_error=2; /* As a percentage error-in-last-digit/.5 */
  64. double max_error_abs=17; /* Used when precision is DBL_DIG */
  65. double max = 0;
  66. double max_abs = 0;
  67. double test = 0; /* Important to test this. */
  68. int precision = 5;
  69. int nonfinite = 0;
  70. int finite = 0;
  71. int ok = 0;
  72. int failcount = 0;
  73. int minorarith = 0;
  74. while (--argc > 0)
  75. if (strcmp(*++argv, "-a") == 0)
  76. showall = 1;
  77. else if (strcmp(*argv, "-e") == 0 && argc > 0)
  78. {
  79. --argc;
  80. max_error = atof(*++argv);
  81. }
  82. else if (strcmp(*argv, "-E") == 0 && argc > 0)
  83. {
  84. --argc;
  85. max_error_abs = atof(*++argv);
  86. }
  87. else
  88. {
  89. fprintf(stderr, "unknown argument %s\n", *argv);
  90. return 1;
  91. }
  92. do
  93. {
  94. png_size_t index;
  95. int state, failed = 0;
  96. char buffer[64];
  97. if (isfinite(test))
  98. ++finite;
  99. else
  100. ++nonfinite;
  101. if (verbose)
  102. fprintf(stderr, "%.*g %d\n", DBL_DIG, test, precision);
  103. /* Check for overflow in the buffer by setting a marker. */
  104. memset(buffer, 71, sizeof buffer);
  105. png_ascii_from_fp(0, buffer, precision+10, test, precision);
  106. /* Allow for a three digit exponent, this stuff will fail if
  107. * the exponent is bigger than this!
  108. */
  109. if (buffer[precision+7] != 71)
  110. {
  111. fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n", test,
  112. precision, buffer, (unsigned long)strlen(buffer));
  113. failed = 1;
  114. }
  115. /* Following are used for the number parser below and must be
  116. * initialized to zero.
  117. */
  118. state = 0;
  119. index = 0;
  120. if (!isfinite(test))
  121. {
  122. /* Expect 'inf' */
  123. if (test >= 0 && strcmp(buffer, "inf") ||
  124. test < 0 && strcmp(buffer, "-inf"))
  125. {
  126. fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n", test,
  127. precision, buffer);
  128. failed = 1;
  129. }
  130. }
  131. else if (!png_check_fp_number(buffer, precision+10, &state, &index) ||
  132. buffer[index] != 0)
  133. {
  134. fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n", test,
  135. precision, buffer, buffer[index]);
  136. failed = 1;
  137. }
  138. else if (PNG_FP_IS_NEGATIVE(state) && !(test < 0))
  139. {
  140. fprintf(stderr, "%g[%d] -> '%s' but negative value not so reported\n",
  141. test, precision, buffer);
  142. failed = 1;
  143. assert(!PNG_FP_IS_ZERO(state));
  144. assert(!PNG_FP_IS_POSITIVE(state));
  145. }
  146. else if (PNG_FP_IS_ZERO(state) && !(test == 0))
  147. {
  148. fprintf(stderr, "%g[%d] -> '%s' but zero value not so reported\n",
  149. test, precision, buffer);
  150. failed = 1;
  151. assert(!PNG_FP_IS_NEGATIVE(state));
  152. assert(!PNG_FP_IS_POSITIVE(state));
  153. }
  154. else if (PNG_FP_IS_POSITIVE(state) && !(test > 0))
  155. {
  156. fprintf(stderr, "%g[%d] -> '%s' but postive value not so reported\n",
  157. test, precision, buffer);
  158. failed = 1;
  159. assert(!PNG_FP_IS_NEGATIVE(state));
  160. assert(!PNG_FP_IS_ZERO(state));
  161. }
  162. else
  163. {
  164. /* Check the result against the original. */
  165. double out = atof(buffer);
  166. double change = fabs((out - test)/test);
  167. double allow = .5/pow(10,
  168. (precision >= DBL_DIG) ? DBL_DIG-1 : precision-1);
  169. /* NOTE: if you hit this error case are you compiling with gcc
  170. * and -O0? Try -O2 - the errors can accumulate if the FP
  171. * code above is not optimized and may drift outside the .5 in
  172. * DBL_DIG allowed. In any case a small number of errors may
  173. * occur (very small ones - 1 or 2%) because of rounding in the
  174. * calculations, either in the conversion API or in atof.
  175. */
  176. if (change >= allow && (isfinite(out) ||
  177. fabs(test/DBL_MAX) <= 1-allow))
  178. {
  179. double percent = (precision >= DBL_DIG) ? max_error_abs : max_error;
  180. double allowp = (change-allow)*100/allow;
  181. if (precision >= DBL_DIG)
  182. {
  183. if (max_abs < allowp) max_abs = allowp;
  184. }
  185. else
  186. {
  187. if (max < allowp) max = allowp;
  188. }
  189. if (showall || allowp >= percent)
  190. {
  191. fprintf(stderr,
  192. "%.*g[%d] -> '%s' -> %.*g number changed (%g > %g (%d%%))\n",
  193. DBL_DIG, test, precision, buffer, DBL_DIG, out, change, allow,
  194. (int)round(allowp));
  195. failed = 1;
  196. }
  197. else
  198. ++minorarith;
  199. }
  200. }
  201. if (failed)
  202. ++failcount;
  203. else
  204. ++ok;
  205. skip:
  206. /* Generate a new number and precision. */
  207. precision = rand();
  208. if (precision & 1) test = -test;
  209. precision >>= 1;
  210. /* Generate random numbers. */
  211. if (test == 0 || !isfinite(test))
  212. test = precision+1;
  213. else
  214. {
  215. /* Derive the exponent from the previous rand() value. */
  216. int exponent = precision % (DBL_MAX_EXP - DBL_MIN_EXP) + DBL_MIN_EXP;
  217. int tmp;
  218. test = frexp(test * rand(), &tmp);
  219. test = ldexp(test, exponent);
  220. precision >>= 8; /* arbitrary */
  221. }
  222. /* This limits the precision to 32 digits, enough for standard
  223. * IEEE implementations which have at most 15 digits.
  224. */
  225. precision = (precision & 0x1f) + 1;
  226. }
  227. while (--count);
  228. printf("Tested %d finite values, %d non-finite, %d OK (%d failed) %d minor "
  229. "arithmetic errors\n", finite, nonfinite, ok, failcount, minorarith);
  230. printf(" Error with >=%d digit precision %.2f%%\n", DBL_DIG, max_abs);
  231. printf(" Error with < %d digit precision %.2f%%\n", DBL_DIG, max);
  232. return 0;
  233. }
  234. /* Observe that valid FP numbers have the forms listed in the PNG extensions
  235. * specification:
  236. *
  237. * [+,-]{integer,integer.fraction,.fraction}[{e,E}[+,-]integer]
  238. *
  239. * Test each of these in turn, including invalid cases.
  240. */
  241. typedef enum checkfp_state
  242. {
  243. start, fraction, exponent, states
  244. } checkfp_state;
  245. /* The characters (other than digits) that characterize the states: */
  246. static const char none[] = "";
  247. static const char hexdigits[16] = "0123456789ABCDEF";
  248. static const struct
  249. {
  250. const char *start; /* Characters valid at the start */
  251. const char *end; /* Valid characters that end the state */
  252. const char *tests; /* Characters to test after 2 digits seen */
  253. }
  254. state_characters[states] =
  255. {
  256. /* start: */ { "+-.", ".eE", "+-.e*0369" },
  257. /* fraction: */ { none, "eE", "+-.E#0147" },
  258. /* exponent: */ { "+-", none, "+-.eE^0258" }
  259. };
  260. typedef struct
  261. {
  262. char number[1024]; /* Buffer for number being tested */
  263. int limit; /* Command line limit */
  264. int verbose; /* Shadows global variable */
  265. int ctimes; /* Number of numbers tested */
  266. int cmillions; /* Count of millions of numbers */
  267. int cinvalid; /* Invalid strings checked */
  268. int cnoaccept; /* Characters not accepted */
  269. }
  270. checkfp_command;
  271. typedef struct
  272. {
  273. int cnumber; /* Index into number string */
  274. checkfp_state check_state; /* Current number state */
  275. int at_start; /* At start (first character) of state */
  276. int cdigits_in_state; /* Digits seen in that state */
  277. int limit; /* Limit on same for checking all chars */
  278. int state; /* Current parser state */
  279. int is_negative; /* Number is negative */
  280. int is_zero; /* Number is (still) zero */
  281. int number_was_valid; /* Previous character validity */
  282. }
  283. checkfp_control;
  284. static int check_all_characters(checkfp_command *co, checkfp_control c);
  285. static int check_some_characters(checkfp_command *co, checkfp_control c,
  286. const char *tests);
  287. static int check_one_character(checkfp_command *co, checkfp_control c, int ch)
  288. {
  289. /* Test this character (ch) to ensure the parser does the correct thing.
  290. */
  291. png_size_t index = 0;
  292. const char test = (char)ch;
  293. const int number_is_valid = png_check_fp_number(&test, 1, &c.state, &index);
  294. const int character_accepted = (index == 1);
  295. if (c.check_state != exponent && isdigit(ch) && ch != '0')
  296. c.is_zero = 0;
  297. if (c.check_state == start && c.at_start && ch == '-')
  298. c.is_negative = 1;
  299. if (isprint(ch))
  300. co->number[c.cnumber++] = (char)ch;
  301. else
  302. {
  303. co->number[c.cnumber++] = '<';
  304. co->number[c.cnumber++] = hexdigits[(ch >> 4) & 0xf];
  305. co->number[c.cnumber++] = hexdigits[ch & 0xf];
  306. co->number[c.cnumber++] = '>';
  307. }
  308. co->number[c.cnumber] = 0;
  309. if (co->verbose > 1)
  310. fprintf(stderr, "%s\n", co->number);
  311. if (++(co->ctimes) == 1000000)
  312. {
  313. if (co->verbose == 1)
  314. fputc('.', stderr);
  315. co->ctimes = 0;
  316. ++(co->cmillions);
  317. }
  318. if (!number_is_valid)
  319. ++(co->cinvalid);
  320. if (!character_accepted)
  321. ++(co->cnoaccept);
  322. /* This should never fail (it's a serious bug if it does): */
  323. if (index != 0 && index != 1)
  324. {
  325. fprintf(stderr, "%s: read beyond end of string (%lu)\n", co->number,
  326. (unsigned long)index);
  327. return 0;
  328. }
  329. /* Validate the new state, note that the PNG_FP_IS_ macros all return
  330. * false unless the number is valid.
  331. */
  332. if (PNG_FP_IS_NEGATIVE(c.state) !=
  333. (number_is_valid && !c.is_zero && c.is_negative))
  334. {
  335. fprintf(stderr, "%s: negative when it is not\n", co->number);
  336. return 0;
  337. }
  338. if (PNG_FP_IS_ZERO(c.state) != (number_is_valid && c.is_zero))
  339. {
  340. fprintf(stderr, "%s: zero when it is not\n", co->number);
  341. return 0;
  342. }
  343. if (PNG_FP_IS_POSITIVE(c.state) !=
  344. (number_is_valid && !c.is_zero && !c.is_negative))
  345. {
  346. fprintf(stderr, "%s: positive when it is not\n", co->number);
  347. return 0;
  348. }
  349. /* Testing a digit */
  350. if (isdigit(ch))
  351. {
  352. if (!character_accepted)
  353. {
  354. fprintf(stderr, "%s: digit '%c' not accepted\n", co->number, ch);
  355. return 0;
  356. }
  357. if (!number_is_valid)
  358. {
  359. fprintf(stderr, "%s: saw a digit (%c) but number not valid\n",
  360. co->number, ch);
  361. return 0;
  362. }
  363. ++c.cdigits_in_state;
  364. c.at_start = 0;
  365. c.number_was_valid = 1;
  366. /* Continue testing characters in this state. Either test all of
  367. * them or, if we have already seen one digit in this state, just test a
  368. * limited set.
  369. */
  370. if (c.cdigits_in_state < 1)
  371. return check_all_characters(co, c);
  372. else
  373. return check_some_characters(co, c,
  374. state_characters[c.check_state].tests);
  375. }
  376. /* A non-digit; is it allowed here? */
  377. else if (((ch == '+' || ch == '-') && c.check_state != fraction &&
  378. c.at_start) ||
  379. (ch == '.' && c.check_state == start) ||
  380. ((ch == 'e' || ch == 'E') && c.number_was_valid &&
  381. c.check_state != exponent))
  382. {
  383. if (!character_accepted)
  384. {
  385. fprintf(stderr, "%s: character '%c' not accepted\n", co->number, ch);
  386. return 0;
  387. }
  388. /* The number remains valid after start of fraction but nowhere else. */
  389. if (number_is_valid && (c.check_state != start || ch != '.'))
  390. {
  391. fprintf(stderr, "%s: saw a non-digit (%c) but number valid\n",
  392. co->number, ch);
  393. return 0;
  394. }
  395. c.number_was_valid = number_is_valid;
  396. /* Check for a state change. When changing to 'fraction' if the number
  397. * is valid at this point set the at_start to false to allow an exponent
  398. * 'e' to come next.
  399. */
  400. if (c.check_state == start && ch == '.')
  401. {
  402. c.check_state = fraction;
  403. c.at_start = !number_is_valid;
  404. c.cdigits_in_state = 0;
  405. c.limit = co->limit;
  406. return check_all_characters(co, c);
  407. }
  408. else if (c.check_state < exponent && (ch == 'e' || ch == 'E'))
  409. {
  410. c.check_state = exponent;
  411. c.at_start = 1;
  412. c.cdigits_in_state = 0;
  413. c.limit = co->limit;
  414. return check_all_characters(co, c);
  415. }
  416. /* Else it was a sign, and the state doesn't change. */
  417. else
  418. {
  419. if (ch != '-' && ch != '+')
  420. {
  421. fprintf(stderr, "checkfp: internal error (1)\n");
  422. return 0;
  423. }
  424. c.at_start = 0;
  425. return check_all_characters(co, c);
  426. }
  427. }
  428. /* Testing an invalid character */
  429. else
  430. {
  431. if (character_accepted)
  432. {
  433. fprintf(stderr, "%s: character '%c' [0x%.2x] accepted\n", co->number,
  434. ch, ch);
  435. return 0;
  436. }
  437. if (number_is_valid != c.number_was_valid)
  438. {
  439. fprintf(stderr,
  440. "%s: character '%c' [0x%.2x] changed number validity\n", co->number,
  441. ch, ch);
  442. return 0;
  443. }
  444. /* Do nothing - the parser has stuck; return success and keep going with
  445. * the next character.
  446. */
  447. }
  448. /* Successful return (the caller will try the next character.) */
  449. return 1;
  450. }
  451. static int check_all_characters(checkfp_command *co, checkfp_control c)
  452. {
  453. int ch;
  454. if (c.cnumber+4 < sizeof co->number) for (ch=0; ch<256; ++ch)
  455. {
  456. if (!check_one_character(co, c, ch))
  457. return 0;
  458. }
  459. return 1;
  460. }
  461. static int check_some_characters(checkfp_command *co, checkfp_control c,
  462. const char *tests)
  463. {
  464. int i;
  465. --(c.limit);
  466. if (c.cnumber+4 < sizeof co->number && c.limit >= 0)
  467. {
  468. if (c.limit > 0) for (i=0; tests[i]; ++i)
  469. {
  470. if (!check_one_character(co, c, tests[i]))
  471. return 0;
  472. }
  473. /* At the end check all the characters. */
  474. else
  475. return check_all_characters(co, c);
  476. }
  477. return 1;
  478. }
  479. int validation_checkfp(int count, int argc, char **argv)
  480. {
  481. int result;
  482. checkfp_command command;
  483. checkfp_control control;
  484. command.number[0] = 0;
  485. command.limit = 3;
  486. command.verbose = verbose;
  487. command.ctimes = 0;
  488. command.cmillions = 0;
  489. command.cinvalid = 0;
  490. command.cnoaccept = 0;
  491. while (--argc > 0)
  492. {
  493. ++argv;
  494. if (argc > 1 && strcmp(*argv, "-l") == 0)
  495. {
  496. --argc;
  497. command.limit = atoi(*++argv);
  498. }
  499. else
  500. {
  501. fprintf(stderr, "unknown argument %s\n", *argv);
  502. return 1;
  503. }
  504. }
  505. control.cnumber = 0;
  506. control.check_state = start;
  507. control.at_start = 1;
  508. control.cdigits_in_state = 0;
  509. control.limit = command.limit;
  510. control.state = 0;
  511. control.is_negative = 0;
  512. control.is_zero = 1;
  513. control.number_was_valid = 0;
  514. result = check_all_characters(&command, control);
  515. printf("checkfp: %s: checked %d,%.3d,%.3d,%.3d strings (%d invalid)\n",
  516. result ? "pass" : "FAIL", command.cmillions / 1000,
  517. command.cmillions % 1000, command.ctimes / 1000, command.ctimes % 1000,
  518. command.cinvalid);
  519. return result;
  520. }
  521. int validation_muldiv(int count, int argc, char **argv)
  522. {
  523. int tested = 0;
  524. int overflow = 0;
  525. int error = 0;
  526. int error64 = 0;
  527. int passed = 0;
  528. int randbits = 0;
  529. png_uint_32 randbuffer;
  530. png_fixed_point a;
  531. png_int_32 times, div;
  532. while (--argc > 0)
  533. {
  534. fprintf(stderr, "unknown argument %s\n", *++argv);
  535. return 1;
  536. }
  537. /* Find out about the random number generator. */
  538. randbuffer = RAND_MAX;
  539. while (randbuffer != 0) ++randbits, randbuffer >>= 1;
  540. printf("Using random number generator that makes %d bits\n", randbits);
  541. for (div=0; div<32; div += randbits)
  542. randbuffer = (randbuffer << randbits) ^ rand();
  543. a = 0;
  544. times = div = 0;
  545. do
  546. {
  547. png_fixed_point result;
  548. /* NOTE: your mileage may vary, a type is required below that can
  549. * hold 64 bits or more, if floating point is used a 64 bit or
  550. * better mantissa is required.
  551. */
  552. long long int fp, fpround;
  553. unsigned long hi, lo;
  554. int ok;
  555. /* Check the values, png_64bit_product can only handle positive
  556. * numbers, so correct for that here.
  557. */
  558. {
  559. long u1, u2;
  560. int n = 0;
  561. if (a < 0) u1 = -a, n = 1; else u1 = a;
  562. if (times < 0) u2 = -times, n = !n; else u2 = times;
  563. png_64bit_product(u1, u2, &hi, &lo);
  564. if (n)
  565. {
  566. /* -x = ~x+1 */
  567. lo = ((~lo) + 1) & 0xffffffff;
  568. hi = ~hi;
  569. if (lo == 0) ++hi;
  570. }
  571. }
  572. fp = a;
  573. fp *= times;
  574. if ((fp & 0xffffffff) != lo || ((fp >> 32) & 0xffffffff) != hi)
  575. {
  576. fprintf(stderr, "png_64bit_product %d * %d -> %lx|%.8lx not %llx\n",
  577. a, times, hi, lo, fp);
  578. ++error64;
  579. }
  580. if (div != 0)
  581. {
  582. /* Round - this is C round to zero. */
  583. if ((fp < 0) != (div < 0))
  584. fp -= div/2;
  585. else
  586. fp += div/2;
  587. fp /= div;
  588. fpround = fp;
  589. /* Assume 2's complement here: */
  590. ok = fpround <= PNG_UINT_31_MAX &&
  591. fpround >= -1-(long long int)PNG_UINT_31_MAX;
  592. if (!ok) ++overflow;
  593. }
  594. else
  595. ok = 0, ++overflow, fpround = fp/*misleading*/;
  596. if (verbose)
  597. fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n", a, times, div,
  598. fp, ok ? "ok" : "overflow");
  599. ++tested;
  600. if (png_muldiv(&result, a, times, div) != ok)
  601. {
  602. ++error;
  603. if (ok)
  604. fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n", a,
  605. times, div, fp);
  606. else
  607. fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n", a,
  608. times, div, result, fp);
  609. }
  610. else if (ok && result != fpround)
  611. {
  612. ++error;
  613. fprintf(stderr, "%d * %d / %d -> %d not %lld\n", a, times, div, result,
  614. fp);
  615. }
  616. else
  617. ++passed;
  618. /* Generate three new values, this uses rand() and rand() only returns
  619. * up to RAND_MAX.
  620. */
  621. /* CRUDE */
  622. a += times;
  623. times += div;
  624. div = randbuffer;
  625. randbuffer = (randbuffer << randbits) ^ rand();
  626. }
  627. while (--count > 0);
  628. printf("%d tests including %d overflows, %d passed, %d failed (%d 64 bit "
  629. "errors)\n", tested, overflow, passed, error, error64);
  630. return 0;
  631. }
  632. /* When FP is on this just becomes a speed test - compile without FP to get real
  633. * validation.
  634. */
  635. #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
  636. #define LN2 .000010576586617430806112933839 /* log(2)/65536 */
  637. #define L2INV 94548.46219969910586572651 /* 65536/log(2) */
  638. /* For speed testing, need the internal functions too: */
  639. static png_uint_32 png_log8bit(unsigned x)
  640. {
  641. if (x > 0)
  642. return (png_uint_32)floor(.5-log(x/255.)*L2INV);
  643. return 0xffffffff;
  644. }
  645. static png_uint_32 png_log16bit(png_uint_32 x)
  646. {
  647. if (x > 0)
  648. return (png_uint_32)floor(.5-log(x/65535.)*L2INV);
  649. return 0xffffffff;
  650. }
  651. static png_uint_32 png_exp(png_uint_32 x)
  652. {
  653. return (png_uint_32)floor(.5 + exp(x * -LN2) * 0xffffffffU);
  654. }
  655. static png_byte png_exp8bit(png_uint_32 log)
  656. {
  657. return (png_byte)floor(.5 + exp(log * -LN2) * 255);
  658. }
  659. static png_uint_16 png_exp16bit(png_uint_32 log)
  660. {
  661. return (png_uint_16)floor(.5 + exp(log * -LN2) * 65535);
  662. }
  663. #endif /* FLOATING_ARITHMETIC */
  664. int validation_gamma(int argc, char **argv)
  665. {
  666. double gamma[9] = { 2.2, 1.8, 1.52, 1.45, 1., 1/1.45, 1/1.52, 1/1.8, 1/2.2 };
  667. double maxerr;
  668. int i, silent=0, onlygamma=0;
  669. /* Silence the output with -s, just test the gamma functions with -g: */
  670. while (--argc > 0)
  671. if (strcmp(*++argv, "-s") == 0)
  672. silent = 1;
  673. else if (strcmp(*argv, "-g") == 0)
  674. onlygamma = 1;
  675. else
  676. {
  677. fprintf(stderr, "unknown argument %s\n", *argv);
  678. return 1;
  679. }
  680. if (!onlygamma)
  681. {
  682. /* First validate the log functions: */
  683. maxerr = 0;
  684. for (i=0; i<256; ++i)
  685. {
  686. double correct = -log(i/255.)/log(2.)*65536;
  687. double error = png_log8bit(i) - correct;
  688. if (i != 0 && fabs(error) > maxerr)
  689. maxerr = fabs(error);
  690. if (i == 0 && png_log8bit(i) != 0xffffffff ||
  691. i != 0 && png_log8bit(i) != floor(correct+.5))
  692. {
  693. fprintf(stderr, "8 bit log error: %d: got %u, expected %f\n",
  694. i, png_log8bit(i), correct);
  695. }
  696. }
  697. if (!silent)
  698. printf("maximum 8 bit log error = %f\n", maxerr);
  699. maxerr = 0;
  700. for (i=0; i<65536; ++i)
  701. {
  702. double correct = -log(i/65535.)/log(2.)*65536;
  703. double error = png_log16bit(i) - correct;
  704. if (i != 0 && fabs(error) > maxerr)
  705. maxerr = fabs(error);
  706. if (i == 0 && png_log16bit(i) != 0xffffffff ||
  707. i != 0 && png_log16bit(i) != floor(correct+.5))
  708. {
  709. if (error > .68) /* By experiment error is less than .68 */
  710. {
  711. fprintf(stderr, "16 bit log error: %d: got %u, expected %f"
  712. " error: %f\n", i, png_log16bit(i), correct, error);
  713. }
  714. }
  715. }
  716. if (!silent)
  717. printf("maximum 16 bit log error = %f\n", maxerr);
  718. /* Now exponentiations. */
  719. maxerr = 0;
  720. for (i=0; i<=0xfffff; ++i)
  721. {
  722. double correct = exp(-i/65536. * log(2.)) * (65536. * 65536);
  723. double error = png_exp(i) - correct;
  724. if (fabs(error) > maxerr)
  725. maxerr = fabs(error);
  726. if (fabs(error) > 1883) /* By experiment. */
  727. {
  728. fprintf(stderr, "32 bit exp error: %d: got %u, expected %f"
  729. " error: %f\n", i, png_exp(i), correct, error);
  730. }
  731. }
  732. if (!silent)
  733. printf("maximum 32 bit exp error = %f\n", maxerr);
  734. maxerr = 0;
  735. for (i=0; i<=0xfffff; ++i)
  736. {
  737. double correct = exp(-i/65536. * log(2.)) * 255;
  738. double error = png_exp8bit(i) - correct;
  739. if (fabs(error) > maxerr)
  740. maxerr = fabs(error);
  741. if (fabs(error) > .50002) /* By experiment */
  742. {
  743. fprintf(stderr, "8 bit exp error: %d: got %u, expected %f"
  744. " error: %f\n", i, png_exp8bit(i), correct, error);
  745. }
  746. }
  747. if (!silent)
  748. printf("maximum 8 bit exp error = %f\n", maxerr);
  749. maxerr = 0;
  750. for (i=0; i<=0xfffff; ++i)
  751. {
  752. double correct = exp(-i/65536. * log(2.)) * 65535;
  753. double error = png_exp16bit(i) - correct;
  754. if (fabs(error) > maxerr)
  755. maxerr = fabs(error);
  756. if (fabs(error) > .524) /* By experiment */
  757. {
  758. fprintf(stderr, "16 bit exp error: %d: got %u, expected %f"
  759. " error: %f\n", i, png_exp16bit(i), correct, error);
  760. }
  761. }
  762. if (!silent)
  763. printf("maximum 16 bit exp error = %f\n", maxerr);
  764. } /* !onlygamma */
  765. /* Test the overall gamma correction. */
  766. for (i=0; i<9; ++i)
  767. {
  768. unsigned j;
  769. double g = gamma[i];
  770. png_fixed_point gfp = floor(g * PNG_FP_1 + .5);
  771. if (!silent)
  772. printf("Test gamma %f\n", g);
  773. maxerr = 0;
  774. for (j=0; j<256; ++j)
  775. {
  776. double correct = pow(j/255., g) * 255;
  777. png_byte out = png_gamma_8bit_correct(j, gfp);
  778. double error = out - correct;
  779. if (fabs(error) > maxerr)
  780. maxerr = fabs(error);
  781. if (out != floor(correct+.5))
  782. {
  783. fprintf(stderr, "8bit %d ^ %f: got %d expected %f error %f\n",
  784. j, g, out, correct, error);
  785. }
  786. }
  787. if (!silent)
  788. printf("gamma %f: maximum 8 bit error %f\n", g, maxerr);
  789. maxerr = 0;
  790. for (j=0; j<65536; ++j)
  791. {
  792. double correct = pow(j/65535., g) * 65535;
  793. png_uint_16 out = png_gamma_16bit_correct(j, gfp);
  794. double error = out - correct;
  795. if (fabs(error) > maxerr)
  796. maxerr = fabs(error);
  797. if (fabs(error) > 1.62)
  798. {
  799. fprintf(stderr, "16bit %d ^ %f: got %d expected %f error %f\n",
  800. j, g, out, correct, error);
  801. }
  802. }
  803. if (!silent)
  804. printf("gamma %f: maximum 16 bit error %f\n", g, maxerr);
  805. }
  806. return 0;
  807. }
  808. /**************************** VALIDATION TESTS ********************************/
  809. /* Various validation routines are included herein, they require some
  810. * definition for png_warning and png_error, seetings of VALIDATION:
  811. *
  812. * 1: validates the ASCII to floating point conversions
  813. * 2: validates png_muldiv
  814. * 3: accuracy test of fixed point gamma tables
  815. */
  816. /* The following COUNT (10^8) takes about 1 hour on a 1GHz Pentium IV
  817. * processor.
  818. */
  819. #define COUNT 1000000000
  820. int main(int argc, char **argv)
  821. {
  822. int count = COUNT;
  823. while (argc > 1)
  824. {
  825. if (argc > 2 && strcmp(argv[1], "-c") == 0)
  826. {
  827. count = atoi(argv[2]);
  828. argc -= 2;
  829. argv += 2;
  830. }
  831. else if (strcmp(argv[1], "-v") == 0)
  832. {
  833. ++verbose;
  834. --argc;
  835. ++argv;
  836. }
  837. else
  838. break;
  839. }
  840. if (count > 0 && argc > 1)
  841. {
  842. if (strcmp(argv[1], "ascii") == 0)
  843. return validation_ascii_to_fp(count, argc-1, argv+1);
  844. else if (strcmp(argv[1], "checkfp") == 0)
  845. return validation_checkfp(count, argc-1, argv+1);
  846. else if (strcmp(argv[1], "muldiv") == 0)
  847. return validation_muldiv(count, argc-1, argv+1);
  848. else if (strcmp(argv[1], "gamma") == 0)
  849. return validation_gamma(argc-1, argv+1);
  850. }
  851. /* Bad argument: */
  852. fprintf(stderr,
  853. "usage: tarith [-v] [-c count] {ascii,muldiv,gamma} [args]\n");
  854. fprintf(stderr, " arguments: ascii [-a (all results)] [-e error%%]\n");
  855. fprintf(stderr, " checkfp [-l max-number-chars]\n");
  856. fprintf(stderr, " muldiv\n");
  857. fprintf(stderr, " gamma -s (silent) -g (only gamma; no log)\n");
  858. return 1;
  859. }