fukasawa e60969
fukasawa e60969
/* tarith.c
fukasawa e60969
 *
fukasawa e60969
 * Copyright (c) 2011-2013 John Cunningham Bowler
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.0 [February 14, 2013]
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 *
fukasawa e60969
 * Test internal arithmetic functions of libpng.
fukasawa e60969
 *
fukasawa e60969
 * This code must be linked against a math library (-lm), but does not require
fukasawa e60969
 * libpng or zlib to work.  Because it includes the complete source of 'png.c'
fukasawa e60969
 * it tests the code with whatever compiler options are used to build it.
fukasawa e60969
 * Changing these options can substantially change the errors in the
fukasawa e60969
 * calculations that the compiler chooses!
fukasawa e60969
 */
fukasawa e60969
#define _POSIX_SOURCE 1
fukasawa e60969
#define _ISOC99_SOURCE 1
fukasawa e60969
fukasawa e60969
/* Obtain a copy of the code to be tested (plus other things), disabling
fukasawa e60969
 * stuff that is not required.
fukasawa e60969
 */
fukasawa e60969
#include <math.h></math.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <ctype.h></ctype.h>
fukasawa e60969
#include <string.h></string.h>
fukasawa e60969
#include <assert.h></assert.h>
fukasawa e60969
fukasawa e60969
#include "../../pngpriv.h"
fukasawa e60969
fukasawa e60969
#define png_error png_warning
fukasawa e60969
fukasawa e60969
void png_warning(png_const_structrp png_ptr, png_const_charp msg)
fukasawa e60969
{
fukasawa e60969
   fprintf(stderr, "validation: %s\n", msg);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#define png_fixed_error png_fixed_warning
fukasawa e60969
fukasawa e60969
void png_fixed_warning(png_const_structrp png_ptr, png_const_charp msg)
fukasawa e60969
{
fukasawa e60969
   fprintf(stderr, "overflow in: %s\n", msg);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#define png_set_error_fn(pp, ep, efp, wfp) ((void)0)
fukasawa e60969
#define png_malloc(pp, s) malloc(s)
fukasawa e60969
#define png_malloc_warn(pp, s) malloc(s)
fukasawa e60969
#define png_malloc_base(pp, s) malloc(s)
fukasawa e60969
#define png_calloc(pp, s) calloc(1, (s))
fukasawa e60969
#define png_free(pp, s) free(s)
fukasawa e60969
fukasawa e60969
#define png_safecat(b, sb, pos, str) (pos)
fukasawa e60969
#define png_format_number(start, end, format, number) (start)
fukasawa e60969
fukasawa e60969
#define crc32(crc, pp, s) (crc)
fukasawa e60969
#define inflateReset(zs) Z_OK
fukasawa e60969
fukasawa e60969
#define png_create_struct(type) (0)
fukasawa e60969
#define png_destroy_struct(pp) ((void)0)
fukasawa e60969
#define png_create_struct_2(type, m, mm) (0)
fukasawa e60969
#define png_destroy_struct_2(pp, f, mm) ((void)0)
fukasawa e60969
fukasawa e60969
#undef PNG_SIMPLIFIED_READ_SUPPORTED
fukasawa e60969
#undef PNG_SIMPLIFIED_WRITE_SUPPORTED
fukasawa e60969
#undef PNG_USER_MEM_SUPPORTED
fukasawa e60969
fukasawa e60969
#include "../../png.c"
fukasawa e60969
fukasawa e60969
/* Validate ASCII to fp routines. */
fukasawa e60969
static int verbose = 0;
fukasawa e60969
fukasawa e60969
int validation_ascii_to_fp(int count, int argc, char **argv)
fukasawa e60969
{
fukasawa e60969
   int    showall = 0;
fukasawa e60969
   double max_error=2;      /* As a percentage error-in-last-digit/.5 */
fukasawa e60969
   double max_error_abs=17; /* Used when precision is DBL_DIG */
fukasawa e60969
   double max = 0;
fukasawa e60969
   double max_abs = 0;
fukasawa e60969
   double test = 0; /* Important to test this. */
fukasawa e60969
   int    precision = 5;
fukasawa e60969
   int    nonfinite = 0;
fukasawa e60969
   int    finite = 0;
fukasawa e60969
   int    ok = 0;
fukasawa e60969
   int    failcount = 0;
fukasawa e60969
   int    minorarith = 0;
fukasawa e60969
fukasawa e60969
   while (--argc > 0)
fukasawa e60969
      if (strcmp(*++argv, "-a") == 0)
fukasawa e60969
         showall = 1;
fukasawa e60969
      else if (strcmp(*argv, "-e") == 0 && argc > 0)
fukasawa e60969
      {
fukasawa e60969
         --argc;
fukasawa e60969
         max_error = atof(*++argv);
fukasawa e60969
      }
fukasawa e60969
      else if (strcmp(*argv, "-E") == 0 && argc > 0)
fukasawa e60969
      {
fukasawa e60969
         --argc;
fukasawa e60969
         max_error_abs = atof(*++argv);
fukasawa e60969
      }
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "unknown argument %s\n", *argv);
fukasawa e60969
         return 1;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
   do
fukasawa e60969
   {
fukasawa e60969
      png_size_t index;
fukasawa e60969
      int state, failed = 0;
fukasawa e60969
      char buffer[64];
fukasawa e60969
fukasawa e60969
      if (isfinite(test))
fukasawa e60969
         ++finite;
fukasawa e60969
      else
fukasawa e60969
         ++nonfinite;
fukasawa e60969
fukasawa e60969
      if (verbose)
fukasawa e60969
         fprintf(stderr, "%.*g %d\n", DBL_DIG, test, precision);
fukasawa e60969
fukasawa e60969
      /* Check for overflow in the buffer by setting a marker. */
fukasawa e60969
      memset(buffer, 71, sizeof buffer);
fukasawa e60969
fukasawa e60969
      png_ascii_from_fp(0, buffer, precision+10, test, precision);
fukasawa e60969
fukasawa e60969
      /* Allow for a three digit exponent, this stuff will fail if
fukasawa e60969
       * the exponent is bigger than this!
fukasawa e60969
       */
fukasawa e60969
      if (buffer[precision+7] != 71)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n", test,
fukasawa e60969
            precision, buffer, (unsigned long)strlen(buffer));
fukasawa e60969
         failed = 1;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Following are used for the number parser below and must be
fukasawa e60969
       * initialized to zero.
fukasawa e60969
       */
fukasawa e60969
      state = 0;
fukasawa e60969
      index = 0;
fukasawa e60969
      if (!isfinite(test))
fukasawa e60969
      {
fukasawa e60969
         /* Expect 'inf' */
fukasawa e60969
         if (test >= 0 && strcmp(buffer, "inf") ||
fukasawa e60969
             test <  0 && strcmp(buffer, "-inf"))
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n", test,
fukasawa e60969
               precision, buffer);
fukasawa e60969
            failed = 1;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
      else if (!png_check_fp_number(buffer, precision+10, &state, &index) ||
fukasawa e60969
          buffer[index] != 0)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n", test,
fukasawa e60969
         precision, buffer, buffer[index]);
fukasawa e60969
         failed = 1;
fukasawa e60969
      }
fukasawa e60969
      else if (PNG_FP_IS_NEGATIVE(state) && !(test < 0))
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%g[%d] -> '%s' but negative value not so reported\n",
fukasawa e60969
            test, precision, buffer);
fukasawa e60969
         failed = 1;
fukasawa e60969
         assert(!PNG_FP_IS_ZERO(state));
fukasawa e60969
         assert(!PNG_FP_IS_POSITIVE(state));
fukasawa e60969
      }
fukasawa e60969
      else if (PNG_FP_IS_ZERO(state) && !(test == 0))
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%g[%d] -> '%s' but zero value not so reported\n",
fukasawa e60969
            test, precision, buffer);
fukasawa e60969
         failed = 1;
fukasawa e60969
         assert(!PNG_FP_IS_NEGATIVE(state));
fukasawa e60969
         assert(!PNG_FP_IS_POSITIVE(state));
fukasawa e60969
      }
fukasawa e60969
      else if (PNG_FP_IS_POSITIVE(state) && !(test > 0))
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%g[%d] -> '%s' but postive value not so reported\n",
fukasawa e60969
            test, precision, buffer);
fukasawa e60969
         failed = 1;
fukasawa e60969
         assert(!PNG_FP_IS_NEGATIVE(state));
fukasawa e60969
         assert(!PNG_FP_IS_ZERO(state));
fukasawa e60969
      }
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         /* Check the result against the original. */
fukasawa e60969
         double out = atof(buffer);
fukasawa e60969
         double change = fabs((out - test)/test);
fukasawa e60969
         double allow = .5/pow(10,
fukasawa e60969
            (precision >= DBL_DIG) ? DBL_DIG-1 : precision-1);
fukasawa e60969
fukasawa e60969
         /* NOTE: if you hit this error case are you compiling with gcc
fukasawa e60969
          * and -O0?  Try -O2 - the errors can accumulate if the FP
fukasawa e60969
          * code above is not optimized and may drift outside the .5 in
fukasawa e60969
          * DBL_DIG allowed.  In any case a small number of errors may
fukasawa e60969
          * occur (very small ones - 1 or 2%) because of rounding in the
fukasawa e60969
          * calculations, either in the conversion API or in atof.
fukasawa e60969
          */
fukasawa e60969
         if (change >= allow && (isfinite(out) ||
fukasawa e60969
             fabs(test/DBL_MAX) <= 1-allow))
fukasawa e60969
         {
fukasawa e60969
            double percent = (precision >= DBL_DIG) ? max_error_abs : max_error;
fukasawa e60969
            double allowp = (change-allow)*100/allow;
fukasawa e60969
fukasawa e60969
            if (precision >= DBL_DIG)
fukasawa e60969
            {
fukasawa e60969
               if (max_abs < allowp) max_abs = allowp;
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
            {
fukasawa e60969
               if (max < allowp) max = allowp;
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (showall || allowp >= percent)
fukasawa e60969
            {
fukasawa e60969
               fprintf(stderr,
fukasawa e60969
                  "%.*g[%d] -> '%s' -> %.*g number changed (%g > %g (%d%%))\n",
fukasawa e60969
                  DBL_DIG, test, precision, buffer, DBL_DIG, out, change, allow,
fukasawa e60969
                  (int)round(allowp));
fukasawa e60969
               failed = 1;
fukasawa e60969
            }
fukasawa e60969
            else
fukasawa e60969
               ++minorarith;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (failed)
fukasawa e60969
         ++failcount;
fukasawa e60969
      else
fukasawa e60969
         ++ok;
fukasawa e60969
fukasawa e60969
skip:
fukasawa e60969
      /* Generate a new number and precision. */
fukasawa e60969
      precision = rand();
fukasawa e60969
      if (precision & 1) test = -test;
fukasawa e60969
      precision >>= 1;
fukasawa e60969
fukasawa e60969
      /* Generate random numbers. */
fukasawa e60969
      if (test == 0 || !isfinite(test))
fukasawa e60969
         test = precision+1;
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         /* Derive the exponent from the previous rand() value. */
fukasawa e60969
         int exponent = precision % (DBL_MAX_EXP - DBL_MIN_EXP) + DBL_MIN_EXP;
fukasawa e60969
         int tmp;
fukasawa e60969
         test = frexp(test * rand(), &tmp);
fukasawa e60969
         test = ldexp(test, exponent);
fukasawa e60969
         precision >>= 8; /* arbitrary */
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* This limits the precision to 32 digits, enough for standard
fukasawa e60969
       * IEEE implementations which have at most 15 digits.
fukasawa e60969
       */
fukasawa e60969
      precision = (precision & 0x1f) + 1;
fukasawa e60969
   }
fukasawa e60969
   while (--count);
fukasawa e60969
fukasawa e60969
   printf("Tested %d finite values, %d non-finite, %d OK (%d failed) %d minor "
fukasawa e60969
      "arithmetic errors\n", finite, nonfinite, ok, failcount, minorarith);
fukasawa e60969
   printf(" Error with >=%d digit precision %.2f%%\n", DBL_DIG, max_abs);
fukasawa e60969
   printf(" Error with < %d digit precision %.2f%%\n", DBL_DIG, max);
fukasawa e60969
fukasawa e60969
   return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Observe that valid FP numbers have the forms listed in the PNG extensions
fukasawa e60969
 * specification:
fukasawa e60969
 *
fukasawa e60969
 * [+,-]{integer,integer.fraction,.fraction}[{e,E}[+,-]integer]
fukasawa e60969
 *
fukasawa e60969
 * Test each of these in turn, including invalid cases.
fukasawa e60969
 */
fukasawa e60969
typedef enum checkfp_state
fukasawa e60969
{
fukasawa e60969
   start, fraction, exponent, states
fukasawa e60969
} checkfp_state;
fukasawa e60969
fukasawa e60969
/* The characters (other than digits) that characterize the states: */
fukasawa e60969
static const char none[] = "";
fukasawa e60969
static const char hexdigits[16] = "0123456789ABCDEF";
fukasawa e60969
fukasawa e60969
static const struct
fukasawa e60969
{
fukasawa e60969
   const char *start; /* Characters valid at the start */
fukasawa e60969
   const char *end;   /* Valid characters that end the state */
fukasawa e60969
   const char *tests; /* Characters to test after 2 digits seen */
fukasawa e60969
}
fukasawa e60969
state_characters[states] =
fukasawa e60969
{
fukasawa e60969
   /* start:    */ { "+-.", ".eE", "+-.e*0369" },
fukasawa e60969
   /* fraction: */ { none, "eE",  "+-.E#0147" },
fukasawa e60969
   /* exponent: */ { "+-", none,  "+-.eE^0258" }
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
typedef struct
fukasawa e60969
{
fukasawa e60969
   char number[1024];  /* Buffer for number being tested */
fukasawa e60969
   int  limit;         /* Command line limit */
fukasawa e60969
   int  verbose;       /* Shadows global variable */
fukasawa e60969
   int  ctimes;        /* Number of numbers tested */
fukasawa e60969
   int  cmillions;     /* Count of millions of numbers */
fukasawa e60969
   int  cinvalid;      /* Invalid strings checked */
fukasawa e60969
   int  cnoaccept;     /* Characters not accepted */
fukasawa e60969
}
fukasawa e60969
checkfp_command;
fukasawa e60969
fukasawa e60969
typedef struct
fukasawa e60969
{
fukasawa e60969
   int           cnumber;          /* Index into number string */
fukasawa e60969
   checkfp_state check_state;      /* Current number state */
fukasawa e60969
   int           at_start;         /* At start (first character) of state */
fukasawa e60969
   int           cdigits_in_state; /* Digits seen in that state */
fukasawa e60969
   int           limit;            /* Limit on same for checking all chars */
fukasawa e60969
   int           state;            /* Current parser state */
fukasawa e60969
   int           is_negative;      /* Number is negative */
fukasawa e60969
   int           is_zero;          /* Number is (still) zero */
fukasawa e60969
   int           number_was_valid; /* Previous character validity */
fukasawa e60969
}
fukasawa e60969
checkfp_control;
fukasawa e60969
fukasawa e60969
static int check_all_characters(checkfp_command *co, checkfp_control c);
fukasawa e60969
fukasawa e60969
static int check_some_characters(checkfp_command *co, checkfp_control c,
fukasawa e60969
   const char *tests);
fukasawa e60969
fukasawa e60969
static int check_one_character(checkfp_command *co, checkfp_control c, int ch)
fukasawa e60969
{
fukasawa e60969
   /* Test this character (ch) to ensure the parser does the correct thing.
fukasawa e60969
    */
fukasawa e60969
   png_size_t index = 0;
fukasawa e60969
   const char test = (char)ch;
fukasawa e60969
   const int number_is_valid = png_check_fp_number(&test, 1, &c.state, &index);
fukasawa e60969
   const int character_accepted = (index == 1);
fukasawa e60969
fukasawa e60969
   if (c.check_state != exponent && isdigit(ch) && ch != '0')
fukasawa e60969
      c.is_zero = 0;
fukasawa e60969
fukasawa e60969
   if (c.check_state == start && c.at_start && ch == '-')
fukasawa e60969
      c.is_negative = 1;
fukasawa e60969
fukasawa e60969
   if (isprint(ch))
fukasawa e60969
      co->number[c.cnumber++] = (char)ch;
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      co->number[c.cnumber++] = '<';
fukasawa e60969
      co->number[c.cnumber++] = hexdigits[(ch >> 4) & 0xf];
fukasawa e60969
      co->number[c.cnumber++] = hexdigits[ch & 0xf];
fukasawa e60969
      co->number[c.cnumber++] = '>';
fukasawa e60969
   }
fukasawa e60969
   co->number[c.cnumber] = 0;
fukasawa e60969
fukasawa e60969
   if (co->verbose > 1)
fukasawa e60969
      fprintf(stderr, "%s\n", co->number);
fukasawa e60969
fukasawa e60969
   if (++(co->ctimes) == 1000000)
fukasawa e60969
   {
fukasawa e60969
      if (co->verbose == 1)
fukasawa e60969
         fputc('.', stderr);
fukasawa e60969
      co->ctimes = 0;
fukasawa e60969
      ++(co->cmillions);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (!number_is_valid)
fukasawa e60969
      ++(co->cinvalid);
fukasawa e60969
fukasawa e60969
   if (!character_accepted)
fukasawa e60969
      ++(co->cnoaccept);
fukasawa e60969
fukasawa e60969
   /* This should never fail (it's a serious bug if it does): */
fukasawa e60969
   if (index != 0 && index != 1)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "%s: read beyond end of string (%lu)\n", co->number,
fukasawa e60969
         (unsigned long)index);
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Validate the new state, note that the PNG_FP_IS_ macros all return
fukasawa e60969
    * false unless the number is valid.
fukasawa e60969
    */
fukasawa e60969
   if (PNG_FP_IS_NEGATIVE(c.state) !=
fukasawa e60969
      (number_is_valid && !c.is_zero && c.is_negative))
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "%s: negative when it is not\n", co->number);
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (PNG_FP_IS_ZERO(c.state) != (number_is_valid && c.is_zero))
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "%s: zero when it is not\n", co->number);
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (PNG_FP_IS_POSITIVE(c.state) !=
fukasawa e60969
      (number_is_valid && !c.is_zero && !c.is_negative))
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "%s: positive when it is not\n", co->number);
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Testing a digit */
fukasawa e60969
   if (isdigit(ch))
fukasawa e60969
   {
fukasawa e60969
      if (!character_accepted)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%s: digit '%c' not accepted\n", co->number, ch);
fukasawa e60969
         return 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!number_is_valid)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%s: saw a digit (%c) but number not valid\n",
fukasawa e60969
            co->number, ch);
fukasawa e60969
         return 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      ++c.cdigits_in_state;
fukasawa e60969
      c.at_start = 0;
fukasawa e60969
      c.number_was_valid = 1;
fukasawa e60969
fukasawa e60969
      /* Continue testing characters in this state.  Either test all of
fukasawa e60969
       * them or, if we have already seen one digit in this state, just test a
fukasawa e60969
       * limited set.
fukasawa e60969
       */
fukasawa e60969
      if (c.cdigits_in_state < 1)
fukasawa e60969
         return check_all_characters(co, c);
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         return check_some_characters(co, c,
fukasawa e60969
            state_characters[c.check_state].tests);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* A non-digit; is it allowed here? */
fukasawa e60969
   else if (((ch == '+' || ch == '-') && c.check_state != fraction &&
fukasawa e60969
               c.at_start) ||
fukasawa e60969
            (ch == '.' && c.check_state == start) ||
fukasawa e60969
            ((ch == 'e' || ch == 'E') && c.number_was_valid &&
fukasawa e60969
               c.check_state != exponent))
fukasawa e60969
   {
fukasawa e60969
      if (!character_accepted)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%s: character '%c' not accepted\n", co->number, ch);
fukasawa e60969
         return 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* The number remains valid after start of fraction but nowhere else. */
fukasawa e60969
      if (number_is_valid && (c.check_state != start || ch != '.'))
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%s: saw a non-digit (%c) but number valid\n",
fukasawa e60969
            co->number, ch);
fukasawa e60969
         return 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      c.number_was_valid = number_is_valid;
fukasawa e60969
fukasawa e60969
      /* Check for a state change.  When changing to 'fraction' if the number
fukasawa e60969
       * is valid at this point set the at_start to false to allow an exponent
fukasawa e60969
       * 'e' to come next.
fukasawa e60969
       */
fukasawa e60969
      if (c.check_state == start && ch == '.')
fukasawa e60969
      {
fukasawa e60969
         c.check_state = fraction;
fukasawa e60969
         c.at_start = !number_is_valid;
fukasawa e60969
         c.cdigits_in_state = 0;
fukasawa e60969
         c.limit = co->limit;
fukasawa e60969
         return check_all_characters(co, c);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (c.check_state < exponent && (ch == 'e' || ch == 'E'))
fukasawa e60969
      {
fukasawa e60969
         c.check_state = exponent;
fukasawa e60969
         c.at_start = 1;
fukasawa e60969
         c.cdigits_in_state = 0;
fukasawa e60969
         c.limit = co->limit;
fukasawa e60969
         return check_all_characters(co, c);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Else it was a sign, and the state doesn't change. */
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         if (ch != '-' && ch != '+')
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "checkfp: internal error (1)\n");
fukasawa e60969
            return 0;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         c.at_start = 0;
fukasawa e60969
         return check_all_characters(co, c);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Testing an invalid character */
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      if (character_accepted)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "%s: character '%c' [0x%.2x] accepted\n", co->number,
fukasawa e60969
            ch, ch);
fukasawa e60969
         return 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (number_is_valid != c.number_was_valid)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr,
fukasawa e60969
            "%s: character '%c' [0x%.2x] changed number validity\n", co->number,
fukasawa e60969
            ch, ch);
fukasawa e60969
         return 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Do nothing - the parser has stuck; return success and keep going with
fukasawa e60969
       * the next character.
fukasawa e60969
       */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Successful return (the caller will try the next character.) */
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int check_all_characters(checkfp_command *co, checkfp_control c)
fukasawa e60969
{
fukasawa e60969
   int ch;
fukasawa e60969
fukasawa e60969
   if (c.cnumber+4 < sizeof co->number) for (ch=0; ch<256; ++ch)
fukasawa e60969
   {
fukasawa e60969
      if (!check_one_character(co, c, ch))
fukasawa e60969
         return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int check_some_characters(checkfp_command *co, checkfp_control c,
fukasawa e60969
   const char *tests)
fukasawa e60969
{
fukasawa e60969
   int i;
fukasawa e60969
fukasawa e60969
   --(c.limit);
fukasawa e60969
fukasawa e60969
   if (c.cnumber+4 < sizeof co->number && c.limit >= 0)
fukasawa e60969
   {
fukasawa e60969
      if (c.limit > 0) for (i=0; tests[i]; ++i)
fukasawa e60969
      {
fukasawa e60969
         if (!check_one_character(co, c, tests[i]))
fukasawa e60969
               return 0;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* At the end check all the characters. */
fukasawa e60969
      else
fukasawa e60969
         return check_all_characters(co, c);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int validation_checkfp(int count, int argc, char **argv)
fukasawa e60969
{
fukasawa e60969
   int result;
fukasawa e60969
   checkfp_command command;
fukasawa e60969
   checkfp_control control;
fukasawa e60969
fukasawa e60969
   command.number[0] = 0;
fukasawa e60969
   command.limit = 3;
fukasawa e60969
   command.verbose = verbose;
fukasawa e60969
   command.ctimes = 0;
fukasawa e60969
   command.cmillions = 0;
fukasawa e60969
   command.cinvalid = 0;
fukasawa e60969
   command.cnoaccept = 0;
fukasawa e60969
fukasawa e60969
   while (--argc > 0)
fukasawa e60969
   {
fukasawa e60969
      ++argv;
fukasawa e60969
      if (argc > 1 && strcmp(*argv, "-l") == 0)
fukasawa e60969
      {
fukasawa e60969
         --argc;
fukasawa e60969
         command.limit = atoi(*++argv);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "unknown argument %s\n", *argv);
fukasawa e60969
         return 1;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   control.cnumber = 0;
fukasawa e60969
   control.check_state = start;
fukasawa e60969
   control.at_start = 1;
fukasawa e60969
   control.cdigits_in_state = 0;
fukasawa e60969
   control.limit = command.limit;
fukasawa e60969
   control.state = 0;
fukasawa e60969
   control.is_negative = 0;
fukasawa e60969
   control.is_zero = 1;
fukasawa e60969
   control.number_was_valid = 0;
fukasawa e60969
fukasawa e60969
   result = check_all_characters(&command, control);
fukasawa e60969
fukasawa e60969
   printf("checkfp: %s: checked %d,%.3d,%.3d,%.3d strings (%d invalid)\n",
fukasawa e60969
      result ? "pass" : "FAIL", command.cmillions / 1000,
fukasawa e60969
      command.cmillions % 1000, command.ctimes / 1000, command.ctimes % 1000,
fukasawa e60969
      command.cinvalid);
fukasawa e60969
fukasawa e60969
   return result;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int validation_muldiv(int count, int argc, char **argv)
fukasawa e60969
{
fukasawa e60969
   int tested = 0;
fukasawa e60969
   int overflow = 0;
fukasawa e60969
   int error = 0;
fukasawa e60969
   int error64 = 0;
fukasawa e60969
   int passed = 0;
fukasawa e60969
   int randbits = 0;
fukasawa e60969
   png_uint_32 randbuffer;
fukasawa e60969
   png_fixed_point a;
fukasawa e60969
   png_int_32 times, div;
fukasawa e60969
fukasawa e60969
   while (--argc > 0)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "unknown argument %s\n", *++argv);
fukasawa e60969
         return 1;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
   /* Find out about the random number generator. */
fukasawa e60969
   randbuffer = RAND_MAX;
fukasawa e60969
   while (randbuffer != 0) ++randbits, randbuffer >>= 1;
fukasawa e60969
   printf("Using random number generator that makes %d bits\n", randbits);
fukasawa e60969
   for (div=0; div<32; div += randbits)
fukasawa e60969
      randbuffer = (randbuffer << randbits) ^ rand();
fukasawa e60969
fukasawa e60969
   a = 0;
fukasawa e60969
   times = div = 0;
fukasawa e60969
   do
fukasawa e60969
   {
fukasawa e60969
      png_fixed_point result;
fukasawa e60969
      /* NOTE: your mileage may vary, a type is required below that can
fukasawa e60969
       * hold 64 bits or more, if floating point is used a 64-bit or
fukasawa e60969
       * better mantissa is required.
fukasawa e60969
       */
fukasawa e60969
      long long int fp, fpround;
fukasawa e60969
      unsigned long hi, lo;
fukasawa e60969
      int ok;
fukasawa e60969
fukasawa e60969
      /* Check the values, png_64bit_product can only handle positive
fukasawa e60969
       * numbers, so correct for that here.
fukasawa e60969
       */
fukasawa e60969
      {
fukasawa e60969
         long u1, u2;
fukasawa e60969
         int n = 0;
fukasawa e60969
         if (a < 0) u1 = -a, n = 1; else u1 = a;
fukasawa e60969
         if (times < 0) u2 = -times, n = !n; else u2 = times;
fukasawa e60969
         png_64bit_product(u1, u2, &hi, &lo);
fukasawa e60969
         if (n)
fukasawa e60969
         {
fukasawa e60969
            /* -x = ~x+1 */
fukasawa e60969
            lo = ((~lo) + 1) & 0xffffffff;
fukasawa e60969
            hi = ~hi;
fukasawa e60969
            if (lo == 0) ++hi;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      fp = a;
fukasawa e60969
      fp *= times;
fukasawa e60969
      if ((fp & 0xffffffff) != lo || ((fp >> 32) & 0xffffffff) != hi)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "png_64bit_product %d * %d -> %lx|%.8lx not %llx\n",
fukasawa e60969
            a, times, hi, lo, fp);
fukasawa e60969
         ++error64;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (div != 0)
fukasawa e60969
      {
fukasawa e60969
         /* Round - this is C round to zero. */
fukasawa e60969
         if ((fp < 0) != (div < 0))
fukasawa e60969
           fp -= div/2;
fukasawa e60969
         else
fukasawa e60969
           fp += div/2;
fukasawa e60969
fukasawa e60969
         fp /= div;
fukasawa e60969
         fpround = fp;
fukasawa e60969
         /* Assume 2's complement here: */
fukasawa e60969
         ok = fpround <= PNG_UINT_31_MAX &&
fukasawa e60969
              fpround >= -1-(long long int)PNG_UINT_31_MAX;
fukasawa e60969
         if (!ok) ++overflow;
fukasawa e60969
      }
fukasawa e60969
      else
fukasawa e60969
        ok = 0, ++overflow, fpround = fp/*misleading*/;
fukasawa e60969
fukasawa e60969
      if (verbose)
fukasawa e60969
         fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n", a, times, div,
fukasawa e60969
            fp, ok ? "ok" : "overflow");
fukasawa e60969
fukasawa e60969
      ++tested;
fukasawa e60969
      if (png_muldiv(&result, a, times, div) != ok)
fukasawa e60969
      {
fukasawa e60969
         ++error;
fukasawa e60969
         if (ok)
fukasawa e60969
             fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n", a,
fukasawa e60969
                times, div, fp);
fukasawa e60969
         else
fukasawa e60969
             fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n", a,
fukasawa e60969
                times, div, result, fp);
fukasawa e60969
      }
fukasawa e60969
      else if (ok && result != fpround)
fukasawa e60969
      {
fukasawa e60969
         ++error;
fukasawa e60969
         fprintf(stderr, "%d * %d / %d -> %d not %lld\n", a, times, div, result,
fukasawa e60969
            fp);
fukasawa e60969
      }
fukasawa e60969
      else
fukasawa e60969
         ++passed;
fukasawa e60969
fukasawa e60969
      /* Generate three new values, this uses rand() and rand() only returns
fukasawa e60969
       * up to RAND_MAX.
fukasawa e60969
       */
fukasawa e60969
      /* CRUDE */
fukasawa e60969
      a += times;
fukasawa e60969
      times += div;
fukasawa e60969
      div = randbuffer;
fukasawa e60969
      randbuffer = (randbuffer << randbits) ^ rand();
fukasawa e60969
   }
fukasawa e60969
   while (--count > 0);
fukasawa e60969
fukasawa e60969
   printf("%d tests including %d overflows, %d passed, %d failed (%d 64-bit "
fukasawa e60969
      "errors)\n", tested, overflow, passed, error, error64);
fukasawa e60969
   return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* When FP is on this just becomes a speed test - compile without FP to get real
fukasawa e60969
 * validation.
fukasawa e60969
 */
fukasawa e60969
#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
fukasawa e60969
#define LN2 .000010576586617430806112933839 /* log(2)/65536 */
fukasawa e60969
#define L2INV 94548.46219969910586572651    /* 65536/log(2) */
fukasawa e60969
fukasawa e60969
/* For speed testing, need the internal functions too: */
fukasawa e60969
static png_uint_32 png_log8bit(unsigned x)
fukasawa e60969
{
fukasawa e60969
   if (x > 0)
fukasawa e60969
      return (png_uint_32)floor(.5-log(x/255.)*L2INV);
fukasawa e60969
fukasawa e60969
   return 0xffffffff;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_uint_32 png_log16bit(png_uint_32 x)
fukasawa e60969
{
fukasawa e60969
   if (x > 0)
fukasawa e60969
      return (png_uint_32)floor(.5-log(x/65535.)*L2INV);
fukasawa e60969
fukasawa e60969
   return 0xffffffff;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_uint_32 png_exp(png_uint_32 x)
fukasawa e60969
{
fukasawa e60969
   return (png_uint_32)floor(.5 + exp(x * -LN2) * 0xffffffffU);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_byte png_exp8bit(png_uint_32 log)
fukasawa e60969
{
fukasawa e60969
   return (png_byte)floor(.5 + exp(log * -LN2) * 255);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_uint_16 png_exp16bit(png_uint_32 log)
fukasawa e60969
{
fukasawa e60969
   return (png_uint_16)floor(.5 + exp(log * -LN2) * 65535);
fukasawa e60969
}
fukasawa e60969
#endif /* FLOATING_ARITHMETIC */
fukasawa e60969
fukasawa e60969
int validation_gamma(int argc, char **argv)
fukasawa e60969
{
fukasawa e60969
   double gamma[9] = { 2.2, 1.8, 1.52, 1.45, 1., 1/1.45, 1/1.52, 1/1.8, 1/2.2 };
fukasawa e60969
   double maxerr;
fukasawa e60969
   int i, silent=0, onlygamma=0;
fukasawa e60969
fukasawa e60969
   /* Silence the output with -s, just test the gamma functions with -g: */
fukasawa e60969
   while (--argc > 0)
fukasawa e60969
      if (strcmp(*++argv, "-s") == 0)
fukasawa e60969
         silent = 1;
fukasawa e60969
      else if (strcmp(*argv, "-g") == 0)
fukasawa e60969
         onlygamma = 1;
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "unknown argument %s\n", *argv);
fukasawa e60969
         return 1;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
   if (!onlygamma)
fukasawa e60969
   {
fukasawa e60969
      /* First validate the log functions: */
fukasawa e60969
      maxerr = 0;
fukasawa e60969
      for (i=0; i<256; ++i)
fukasawa e60969
      {
fukasawa e60969
         double correct = -log(i/255.)/log(2.)*65536;
fukasawa e60969
         double error = png_log8bit(i) - correct;
fukasawa e60969
fukasawa e60969
         if (i != 0 && fabs(error) > maxerr)
fukasawa e60969
            maxerr = fabs(error);
fukasawa e60969
fukasawa e60969
         if (i == 0 && png_log8bit(i) != 0xffffffff ||
fukasawa e60969
             i != 0 && png_log8bit(i) != floor(correct+.5))
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "8-bit log error: %d: got %u, expected %f\n",
fukasawa e60969
               i, png_log8bit(i), correct);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!silent)
fukasawa e60969
         printf("maximum 8-bit log error = %f\n", maxerr);
fukasawa e60969
fukasawa e60969
      maxerr = 0;
fukasawa e60969
      for (i=0; i<65536; ++i)
fukasawa e60969
      {
fukasawa e60969
         double correct = -log(i/65535.)/log(2.)*65536;
fukasawa e60969
         double error = png_log16bit(i) - correct;
fukasawa e60969
fukasawa e60969
         if (i != 0 && fabs(error) > maxerr)
fukasawa e60969
            maxerr = fabs(error);
fukasawa e60969
fukasawa e60969
         if (i == 0 && png_log16bit(i) != 0xffffffff ||
fukasawa e60969
             i != 0 && png_log16bit(i) != floor(correct+.5))
fukasawa e60969
         {
fukasawa e60969
            if (error > .68) /* By experiment error is less than .68 */
fukasawa e60969
            {
fukasawa e60969
               fprintf(stderr, "16-bit log error: %d: got %u, expected %f"
fukasawa e60969
                  " error: %f\n", i, png_log16bit(i), correct, error);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!silent)
fukasawa e60969
         printf("maximum 16-bit log error = %f\n", maxerr);
fukasawa e60969
fukasawa e60969
      /* Now exponentiations. */
fukasawa e60969
      maxerr = 0;
fukasawa e60969
      for (i=0; i<=0xfffff; ++i)
fukasawa e60969
      {
fukasawa e60969
         double correct = exp(-i/65536. * log(2.)) * (65536. * 65536);
fukasawa e60969
         double error = png_exp(i) - correct;
fukasawa e60969
fukasawa e60969
         if (fabs(error) > maxerr)
fukasawa e60969
            maxerr = fabs(error);
fukasawa e60969
         if (fabs(error) > 1883) /* By experiment. */
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "32-bit exp error: %d: got %u, expected %f"
fukasawa e60969
                  " error: %f\n", i, png_exp(i), correct, error);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!silent)
fukasawa e60969
         printf("maximum 32-bit exp error = %f\n", maxerr);
fukasawa e60969
fukasawa e60969
      maxerr = 0;
fukasawa e60969
      for (i=0; i<=0xfffff; ++i)
fukasawa e60969
      {
fukasawa e60969
         double correct = exp(-i/65536. * log(2.)) * 255;
fukasawa e60969
         double error = png_exp8bit(i) - correct;
fukasawa e60969
fukasawa e60969
         if (fabs(error) > maxerr)
fukasawa e60969
            maxerr = fabs(error);
fukasawa e60969
         if (fabs(error) > .50002) /* By experiment */
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "8-bit exp error: %d: got %u, expected %f"
fukasawa e60969
                  " error: %f\n", i, png_exp8bit(i), correct, error);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!silent)
fukasawa e60969
         printf("maximum 8-bit exp error = %f\n", maxerr);
fukasawa e60969
fukasawa e60969
      maxerr = 0;
fukasawa e60969
      for (i=0; i<=0xfffff; ++i)
fukasawa e60969
      {
fukasawa e60969
         double correct = exp(-i/65536. * log(2.)) * 65535;
fukasawa e60969
         double error = png_exp16bit(i) - correct;
fukasawa e60969
fukasawa e60969
         if (fabs(error) > maxerr)
fukasawa e60969
            maxerr = fabs(error);
fukasawa e60969
         if (fabs(error) > .524) /* By experiment */
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "16-bit exp error: %d: got %u, expected %f"
fukasawa e60969
                  " error: %f\n", i, png_exp16bit(i), correct, error);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!silent)
fukasawa e60969
         printf("maximum 16-bit exp error = %f\n", maxerr);
fukasawa e60969
   } /* !onlygamma */
fukasawa e60969
fukasawa e60969
   /* Test the overall gamma correction. */
fukasawa e60969
   for (i=0; i<9; ++i)
fukasawa e60969
   {
fukasawa e60969
      unsigned j;
fukasawa e60969
      double g = gamma[i];
fukasawa e60969
      png_fixed_point gfp = floor(g * PNG_FP_1 + .5);
fukasawa e60969
fukasawa e60969
      if (!silent)
fukasawa e60969
         printf("Test gamma %f\n", g);
fukasawa e60969
fukasawa e60969
      maxerr = 0;
fukasawa e60969
      for (j=0; j<256; ++j)
fukasawa e60969
      {
fukasawa e60969
         double correct = pow(j/255., g) * 255;
fukasawa e60969
         png_byte out = png_gamma_8bit_correct(j, gfp);
fukasawa e60969
         double error = out - correct;
fukasawa e60969
fukasawa e60969
         if (fabs(error) > maxerr)
fukasawa e60969
            maxerr = fabs(error);
fukasawa e60969
         if (out != floor(correct+.5))
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "8bit %d ^ %f: got %d expected %f error %f\n",
fukasawa e60969
               j, g, out, correct, error);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!silent)
fukasawa e60969
         printf("gamma %f: maximum 8-bit error %f\n", g, maxerr);
fukasawa e60969
fukasawa e60969
      maxerr = 0;
fukasawa e60969
      for (j=0; j<65536; ++j)
fukasawa e60969
      {
fukasawa e60969
         double correct = pow(j/65535., g) * 65535;
fukasawa e60969
         png_uint_16 out = png_gamma_16bit_correct(j, gfp);
fukasawa e60969
         double error = out - correct;
fukasawa e60969
fukasawa e60969
         if (fabs(error) > maxerr)
fukasawa e60969
            maxerr = fabs(error);
fukasawa e60969
         if (fabs(error) > 1.62)
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "16bit %d ^ %f: got %d expected %f error %f\n",
fukasawa e60969
               j, g, out, correct, error);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (!silent)
fukasawa e60969
         printf("gamma %f: maximum 16-bit error %f\n", g, maxerr);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/**************************** VALIDATION TESTS ********************************/
fukasawa e60969
/* Various validation routines are included herein, they require some
fukasawa e60969
 * definition for png_warning and png_error, seetings of VALIDATION:
fukasawa e60969
 *
fukasawa e60969
 * 1: validates the ASCII to floating point conversions
fukasawa e60969
 * 2: validates png_muldiv
fukasawa e60969
 * 3: accuracy test of fixed point gamma tables
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
/* The following COUNT (10^8) takes about 1 hour on a 1GHz Pentium IV
fukasawa e60969
 * processor.
fukasawa e60969
 */
fukasawa e60969
#define COUNT 1000000000
fukasawa e60969
fukasawa e60969
int main(int argc, char **argv)
fukasawa e60969
{
fukasawa e60969
   int count = COUNT;
fukasawa e60969
fukasawa e60969
   while (argc > 1)
fukasawa e60969
   {
fukasawa e60969
      if (argc > 2 && strcmp(argv[1], "-c") == 0)
fukasawa e60969
      {
fukasawa e60969
         count = atoi(argv[2]);
fukasawa e60969
         argc -= 2;
fukasawa e60969
         argv += 2;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else if (strcmp(argv[1], "-v") == 0)
fukasawa e60969
      {
fukasawa e60969
         ++verbose;
fukasawa e60969
         --argc;
fukasawa e60969
         ++argv;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (count > 0 && argc > 1)
fukasawa e60969
   {
fukasawa e60969
      if (strcmp(argv[1], "ascii") == 0)
fukasawa e60969
         return validation_ascii_to_fp(count, argc-1, argv+1);
fukasawa e60969
      else if (strcmp(argv[1], "checkfp") == 0)
fukasawa e60969
         return validation_checkfp(count, argc-1, argv+1);
fukasawa e60969
      else if (strcmp(argv[1], "muldiv") == 0)
fukasawa e60969
         return validation_muldiv(count, argc-1, argv+1);
fukasawa e60969
      else if (strcmp(argv[1], "gamma") == 0)
fukasawa e60969
         return validation_gamma(argc-1, argv+1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Bad argument: */
fukasawa e60969
   fprintf(stderr,
fukasawa e60969
      "usage: tarith [-v] [-c count] {ascii,muldiv,gamma} [args]\n");
fukasawa e60969
   fprintf(stderr, " arguments: ascii [-a (all results)] [-e error%%]\n");
fukasawa e60969
   fprintf(stderr, "            checkfp [-l max-number-chars]\n");
fukasawa e60969
   fprintf(stderr, "            muldiv\n");
fukasawa e60969
   fprintf(stderr, "            gamma -s (silent) -g (only gamma; no log)\n");
fukasawa e60969
   return 1;
fukasawa e60969
}