Example #1
0
int
printf_size (FILE *fp, const struct printf_info *info, const void *const *args)
{
  /* Units for the both formats.  */
#define BINARY_UNITS	" kmgtpezy"
#define DECIMAL_UNITS	" KMGTPEZY"
  static const char units[2][sizeof (BINARY_UNITS)] =
  {
    BINARY_UNITS,	/* For binary format.  */
    DECIMAL_UNITS	/* For decimal format.  */
  };
  const char *tag = units[isupper (info->spec) != 0];
  int divisor = isupper (info->spec) ? 1000 : 1024;

  /* The floating-point value to output.  */
  union
    {
      union ieee754_double dbl;
      union ieee854_long_double ldbl;
    }
  fpnum;
  const void *ptr = &fpnum;

  int negative = 0;

  /* "NaN" or "Inf" for the special cases.  */
  const char *special = NULL;
  const wchar_t *wspecial = NULL;

  struct printf_info fp_info;
  int done = 0;
  int wide = info->wide;


  /* Fetch the argument value.	*/
#ifndef __NO_LONG_DOUBLE_MATH
  if (info->is_long_double && sizeof (long double) > sizeof (double))
    {
      fpnum.ldbl.d = *(const long double *) args[0];

      /* Check for special values: not a number or infinity.  */
      if (__isnanl (fpnum.ldbl.d))
	{
	  special = "nan";
	  wspecial = L"nan";
	  negative = 0;
	}
      else if (__isinfl (fpnum.ldbl.d))
	{
	  special = "inf";
	  wspecial = L"inf";

	  negative = fpnum.ldbl.d < 0;
	}
      else
	while (fpnum.ldbl.d >= divisor && tag[1] != '\0')
	  {
	    fpnum.ldbl.d /= divisor;
	    ++tag;
	  }
    }
  else
#endif	/* no long double */
    {
      fpnum.dbl.d = *(const double *) args[0];

      /* Check for special values: not a number or infinity.  */
      if (__isnan (fpnum.dbl.d))
	{
	  special = "nan";
	  wspecial = L"nan";
	  negative = 0;
	}
      else if (__isinf (fpnum.dbl.d))
	{
	  special = "inf";
	  wspecial = L"inf";

	  negative = fpnum.dbl.d < 0;
	}
      else
	while (fpnum.dbl.d >= divisor && tag[1] != '\0')
	  {
	    fpnum.dbl.d /= divisor;
	    ++tag;
	  }
    }

  if (special)
    {
      int width = info->prec > width ? info->prec : info->width;

      if (negative || info->showsign || info->space)
	--width;
      width -= 3;

      if (!info->left && width > 0)
	PADN (' ', width);

      if (negative)
	outchar ('-');
      else if (info->showsign)
	outchar ('+');
      else if (info->space)
	outchar (' ');

      PRINT (special, wspecial, 3);

      if (info->left && width > 0)
	PADN (' ', width);

      return done;
    }

  /* Prepare to print the number.  We want to use `__printf_fp' so we
     have to prepare a `printf_info' structure.  */
  fp_info.spec = 'f';
  fp_info.prec = info->prec < 0 ? 3 : info->prec;
  fp_info.is_long_double = info->is_long_double;
  fp_info.is_short = info->is_short;
  fp_info.is_long = info->is_long;
  fp_info.alt = info->alt;
  fp_info.space = info->space;
  fp_info.left = info->left;
  fp_info.showsign = info->showsign;
  fp_info.group = info->group;
  fp_info.extra = info->extra;
  fp_info.pad = info->pad;
  fp_info.wide = wide;

  if (fp_info.left && fp_info.pad == L' ')
    {
      /* We must do the padding ourself since the unit character must
	 be placed before the padding spaces.  */
      fp_info.width = 0;

      done = __printf_fp (fp, &fp_info, &ptr);
      if (done > 0)
	{
	  outchar (*tag);
	  if (info->width > done)
	    PADN (' ', info->width - done);
	}
    }
  else
    {
      /* We can let __printf_fp do all the printing and just add our
	 unit character afterwards.  */
      fp_info.width = info->width - 1;

      done = __printf_fp (fp, &fp_info, &ptr);
      if (done > 0)
	outchar (*tag);
    }

  return done;
}
Example #2
0
int
__quadmath_printf_fphex (struct __quadmath_printf_file *fp,
			 const struct printf_info *info,
			 const void *const *args)
{
  /* The floating-point value to output.  */
  ieee854_float128 fpnum;

  /* Locale-dependent representation of decimal point.	*/
  const char *decimal;
  wchar_t decimalwc;

  /* "NaN" or "Inf" for the special cases.  */
  const char *special = NULL;
  const wchar_t *wspecial = NULL;

  /* Buffer for the generated number string for the mantissa.  The
     maximal size for the mantissa is 128 bits.  */
  char numbuf[32];
  char *numstr;
  char *numend;
  wchar_t wnumbuf[32];
  wchar_t *wnumstr;
  wchar_t *wnumend;
  int negative;

  /* The maximal exponent of two in decimal notation has 5 digits.  */
  char expbuf[5];
  char *expstr;
  wchar_t wexpbuf[5];
  wchar_t *wexpstr;
  int expnegative;
  int exponent;

  /* Non-zero is mantissa is zero.  */
  int zero_mantissa;

  /* The leading digit before the decimal point.  */
  char leading;

  /* Precision.  */
  int precision = info->prec;

  /* Width.  */
  int width = info->width;

  /* Number of characters written.  */
  int done = 0;

  /* Nonzero if this is output on a wide character stream.  */
  int wide = info->wide;

  bool do_round_away;

  /* Figure out the decimal point character.  */
#ifdef USE_NL_LANGINFO
  if (info->extra == 0)
    decimal = nl_langinfo (DECIMAL_POINT);
  else
    {
      decimal = nl_langinfo (MON_DECIMAL_POINT);
      if (*decimal == '\0')
	decimal = nl_langinfo (DECIMAL_POINT);
    }
  /* The decimal point character must never be zero.  */
  assert (*decimal != '\0');
#elif defined USE_LOCALECONV
  const struct lconv *lc = localeconv ();
  if (info->extra == 0)
    decimal = lc->decimal_point;
  else
    {
      decimal = lc->mon_decimal_point;
      if (decimal == NULL || *decimal == '\0')
	decimal = lc->decimal_point;
    }
  if (decimal == NULL || *decimal == '\0')
    decimal = ".";
#else
  decimal = ".";
#endif
#ifdef USE_NL_LANGINFO_WC
  if (info->extra == 0)
    decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
  else
    {
      decimalwc = nl_langinfo_wc (_NL_MONETARY_DECIMAL_POINT_WC);
      if (decimalwc == L_('\0'))
	decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
    }
  /* The decimal point character must never be zero.  */
  assert (decimalwc != L_('\0'));
#else
  decimalwc = L_('.');
#endif

  /* Fetch the argument value.	*/
    {
      fpnum.value = **(const __float128 **) args[0];

      /* Check for special values: not a number or infinity.  */
      if (isnanq (fpnum.value))
	{
	  negative = fpnum.ieee.negative != 0;
	  if (isupper (info->spec))
	    {
	      special = "NAN";
	      wspecial = L_("NAN");
	    }
	  else
	    {
	      special = "nan";
	      wspecial = L_("nan");
	    }
	}
      else
	{
	  if (isinfq (fpnum.value))
	    {
	      if (isupper (info->spec))
		{
		  special = "INF";
		  wspecial = L_("INF");
		}
	      else
		{
		  special = "inf";
		  wspecial = L_("inf");
		}
	    }

	  negative = signbitq (fpnum.value);
	}
    }

  if (special)
    {
      int width = info->width;

      if (negative || info->showsign || info->space)
	--width;
      width -= 3;

      if (!info->left && width > 0)
	PADN (' ', width);

      if (negative)
	outchar ('-');
      else if (info->showsign)
	outchar ('+');
      else if (info->space)
	outchar (' ');

      PRINT (special, wspecial, 3);

      if (info->left && width > 0)
	PADN (' ', width);

      return done;
    }

    {
      /* We have 112 bits of mantissa plus one implicit digit.  Since
	 112 bits are representable without rest using hexadecimal
	 digits we use only the implicit digits for the number before
	 the decimal point.  */
      uint64_t num0, num1;

      assert (sizeof (long double) == 16);

      num0 = fpnum.ieee.mant_high;
      num1 = fpnum.ieee.mant_low;

      zero_mantissa = (num0|num1) == 0;

      if (sizeof (unsigned long int) > 6)
	{
	  numstr = _itoa_word (num1, numbuf + sizeof numbuf, 16,
			       info->spec == 'A');
	  wnumstr = _itowa_word (num1,
				 wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),
				 16, info->spec == 'A');
	}
      else
	{
	  numstr = _itoa (num1, numbuf + sizeof numbuf, 16,
			  info->spec == 'A');
	  wnumstr = _itowa (num1,
			    wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),
			    16, info->spec == 'A');
	}

      while (numstr > numbuf + (sizeof numbuf - 64 / 4))
	{
	  *--numstr = '0';
	  *--wnumstr = L_('0');
	}

      if (sizeof (unsigned long int) > 6)
	{
	  numstr = _itoa_word (num0, numstr, 16, info->spec == 'A');
	  wnumstr = _itowa_word (num0, wnumstr, 16, info->spec == 'A');
	}
      else
	{
	  numstr = _itoa (num0, numstr, 16, info->spec == 'A');
	  wnumstr = _itowa (num0, wnumstr, 16, info->spec == 'A');
	}

      /* Fill with zeroes.  */
      while (numstr > numbuf + (sizeof numbuf - 112 / 4))
	{
	  *--wnumstr = L_('0');
	  *--numstr = '0';
	}

      leading = fpnum.ieee.exponent == 0 ? '0' : '1';

      exponent = fpnum.ieee.exponent;

      if (exponent == 0)
	{
	  if (zero_mantissa)
	    expnegative = 0;
	  else
	    {
	      /* This is a denormalized number.  */
	      expnegative = 1;
	      exponent = IEEE854_FLOAT128_BIAS - 1;
	    }
	}
      else if (exponent >= IEEE854_FLOAT128_BIAS)
	{
	  expnegative = 0;
	  exponent -= IEEE854_FLOAT128_BIAS;
	}
      else
	{
	  expnegative = 1;
	  exponent = -(exponent - IEEE854_FLOAT128_BIAS);
	}
    }

  /* Look for trailing zeroes.  */
  if (! zero_mantissa)
    {
      wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]];
      numend = &numbuf[sizeof numbuf / sizeof numbuf[0]];
      while (wnumend[-1] == L_('0'))
	{
	  --wnumend;
	  --numend;
	}

      do_round_away = false;

      if (precision != -1 && precision < numend - numstr)
	{
	  char last_digit = precision > 0 ? numstr[precision - 1] : leading;
	  char next_digit = numstr[precision];
	  int last_digit_value = (last_digit >= 'A' && last_digit <= 'F'
				  ? last_digit - 'A' + 10
				  : (last_digit >= 'a' && last_digit <= 'f'
				     ? last_digit - 'a' + 10
				     : last_digit - '0'));
	  int next_digit_value = (next_digit >= 'A' && next_digit <= 'F'
				  ? next_digit - 'A' + 10
				  : (next_digit >= 'a' && next_digit <= 'f'
				     ? next_digit - 'a' + 10
				     : next_digit - '0'));
	  bool more_bits = ((next_digit_value & 7) != 0
			    || precision + 1 < numend - numstr);
#ifdef HAVE_FENV_H
	  int rounding_mode = get_rounding_mode ();
	  do_round_away = round_away (negative, last_digit_value & 1,
				      next_digit_value >= 8, more_bits,
				      rounding_mode);
#endif
	}

      if (precision == -1)
	precision = numend - numstr;
      else if (do_round_away)
	{
	  /* Round up.  */
	  int cnt = precision;
	  while (--cnt >= 0)
	    {
	      char ch = numstr[cnt];
	      /* We assume that the digits and the letters are ordered
		 like in ASCII.  This is true for the rest of GNU, too.  */
	      if (ch == '9')
		{
		  wnumstr[cnt] = (wchar_t) info->spec;
		  numstr[cnt] = info->spec;	/* This is tricky,
		  				   think about it!  */
		  break;
		}
	      else if (tolower (ch) < 'f')
		{
		  ++numstr[cnt];
		  ++wnumstr[cnt];
		  break;
		}
	      else
		{
		  numstr[cnt] = '0';
		  wnumstr[cnt] = L_('0');
		}
	    }
	  if (cnt < 0)
	    {
	      /* The mantissa so far was fff...f  Now increment the
		 leading digit.  Here it is again possible that we
		 get an overflow.  */
	      if (leading == '9')
		leading = info->spec;
	      else if (tolower (leading) < 'f')
		++leading;
	      else
		{
		  leading = '1';
		  if (expnegative)
		    {
		      exponent -= 4;
		      if (exponent <= 0)
			{
			  exponent = -exponent;
			  expnegative = 0;
			}
		    }
		  else
		    exponent += 4;
		}
	    }
	}
    }
  else
    {
      if (precision == -1)
	precision = 0;
      numend = numstr;
      wnumend = wnumstr;
    }

  /* Now we can compute the exponent string.  */
  expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
  wexpstr = _itowa_word (exponent,
			 wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);

  /* Now we have all information to compute the size.  */
  width -= ((negative || info->showsign || info->space)
	    /* Sign.  */
	    + 2    + 1 + 0 + precision + 1 + 1
	    /* 0x    h   .   hhh         P   ExpoSign.  */
	    + ((expbuf + sizeof expbuf) - expstr));
	    /* Exponent.  */

  /* Count the decimal point.
     A special case when the mantissa or the precision is zero and the `#'
     is not given.  In this case we must not print the decimal point.  */
  if (precision > 0 || info->alt)
    width -= wide ? 1 : strlen (decimal);

  if (!info->left && info->pad != '0' && width > 0)
    PADN (' ', width);

  if (negative)
    outchar ('-');
  else if (info->showsign)
    outchar ('+');
  else if (info->space)
    outchar (' ');

  outchar ('0');
  if ('X' - 'A' == 'x' - 'a')
    outchar (info->spec + ('x' - 'a'));
  else
    outchar (info->spec == 'A' ? 'X' : 'x');

  if (!info->left && info->pad == '0' && width > 0)
    PADN ('0', width);

  outchar (leading);

  if (precision > 0 || info->alt)
    {
      const wchar_t *wtmp = &decimalwc;
      PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
    }

  if (precision > 0)
    {
      ssize_t tofill = precision - (numend - numstr);
      PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
      if (tofill > 0)
	PADN ('0', tofill);
    }

  if ('P' - 'A' == 'p' - 'a')
    outchar (info->spec + ('p' - 'a'));
  else
    outchar (info->spec == 'A' ? 'P' : 'p');

  outchar (expnegative ? '-' : '+');

  PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);

  if (info->left && info->pad != '0' && width > 0)
    PADN (info->pad, width);

  return done;
}
Example #3
0
int
__printf_dfp (FILE *fp,
	      const struct printf_info *info,
	      const void *const *args)
{
  int wide = info->wide;
  /* Counter for number of written characters.  */
  int done = 0;

  /* Locale-dependent representation of decimal point.  */
  const char *decimal;

  union { const char *mb; int wc; } decimalwc;

  char spec = tolower(info->spec);

  /* Locale-dependent thousands separator and grouping specification.  */
  const char *thousands_sep = NULL;
  wchar_t thousands_sepwc = 0;
  const char * thousands_sepmb;

  const char *grouping;

#ifdef OPTION_EGLIBC_LOCALE_CODE
  if (info->extra == 0)
    {
      decimal = nl_langinfo (__DECIMAL_POINT);
      decimalwc.mb = nl_langinfo (_NL_NUMERIC_DECIMAL_POINT_WC);
    }
  else
    {
      decimal = nl_langinfo (__MON_DECIMAL_POINT);
      if (*decimal == '\0')
	decimal = nl_langinfo (__DECIMAL_POINT);

      decimalwc.mb = nl_langinfo (_NL_MONETARY_DECIMAL_POINT_WC);
      if (decimalwc.wc == L'\0')
	decimalwc.mb = nl_langinfo (_NL_NUMERIC_DECIMAL_POINT_WC);
    }
  /* The decimal point character must not be zero.  */
  assert (*decimal != '\0');
  assert (decimalwc.wc != L'\0');
#else
  /* Hard-code values from 'C' locale.  */
  decimal = ".";
  decimalwc.wc = L'.';
#endif

#ifdef OPTION_EGLIBC_LOCALE_CODE
  if (info->group)
    {
      if (info->extra == 0)
	grouping = nl_langinfo (__GROUPING);
      else
	grouping = nl_langinfo (__MON_GROUPING);

      if (*grouping <= 0 || *grouping == CHAR_MAX)
	grouping = NULL;
      else
	{
	  /* Figure out the thousands separator character.  */
	  if (wide)
	    {
	      if (info->extra == 0)
		{
		  thousands_sepmb = nl_langinfo (_NL_NUMERIC_THOUSANDS_SEP_WC);
		  mbrtowc(&thousands_sepwc,thousands_sepmb, CHAR_MAX, NULL);
		}
	      else
		{
		  thousands_sepmb = nl_langinfo (_NL_MONETARY_THOUSANDS_SEP_WC);
		  mbrtowc(&thousands_sepwc,thousands_sepmb, CHAR_MAX, NULL);
		}
	    }
	  else
	    {
	      if (info->extra == 0)
		thousands_sep = nl_langinfo (__THOUSANDS_SEP);
	      else
		thousands_sep = nl_langinfo (__MON_THOUSANDS_SEP);
	    }

	  if ((wide && thousands_sepwc == L'\0')
	      || (! wide && *thousands_sep == '\0'))
	    grouping = NULL;
	  else if (thousands_sepwc == L'\0')
	    /* If we are printing multibyte characters and there is a
	       multibyte representation for the thousands separator,
	       we must ensure the wide character thousands separator
	       is available, even if it is fake.  */
	    thousands_sepwc = 0xfffffffe;
	}
    }
  else
    grouping = NULL;
#else
  grouping = NULL;
#endif

  /* Seriously, only touch this code if you MUST.  */

{
  char digits[DECIMAL_PRINTF_BUF_SIZE];
  int exp,       /* The exponent. */
   is_neg,       /* Is negative?  */
   is_nan,       /* Is not a number?  */
   is_inf,       /* Is infinite? */
   decpt = 2,    /* decimal point offset into digits[] */
   prec,         /* number of digits that follow the decimal point, or number of significant digits for %g */
   default_prec = 6, /* Default precision, per the C Spec.  */
   input_prec = 0,   /* Precision of the _Decimal* value.  */
   mw,           /* Mantissa Width  */
   n,            /* Current digit offset into digits[] */
   nd,           /* num_digits before the get_digits call. */
   width,        /* Width of the field */
   is_zero = 0;  /* Used in some of the output tests.  */

  digits[0] = '0'; /* need an extra digit for rounding up */

  if (info->user & mod_D)
    {
      _Decimal64 d64 = **(_Decimal64**)args[0];
      if (d64 == 0) is_zero = 1;
      nd = numdigitsd64(d64);
      __get_digits_d64 (d64, digits+1, &exp, &is_neg, &is_nan, &is_inf);
      mw = __DEC64_MANT_DIG__ + 1;
    }
  else if (info->user & mod_DD)
    {
      _Decimal128 d128 = **(_Decimal128**)args[0];
      if (d128 == 0) is_zero = 1;
      nd = numdigitsd128(d128);
      __get_digits_d128 (d128, digits+1, &exp, &is_neg, &is_nan, &is_inf);
      mw = __DEC128_MANT_DIG__ + 1;
    }
  else if (info->user & mod_H)
    {
       _Decimal32 d32 = **(_Decimal32**)args[0];
       if (d32 == 0) is_zero = 1;
       nd = numdigitsd32(d32);
       __get_digits_d32 (d32, digits+1, &exp, &is_neg, &is_nan, &is_inf);
       mw = __DEC32_MANT_DIG__ + 1;
    }
  else /* We shouldn't get here, but it is possible.  */
    return -2;

  /* The first digit is always a zero to allow rounding.  */
  n = 0;

  /* 'n' = position of first non-zero digit in the right-justified mantissa.  */
  n = mw - nd;

  /* Width and precision can not both be set or the results are undefined per
   * the C Spec.  */
  width = info->width;

  /* The user specified precision overrides the input's inherent precision.
   * This gets complicated quickly.  */
  prec = info->prec;

  if (is_nan || is_inf)
    {
      width -= 3;
      /*if (is_nan) is_neg = 0;*/
      if (is_neg || info->showsign || info->space) width--;

      if (!info->left && width > 0)
	PADN (' ', width);

      if (is_neg)
	outchar ('-');
      else if (info->showsign)
	outchar ('+');
      else if (info->space)
	outchar (' ');

      if (is_nan)
	{
	  if (isupper(info->spec))
	    { outchar ('N'); outchar ('A'); outchar ('N'); }
	  else
	    { outchar ('n'); outchar ('a'); outchar ('n'); }
	}
      else
	{
	  if (isupper(info->spec))
	    { outchar ('I'); outchar ('N'); outchar ('F'); }
	  else
	    { outchar ('i'); outchar ('n'); outchar ('f'); }
	}
      if (info->left && width > 0)
	PADN (' ', width);

	return 0;
    }

  /* The term "precision" refers to the number of significant digits right of
   * the decimal place.  Determine the implicit precision of the input value.
   * There are special rules for each of the supported flags.*/
  switch (spec)
    {
      case 'a':
	  {
	    /* The DFP spec addition for %a refers to all of the significant
	     * digits in the precision.  */
	    input_prec = nd;

	    /* This same check is done in two different places but it'll only
	     * effect a single pass through once.  If prec is not set it'll hit
	     * this instance.  If prec is set it'll hit the next instance.  This
	     * is because the DFP spec requires this to be run after rounding
	     * when prec < input_prec.  */
	    if (prec < 0 || prec >= input_prec)
	    {
	      /* Per the DFP specification (s,c,q), c == digits, q = exp, s ==
	       * is_neg.  */
	      if (exp >= -(nd+5) && exp <= 0)
	        {
	          prec = -exp;
	          spec = 'f';
	        }
	      else
	        {
	          prec = nd - 1;
	          spec = 'e';
	          input_prec = nd - 1;
	        }
	      }
	    break;
	  }
	case 'g':
	  {
	    int P = prec;

	    /* When the C specification refers to X as the exponent it means the
	     * exponent when the input value encoding is normalized to the form
	     * d.dddd.  This means we have to do that before we can do the goof
	     * check.
	     *
	     * e.g., 123.456E-5
	     * right-justified -> 00123456E-9
	     * normalized -> 1.23456E-4
	     *
	     * Normalize X to d.ddd... form by taking (exp) + (nd - 1)
	     *
	     * X == -4  */
	    int X = exp + (nd -1);

	    /* The C Specification also indicates how to compute P. */
	    if (prec < 0)
	      P = 6;
	    else if (prec == 0)
	      P = 1;

	    /* Straight from the specification which assumes X is exponent normalized to
	     * d.ddd... form.  */
	    if (X >= -4 && P > X)
	      {
		prec = (P - (X + 1));
		spec = 'f';
	      }
	    else
	      {
		prec = P - 1;
		spec = 'e';
	      }
	    input_prec = nd - 1;
	  break;
	  }
	case 'e':
	  input_prec = nd - 1;
	  break;
	case 'f':
	  if(exp < 0 && (-exp) > default_prec)
	    /*  00123456E-7 has an input_prec of 7. */
	    input_prec = (-exp);
	  else
	    /*  01234567E-6 has an input_prec of 6. */
	    /*  00000190E6 has an input_prec of 6. */
	    /*  00000123E1 has an input_prec of 6.  */
	    /*  00000123E0 has an input_prec of 6.  */
	    input_prec = default_prec;
	  break;
    }

  /* The specs 'g' and 'a' may have already modified prec so this won't happen for
   * those cases.  */
  if (prec < 0)
    prec = default_prec;

  /* Do rounding if precision is less than the decimal type.  On hardware DFP
   * this could probably easily be done with quantize but on soft-dfp the
   * existing method would be faster.  */
  if (prec < input_prec)
    {
      int index, roundmode = 0;
      char rounddigit = '4';

      if (spec == 'f')
	/* This may force index to negative, in which case we ignore it at a
	 * later time.  */
	index = n + nd + exp + prec;
      /* Goofy special case where we round significant digits which aren't
       * right of the decimal place.  */
      else if (tolower(info->spec) == 'a' && prec > 0)
       {
	index = n + prec;
       }
      else
	index = n + prec + 1;

      /* FIXME: we should check rounding mode for %a */
      if (__printf_dfp_getround_callback)
        {
          roundmode = (*__printf_dfp_getround_callback)();

	  switch (roundmode)
	    {
	      case FE_DEC_TONEAREST: rounddigit = '4'; break;
	      case FE_DEC_TOWARDZERO: rounddigit = '9'; break;
	      case FE_DEC_UPWARD: rounddigit = (is_neg ? '9' : '0'-1); break;
	      case FE_DEC_DOWNWARD: rounddigit = (is_neg ? '0'-1 : '9'); break;
	      case FE_DEC_TONEARESTFROMZERO: rounddigit = '4'; break;
	      case 5: rounddigit = '4'; break; /* nearest, ties toward zero */
	      case 6: rounddigit = '0'-1; break; /* away from zero */
	      case 7: rounddigit = '4'; break; /* round for shorter precision */
	      default: rounddigit = '4'; break;
	    }
	}

      /* If this is true then the requested precision is smaller than the
      * default and rounding is required.  If 'exp' was sufficiently negative
      * 'index' may be negative, in which case we don't need to round.  */
      if (index > 0 && index < mw && digits[index] > rounddigit) 
	do {
	  int trailzero = index+1;
	  if (digits[index] == rounddigit+1)
	    {
	      while (trailzero < mw)
		{
		  if (digits[trailzero] != '0')
		    {
		      trailzero = 0;
		      break;
		    }
		  ++trailzero;
		}
	      if (roundmode == FE_DEC_TONEAREST && trailzero &&
	        (digits[index-1] & 1) == 0) break;
	      if (roundmode == FE_DEC_UPWARD && !trailzero) break;
	      if (roundmode == FE_DEC_DOWNWARD && !trailzero) break;
	      if (roundmode == 5 && trailzero) break;
	      if (roundmode == 6 && trailzero) break;
	  }

	while (digits[--index] == '9')
	  digits[index] = '0';
	digits[index]++;
	if (index < n)
	  {
	    n--;
	    nd++;
	  }
      } while (0);
    } /* Done rounding.  */

  /* If spec == 'a' at this point it means that prec was set by the user
   * and rounding had to be considered.  The spec now requires that the
   * 'a' format presentation algorithm be calculated again.  If prec
   * wasn't set by the user then this was handled earlier and spec has already
   * been set to either 'e' or 'f'.  */
  if (spec == 'a')
    {
      int old_exp = exp;

      /* The goofy DFP specification requires that we now assume that after
       * rounding the digits are right justified and truncated and the
       * algorithm recomputed using the new values for nd and exp, e.g.,
       *
       * 00654300E-2 with %.1Hf -> 00000007E3.  */

       exp = nd + exp - prec;
       nd = prec;

      /* Per the DFP specification (s,c,q), c == digits, q = exp, s ==
      * is_neg.  */
      if (exp >= -(nd+5) && exp <= 0)
	{
	  prec = -exp;
	  spec = 'f';
	}
      else
	{
	  prec = nd - 1;
	  if (prec < 0) prec = 0;
	  spec = 'e';
	  input_prec = nd - 1;
	  /* Return exp to the original value because the 'e' case below will
	   * recompute it.  */
	  exp = old_exp;
	}
	/* spec will have been changed to 'e' or 'f' at this point, so determine
	* the decimal point now.  */
    }

  /* Calculate decimal point, adjust prec and exp if necessary.
   * By this point everything should be represented as either %e or %f.  */
  if (spec == 'f')
    {
      if (exp < 0)
	decpt = exp + nd + n;
      else if (is_zero)
	decpt = n + 1;
      else
	decpt = n + nd + exp;
    }
  else if (spec == 'e')
    {
      decpt = n + 1;
      exp = mw + exp - decpt;
    }

  /* Remove trailing zeroes for %g */
  if (tolower(info->spec) == 'g' && !info->alt)
    {
      while (prec > 0 && decpt+prec > mw) prec--;
      while (prec > 0 && digits[decpt+prec-1] == '0') prec--;
    }

  /* Remove trailing zeroes for %a, but only if they are not significant.  */
  if (tolower(info->spec) == 'a')
    {
      while (prec > 0 && decpt+prec > mw) prec--;
      while (prec > 0 && decpt+prec > n+nd && digits[decpt+prec-1] == '0') prec--;
    }

  /* Digits to the left of the decimal pt. */
  if (n < decpt)
    {
      width -= decpt - n;
      if (grouping) width -= (decpt-n)/3;
    }
  else width--;  /* none to the left of the decimal point */

  /* Digits to the right of the decimal pt. */
  if (prec > 0) width -= 1 + prec;
  else if (info->alt) width -= 1;

  if (spec != 'f')
    {
      width -= 3;
      if (0!=(exp/10) || spec!='a') --width;
      if (0!=(exp/100)) --width;
      if (0!=(exp/1000)) --width;
    }

  if (is_neg || info->showsign || info->space) width--;

  if (!info->left && info->pad != '0' && width > 0)
    PADN (info->pad, width);

  if (is_neg)
    outchar ('-');
  else if (info->showsign)
    outchar ('+');
  else if (info->space)
    outchar (' ');

  if (!info->left && info->pad == '0' && width > 0)
    PADN ('0', width);

  /* Print zero, decimal point and leading zeroes if needed */
  if (decpt <= n)
    {
      n = decpt;
      outchar ('0');
      if (n < 0)
	{
	  outchar (wide ? decimalwc.wc : *decimal);
	  while (n < 0 && n < decpt + prec)
	    {
	      outchar ('0');
	      n++;
	    }
	}
    }

  /* Print the digits.  If decpt exceeds mw then we know that
   * they're simply trailing zeros and we don't need to display them.  */
  while (n < mw && n < decpt + prec)
    {
      if (n == decpt)
      {
	outchar (wide ? decimalwc.wc : *decimal);
      }
      else if (grouping && n < decpt && (decpt-n)%3 == 0)
	outchar (wide ? thousands_sepwc : *thousands_sep);
      outchar (digits[n]);
      n++;
    }

  /* print trailing zeroes */
  while (n < decpt + prec)
    {
      if (n == decpt)
	outchar (wide ? decimalwc.wc : *decimal);
      else if (grouping && n < decpt && (decpt-n)%3 == 0)
	outchar (wide ? thousands_sepwc : *thousands_sep);
      outchar ('0');
      n++;
    }

  /* print decimal point, if needed */
  if (n == decpt && info->alt) outchar (wide ? decimalwc.wc : *decimal);

  /* The C spec says that for %e, if the value is zero the exponent is zero.
   * This isn't true for the DFP spec for %a so make sure to check info->spec
   * and not spec since it could have promoted 'a' to 'e'.  */
  if(spec == 'e' && (tolower(info->spec) != 'a' && is_zero))
    exp = 0;

  /* Don't display the exponent part for 'f' because it is never used and don't
   * do it for 'g' if the value is zero.  */
  if (spec != 'f' && !((tolower(info->spec) == 'g') && is_zero))
    {
      outchar (isupper(info->spec) ? 'E' : 'e');
      if (exp < 0)
	{ outchar ('-'); n = -exp; }
      else
	{ outchar ('+'); n = exp; }
      if (n >= 1000) outchar ('0'+((n/1000)%10));
      if (n >= 100) outchar ('0'+((n/100)%10));
      if (n >= 10 || (tolower(info->spec) != 'a')) outchar ('0'+((n/10)%10));
      outchar ('0'+(n%10));
    }

  if (info->left && width > 0)
    PADN (info->pad, width);
} /* Done output block.  */

   return 0;
}
Example #4
0
int
__printf_fphex (FILE *fp,
		const struct printf_info *info,
		const void *const *args)
{
  /* The floating-point value to output.  */
  union
    {
      union ieee754_double dbl;
      long double ldbl;
    }
  fpnum;

  /* Locale-dependent representation of decimal point.	*/
  const char *decimal;
  wchar_t decimalwc;

  /* "NaN" or "Inf" for the special cases.  */
  const char *special = NULL;
  const wchar_t *wspecial = NULL;

  /* Buffer for the generated number string for the mantissa.  The
     maximal size for the mantissa is 128 bits.  */
  char numbuf[32];
  char *numstr;
  char *numend;
  wchar_t wnumbuf[32];
  wchar_t *wnumstr;
  wchar_t *wnumend;
  int negative;

  /* The maximal exponent of two in decimal notation has 5 digits.  */
  char expbuf[5];
  char *expstr;
  wchar_t wexpbuf[5];
  wchar_t *wexpstr;
  int expnegative;
  int exponent;

  /* Non-zero is mantissa is zero.  */
  int zero_mantissa;

  /* The leading digit before the decimal point.  */
  char leading;

  /* Precision.  */
  int precision = info->prec;

  /* Width.  */
  int width = info->width;

  /* Number of characters written.  */
  int done = 0;

  /* Nonzero if this is output on a wide character stream.  */
#if __OPTION_POSIX_C_LANG_WIDE_CHAR
  int wide = info->wide;
#else
  /* This should never be called on a wide-oriented stream when
     OPTION_POSIX_C_LANG_WIDE_CHAR is disabled, but the compiler can't
     be trusted to figure that out.  */
  const int wide = 0;
#endif


  /* Figure out the decimal point character.  */
#if __OPTION_EGLIBC_LOCALE_CODE
  if (info->extra == 0)
    {
      decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
      decimalwc = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
    }
  else
    {
      decimal = _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
      decimalwc = _NL_CURRENT_WORD (LC_MONETARY,
				    _NL_MONETARY_DECIMAL_POINT_WC);
    }
  /* The decimal point character must never be zero.  */
  assert (*decimal != '\0' && decimalwc != L'\0');
#else
  decimal = ".";
  decimalwc = L'.';
#endif


  /* Fetch the argument value.	*/
#ifndef __NO_LONG_DOUBLE_MATH
  if (info->is_long_double && sizeof (long double) > sizeof (double))
    {
      fpnum.ldbl = *(const long double *) args[0];

      /* Check for special values: not a number or infinity.  */
      if (__isnanl (fpnum.ldbl))
	{
	  if (isupper (info->spec))
	    {
	      special = "NAN";
	      wspecial = L"NAN";
	    }
	  else
	    {
	      special = "nan";
	      wspecial = L"nan";
	    }
	}
      else
	{
	  if (__isinfl (fpnum.ldbl))
	    {
	      if (isupper (info->spec))
		{
		  special = "INF";
		  wspecial = L"INF";
		}
	      else
		{
		  special = "inf";
		  wspecial = L"inf";
		}
	    }
	}
      negative = signbit (fpnum.ldbl);
    }
  else
#endif	/* no long double */
    {
      fpnum.dbl.d = *(const double *) args[0];

      /* Check for special values: not a number or infinity.  */
      if (__isnan (fpnum.dbl.d))
	{
	  negative = fpnum.dbl.ieee.negative != 0;
	  if (isupper (info->spec))
	    {
	      special = "NAN";
	      wspecial = L"NAN";
	    }
	  else
	    {
	      special = "nan";
	      wspecial = L"nan";
	    }
	}
      else
	{
	  int res = __isinf (fpnum.dbl.d);
	  if (res)
	    {
	      if (isupper (info->spec))
		{
		  special = "INF";
		  wspecial = L"INF";
		}
	      else
		{
		  special = "inf";
		  wspecial = L"inf";
		}
	      negative = res < 0;
	    }
	  else
	    negative = signbit (fpnum.dbl.d);
	}
    }

  if (special)
    {
      int width = info->width;

      if (negative || info->showsign || info->space)
	--width;
      width -= 3;

      if (!info->left && width > 0)
	PADN (' ', width);

      if (negative)
	outchar ('-');
      else if (info->showsign)
	outchar ('+');
      else if (info->space)
	outchar (' ');

      PRINT (special, wspecial, 3);

      if (info->left && width > 0)
	PADN (' ', width);

      return done;
    }

  if (info->is_long_double == 0 || sizeof (double) == sizeof (long double))
    {
      /* We have 52 bits of mantissa plus one implicit digit.  Since
	 52 bits are representable without rest using hexadecimal
	 digits we use only the implicit digits for the number before
	 the decimal point.  */
      unsigned long long int num;

      num = (((unsigned long long int) fpnum.dbl.ieee.mantissa0) << 32
	     | fpnum.dbl.ieee.mantissa1);

      zero_mantissa = num == 0;

      if (sizeof (unsigned long int) > 6)
	{
	  wnumstr = _itowa_word (num, wnumbuf + (sizeof wnumbuf) / sizeof (wchar_t), 16,
				 info->spec == 'A');
	  numstr = _itoa_word (num, numbuf + sizeof numbuf, 16,
			       info->spec == 'A');
	}
      else
	{
	  wnumstr = _itowa (num, wnumbuf + sizeof wnumbuf / sizeof (wchar_t), 16,
			    info->spec == 'A');
	  numstr = _itoa (num, numbuf + sizeof numbuf, 16,
			  info->spec == 'A');
	}

      /* Fill with zeroes.  */
      while (wnumstr > wnumbuf + (sizeof wnumbuf - 52) / sizeof (wchar_t))
	{
	  *--wnumstr = L'0';
	  *--numstr = '0';
	}

      leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1';

      exponent = fpnum.dbl.ieee.exponent;

      if (exponent == 0)
	{
	  if (zero_mantissa)
	    expnegative = 0;
	  else
	    {
	      /* This is a denormalized number.  */
	      expnegative = 1;
	      exponent = IEEE754_DOUBLE_BIAS - 1;
	    }
	}
      else if (exponent >= IEEE754_DOUBLE_BIAS)
	{
	  expnegative = 0;
	  exponent -= IEEE754_DOUBLE_BIAS;
	}
      else
	{
	  expnegative = 1;
	  exponent = -(exponent - IEEE754_DOUBLE_BIAS);
	}
    }
#ifdef PRINT_FPHEX_LONG_DOUBLE
  else
    PRINT_FPHEX_LONG_DOUBLE;
#endif

  /* Look for trailing zeroes.  */
  if (! zero_mantissa)
    {
      wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]];
      numend = &numbuf[sizeof numbuf / sizeof numbuf[0]];
      while (wnumend[-1] == L'0')
	{
	  --wnumend;
	  --numend;
	}

      bool do_round_away = false;

      if (precision != -1 && precision < numend - numstr)
	{
	  char last_digit = precision > 0 ? numstr[precision - 1] : leading;
	  char next_digit = numstr[precision];
	  int last_digit_value = (last_digit >= 'A' && last_digit <= 'F'
				  ? last_digit - 'A' + 10
				  : (last_digit >= 'a' && last_digit <= 'f'
				     ? last_digit - 'a' + 10
				     : last_digit - '0'));
	  int next_digit_value = (next_digit >= 'A' && next_digit <= 'F'
				  ? next_digit - 'A' + 10
				  : (next_digit >= 'a' && next_digit <= 'f'
				     ? next_digit - 'a' + 10
				     : next_digit - '0'));
	  bool more_bits = ((next_digit_value & 7) != 0
			    || precision + 1 < numend - numstr);
	  int rounding_mode = get_rounding_mode ();
	  do_round_away = round_away (negative, last_digit_value & 1,
				      next_digit_value >= 8, more_bits,
				      rounding_mode);
	}

      if (precision == -1)
	precision = numend - numstr;
      else if (do_round_away)
	{
	  /* Round up.  */
	  int cnt = precision;
	  while (--cnt >= 0)
	    {
	      char ch = numstr[cnt];
	      /* We assume that the digits and the letters are ordered
		 like in ASCII.  This is true for the rest of GNU, too.  */
	      if (ch == '9')
		{
		  wnumstr[cnt] = (wchar_t) info->spec;
		  numstr[cnt] = info->spec;	/* This is tricky,
						   think about it!  */
		  break;
		}
	      else if (tolower (ch) < 'f')
		{
		  ++numstr[cnt];
		  ++wnumstr[cnt];
		  break;
		}
	      else
		{
		  numstr[cnt] = '0';
		  wnumstr[cnt] = L'0';
		}
	    }
	  if (cnt < 0)
	    {
	      /* The mantissa so far was fff...f  Now increment the
		 leading digit.  Here it is again possible that we
		 get an overflow.  */
	      if (leading == '9')
		leading = info->spec;
	      else if (tolower (leading) < 'f')
		++leading;
	      else
		{
		  leading = '1';
		  if (expnegative)
		    {
		      exponent -= 4;
		      if (exponent <= 0)
			{
			  exponent = -exponent;
			  expnegative = 0;
			}
		    }
		  else
		    exponent += 4;
		}
	    }
	}
    }
  else
    {
      if (precision == -1)
	precision = 0;
      numend = numstr;
      wnumend = wnumstr;
    }

  /* Now we can compute the exponent string.  */
  expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
  wexpstr = _itowa_word (exponent,
			 wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);

  /* Now we have all information to compute the size.  */
  width -= ((negative || info->showsign || info->space)
	    /* Sign.  */
	    + 2    + 1 + 0 + precision + 1 + 1
	    /* 0x    h   .   hhh         P   ExpoSign.  */
	    + ((expbuf + sizeof expbuf) - expstr));
	    /* Exponent.  */

  /* Count the decimal point.
     A special case when the mantissa or the precision is zero and the `#'
     is not given.  In this case we must not print the decimal point.  */
  if (precision > 0 || info->alt)
    width -= wide ? 1 : strlen (decimal);

  if (!info->left && info->pad != '0' && width > 0)
    PADN (' ', width);

  if (negative)
    outchar ('-');
  else if (info->showsign)
    outchar ('+');
  else if (info->space)
    outchar (' ');

  outchar ('0');
  if ('X' - 'A' == 'x' - 'a')
    outchar (info->spec + ('x' - 'a'));
  else
    outchar (info->spec == 'A' ? 'X' : 'x');

  if (!info->left && info->pad == '0' && width > 0)
    PADN ('0', width);

  outchar (leading);

  if (precision > 0 || info->alt)
    {
      const wchar_t *wtmp = &decimalwc;
      PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
    }

  if (precision > 0)
    {
      ssize_t tofill = precision - (numend - numstr);
      PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
      if (tofill > 0)
	PADN ('0', tofill);
    }

  if ('P' - 'A' == 'p' - 'a')
    outchar (info->spec + ('p' - 'a'));
  else
    outchar (info->spec == 'A' ? 'P' : 'p');

  outchar (expnegative ? '-' : '+');

  PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);

  if (info->left && info->pad != '0' && width > 0)
    PADN (info->pad, width);

  return done;
}
Example #5
0
int
__printf_size (FILE *fp, const struct printf_info *info,
	       const void *const *args)
{
  /* Units for the both formats.  */
#define BINARY_UNITS	" kmgtpezy"
#define DECIMAL_UNITS	" KMGTPEZY"
  static const char units[2][sizeof (BINARY_UNITS)] =
  {
    BINARY_UNITS,	/* For binary format.  */
    DECIMAL_UNITS	/* For decimal format.  */
  };
  const char *tag = units[isupper (info->spec) != 0];
  int divisor = isupper (info->spec) ? 1000 : 1024;

  /* The floating-point value to output.  */
  union
    {
      union ieee754_double dbl;
      union ieee854_long_double ldbl;
    }
  fpnum;
  const void *ptr = &fpnum;

  int fpnum_sign = 0;

  /* "NaN" or "Inf" for the special cases.  */
  const char *special = NULL;
  const wchar_t *wspecial = NULL;

  struct printf_info fp_info;
  int done = 0;
#if __OPTION_POSIX_C_LANG_WIDE_CHAR
  int wide = info->wide;
#else
  /* This should never be called on a wide-oriented stream when
     OPTION_POSIX_C_LANG_WIDE_CHAR is disabled, but the compiler can't
     be trusted to figure that out.  */
  const int wide = 0;
#endif
  int res;

  /* Fetch the argument value.	*/
#ifndef __NO_LONG_DOUBLE_MATH
  if (info->is_long_double && sizeof (long double) > sizeof (double))
    {
      fpnum.ldbl.d = *(const long double *) args[0];

      /* Check for special values: not a number or infinity.  */
      if (__isnanl (fpnum.ldbl.d))
	{
	  special = "nan";
	  wspecial = L"nan";
	  // fpnum_sign = 0;	Already zero
	}
      else if ((res = __isinfl (fpnum.ldbl.d)))
	{
	  fpnum_sign = res;
	  special = "inf";
	  wspecial = L"inf";
	}
      else
	while (fpnum.ldbl.d >= divisor && tag[1] != '\0')
	  {
	    fpnum.ldbl.d /= divisor;
	    ++tag;
	  }
    }
  else
#endif	/* no long double */
    {
      fpnum.dbl.d = *(const double *) args[0];

      /* Check for special values: not a number or infinity.  */
      if (__isnan (fpnum.dbl.d))
	{
	  special = "nan";
	  wspecial = L"nan";
	  // fpnum_sign = 0;	Already zero
	}
      else if ((res = __isinf (fpnum.dbl.d)))
	{
	  fpnum_sign = res;
	  special = "inf";
	  wspecial = L"inf";
	}
      else
	while (fpnum.dbl.d >= divisor && tag[1] != '\0')
	  {
	    fpnum.dbl.d /= divisor;
	    ++tag;
	  }
    }

  if (special)
    {
      int width = info->prec > info->width ? info->prec : info->width;

      if (fpnum_sign < 0 || info->showsign || info->space)
	--width;
      width -= 3;

      if (!info->left && width > 0)
	PADN (' ', width);

      if (fpnum_sign < 0)
	outchar ('-');
      else if (info->showsign)
	outchar ('+');
      else if (info->space)
	outchar (' ');

      PRINT (special, wspecial, 3);

      if (info->left && width > 0)
	PADN (' ', width);

      return done;
    }

  /* Prepare to print the number.  We want to use `__printf_fp' so we
     have to prepare a `printf_info' structure.  */
  fp_info = *info;
  fp_info.spec = 'f';
  fp_info.prec = info->prec < 0 ? 3 : info->prec;
  fp_info.wide = wide;

  if (fp_info.left && fp_info.pad == L' ')
    {
      /* We must do the padding ourself since the unit character must
	 be placed before the padding spaces.  */
      fp_info.width = 0;

      done = __printf_fp (fp, &fp_info, &ptr);
      if (done > 0)
	{
	  outchar (*tag);
	  if (info->width > done)
	    PADN (' ', info->width - done);
	}
    }
  else
    {
      /* We can let __printf_fp do all the printing and just add our
	 unit character afterwards.  */
      fp_info.width = info->width - 1;

      done = __printf_fp (fp, &fp_info, &ptr);
      if (done > 0)
	outchar (*tag);
    }

  return done;
}