Blame stdio-common/printf_size.c

Packit 6c4009
/* Print size value using units for orders of magnitude.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
Packit 6c4009
   Based on a proposal by Larry McVoy <lm@sgi.com>.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <ieee754.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <printf.h>
Packit 6c4009
#include <libioP.h>
Packit 6c4009
Packit 6c4009
#define PUT(f, s, n) _IO_sputn (f, s, n)
Packit 6c4009
#define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
Packit 6c4009
#undef putc
Packit 6c4009
#define putc(c, f) (wide \
Packit 6c4009
		    ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
Packit 6c4009
Packit 6c4009

Packit 6c4009
/* Macros for doing the actual output.  */
Packit 6c4009
Packit 6c4009
#define outchar(ch)							      \
Packit 6c4009
  do									      \
Packit 6c4009
    {									      \
Packit 6c4009
      const int outc = (ch);						      \
Packit 6c4009
      if (putc (outc, fp) == EOF)					      \
Packit 6c4009
	return -1;							      \
Packit 6c4009
      ++done;								      \
Packit 6c4009
    } while (0)
Packit 6c4009
Packit 6c4009
#define PRINT(ptr, wptr, len)						      \
Packit 6c4009
  do									      \
Packit 6c4009
    {									      \
Packit 6c4009
      size_t outlen = (len);						      \
Packit 6c4009
      if (len > 20)							      \
Packit 6c4009
	{								      \
Packit 6c4009
	  if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen)   \
Packit 6c4009
	    return -1;							      \
Packit 6c4009
	  ptr += outlen;						      \
Packit 6c4009
	  done += outlen;						      \
Packit 6c4009
	}								      \
Packit 6c4009
      else								      \
Packit 6c4009
	{								      \
Packit 6c4009
	  if (wide)							      \
Packit 6c4009
	    while (outlen-- > 0)					      \
Packit 6c4009
	      outchar (*wptr++);					      \
Packit 6c4009
	  else								      \
Packit 6c4009
	    while (outlen-- > 0)					      \
Packit 6c4009
	      outchar (*ptr++);						      \
Packit 6c4009
	}								      \
Packit 6c4009
    } while (0)
Packit 6c4009
Packit 6c4009
#define PADN(ch, len)							      \
Packit 6c4009
  do									      \
Packit 6c4009
    {									      \
Packit 6c4009
      if (PAD (fp, ch, len) != len)					      \
Packit 6c4009
	return -1;							      \
Packit 6c4009
      done += len;							      \
Packit 6c4009
    }									      \
Packit 6c4009
  while (0)
Packit 6c4009

Packit 6c4009
/* Prototype for helper functions.  */
Packit 6c4009
extern int __printf_fp (FILE *fp, const struct printf_info *info,
Packit 6c4009
			const void *const *args);
Packit 6c4009
Packit 6c4009

Packit 6c4009
int
Packit 6c4009
__printf_size (FILE *fp, const struct printf_info *info,
Packit 6c4009
	       const void *const *args)
Packit 6c4009
{
Packit 6c4009
  /* Units for the both formats.  */
Packit 6c4009
#define BINARY_UNITS	" kmgtpezy"
Packit 6c4009
#define DECIMAL_UNITS	" KMGTPEZY"
Packit 6c4009
  static const char units[2][sizeof (BINARY_UNITS)] =
Packit 6c4009
  {
Packit 6c4009
    BINARY_UNITS,	/* For binary format.  */
Packit 6c4009
    DECIMAL_UNITS	/* For decimal format.  */
Packit 6c4009
  };
Packit 6c4009
  const char *tag = units[isupper (info->spec) != 0];
Packit 6c4009
  int divisor = isupper (info->spec) ? 1000 : 1024;
Packit 6c4009
Packit 6c4009
  /* The floating-point value to output.  */
Packit 6c4009
  union
Packit 6c4009
    {
Packit 6c4009
      union ieee754_double dbl;
Packit 6c4009
      long double ldbl;
Packit 6c4009
#if __HAVE_DISTINCT_FLOAT128
Packit 6c4009
      _Float128 f128;
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
  fpnum;
Packit 6c4009
  const void *ptr = &fpnum;
Packit 6c4009
Packit 6c4009
  int is_neg = 0;
Packit 6c4009
Packit 6c4009
  /* "NaN" or "Inf" for the special cases.  */
Packit 6c4009
  const char *special = NULL;
Packit 6c4009
  const wchar_t *wspecial = NULL;
Packit 6c4009
Packit 6c4009
  struct printf_info fp_info;
Packit 6c4009
  int done = 0;
Packit 6c4009
  int wide = info->wide;
Packit 6c4009
Packit 6c4009
#define PRINTF_SIZE_FETCH(FLOAT, VAR)					\
Packit 6c4009
  {									\
Packit 6c4009
    (VAR) = *(const FLOAT *) args[0];					\
Packit 6c4009
									\
Packit 6c4009
    /* Check for special values: not a number or infinity.  */		\
Packit 6c4009
    if (isnan (VAR))							\
Packit 6c4009
      {									\
Packit 6c4009
	special = "nan";						\
Packit 6c4009
	wspecial = L"nan";						\
Packit 6c4009
	/* is_neg = 0; Already zero */					\
Packit 6c4009
      }									\
Packit 6c4009
    else if (isinf (VAR))						\
Packit 6c4009
      {									\
Packit 6c4009
	is_neg = signbit (VAR);						\
Packit 6c4009
	special = "inf";						\
Packit 6c4009
	wspecial = L"inf";						\
Packit 6c4009
      }									\
Packit 6c4009
    else								\
Packit 6c4009
      while ((VAR) >= divisor && tag[1] != '\0')			\
Packit 6c4009
	{								\
Packit 6c4009
	  (VAR) /= divisor;						\
Packit 6c4009
	  ++tag;							\
Packit 6c4009
	}								\
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Fetch the argument value.	*/
Packit 6c4009
#if __HAVE_DISTINCT_FLOAT128
Packit 6c4009
  if (info->is_binary128)
Packit 6c4009
    PRINTF_SIZE_FETCH (_Float128, fpnum.f128)
Packit 6c4009
  else
Packit 6c4009
#endif
Packit 6c4009
#ifndef __NO_LONG_DOUBLE_MATH
Packit 6c4009
  if (info->is_long_double && sizeof (long double) > sizeof (double))
Packit 6c4009
    PRINTF_SIZE_FETCH (long double, fpnum.ldbl)
Packit 6c4009
  else
Packit 6c4009
#endif
Packit 6c4009
    PRINTF_SIZE_FETCH (double, fpnum.dbl.d)
Packit 6c4009
Packit 6c4009
#undef PRINTF_SIZE_FETCH
Packit 6c4009
Packit 6c4009
  if (special)
Packit 6c4009
    {
Packit 6c4009
      int width = info->prec > info->width ? info->prec : info->width;
Packit 6c4009
Packit 6c4009
      if (is_neg || info->showsign || info->space)
Packit 6c4009
	--width;
Packit 6c4009
      width -= 3;
Packit 6c4009
Packit 6c4009
      if (!info->left && width > 0)
Packit 6c4009
	PADN (' ', width);
Packit 6c4009
Packit 6c4009
      if (is_neg)
Packit 6c4009
	outchar ('-');
Packit 6c4009
      else if (info->showsign)
Packit 6c4009
	outchar ('+');
Packit 6c4009
      else if (info->space)
Packit 6c4009
	outchar (' ');
Packit 6c4009
Packit 6c4009
      PRINT (special, wspecial, 3);
Packit 6c4009
Packit 6c4009
      if (info->left && width > 0)
Packit 6c4009
	PADN (' ', width);
Packit 6c4009
Packit 6c4009
      return done;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Prepare to print the number.  We want to use `__printf_fp' so we
Packit 6c4009
     have to prepare a `printf_info' structure.  */
Packit 6c4009
  fp_info = *info;
Packit 6c4009
  fp_info.spec = 'f';
Packit 6c4009
  fp_info.prec = info->prec < 0 ? 3 : info->prec;
Packit 6c4009
  fp_info.wide = wide;
Packit 6c4009
Packit 6c4009
  if (fp_info.left && fp_info.pad == L' ')
Packit 6c4009
    {
Packit 6c4009
      /* We must do the padding ourself since the unit character must
Packit 6c4009
	 be placed before the padding spaces.  */
Packit 6c4009
      fp_info.width = 0;
Packit 6c4009
Packit 6c4009
      done = __printf_fp (fp, &fp_info, &ptr);
Packit 6c4009
      if (done > 0)
Packit 6c4009
	{
Packit 6c4009
	  outchar (*tag);
Packit 6c4009
	  if (info->width > done)
Packit 6c4009
	    PADN (' ', info->width - done);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* We can let __printf_fp do all the printing and just add our
Packit 6c4009
	 unit character afterwards.  */
Packit 6c4009
      fp_info.width = info->width - 1;
Packit 6c4009
Packit 6c4009
      done = __printf_fp (fp, &fp_info, &ptr);
Packit 6c4009
      if (done > 0)
Packit 6c4009
	outchar (*tag);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return done;
Packit 6c4009
}
Packit 6c4009
ldbl_strong_alias (__printf_size, printf_size);
Packit 6c4009

Packit 6c4009
/* This is the function used by `vfprintf' to determine number and
Packit 6c4009
   type of the arguments.  */
Packit 6c4009
int
Packit 6c4009
printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
Packit 6c4009
{
Packit 6c4009
  /* We need only one double or long double argument.  */
Packit 6c4009
  if (n >= 1)
Packit 6c4009
    argtypes[0] = PA_DOUBLE | (info->is_long_double ? PA_FLAG_LONG_DOUBLE : 0);
Packit 6c4009
Packit 6c4009
  return 1;
Packit 6c4009
}