Tweak the size computations and reallocations.
[pspp] / lib / vasnprintf.c
index c60888cdefed45c438985814773d9165a7971b84..6b7e9c39213490790e5911c4c41e607a325d9f68 100644 (file)
@@ -56,6 +56,7 @@
 #if NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
 # include <math.h>
 # include "float+.h"
+# include "fpucw.h"
 #endif
 
 #if NEED_PRINTF_INFINITE_DOUBLE && !defined IN_LIBINTL
@@ -66,6 +67,7 @@
 #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !defined IN_LIBINTL
 # include <math.h>
 # include "isnanl-nolibm.h"
+# include "fpucw.h"
 #endif
 
 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
@@ -105,9 +107,26 @@ local_wcslen (const wchar_t *s)
 # endif
 #endif
 
+/* Define some macros that parametrize the code:
+     VASNPRINTF         The name of the function being defined.
+     FCHAR_T            The element type of the format string.
+     DCHAR_T            The element type of the destination (result) string.
+     TCHAR_T            The element type of the temporary buffer that is
+                        filled with a simple format directive, executed by
+                        the system's sprintf/snprintf (or similar) function.
+     DIRECTIVE          Structure denoting a format directive.
+                        Depends on FCHAR_T.
+     DIRECTIVES         Structure denoting the set of format directives of a
+                        format string.  Depends on FCHAR_T.
+     PRINTF_PARSE       Function that parses a format string.
+                        Depends on FCHAR_T.
+     SNPRINTF           The system's snprintf (or similar) function.
+                        Depends on DCHAR_T.  */
 #if WIDE_CHAR_VERSION
 # define VASNPRINTF vasnwprintf
-# define CHAR_T wchar_t
+# define FCHAR_T wchar_t
+# define DCHAR_T wchar_t
+# define TCHAR_T wchar_t
 # define DIRECTIVE wchar_t_directive
 # define DIRECTIVES wchar_t_directives
 # define PRINTF_PARSE wprintf_parse
@@ -122,7 +141,9 @@ local_wcslen (const wchar_t *s)
 # endif
 #else
 # define VASNPRINTF vasnprintf
-# define CHAR_T char
+# define FCHAR_T char
+# define DCHAR_T char
+# define TCHAR_T char
 # define DIRECTIVE char_directive
 # define DIRECTIVES char_directives
 # define PRINTF_PARSE printf_parse
@@ -1135,8 +1156,8 @@ floorlog10l (long double x)
 
 #endif
 
-CHAR_T *
-VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
+DCHAR_T *
+VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, const FCHAR_T *format, va_list args)
 {
   DIRECTIVES d;
   arguments a;
@@ -1161,13 +1182,13 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
   {
     size_t buf_neededlength;
-    CHAR_T *buf;
-    CHAR_T *buf_malloced;
-    const CHAR_T *cp;
+    TCHAR_T *buf;
+    TCHAR_T *buf_malloced;
+    const FCHAR_T *cp;
     size_t i;
     DIRECTIVE *dp;
     /* Output string accumulator.  */
-    CHAR_T *result;
+    DCHAR_T *result;
     size_t allocated;
     size_t length;
 
@@ -1176,18 +1197,18 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
     buf_neededlength =
       xsum4 (7, d.max_width_length, d.max_precision_length, 6);
 #if HAVE_ALLOCA
-    if (buf_neededlength < 4000 / sizeof (CHAR_T))
+    if (buf_neededlength < 4000 / sizeof (TCHAR_T))
       {
-       buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
+       buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
        buf_malloced = NULL;
       }
     else
 #endif
       {
-       size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
+       size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
        if (size_overflow_p (buf_memsize))
          goto out_of_memory_1;
-       buf = (CHAR_T *) malloc (buf_memsize);
+       buf = (TCHAR_T *) malloc (buf_memsize);
        if (buf == NULL)
          goto out_of_memory_1;
        buf_malloced = buf;
@@ -1214,22 +1235,22 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
     if ((needed) > allocated)                                               \
       {                                                                             \
        size_t memory_size;                                                  \
-       CHAR_T *memory;                                                      \
+       DCHAR_T *memory;                                                     \
                                                                             \
        allocated = (allocated > 0 ? xtimes (allocated, 2) : 12);            \
        if ((needed) > allocated)                                            \
          allocated = (needed);                                              \
-       memory_size = xtimes (allocated, sizeof (CHAR_T));                   \
+       memory_size = xtimes (allocated, sizeof (DCHAR_T));                  \
        if (size_overflow_p (memory_size))                                   \
          goto out_of_memory;                                                \
        if (result == resultbuf || result == NULL)                           \
-         memory = (CHAR_T *) malloc (memory_size);                          \
+         memory = (DCHAR_T *) malloc (memory_size);                         \
        else                                                                 \
-         memory = (CHAR_T *) realloc (result, memory_size);                 \
+         memory = (DCHAR_T *) realloc (result, memory_size);                \
        if (memory == NULL)                                                  \
          goto out_of_memory;                                                \
        if (result == resultbuf && length > 0)                               \
-         memcpy (memory, result, length * sizeof (CHAR_T));                 \
+         memcpy (memory, result, length * sizeof (DCHAR_T));                \
        result = memory;                                                     \
       }
 
@@ -1241,7 +1262,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
            size_t augmented_length = xsum (length, n);
 
            ENSURE_ALLOCATION (augmented_length);
-           memcpy (result + length, cp, n * sizeof (CHAR_T));
+           memcpy (result + length, cp, n * sizeof (DCHAR_T));
            length = augmented_length;
          }
        if (i == d.count)
@@ -1289,41 +1310,20 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    abort ();
                  }
              }
-#if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
-           else if ((dp->conversion == 'f' || dp->conversion == 'F'
-                     || dp->conversion == 'e' || dp->conversion == 'E'
-                     || dp->conversion == 'g' || dp->conversion == 'G')
-                    && (0
-# if NEED_PRINTF_INFINITE_DOUBLE
-                        || (a.arg[dp->arg_index].type == TYPE_DOUBLE
-                            /* The systems (mingw) which produce wrong output
-                               for Inf, -Inf, and NaN also do so for -0.0.
-                               Therefore we treat this case here as well.  */
-                            && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
-# endif
-# if NEED_PRINTF_LONG_DOUBLE
-                        || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
-# elif NEED_PRINTF_INFINITE_LONG_DOUBLE
-                        || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
-                            /* Some systems produce wrong output for Inf,
-                               -Inf, and NaN.  */
-                            && is_infinitel (a.arg[dp->arg_index].a.a_longdouble))
-# endif
-                       ))
+#if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
+           else if (dp->conversion == 'a' || dp->conversion == 'A')
              {
-# if NEED_PRINTF_INFINITE_DOUBLE && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
                arg_type type = a.arg[dp->arg_index].type;
-# endif
                int flags = dp->flags;
                int has_width;
                size_t width;
                int has_precision;
                size_t precision;
                size_t tmp_length;
-               CHAR_T tmpbuf[700];
-               CHAR_T *tmp;
-               CHAR_T *pad_ptr;
-               CHAR_T *p;
+               DCHAR_T tmpbuf[700];
+               DCHAR_T *tmp;
+               DCHAR_T *pad_ptr;
+               DCHAR_T *p;
 
                has_width = 0;
                width = 0;
@@ -1348,7 +1348,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      }
                    else
                      {
-                       const CHAR_T *digitp = dp->width_start;
+                       const FCHAR_T *digitp = dp->width_start;
 
                        do
                          width = xsum (xtimes (width, 10), *digitp++ - '0');
@@ -1378,7 +1378,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      }
                    else
                      {
-                       const CHAR_T *digitp = dp->precision_start + 1;
+                       const FCHAR_T *digitp = dp->precision_start + 1;
 
                        precision = 0;
                        while (digitp != dp->precision_end)
@@ -1387,38 +1387,21 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      }
                  }
 
-               /* POSIX specifies the default precision to be 6 for %f, %F,
-                  %e, %E, but not for %g, %G.  Implementations appear to use
-                  the same default precision also for %g, %G.  */
-               if (!has_precision)
-                 precision = 6;
-
                /* Allocate a temporary buffer of sufficient size.  */
-# if NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
-               tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
-# elif NEED_PRINTF_LONG_DOUBLE
-               tmp_length = LDBL_DIG + 1;
-# else
-               tmp_length = 0;
-# endif
+               if (type == TYPE_LONGDOUBLE)
+                 tmp_length =
+                   (unsigned int) ((LDBL_DIG + 1)
+                                   * 0.831 /* decimal -> hexadecimal */
+                                  )
+                   + 1; /* turn floor into ceil */
+               else
+                 tmp_length =
+                   (unsigned int) ((DBL_DIG + 1)
+                                   * 0.831 /* decimal -> hexadecimal */
+                                  )
+                   + 1; /* turn floor into ceil */
                if (tmp_length < precision)
                  tmp_length = precision;
-# if NEED_PRINTF_LONG_DOUBLE
-#  if NEED_PRINTF_INFINITE_DOUBLE
-               if (type == TYPE_LONGDOUBLE)
-#  endif
-                 if (dp->conversion == 'f' || dp->conversion == 'F')
-                   {
-                     long double arg = a.arg[dp->arg_index].a.a_longdouble;
-                     if (!(isnanl (arg) || arg + arg == arg))
-                       {
-                         /* arg is finite and nonzero.  */
-                         int exponent = floorlog10l (arg < 0 ? -arg : arg);
-                         if (exponent >= 0 && tmp_length < exponent + precision)
-                           tmp_length = exponent + precision;
-                       }
-                   }
-# endif
                /* Account for sign, decimal point etc. */
                tmp_length = xsum (tmp_length, 12);
 
@@ -1427,16 +1410,16 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
 
-               if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
+               if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
                  tmp = tmpbuf;
                else
                  {
-                   size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
+                   size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
 
                    if (size_overflow_p (tmp_memsize))
                      /* Overflow, would lead to out of memory.  */
                      goto out_of_memory;
-                   tmp = (CHAR_T *) malloc (tmp_memsize);
+                   tmp = (DCHAR_T *) malloc (tmp_memsize);
                    if (tmp == NULL)
                      /* Out of memory.  */
                      goto out_of_memory;
@@ -1444,17 +1427,13 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                pad_ptr = NULL;
                p = tmp;
-
-# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
-#  if NEED_PRINTF_INFINITE_DOUBLE
                if (type == TYPE_LONGDOUBLE)
-#  endif
                  {
                    long double arg = a.arg[dp->arg_index].a.a_longdouble;
 
                    if (isnanl (arg))
                      {
-                       if (dp->conversion >= 'A' && dp->conversion <= 'Z')
+                       if (dp->conversion == 'A')
                          {
                            *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
                          }
@@ -1485,7 +1464,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                        if (arg > 0.0L && arg + arg == arg)
                          {
-                           if (dp->conversion >= 'A' && dp->conversion <= 'Z')
+                           if (dp->conversion == 'A')
                              {
                                *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
                              }
@@ -1496,302 +1475,102 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          }
                        else
                          {
-#  if NEED_PRINTF_LONG_DOUBLE
-                           pad_ptr = p;
+                           int exponent;
+                           long double mantissa;
 
-                           if (dp->conversion == 'f' || dp->conversion == 'F')
+                           if (arg > 0.0L)
+                             mantissa = printf_frexpl (arg, &exponent);
+                           else
                              {
-                               char *digits;
-                               size_t ndigits;
-
-                               digits =
-                                 scale10_round_decimal_long_double (arg, precision);
-                               if (digits == NULL)
-                                 {
-                                   END_LONG_DOUBLE_ROUNDING ();
-                                   goto out_of_memory;
-                                 }
-                               ndigits = strlen (digits);
-
-                               if (ndigits > precision)
-                                 do
-                                   {
-                                     --ndigits;
-                                     *p++ = digits[ndigits];
-                                   }
-                                 while (ndigits > precision);
-                               else
-                                 *p++ = '0';
-                               /* Here ndigits <= precision.  */
-                               if ((flags & FLAG_ALT) || precision > 0)
-                                 {
-                                   *p++ = decimal_point_char ();
-                                   for (; precision > ndigits; precision--)
-                                     *p++ = '0';
-                                   while (ndigits > 0)
-                                     {
-                                       --ndigits;
-                                       *p++ = digits[ndigits];
-                                     }
-                                 }
-
-                               free (digits);
+                               exponent = 0;
+                               mantissa = 0.0L;
                              }
-                           else if (dp->conversion == 'e' || dp->conversion == 'E')
+
+                           if (has_precision
+                               && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
                              {
-                               int exponent;
+                               /* Round the mantissa.  */
+                               long double tail = mantissa;
+                               size_t q;
 
-                               if (arg == 0.0L)
-                                 {
-                                   exponent = 0;
-                                   *p++ = '0';
-                                   if ((flags & FLAG_ALT) || precision > 0)
-                                     {
-                                       *p++ = decimal_point_char ();
-                                       for (; precision > 0; precision--)
-                                         *p++ = '0';
-                                     }
-                                 }
-                               else
+                               for (q = precision; ; q--)
                                  {
-                                   /* arg > 0.0L.  */
-                                   int adjusted;
-                                   char *digits;
-                                   size_t ndigits;
-
-                                   exponent = floorlog10l (arg);
-                                   adjusted = 0;
-                                   for (;;)
+                                   int digit = (int) tail;
+                                   tail -= digit;
+                                   if (q == 0)
                                      {
-                                       digits =
-                                         scale10_round_decimal_long_double (arg,
-                                                                            (int)precision - exponent);
-                                       if (digits == NULL)
-                                         {
-                                           END_LONG_DOUBLE_ROUNDING ();
-                                           goto out_of_memory;
-                                         }
-                                       ndigits = strlen (digits);
-
-                                       if (ndigits == precision + 1)
-                                         break;
-                                       if (ndigits < precision
-                                           || ndigits > precision + 2)
-                                         /* The exponent was not guessed
-                                            precisely enough.  */
-                                         abort ();
-                                       if (adjusted)
-                                         /* None of two values of exponent is
-                                            the right one.  Prevent an endless
-                                            loop.  */
-                                         abort ();
-                                       free (digits);
-                                       if (ndigits == precision)
-                                         exponent -= 1;
+                                       if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
+                                         tail = 1 - tail;
                                        else
-                                         exponent += 1;
-                                       adjusted = 1;
-                                     }
-
-                                   /* Here ndigits = precision+1.  */
-                                   *p++ = digits[--ndigits];
-                                   if ((flags & FLAG_ALT) || precision > 0)
-                                     {
-                                       *p++ = decimal_point_char ();
-                                       while (ndigits > 0)
-                                         {
-                                           --ndigits;
-                                           *p++ = digits[ndigits];
-                                         }
+                                         tail = - tail;
+                                       break;
                                      }
-
-                                   free (digits);
+                                   tail *= 16.0L;
                                  }
+                               if (tail != 0.0L)
+                                 for (q = precision; q > 0; q--)
+                                   tail *= 0.0625L;
+                               mantissa += tail;
+                             }
 
-                               *p++ = dp->conversion; /* 'e' or 'E' */
-#   if WIDE_CHAR_VERSION
+                           *p++ = '0';
+                           *p++ = dp->conversion - 'A' + 'X';
+                           pad_ptr = p;
+                           {
+                             int digit;
+
+                             digit = (int) mantissa;
+                             mantissa -= digit;
+                             *p++ = '0' + digit;
+                             if ((flags & FLAG_ALT)
+                                 || mantissa > 0.0L || precision > 0)
                                {
-                                 static const wchar_t decimal_format[] =
-                                   { '%', '+', '.', '2', 'd', '\0' };
-                                 SNPRINTF (p, 6 + 1, decimal_format, exponent);
+                                 *p++ = decimal_point_char ();
+                                 /* This loop terminates because we assume
+                                    that FLT_RADIX is a power of 2.  */
+                                 while (mantissa > 0.0L)
+                                   {
+                                     mantissa *= 16.0L;
+                                     digit = (int) mantissa;
+                                     mantissa -= digit;
+                                     *p++ = digit
+                                            + (digit < 10
+                                               ? '0'
+                                               : dp->conversion - 10);
+                                     if (precision > 0)
+                                       precision--;
+                                   }
+                                 while (precision > 0)
+                                   {
+                                     *p++ = '0';
+                                     precision--;
+                                   }
                                }
-#   else
-                               sprintf (p, "%+.2d", exponent);
-#   endif
-                               while (*p != '\0')
-                                 p++;
                              }
-                           else if (dp->conversion == 'g' || dp->conversion == 'G')
+                             *p++ = dp->conversion - 'A' + 'P';
+# if WIDE_CHAR_VERSION
                              {
-                               if (precision == 0)
-                                 precision = 1;
-                               /* precision >= 1.  */
-
-                               if (arg == 0.0L)
-                                 /* The exponent is 0, >= -4, < precision.
-                                    Use fixed-point notation.  */
-                                 {
-                                   size_t ndigits = precision;
-                                   /* Number of trailing zeroes that have to be
-                                      dropped.  */
-                                   size_t nzeroes =
-                                     (flags & FLAG_ALT ? 0 : precision - 1);
-
-                                   --ndigits;
-                                   *p++ = '0';
-                                   if ((flags & FLAG_ALT) || ndigits > nzeroes)
-                                     {
-                                       *p++ = decimal_point_char ();
-                                       while (ndigits > nzeroes)
-                                         {
-                                           --ndigits;
-                                           *p++ = '0';
-                                         }
-                                     }
-                                 }
-                               else
-                                 {
-                                   /* arg > 0.0L.  */
-                                   int exponent;
-                                   int adjusted;
-                                   char *digits;
-                                   size_t ndigits;
-                                   size_t nzeroes;
-
-                                   exponent = floorlog10l (arg);
-                                   adjusted = 0;
-                                   for (;;)
-                                     {
-                                       digits =
-                                         scale10_round_decimal_long_double (arg,
-                                                                            (int)(precision - 1) - exponent);
-                                       if (digits == NULL)
-                                         {
-                                           END_LONG_DOUBLE_ROUNDING ();
-                                           goto out_of_memory;
-                                         }
-                                       ndigits = strlen (digits);
-
-                                       if (ndigits == precision)
-                                         break;
-                                       if (ndigits < precision - 1
-                                           || ndigits > precision + 1)
-                                         /* The exponent was not guessed
-                                            precisely enough.  */
-                                         abort ();
-                                       if (adjusted)
-                                         /* None of two values of exponent is
-                                            the right one.  Prevent an endless
-                                            loop.  */
-                                         abort ();
-                                       free (digits);
-                                       if (ndigits < precision)
-                                         exponent -= 1;
-                                       else
-                                         exponent += 1;
-                                       adjusted = 1;
-                                     }
-                                   /* Here ndigits = precision.  */
-
-                                   /* Determine the number of trailing zeroes
-                                      that have to be dropped.  */
-                                   nzeroes = 0;
-                                   if ((flags & FLAG_ALT) == 0)
-                                     while (nzeroes < ndigits
-                                            && digits[nzeroes] == '0')
-                                       nzeroes++;
-
-                                   /* The exponent is now determined.  */
-                                   if (exponent >= -4
-                                       && exponent < (long)precision)
-                                     {
-                                       /* Fixed-point notation:
-                                          max(exponent,0)+1 digits, then the
-                                          decimal point, then the remaining
-                                          digits without trailing zeroes.  */
-                                       if (exponent >= 0)
-                                         {
-                                           size_t count = exponent + 1;
-                                           /* Note: count <= precision = ndigits.  */
-                                           for (; count > 0; count--)
-                                             *p++ = digits[--ndigits];
-                                           if ((flags & FLAG_ALT) || ndigits > nzeroes)
-                                             {
-                                               *p++ = decimal_point_char ();
-                                               while (ndigits > nzeroes)
-                                                 {
-                                                   --ndigits;
-                                                   *p++ = digits[ndigits];
-                                                 }
-                                             }
-                                         }
-                                       else
-                                         {
-                                           size_t count = -exponent - 1;
-                                           *p++ = '0';
-                                           *p++ = decimal_point_char ();
-                                           for (; count > 0; count--)
-                                             *p++ = '0';
-                                           while (ndigits > nzeroes)
-                                             {
-                                               --ndigits;
-                                               *p++ = digits[ndigits];
-                                             }
-                                         }
-                                     }
-                                   else
-                                     {
-                                       /* Exponential notation.  */
-                                       *p++ = digits[--ndigits];
-                                       if ((flags & FLAG_ALT) || ndigits > nzeroes)
-                                         {
-                                           *p++ = decimal_point_char ();
-                                           while (ndigits > nzeroes)
-                                             {
-                                               --ndigits;
-                                               *p++ = digits[ndigits];
-                                             }
-                                         }
-                                       *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
-#   if WIDE_CHAR_VERSION
-                                       {
-                                         static const wchar_t decimal_format[] =
-                                           { '%', '+', '.', '2', 'd', '\0' };
-                                         SNPRINTF (p, 6 + 1, decimal_format, exponent);
-                                       }
-#   else
-                                       sprintf (p, "%+.2d", exponent);
-#   endif
-                                       while (*p != '\0')
-                                         p++;
-                                     }
-
-                                   free (digits);
-                                 }
+                               static const wchar_t decimal_format[] =
+                                 { '%', '+', 'd', '\0' };
+                               SNPRINTF (p, 6 + 1, decimal_format, exponent);
                              }
-                           else
-                             abort ();
-#  else
-                           /* arg is finite.  */
-                           abort ();
-#  endif
+# else
+                             sprintf (p, "%+d", exponent);
+# endif
+                             while (*p != '\0')
+                               p++;
                          }
 
                        END_LONG_DOUBLE_ROUNDING ();
                      }
                  }
-#  if NEED_PRINTF_INFINITE_DOUBLE
                else
-#  endif
-# endif
-# if NEED_PRINTF_INFINITE_DOUBLE
                  {
-                   /* Simpler than above: handle only NaN, Infinity, zero.  */
                    double arg = a.arg[dp->arg_index].a.a_double;
 
                    if (isnan (arg))
                      {
-                       if (dp->conversion >= 'A' && dp->conversion <= 'Z')
+                       if (dp->conversion == 'A')
                          {
                            *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
                          }
@@ -1804,7 +1583,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      {
                        int sign = 0;
 
-                       if (signbit (arg)) /* arg < 0.0L or negative zero */
+                       if (signbit (arg)) /* arg < 0.0 or negative zero */
                          {
                            sign = -1;
                            arg = -arg;
@@ -1819,7 +1598,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                        if (arg > 0.0 && arg + arg == arg)
                          {
-                           if (dp->conversion >= 'A' && dp->conversion <= 'Z')
+                           if (dp->conversion == 'A')
                              {
                                *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
                              }
@@ -1830,65 +1609,99 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          }
                        else
                          {
-                           if (!(arg == 0.0))
-                             abort ();
+                           int exponent;
+                           double mantissa;
 
-                           pad_ptr = p;
+                           if (arg > 0.0)
+                             mantissa = printf_frexp (arg, &exponent);
+                           else
+                             {
+                               exponent = 0;
+                               mantissa = 0.0;
+                             }
 
-                           if (dp->conversion == 'f' || dp->conversion == 'F')
+                           if (has_precision
+                               && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
                              {
-                               *p++ = '0';
-                               if ((flags & FLAG_ALT) || precision > 0)
+                               /* Round the mantissa.  */
+                               double tail = mantissa;
+                               size_t q;
+
+                               for (q = precision; ; q--)
                                  {
-                                   *p++ = decimal_point_char ();
-                                   for (; precision > 0; precision--)
-                                     *p++ = '0';
+                                   int digit = (int) tail;
+                                   tail -= digit;
+                                   if (q == 0)
+                                     {
+                                       if (digit & 1 ? tail >= 0.5 : tail > 0.5)
+                                         tail = 1 - tail;
+                                       else
+                                         tail = - tail;
+                                       break;
+                                     }
+                                   tail *= 16.0;
                                  }
+                               if (tail != 0.0)
+                                 for (q = precision; q > 0; q--)
+                                   tail *= 0.0625;
+                               mantissa += tail;
                              }
-                           else if (dp->conversion == 'e' || dp->conversion == 'E')
-                             {
-                               *p++ = '0';
-                               if ((flags & FLAG_ALT) || precision > 0)
-                                 {
-                                   *p++ = decimal_point_char ();
-                                   for (; precision > 0; precision--)
+
+                           *p++ = '0';
+                           *p++ = dp->conversion - 'A' + 'X';
+                           pad_ptr = p;
+                           {
+                             int digit;
+
+                             digit = (int) mantissa;
+                             mantissa -= digit;
+                             *p++ = '0' + digit;
+                             if ((flags & FLAG_ALT)
+                                 || mantissa > 0.0 || precision > 0)
+                               {
+                                 *p++ = decimal_point_char ();
+                                 /* This loop terminates because we assume
+                                    that FLT_RADIX is a power of 2.  */
+                                 while (mantissa > 0.0)
+                                   {
+                                     mantissa *= 16.0;
+                                     digit = (int) mantissa;
+                                     mantissa -= digit;
+                                     *p++ = digit
+                                            + (digit < 10
+                                               ? '0'
+                                               : dp->conversion - 10);
+                                     if (precision > 0)
+                                       precision--;
+                                   }
+                                 while (precision > 0)
+                                   {
                                      *p++ = '0';
-                                 }
-                               *p++ = dp->conversion; /* 'e' or 'E' */
-                               *p++ = '+';
-                               /* Produce the same number of exponent digits as
-                                  the native printf implementation.  */
-#  if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
-                               *p++ = '0';
-#  endif
-                               *p++ = '0';
-                               *p++ = '0';
+                                     precision--;
+                                   }
+                               }
                              }
-                           else if (dp->conversion == 'g' || dp->conversion == 'G')
+                             *p++ = dp->conversion - 'A' + 'P';
+# if WIDE_CHAR_VERSION
                              {
-                               *p++ = '0';
-                               if (flags & FLAG_ALT)
-                                 {
-                                   size_t ndigits =
-                                     (precision > 0 ? precision - 1 : 0);
-                                   *p++ = decimal_point_char ();
-                                   for (; ndigits > 0; --ndigits)
-                                     *p++ = '0';
-                                 }
+                               static const wchar_t decimal_format[] =
+                                 { '%', '+', 'd', '\0' };
+                               SNPRINTF (p, 6 + 1, decimal_format, exponent);
                              }
-                           else
-                             abort ();
+# else
+                             sprintf (p, "%+d", exponent);
+# endif
+                             while (*p != '\0')
+                               p++;
                          }
                      }
                  }
-# endif
-
                /* The generated string now extends from tmp to p, with the
                   zero padding insertion point being at pad_ptr.  */
                if (has_width && p - tmp < width)
                  {
                    size_t pad = width - (p - tmp);
-                   CHAR_T *end = p + pad;
+                   DCHAR_T *end = p + pad;
 
                    if (flags & FLAG_LEFT)
                      {
@@ -1899,7 +1712,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
                      {
                        /* Pad with zeroes.  */
-                       CHAR_T *q = end;
+                       DCHAR_T *q = end;
 
                        while (p > pad_ptr)
                          *--q = *--p;
@@ -1909,7 +1722,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    else
                      {
                        /* Pad with spaces on the left.  */
-                       CHAR_T *q = end;
+                       DCHAR_T *q = end;
 
                        while (p > tmp)
                          *--q = *--p;
@@ -1937,27 +1750,49 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    }
 
                  /* Append the result.  */
-                 memcpy (result + length, tmp, count * sizeof (CHAR_T));
+                 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
                  if (tmp != tmpbuf)
                    free (tmp);
                  length += count;
                }
              }
 #endif
-#if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
-           else if (dp->conversion == 'a' || dp->conversion == 'A')
+#if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
+           else if ((dp->conversion == 'f' || dp->conversion == 'F'
+                     || dp->conversion == 'e' || dp->conversion == 'E'
+                     || dp->conversion == 'g' || dp->conversion == 'G'
+                     || dp->conversion == 'a' || dp->conversion == 'A')
+                    && (0
+# if NEED_PRINTF_INFINITE_DOUBLE
+                        || (a.arg[dp->arg_index].type == TYPE_DOUBLE
+                            /* The systems (mingw) which produce wrong output
+                               for Inf, -Inf, and NaN also do so for -0.0.
+                               Therefore we treat this case here as well.  */
+                            && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
+# endif
+# if NEED_PRINTF_LONG_DOUBLE
+                        || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
+# elif NEED_PRINTF_INFINITE_LONG_DOUBLE
+                        || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
+                            /* Some systems produce wrong output for Inf,
+                               -Inf, and NaN.  */
+                            && is_infinitel (a.arg[dp->arg_index].a.a_longdouble))
+# endif
+                       ))
              {
+# if NEED_PRINTF_INFINITE_DOUBLE && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
                arg_type type = a.arg[dp->arg_index].type;
+# endif
                int flags = dp->flags;
                int has_width;
                size_t width;
                int has_precision;
                size_t precision;
                size_t tmp_length;
-               CHAR_T tmpbuf[700];
-               CHAR_T *tmp;
-               CHAR_T *pad_ptr;
-               CHAR_T *p;
+               DCHAR_T tmpbuf[700];
+               DCHAR_T *tmp;
+               DCHAR_T *pad_ptr;
+               DCHAR_T *p;
 
                has_width = 0;
                width = 0;
@@ -1982,7 +1817,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      }
                    else
                      {
-                       const CHAR_T *digitp = dp->width_start;
+                       const FCHAR_T *digitp = dp->width_start;
 
                        do
                          width = xsum (xtimes (width, 10), *digitp++ - '0');
@@ -2012,7 +1847,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      }
                    else
                      {
-                       const CHAR_T *digitp = dp->precision_start + 1;
+                       const FCHAR_T *digitp = dp->precision_start + 1;
 
                        precision = 0;
                        while (digitp != dp->precision_end)
@@ -2021,21 +1856,38 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      }
                  }
 
-               /* Allocate a temporary buffer of sufficient size.  */
-               if (type == TYPE_LONGDOUBLE)
-                 tmp_length =
-                   (unsigned int) ((LDBL_DIG + 1)
-                                   * 0.831 /* decimal -> hexadecimal */
-                                  )
-                   + 1; /* turn floor into ceil */
-               else
-                 tmp_length =
-                   (unsigned int) ((DBL_DIG + 1)
-                                   * 0.831 /* decimal -> hexadecimal */
-                                  )
-                   + 1; /* turn floor into ceil */
+               /* POSIX specifies the default precision to be 6 for %f, %F,
+                  %e, %E, but not for %g, %G.  Implementations appear to use
+                  the same default precision also for %g, %G.  */
+               if (!has_precision)
+                 precision = 6;
+
+               /* Allocate a temporary buffer of sufficient size.  */
+# if NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
+               tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
+# elif NEED_PRINTF_LONG_DOUBLE
+               tmp_length = LDBL_DIG + 1;
+# else
+               tmp_length = 0;
+# endif
                if (tmp_length < precision)
                  tmp_length = precision;
+# if NEED_PRINTF_LONG_DOUBLE
+#  if NEED_PRINTF_INFINITE_DOUBLE
+               if (type == TYPE_LONGDOUBLE)
+#  endif
+                 if (dp->conversion == 'f' || dp->conversion == 'F')
+                   {
+                     long double arg = a.arg[dp->arg_index].a.a_longdouble;
+                     if (!(isnanl (arg) || arg + arg == arg))
+                       {
+                         /* arg is finite and nonzero.  */
+                         int exponent = floorlog10l (arg < 0 ? -arg : arg);
+                         if (exponent >= 0 && tmp_length < exponent + precision)
+                           tmp_length = exponent + precision;
+                       }
+                   }
+# endif
                /* Account for sign, decimal point etc. */
                tmp_length = xsum (tmp_length, 12);
 
@@ -2044,16 +1896,16 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
 
-               if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
+               if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
                  tmp = tmpbuf;
                else
                  {
-                   size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
+                   size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
 
                    if (size_overflow_p (tmp_memsize))
                      /* Overflow, would lead to out of memory.  */
                      goto out_of_memory;
-                   tmp = (CHAR_T *) malloc (tmp_memsize);
+                   tmp = (DCHAR_T *) malloc (tmp_memsize);
                    if (tmp == NULL)
                      /* Out of memory.  */
                      goto out_of_memory;
@@ -2061,13 +1913,17 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                pad_ptr = NULL;
                p = tmp;
+
+# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
+#  if NEED_PRINTF_INFINITE_DOUBLE
                if (type == TYPE_LONGDOUBLE)
+#  endif
                  {
                    long double arg = a.arg[dp->arg_index].a.a_longdouble;
 
                    if (isnanl (arg))
                      {
-                       if (dp->conversion == 'A')
+                       if (dp->conversion >= 'A' && dp->conversion <= 'Z')
                          {
                            *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
                          }
@@ -2098,7 +1954,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                        if (arg > 0.0L && arg + arg == arg)
                          {
-                           if (dp->conversion == 'A')
+                           if (dp->conversion >= 'A' && dp->conversion <= 'Z')
                              {
                                *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
                              }
@@ -2109,102 +1965,302 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          }
                        else
                          {
-                           int exponent;
-                           long double mantissa;
+#  if NEED_PRINTF_LONG_DOUBLE
+                           pad_ptr = p;
 
-                           if (arg > 0.0L)
-                             mantissa = printf_frexpl (arg, &exponent);
-                           else
+                           if (dp->conversion == 'f' || dp->conversion == 'F')
                              {
-                               exponent = 0;
-                               mantissa = 0.0L;
+                               char *digits;
+                               size_t ndigits;
+
+                               digits =
+                                 scale10_round_decimal_long_double (arg, precision);
+                               if (digits == NULL)
+                                 {
+                                   END_LONG_DOUBLE_ROUNDING ();
+                                   goto out_of_memory;
+                                 }
+                               ndigits = strlen (digits);
+
+                               if (ndigits > precision)
+                                 do
+                                   {
+                                     --ndigits;
+                                     *p++ = digits[ndigits];
+                                   }
+                                 while (ndigits > precision);
+                               else
+                                 *p++ = '0';
+                               /* Here ndigits <= precision.  */
+                               if ((flags & FLAG_ALT) || precision > 0)
+                                 {
+                                   *p++ = decimal_point_char ();
+                                   for (; precision > ndigits; precision--)
+                                     *p++ = '0';
+                                   while (ndigits > 0)
+                                     {
+                                       --ndigits;
+                                       *p++ = digits[ndigits];
+                                     }
+                                 }
+
+                               free (digits);
                              }
+                           else if (dp->conversion == 'e' || dp->conversion == 'E')
+                             {
+                               int exponent;
 
-                           if (has_precision
-                               && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
+                               if (arg == 0.0L)
+                                 {
+                                   exponent = 0;
+                                   *p++ = '0';
+                                   if ((flags & FLAG_ALT) || precision > 0)
+                                     {
+                                       *p++ = decimal_point_char ();
+                                       for (; precision > 0; precision--)
+                                         *p++ = '0';
+                                     }
+                                 }
+                               else
+                                 {
+                                   /* arg > 0.0L.  */
+                                   int adjusted;
+                                   char *digits;
+                                   size_t ndigits;
+
+                                   exponent = floorlog10l (arg);
+                                   adjusted = 0;
+                                   for (;;)
+                                     {
+                                       digits =
+                                         scale10_round_decimal_long_double (arg,
+                                                                            (int)precision - exponent);
+                                       if (digits == NULL)
+                                         {
+                                           END_LONG_DOUBLE_ROUNDING ();
+                                           goto out_of_memory;
+                                         }
+                                       ndigits = strlen (digits);
+
+                                       if (ndigits == precision + 1)
+                                         break;
+                                       if (ndigits < precision
+                                           || ndigits > precision + 2)
+                                         /* The exponent was not guessed
+                                            precisely enough.  */
+                                         abort ();
+                                       if (adjusted)
+                                         /* None of two values of exponent is
+                                            the right one.  Prevent an endless
+                                            loop.  */
+                                         abort ();
+                                       free (digits);
+                                       if (ndigits == precision)
+                                         exponent -= 1;
+                                       else
+                                         exponent += 1;
+                                       adjusted = 1;
+                                     }
+
+                                   /* Here ndigits = precision+1.  */
+                                   *p++ = digits[--ndigits];
+                                   if ((flags & FLAG_ALT) || precision > 0)
+                                     {
+                                       *p++ = decimal_point_char ();
+                                       while (ndigits > 0)
+                                         {
+                                           --ndigits;
+                                           *p++ = digits[ndigits];
+                                         }
+                                     }
+
+                                   free (digits);
+                                 }
+
+                               *p++ = dp->conversion; /* 'e' or 'E' */
+#   if WIDE_CHAR_VERSION
+                               {
+                                 static const wchar_t decimal_format[] =
+                                   { '%', '+', '.', '2', 'd', '\0' };
+                                 SNPRINTF (p, 6 + 1, decimal_format, exponent);
+                               }
+#   else
+                               sprintf (p, "%+.2d", exponent);
+#   endif
+                               while (*p != '\0')
+                                 p++;
+                             }
+                           else if (dp->conversion == 'g' || dp->conversion == 'G')
                              {
-                               /* Round the mantissa.  */
-                               long double tail = mantissa;
-                               size_t q;
+                               if (precision == 0)
+                                 precision = 1;
+                               /* precision >= 1.  */
 
-                               for (q = precision; ; q--)
+                               if (arg == 0.0L)
+                                 /* The exponent is 0, >= -4, < precision.
+                                    Use fixed-point notation.  */
                                  {
-                                   int digit = (int) tail;
-                                   tail -= digit;
-                                   if (q == 0)
+                                   size_t ndigits = precision;
+                                   /* Number of trailing zeroes that have to be
+                                      dropped.  */
+                                   size_t nzeroes =
+                                     (flags & FLAG_ALT ? 0 : precision - 1);
+
+                                   --ndigits;
+                                   *p++ = '0';
+                                   if ((flags & FLAG_ALT) || ndigits > nzeroes)
                                      {
-                                       if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
-                                         tail = 1 - tail;
+                                       *p++ = decimal_point_char ();
+                                       while (ndigits > nzeroes)
+                                         {
+                                           --ndigits;
+                                           *p++ = '0';
+                                         }
+                                     }
+                                 }
+                               else
+                                 {
+                                   /* arg > 0.0L.  */
+                                   int exponent;
+                                   int adjusted;
+                                   char *digits;
+                                   size_t ndigits;
+                                   size_t nzeroes;
+
+                                   exponent = floorlog10l (arg);
+                                   adjusted = 0;
+                                   for (;;)
+                                     {
+                                       digits =
+                                         scale10_round_decimal_long_double (arg,
+                                                                            (int)(precision - 1) - exponent);
+                                       if (digits == NULL)
+                                         {
+                                           END_LONG_DOUBLE_ROUNDING ();
+                                           goto out_of_memory;
+                                         }
+                                       ndigits = strlen (digits);
+
+                                       if (ndigits == precision)
+                                         break;
+                                       if (ndigits < precision - 1
+                                           || ndigits > precision + 1)
+                                         /* The exponent was not guessed
+                                            precisely enough.  */
+                                         abort ();
+                                       if (adjusted)
+                                         /* None of two values of exponent is
+                                            the right one.  Prevent an endless
+                                            loop.  */
+                                         abort ();
+                                       free (digits);
+                                       if (ndigits < precision)
+                                         exponent -= 1;
+                                       else
+                                         exponent += 1;
+                                       adjusted = 1;
+                                     }
+                                   /* Here ndigits = precision.  */
+
+                                   /* Determine the number of trailing zeroes
+                                      that have to be dropped.  */
+                                   nzeroes = 0;
+                                   if ((flags & FLAG_ALT) == 0)
+                                     while (nzeroes < ndigits
+                                            && digits[nzeroes] == '0')
+                                       nzeroes++;
+
+                                   /* The exponent is now determined.  */
+                                   if (exponent >= -4
+                                       && exponent < (long)precision)
+                                     {
+                                       /* Fixed-point notation:
+                                          max(exponent,0)+1 digits, then the
+                                          decimal point, then the remaining
+                                          digits without trailing zeroes.  */
+                                       if (exponent >= 0)
+                                         {
+                                           size_t count = exponent + 1;
+                                           /* Note: count <= precision = ndigits.  */
+                                           for (; count > 0; count--)
+                                             *p++ = digits[--ndigits];
+                                           if ((flags & FLAG_ALT) || ndigits > nzeroes)
+                                             {
+                                               *p++ = decimal_point_char ();
+                                               while (ndigits > nzeroes)
+                                                 {
+                                                   --ndigits;
+                                                   *p++ = digits[ndigits];
+                                                 }
+                                             }
+                                         }
                                        else
-                                         tail = - tail;
-                                       break;
+                                         {
+                                           size_t count = -exponent - 1;
+                                           *p++ = '0';
+                                           *p++ = decimal_point_char ();
+                                           for (; count > 0; count--)
+                                             *p++ = '0';
+                                           while (ndigits > nzeroes)
+                                             {
+                                               --ndigits;
+                                               *p++ = digits[ndigits];
+                                             }
+                                         }
+                                     }
+                                   else
+                                     {
+                                       /* Exponential notation.  */
+                                       *p++ = digits[--ndigits];
+                                       if ((flags & FLAG_ALT) || ndigits > nzeroes)
+                                         {
+                                           *p++ = decimal_point_char ();
+                                           while (ndigits > nzeroes)
+                                             {
+                                               --ndigits;
+                                               *p++ = digits[ndigits];
+                                             }
+                                         }
+                                       *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
+#   if WIDE_CHAR_VERSION
+                                       {
+                                         static const wchar_t decimal_format[] =
+                                           { '%', '+', '.', '2', 'd', '\0' };
+                                         SNPRINTF (p, 6 + 1, decimal_format, exponent);
+                                       }
+#   else
+                                       sprintf (p, "%+.2d", exponent);
+#   endif
+                                       while (*p != '\0')
+                                         p++;
                                      }
-                                   tail *= 16.0L;
-                                 }
-                               if (tail != 0.0L)
-                                 for (q = precision; q > 0; q--)
-                                   tail *= 0.0625L;
-                               mantissa += tail;
-                             }
-
-                           *p++ = '0';
-                           *p++ = dp->conversion - 'A' + 'X';
-                           pad_ptr = p;
-                           {
-                             int digit;
 
-                             digit = (int) mantissa;
-                             mantissa -= digit;
-                             *p++ = '0' + digit;
-                             if ((flags & FLAG_ALT)
-                                 || mantissa > 0.0L || precision > 0)
-                               {
-                                 *p++ = decimal_point_char ();
-                                 /* This loop terminates because we assume
-                                    that FLT_RADIX is a power of 2.  */
-                                 while (mantissa > 0.0L)
-                                   {
-                                     mantissa *= 16.0L;
-                                     digit = (int) mantissa;
-                                     mantissa -= digit;
-                                     *p++ = digit
-                                            + (digit < 10
-                                               ? '0'
-                                               : dp->conversion - 10);
-                                     if (precision > 0)
-                                       precision--;
-                                   }
-                                 while (precision > 0)
-                                   {
-                                     *p++ = '0';
-                                     precision--;
-                                   }
-                               }
-                             }
-                             *p++ = dp->conversion - 'A' + 'P';
-# if WIDE_CHAR_VERSION
-                             {
-                               static const wchar_t decimal_format[] =
-                                 { '%', '+', 'd', '\0' };
-                               SNPRINTF (p, 6 + 1, decimal_format, exponent);
+                                   free (digits);
+                                 }
                              }
-# else
-                             sprintf (p, "%+d", exponent);
-# endif
-                             while (*p != '\0')
-                               p++;
+                           else
+                             abort ();
+#  else
+                           /* arg is finite.  */
+                           abort ();
+#  endif
                          }
 
                        END_LONG_DOUBLE_ROUNDING ();
                      }
                  }
+#  if NEED_PRINTF_INFINITE_DOUBLE
                else
+#  endif
+# endif
+# if NEED_PRINTF_INFINITE_DOUBLE
                  {
+                   /* Simpler than above: handle only NaN, Infinity, zero.  */
                    double arg = a.arg[dp->arg_index].a.a_double;
 
                    if (isnan (arg))
                      {
-                       if (dp->conversion == 'A')
+                       if (dp->conversion >= 'A' && dp->conversion <= 'Z')
                          {
                            *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
                          }
@@ -2217,7 +2273,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      {
                        int sign = 0;
 
-                       if (signbit (arg)) /* arg < 0.0 or negative zero */
+                       if (signbit (arg)) /* arg < 0.0L or negative zero */
                          {
                            sign = -1;
                            arg = -arg;
@@ -2232,7 +2288,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                        if (arg > 0.0 && arg + arg == arg)
                          {
-                           if (dp->conversion == 'A')
+                           if (dp->conversion >= 'A' && dp->conversion <= 'Z')
                              {
                                *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
                              }
@@ -2243,99 +2299,65 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          }
                        else
                          {
-                           int exponent;
-                           double mantissa;
+                           if (!(arg == 0.0))
+                             abort ();
 
-                           if (arg > 0.0)
-                             mantissa = printf_frexp (arg, &exponent);
-                           else
-                             {
-                               exponent = 0;
-                               mantissa = 0.0;
-                             }
+                           pad_ptr = p;
 
-                           if (has_precision
-                               && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
+                           if (dp->conversion == 'f' || dp->conversion == 'F')
                              {
-                               /* Round the mantissa.  */
-                               double tail = mantissa;
-                               size_t q;
-
-                               for (q = precision; ; q--)
+                               *p++ = '0';
+                               if ((flags & FLAG_ALT) || precision > 0)
                                  {
-                                   int digit = (int) tail;
-                                   tail -= digit;
-                                   if (q == 0)
-                                     {
-                                       if (digit & 1 ? tail >= 0.5 : tail > 0.5)
-                                         tail = 1 - tail;
-                                       else
-                                         tail = - tail;
-                                       break;
-                                     }
-                                   tail *= 16.0;
+                                   *p++ = decimal_point_char ();
+                                   for (; precision > 0; precision--)
+                                     *p++ = '0';
                                  }
-                               if (tail != 0.0)
-                                 for (q = precision; q > 0; q--)
-                                   tail *= 0.0625;
-                               mantissa += tail;
                              }
-
-                           *p++ = '0';
-                           *p++ = dp->conversion - 'A' + 'X';
-                           pad_ptr = p;
-                           {
-                             int digit;
-
-                             digit = (int) mantissa;
-                             mantissa -= digit;
-                             *p++ = '0' + digit;
-                             if ((flags & FLAG_ALT)
-                                 || mantissa > 0.0 || precision > 0)
-                               {
-                                 *p++ = decimal_point_char ();
-                                 /* This loop terminates because we assume
-                                    that FLT_RADIX is a power of 2.  */
-                                 while (mantissa > 0.0)
-                                   {
-                                     mantissa *= 16.0;
-                                     digit = (int) mantissa;
-                                     mantissa -= digit;
-                                     *p++ = digit
-                                            + (digit < 10
-                                               ? '0'
-                                               : dp->conversion - 10);
-                                     if (precision > 0)
-                                       precision--;
-                                   }
-                                 while (precision > 0)
-                                   {
+                           else if (dp->conversion == 'e' || dp->conversion == 'E')
+                             {
+                               *p++ = '0';
+                               if ((flags & FLAG_ALT) || precision > 0)
+                                 {
+                                   *p++ = decimal_point_char ();
+                                   for (; precision > 0; precision--)
                                      *p++ = '0';
-                                     precision--;
-                                   }
-                               }
+                                 }
+                               *p++ = dp->conversion; /* 'e' or 'E' */
+                               *p++ = '+';
+                               /* Produce the same number of exponent digits as
+                                  the native printf implementation.  */
+#  if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+                               *p++ = '0';
+#  endif
+                               *p++ = '0';
+                               *p++ = '0';
                              }
-                             *p++ = dp->conversion - 'A' + 'P';
-# if WIDE_CHAR_VERSION
+                           else if (dp->conversion == 'g' || dp->conversion == 'G')
                              {
-                               static const wchar_t decimal_format[] =
-                                 { '%', '+', 'd', '\0' };
-                               SNPRINTF (p, 6 + 1, decimal_format, exponent);
+                               *p++ = '0';
+                               if (flags & FLAG_ALT)
+                                 {
+                                   size_t ndigits =
+                                     (precision > 0 ? precision - 1 : 0);
+                                   *p++ = decimal_point_char ();
+                                   for (; ndigits > 0; --ndigits)
+                                     *p++ = '0';
+                                 }
                              }
-# else
-                             sprintf (p, "%+d", exponent);
-# endif
-                             while (*p != '\0')
-                               p++;
+                           else
+                             abort ();
                          }
                      }
                  }
+# endif
+
                /* The generated string now extends from tmp to p, with the
                   zero padding insertion point being at pad_ptr.  */
                if (has_width && p - tmp < width)
                  {
                    size_t pad = width - (p - tmp);
-                   CHAR_T *end = p + pad;
+                   DCHAR_T *end = p + pad;
 
                    if (flags & FLAG_LEFT)
                      {
@@ -2346,7 +2368,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
                      {
                        /* Pad with zeroes.  */
-                       CHAR_T *q = end;
+                       DCHAR_T *q = end;
 
                        while (p > pad_ptr)
                          *--q = *--p;
@@ -2356,7 +2378,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    else
                      {
                        /* Pad with spaces on the left.  */
-                       CHAR_T *q = end;
+                       DCHAR_T *q = end;
 
                        while (p > tmp)
                          *--q = *--p;
@@ -2384,7 +2406,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    }
 
                  /* Append the result.  */
-                 memcpy (result + length, tmp, count * sizeof (CHAR_T));
+                 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
                  if (tmp != tmpbuf)
                    free (tmp);
                  length += count;
@@ -2404,13 +2426,13 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 #else
 #              define pad_ourselves 0
 #endif
-               CHAR_T *fbp;
+               TCHAR_T *fbp;
                unsigned int prefix_count;
                int prefixes[2];
 #if !USE_SNPRINTF
                size_t tmp_length;
-               CHAR_T tmpbuf[700];
-               CHAR_T *tmp;
+               TCHAR_T tmpbuf[700];
+               TCHAR_T *tmp;
 #endif
 
 #if !USE_SNPRINTF || NEED_PRINTF_FLAG_ZERO
@@ -2437,7 +2459,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      }
                    else
                      {
-                       const CHAR_T *digitp = dp->width_start;
+                       const FCHAR_T *digitp = dp->width_start;
 
                        do
                          width = xsum (xtimes (width, 10), *digitp++ - '0');
@@ -2467,7 +2489,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                        }
                      else
                        {
-                         const CHAR_T *digitp = dp->precision_start + 1;
+                         const FCHAR_T *digitp = dp->precision_start + 1;
 
                          precision = 0;
                          while (digitp != dp->precision_end)
@@ -2653,16 +2675,16 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                  tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
                }
 
-               if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
+               if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
                  tmp = tmpbuf;
                else
                  {
-                   size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
+                   size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
 
                    if (size_overflow_p (tmp_memsize))
                      /* Overflow, would lead to out of memory.  */
                      goto out_of_memory;
-                   tmp = (CHAR_T *) malloc (tmp_memsize);
+                   tmp = (TCHAR_T *) malloc (tmp_memsize);
                    if (tmp == NULL)
                      /* Out of memory.  */
                      goto out_of_memory;
@@ -2710,14 +2732,14 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    if (dp->width_start != dp->width_end)
                      {
                        size_t n = dp->width_end - dp->width_start;
-                       memcpy (fbp, dp->width_start, n * sizeof (CHAR_T));
+                       memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
                        fbp += n;
                      }
                  }
                if (dp->precision_start != dp->precision_end)
                  {
                    size_t n = dp->precision_end - dp->precision_start;
-                   memcpy (fbp, dp->precision_start, n * sizeof (CHAR_T));
+                   memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
                    fbp += n;
                  }
 
@@ -2790,15 +2812,11 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                for (;;)
                  {
-                   size_t maxlen;
-                   int count;
-                   int retcount;
-
-                   maxlen = allocated - length;
-                   count = -1;
-                   retcount = 0;
+                   int count = -1;
+                   int retcount = 0;
 
 #if USE_SNPRINTF
+                   size_t maxlen = allocated - length;
                    /* SNPRINTF can fail if maxlen > INT_MAX.  */
                    if (maxlen > INT_MAX)
                      goto overflow;
@@ -3013,6 +3031,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                        return NULL;
                      }
 
+#if USE_SNPRINTF
                    /* Make room for the result.  */
                    if (count >= maxlen)
                      {
@@ -3023,12 +3042,24 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          xmax (xsum (length, count), xtimes (allocated, 2));
 
                        ENSURE_ALLOCATION (n);
-#if USE_SNPRINTF
                        continue;
-#else
-                       maxlen = allocated - length;
+                     }
 #endif
+
+#if !USE_SNPRINTF
+                   /* Make room for the result.  */
+                   if (count > allocated - length)
+                     {
+                       /* Need at least count bytes.  But allocate
+                          proportionally.  */
+                       size_t n =
+                         xmax (xsum (length, count), xtimes (allocated, 2));
+
+                       ENSURE_ALLOCATION (n);
                      }
+#endif
+
+                   /* Here count <= allocated - length.  */
 
                    /* Perform padding.  */
 #if NEED_PRINTF_FLAG_ZERO
@@ -3036,32 +3067,31 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      {
 # if USE_SNPRINTF
                        /* Make room for the result.  */
-                       if (width >= maxlen)
+                       if (width > maxlen)
                          {
                            /* Need at least width bytes.  But allocate
                               proportionally, to avoid looping eternally if
                               snprintf() reports a too small count.  */
                            size_t n =
-                             xmax (xsum (length + 1, width),
+                             xmax (xsum (length, width),
                                    xtimes (allocated, 2));
 
                            length += count;
                            ENSURE_ALLOCATION (n);
                            length -= count;
-                           maxlen = allocated - length; /* > width */
                          }
-                       /* Here width < maxlen.  */
+                       /* Here width <= allocated - length.  */
 # endif
                        {
 # if USE_SNPRINTF
-                         CHAR_T * const rp = result + length;
+                         DCHAR_T * const rp = result + length;
 # else
-                         CHAR_T * const rp = tmp;
+                         DCHAR_T * const rp = tmp;
 # endif
-                         CHAR_T *p = rp + count;
+                         DCHAR_T *p = rp + count;
                          size_t pad = width - count;
-                         CHAR_T *end = p + pad;
-                         CHAR_T *pad_ptr = (*rp == '-' ? rp + 1 : rp);
+                         DCHAR_T *end = p + pad;
+                         DCHAR_T *pad_ptr = (*rp == '-' ? rp + 1 : rp);
                          /* No zero-padding of "inf" and "nan".  */
                          if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
                              || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
@@ -3079,7 +3109,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
                            {
                              /* Pad with zeroes.  */
-                             CHAR_T *q = end;
+                             DCHAR_T *q = end;
 
                              while (p > pad_ptr)
                                *--q = *--p;
@@ -3089,7 +3119,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          else
                            {
                              /* Pad with spaces on the left.  */
-                             CHAR_T *q = end;
+                             DCHAR_T *q = end;
 
                              while (p > rp)
                                *--q = *--p;
@@ -3109,13 +3139,13 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      abort ();
 #endif
 
-                   /* Here still count < maxlen.  */
+                   /* Here still count <= allocated - length.  */
 
 #if USE_SNPRINTF
                    /* The snprintf() result did fit.  */
 #else
                    /* Append the sprintf() result.  */
-                   memcpy (result + length, tmp, count * sizeof (CHAR_T));
+                   memcpy (result + length, tmp, count * sizeof (DCHAR_T));
                    if (tmp != tmpbuf)
                      free (tmp);
 #endif
@@ -3124,7 +3154,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    if (dp->conversion == 'F')
                      {
                        /* Convert the %f result to upper case for %F.  */
-                       CHAR_T *rp = result + length;
+                       DCHAR_T *rp = result + length;
                        size_t rc;
                        for (rc = count; rc > 0; rc--, rp++)
                          if (*rp >= 'a' && *rp <= 'z')
@@ -3146,9 +3176,9 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
     if (result != resultbuf && length + 1 < allocated)
       {
        /* Shrink the allocated memory if possible.  */
-       CHAR_T *memory;
+       DCHAR_T *memory;
 
-       memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
+       memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
        if (memory != NULL)
          result = memory;
       }
@@ -3189,5 +3219,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 #undef PRINTF_PARSE
 #undef DIRECTIVES
 #undef DIRECTIVE
-#undef CHAR_T
+#undef TCHAR_T
+#undef DCHAR_T
+#undef FCHAR_T
 #undef VASNPRINTF