4 * Copyright (c) 2021 Cosmin Truta
5 * Copyright (c) 2011-2013 John Cunningham Bowler
7 * This code is released under the libpng license.
8 * For conditions of distribution and use, see the disclaimer
11 * Test internal arithmetic functions of libpng.
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!
19 #define _POSIX_SOURCE 1
20 #define _ISOC99_SOURCE 1
22 /* Obtain a copy of the code to be tested (plus other things), disabling
23 * stuff that is not required.
31 #include "../../pngpriv.h"
33 #define png_error png_warning
35 void png_warning(png_const_structrp png_ptr, png_const_charp msg)
37 fprintf(stderr, "validation: %s\n", msg);
40 #define png_fixed_error png_fixed_warning
42 void png_fixed_warning(png_const_structrp png_ptr, png_const_charp msg)
44 fprintf(stderr, "overflow in: %s\n", msg);
47 #define png_set_error_fn(pp, ep, efp, wfp) ((void)0)
48 #define png_malloc(pp, s) malloc(s)
49 #define png_malloc_warn(pp, s) malloc(s)
50 #define png_malloc_base(pp, s) malloc(s)
51 #define png_calloc(pp, s) calloc(1, (s))
52 #define png_free(pp, s) free(s)
54 #define png_safecat(b, sb, pos, str) (pos)
55 #define png_format_number(start, end, format, number) (start)
57 #define crc32(crc, pp, s) (crc)
58 #define inflateReset(zs) Z_OK
60 #define png_create_struct(type) (0)
61 #define png_destroy_struct(pp) ((void)0)
62 #define png_create_struct_2(type, m, mm) (0)
63 #define png_destroy_struct_2(pp, f, mm) ((void)0)
65 #undef PNG_SIMPLIFIED_READ_SUPPORTED
66 #undef PNG_SIMPLIFIED_WRITE_SUPPORTED
67 #undef PNG_USER_MEM_SUPPORTED
69 #include "../../png.c"
71 /* Validate ASCII to fp routines. */
72 static int verbose = 0;
74 int validation_ascii_to_fp(int count, int argc, char **argv)
77 double max_error=2; /* As a percentage error-in-last-digit/.5 */
78 double max_error_abs=17; /* Used when precision is DBL_DIG */
81 double test = 0; /* Important to test this. */
91 if (strcmp(*++argv, "-a") == 0)
93 else if (strcmp(*argv, "-e") == 0 && argc > 0)
96 max_error = atof(*++argv);
98 else if (strcmp(*argv, "-E") == 0 && argc > 0)
101 max_error_abs = atof(*++argv);
105 fprintf(stderr, "unknown argument %s\n", *argv);
113 int state, failed = 0;
122 fprintf(stderr, "%.*g %d\n", DBL_DIG, test, precision);
124 /* Check for overflow in the buffer by setting a marker. */
125 memset(buffer, 71, sizeof buffer);
127 png_ascii_from_fp(0, buffer, precision+10, test, precision);
129 /* Allow for a three digit exponent, this stuff will fail if
130 * the exponent is bigger than this!
132 if (buffer[precision+7] != 71)
134 fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n",
135 test, precision, buffer, (unsigned long)strlen(buffer));
139 /* Following are used for the number parser below and must be
140 * initialized to zero.
147 if (test >= 0 && strcmp(buffer, "inf") ||
148 test < 0 && strcmp(buffer, "-inf"))
150 fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n",
151 test, precision, buffer);
155 else if (!png_check_fp_number(buffer, precision+10, &state, &index) ||
158 fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n",
159 test, precision, buffer, buffer[index]);
162 else if (PNG_FP_IS_NEGATIVE(state) && !(test < 0))
164 fprintf(stderr, "%g[%d] -> '%s' but negative value not so reported\n",
165 test, precision, buffer);
167 assert(!PNG_FP_IS_ZERO(state));
168 assert(!PNG_FP_IS_POSITIVE(state));
170 else if (PNG_FP_IS_ZERO(state) && !(test == 0))
172 fprintf(stderr, "%g[%d] -> '%s' but zero value not so reported\n",
173 test, precision, buffer);
175 assert(!PNG_FP_IS_NEGATIVE(state));
176 assert(!PNG_FP_IS_POSITIVE(state));
178 else if (PNG_FP_IS_POSITIVE(state) && !(test > 0))
180 fprintf(stderr, "%g[%d] -> '%s' but positive value not so reported\n",
181 test, precision, buffer);
183 assert(!PNG_FP_IS_NEGATIVE(state));
184 assert(!PNG_FP_IS_ZERO(state));
188 /* Check the result against the original. */
189 double out = atof(buffer);
190 double change = fabs((out - test)/test);
191 double allow = .5 / pow(10,
192 (precision >= DBL_DIG) ? DBL_DIG-1 : precision-1);
194 /* NOTE: if you hit this error case are you compiling with gcc
195 * and -O0? Try -O2 - the errors can accumulate if the FP
196 * code above is not optimized and may drift outside the .5 in
197 * DBL_DIG allowed. In any case a small number of errors may
198 * occur (very small ones - 1 or 2%) because of rounding in the
199 * calculations, either in the conversion API or in atof.
201 if (change >= allow && (isfinite(out) ||
202 fabs(test/DBL_MAX) <= 1-allow))
204 double percent = (precision >= DBL_DIG) ? max_error_abs : max_error;
205 double allowp = (change-allow)*100/allow;
207 if (precision >= DBL_DIG)
209 if (max_abs < allowp) max_abs = allowp;
214 if (max < allowp) max = allowp;
217 if (showall || allowp >= percent)
220 "%.*g[%d] -> '%s' -> %.*g number changed (%g > %g (%d%%))\n",
221 DBL_DIG, test, precision, buffer, DBL_DIG, out, change, allow,
236 /* Generate a new number and precision. */
238 if (precision & 1) test = -test;
241 /* Generate random numbers. */
242 if (test == 0 || !isfinite(test))
246 /* Derive the exponent from the previous rand() value. */
247 int exponent = precision % (DBL_MAX_EXP - DBL_MIN_EXP) + DBL_MIN_EXP;
249 test = frexp(test * rand(), &tmp);
250 test = ldexp(test, exponent);
251 precision >>= 8; /* arbitrary */
254 /* This limits the precision to 32 digits, enough for standard
255 * IEEE implementations which have at most 15 digits.
257 precision = (precision & 0x1f) + 1;
261 printf("Tested %d finite values, %d non-finite, %d OK (%d failed) "
262 "%d minor arithmetic errors\n",
263 finite, nonfinite, ok, failcount, minorarith);
264 printf(" Error with >=%d digit precision %.2f%%\n", DBL_DIG, max_abs);
265 printf(" Error with < %d digit precision %.2f%%\n", DBL_DIG, max);
270 /* Observe that valid FP numbers have the forms listed in the PNG extensions
273 * [+,-]{integer,integer.fraction,.fraction}[{e,E}[+,-]integer]
275 * Test each of these in turn, including invalid cases.
277 typedef enum checkfp_state
279 start, fraction, exponent, states
282 /* The characters (other than digits) that characterize the states: */
283 static const char none[] = "";
284 static const char hexdigits[16] = "0123456789ABCDEF";
288 const char *start; /* Characters valid at the start */
289 const char *end; /* Valid characters that end the state */
290 const char *tests; /* Characters to test after 2 digits seen */
292 state_characters[states] =
294 /* start: */ { "+-.", ".eE", "+-.e*0369" },
295 /* fraction: */ { none, "eE", "+-.E#0147" },
296 /* exponent: */ { "+-", none, "+-.eE^0258" }
301 char number[1024]; /* Buffer for number being tested */
302 int limit; /* Command line limit */
303 int verbose; /* Shadows global variable */
304 int ctimes; /* Number of numbers tested */
305 int cmillions; /* Count of millions of numbers */
306 int cinvalid; /* Invalid strings checked */
307 int cnoaccept; /* Characters not accepted */
313 int cnumber; /* Index into number string */
314 checkfp_state check_state; /* Current number state */
315 int at_start; /* At start (first character) of state */
316 int cdigits_in_state; /* Digits seen in that state */
317 int limit; /* Limit on same for checking all chars */
318 int state; /* Current parser state */
319 int is_negative; /* Number is negative */
320 int is_zero; /* Number is (still) zero */
321 int number_was_valid; /* Previous character validity */
325 static int check_all_characters(checkfp_command *co, checkfp_control c);
327 static int check_some_characters(checkfp_command *co, checkfp_control c,
330 static int check_one_character(checkfp_command *co, checkfp_control c, int ch)
332 /* Test this character (ch) to ensure the parser does the correct thing.
335 const char test = (char)ch;
336 int number_is_valid = png_check_fp_number(&test, 1, &c.state, &index);
337 int character_accepted = (index == 1);
339 if (c.check_state != exponent && isdigit(ch) && ch != '0')
342 if (c.check_state == start && c.at_start && ch == '-')
346 co->number[c.cnumber++] = (char)ch;
349 co->number[c.cnumber++] = '<';
350 co->number[c.cnumber++] = hexdigits[(ch >> 4) & 0xf];
351 co->number[c.cnumber++] = hexdigits[ch & 0xf];
352 co->number[c.cnumber++] = '>';
354 co->number[c.cnumber] = 0;
357 fprintf(stderr, "%s\n", co->number);
359 if (++(co->ctimes) == 1000000)
361 if (co->verbose == 1)
367 if (!number_is_valid)
370 if (!character_accepted)
373 /* This should never fail (it's a serious bug if it does): */
374 if (index != 0 && index != 1)
376 fprintf(stderr, "%s: read beyond end of string (%lu)\n",
377 co->number, (unsigned long)index);
381 /* Validate the new state, note that the PNG_FP_IS_ macros all return
382 * false unless the number is valid.
384 if (PNG_FP_IS_NEGATIVE(c.state) !=
385 (number_is_valid && !c.is_zero && c.is_negative))
387 fprintf(stderr, "%s: negative when it is not\n", co->number);
391 if (PNG_FP_IS_ZERO(c.state) != (number_is_valid && c.is_zero))
393 fprintf(stderr, "%s: zero when it is not\n", co->number);
397 if (PNG_FP_IS_POSITIVE(c.state) !=
398 (number_is_valid && !c.is_zero && !c.is_negative))
400 fprintf(stderr, "%s: positive when it is not\n", co->number);
404 /* Testing a digit */
407 if (!character_accepted)
409 fprintf(stderr, "%s: digit '%c' not accepted\n", co->number, ch);
413 if (!number_is_valid)
415 fprintf(stderr, "%s: saw a digit (%c) but number not valid\n",
420 ++c.cdigits_in_state;
422 c.number_was_valid = 1;
424 /* Continue testing characters in this state. Either test all of
425 * them or, if we have already seen one digit in this state, just test a
428 if (c.cdigits_in_state < 1)
429 return check_all_characters(co, c);
432 return check_some_characters(co, c,
433 state_characters[c.check_state].tests);
436 /* A non-digit; is it allowed here? */
437 else if (((ch == '+' || ch == '-') && c.check_state != fraction &&
439 (ch == '.' && c.check_state == start) ||
440 ((ch == 'e' || ch == 'E') && c.number_was_valid &&
441 c.check_state != exponent))
443 if (!character_accepted)
445 fprintf(stderr, "%s: character '%c' not accepted\n", co->number, ch);
449 /* The number remains valid after start of fraction but nowhere else. */
450 if (number_is_valid && (c.check_state != start || ch != '.'))
452 fprintf(stderr, "%s: saw a non-digit (%c) but number valid\n",
457 c.number_was_valid = number_is_valid;
459 /* Check for a state change. When changing to 'fraction' if the number
460 * is valid at this point set the at_start to false to allow an exponent
463 if (c.check_state == start && ch == '.')
465 c.check_state = fraction;
466 c.at_start = !number_is_valid;
467 c.cdigits_in_state = 0;
469 return check_all_characters(co, c);
472 else if (c.check_state < exponent && (ch == 'e' || ch == 'E'))
474 c.check_state = exponent;
476 c.cdigits_in_state = 0;
478 return check_all_characters(co, c);
481 /* Else it was a sign, and the state doesn't change. */
484 if (ch != '-' && ch != '+')
486 fprintf(stderr, "checkfp: internal error (1)\n");
491 return check_all_characters(co, c);
495 /* Testing an invalid character */
498 if (character_accepted)
500 fprintf(stderr, "%s: character '%c' [0x%.2x] accepted\n", co->number,
505 if (number_is_valid != c.number_was_valid)
508 "%s: character '%c' [0x%.2x] changed number validity\n",
513 /* Do nothing - the parser has stuck; return success and keep going with
514 * the next character.
518 /* Successful return (the caller will try the next character.) */
522 static int check_all_characters(checkfp_command *co, checkfp_control c)
526 if (c.cnumber+4 < sizeof co->number)
528 for (ch=0; ch<256; ++ch)
530 if (!check_one_character(co, c, ch))
538 static int check_some_characters(checkfp_command *co, checkfp_control c,
545 if (c.cnumber+4 < sizeof co->number && c.limit >= 0)
549 for (i=0; tests[i]; ++i)
551 if (!check_one_character(co, c, tests[i]))
556 /* At the end check all the characters. */
558 return check_all_characters(co, c);
564 int validation_checkfp(int count, int argc, char **argv)
567 checkfp_command command;
568 checkfp_control control;
570 command.number[0] = 0;
572 command.verbose = verbose;
574 command.cmillions = 0;
575 command.cinvalid = 0;
576 command.cnoaccept = 0;
581 if (argc > 1 && strcmp(*argv, "-l") == 0)
584 command.limit = atoi(*++argv);
589 fprintf(stderr, "unknown argument %s\n", *argv);
595 control.check_state = start;
596 control.at_start = 1;
597 control.cdigits_in_state = 0;
598 control.limit = command.limit;
600 control.is_negative = 0;
602 control.number_was_valid = 0;
604 result = check_all_characters(&command, control);
606 printf("checkfp: %s: checked %d,%.3d,%.3d,%.3d strings (%d invalid)\n",
607 result ? "pass" : "FAIL", command.cmillions / 1000,
608 command.cmillions % 1000, command.ctimes / 1000, command.ctimes % 1000,
614 int validation_muldiv(int count, int argc, char **argv)
622 png_uint_32 randbuffer;
624 png_int_32 times, div;
628 fprintf(stderr, "unknown argument %s\n", *++argv);
632 /* Find out about the random number generator. */
633 randbuffer = RAND_MAX;
634 while (randbuffer != 0) ++randbits, randbuffer >>= 1;
635 printf("Using random number generator that makes %d bits\n", randbits);
636 for (div=0; div<32; div += randbits)
637 randbuffer = (randbuffer << randbits) ^ rand();
643 png_fixed_point result;
644 /* NOTE: your mileage may vary, a type is required below that can
645 * hold 64 bits or more, if floating point is used a 64-bit or
646 * better mantissa is required.
648 long long int fp, fpround;
649 unsigned long hi, lo;
652 /* Check the values, png_64bit_product can only handle positive
653 * numbers, so correct for that here.
658 if (a < 0) u1 = -a, n = 1; else u1 = a;
659 if (times < 0) u2 = -times, n = !n; else u2 = times;
660 png_64bit_product(u1, u2, &hi, &lo);
664 lo = ((~lo) + 1) & 0xffffffff;
672 if ((fp & 0xffffffff) != lo || ((fp >> 32) & 0xffffffff) != hi)
674 fprintf(stderr, "png_64bit_product %d * %d -> %lx|%.8lx not %llx\n",
675 a, times, hi, lo, fp);
681 /* Round - this is C round to zero. */
682 if ((fp < 0) != (div < 0))
689 /* Assume 2's complement here: */
690 ok = fpround <= PNG_UINT_31_MAX &&
691 fpround >= -1-(long long int)PNG_UINT_31_MAX;
695 ok = 0, ++overflow, fpround = fp/*misleading*/;
698 fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n",
699 a, times, div, fp, ok ? "ok" : "overflow");
702 if (png_muldiv(&result, a, times, div) != ok)
706 fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n",
709 fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n",
710 a, times, div, result, fp);
712 else if (ok && result != fpround)
715 fprintf(stderr, "%d * %d / %d -> %d not %lld\n",
716 a, times, div, result, fp);
721 /* Generate three new values, this uses rand() and rand() only returns
728 randbuffer = (randbuffer << randbits) ^ rand();
732 printf("%d tests including %d overflows, %d passed, %d failed "
733 "(%d 64-bit errors)\n", tested, overflow, passed, error, error64);
737 /* When FP is on this just becomes a speed test - compile without FP to get real
740 #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
741 #define LN2 .000010576586617430806112933839 /* log(2)/65536 */
742 #define L2INV 94548.46219969910586572651 /* 65536/log(2) */
744 /* For speed testing, need the internal functions too: */
745 static png_uint_32 png_log8bit(unsigned x)
748 return (png_uint_32)floor(.5-log(x/255.)*L2INV);
753 static png_uint_32 png_log16bit(png_uint_32 x)
756 return (png_uint_32)floor(.5-log(x/65535.)*L2INV);
761 static png_uint_32 png_exp(png_uint_32 x)
763 return (png_uint_32)floor(.5 + exp(x * -LN2) * 0xffffffffU);
766 static png_byte png_exp8bit(png_uint_32 log)
768 return (png_byte)floor(.5 + exp(log * -LN2) * 255);
771 static png_uint_16 png_exp16bit(png_uint_32 log)
773 return (png_uint_16)floor(.5 + exp(log * -LN2) * 65535);
775 #endif /* FLOATING_ARITHMETIC */
777 int validation_gamma(int argc, char **argv)
779 double gamma[9] = { 2.2, 1.8, 1.52, 1.45, 1., 1/1.45, 1/1.52, 1/1.8, 1/2.2 };
781 int i, silent=0, onlygamma=0;
783 /* Silence the output with -s, just test the gamma functions with -g: */
785 if (strcmp(*++argv, "-s") == 0)
787 else if (strcmp(*argv, "-g") == 0)
791 fprintf(stderr, "unknown argument %s\n", *argv);
797 /* First validate the log functions: */
799 for (i=0; i<256; ++i)
801 double correct = -log(i/255.)/log(2.)*65536;
802 double error = png_log8bit(i) - correct;
804 if (i != 0 && fabs(error) > maxerr)
805 maxerr = fabs(error);
807 if (i == 0 && png_log8bit(i) != 0xffffffff ||
808 i != 0 && png_log8bit(i) != floor(correct+.5))
810 fprintf(stderr, "8-bit log error: %d: got %u, expected %f\n",
811 i, png_log8bit(i), correct);
816 printf("maximum 8-bit log error = %f\n", maxerr);
819 for (i=0; i<65536; ++i)
821 double correct = -log(i/65535.)/log(2.)*65536;
822 double error = png_log16bit(i) - correct;
824 if (i != 0 && fabs(error) > maxerr)
825 maxerr = fabs(error);
827 if (i == 0 && png_log16bit(i) != 0xffffffff ||
828 i != 0 && png_log16bit(i) != floor(correct+.5))
830 if (error > .68) /* By experiment error is less than .68 */
833 "16-bit log error: %d: got %u, expected %f error: %f\n",
834 i, png_log16bit(i), correct, error);
840 printf("maximum 16-bit log error = %f\n", maxerr);
842 /* Now exponentiations. */
844 for (i=0; i<=0xfffff; ++i)
846 double correct = exp(-i/65536. * log(2.)) * (65536. * 65536);
847 double error = png_exp(i) - correct;
849 if (fabs(error) > maxerr)
850 maxerr = fabs(error);
851 if (fabs(error) > 1883) /* By experiment. */
854 "32-bit exp error: %d: got %u, expected %f error: %f\n",
855 i, png_exp(i), correct, error);
860 printf("maximum 32-bit exp error = %f\n", maxerr);
863 for (i=0; i<=0xfffff; ++i)
865 double correct = exp(-i/65536. * log(2.)) * 255;
866 double error = png_exp8bit(i) - correct;
868 if (fabs(error) > maxerr)
869 maxerr = fabs(error);
870 if (fabs(error) > .50002) /* By experiment */
873 "8-bit exp error: %d: got %u, expected %f error: %f\n",
874 i, png_exp8bit(i), correct, error);
879 printf("maximum 8-bit exp error = %f\n", maxerr);
882 for (i=0; i<=0xfffff; ++i)
884 double correct = exp(-i/65536. * log(2.)) * 65535;
885 double error = png_exp16bit(i) - correct;
887 if (fabs(error) > maxerr)
888 maxerr = fabs(error);
889 if (fabs(error) > .524) /* By experiment */
892 "16-bit exp error: %d: got %u, expected %f error: %f\n",
893 i, png_exp16bit(i), correct, error);
898 printf("maximum 16-bit exp error = %f\n", maxerr);
901 /* Test the overall gamma correction. */
906 png_fixed_point gfp = floor(g * PNG_FP_1 + .5);
909 printf("Test gamma %f\n", g);
912 for (j=0; j<256; ++j)
914 double correct = pow(j/255., g) * 255;
915 png_byte out = png_gamma_8bit_correct(j, gfp);
916 double error = out - correct;
918 if (fabs(error) > maxerr)
919 maxerr = fabs(error);
920 if (out != floor(correct+.5))
922 fprintf(stderr, "8bit %d ^ %f: got %d expected %f error %f\n",
923 j, g, out, correct, error);
928 printf("gamma %f: maximum 8-bit error %f\n", g, maxerr);
931 for (j=0; j<65536; ++j)
933 double correct = pow(j/65535., g) * 65535;
934 png_uint_16 out = png_gamma_16bit_correct(j, gfp);
935 double error = out - correct;
937 if (fabs(error) > maxerr)
938 maxerr = fabs(error);
939 if (fabs(error) > 1.62)
941 fprintf(stderr, "16bit %d ^ %f: got %d expected %f error %f\n",
942 j, g, out, correct, error);
947 printf("gamma %f: maximum 16-bit error %f\n", g, maxerr);
953 /**************************** VALIDATION TESTS ********************************/
954 /* Various validation routines are included herein, they require some
955 * definition for png_warning and png_error, seetings of VALIDATION:
957 * 1: validates the ASCII to floating point conversions
958 * 2: validates png_muldiv
959 * 3: accuracy test of fixed point gamma tables
962 /* The following COUNT (10^8) takes about 1 hour on a 1GHz Pentium IV
965 #define COUNT 1000000000
967 int main(int argc, char **argv)
973 if (argc > 2 && strcmp(argv[1], "-c") == 0)
975 count = atoi(argv[2]);
980 else if (strcmp(argv[1], "-v") == 0)
991 if (count > 0 && argc > 1)
993 if (strcmp(argv[1], "ascii") == 0)
994 return validation_ascii_to_fp(count, argc-1, argv+1);
995 else if (strcmp(argv[1], "checkfp") == 0)
996 return validation_checkfp(count, argc-1, argv+1);
997 else if (strcmp(argv[1], "muldiv") == 0)
998 return validation_muldiv(count, argc-1, argv+1);
999 else if (strcmp(argv[1], "gamma") == 0)
1000 return validation_gamma(argc-1, argv+1);
1005 "usage: tarith [-v] [-c count] {ascii,muldiv,gamma} [args]\n");
1006 fprintf(stderr, " arguments: ascii [-a (all results)] [-e error%%]\n");
1007 fprintf(stderr, " checkfp [-l max-number-chars]\n");
1008 fprintf(stderr, " muldiv\n");
1009 fprintf(stderr, " gamma -s (silent) -g (only gamma; no log)\n");