Blame stdio-common/printf_fphex.c

Packit 6c4009
/* Print floating point number in hexadecimal notation according to ISO C99.
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
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 <array_length.h>
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <ieee754.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <printf.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <_itoa.h>
Packit 6c4009
#include <_itowa.h>
Packit 6c4009
#include <locale/localeinfo.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <rounding-mode.h>
Packit 6c4009
Packit 6c4009
#if __HAVE_DISTINCT_FLOAT128
Packit 6c4009
# include "ieee754_float128.h"
Packit 6c4009
# include <ldbl-128/printf_fphex_macros.h>
Packit 6c4009
# define PRINT_FPHEX_FLOAT128 \
Packit 6c4009
   PRINT_FPHEX (_Float128, fpnum.flt128, ieee854_float128, \
Packit 6c4009
		IEEE854_FLOAT128_BIAS)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* #define NDEBUG 1*/		/* Undefine this for debugging assertions.  */
Packit 6c4009
#include <assert.h>
Packit 6c4009
Packit 6c4009
#include <libioP.h>
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 (wide)								      \
Packit 6c4009
	while (outlen-- > 0)						      \
Packit 6c4009
	  outchar (*wptr++);						      \
Packit 6c4009
      else								      \
Packit 6c4009
	while (outlen-- > 0)						      \
Packit 6c4009
	  outchar (*ptr++);						      \
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
#ifndef MIN
Packit 6c4009
# define MIN(a,b) ((a)<(b)?(a):(b))
Packit 6c4009
#endif
Packit 6c4009

Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__printf_fphex (FILE *fp,
Packit 6c4009
		const struct printf_info *info,
Packit 6c4009
		const void *const *args)
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 flt128;
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
  fpnum;
Packit 6c4009
Packit 6c4009
  /* Locale-dependent representation of decimal point.	*/
Packit 6c4009
  const char *decimal;
Packit 6c4009
  wchar_t decimalwc;
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
  /* Buffer for the generated number string for the mantissa.  The
Packit 6c4009
     maximal size for the mantissa is 128 bits.  */
Packit 6c4009
  char numbuf[32];
Packit 6c4009
  char *numstr;
Packit 6c4009
  char *numend;
Packit 6c4009
  wchar_t wnumbuf[32];
Packit 6c4009
  wchar_t *wnumstr;
Packit 6c4009
  wchar_t *wnumend;
Packit 6c4009
  int negative;
Packit 6c4009
Packit 6c4009
  /* The maximal exponent of two in decimal notation has 5 digits.  */
Packit 6c4009
  char expbuf[5];
Packit 6c4009
  char *expstr;
Packit 6c4009
  wchar_t wexpbuf[5];
Packit 6c4009
  wchar_t *wexpstr;
Packit 6c4009
  int expnegative;
Packit 6c4009
  int exponent;
Packit 6c4009
Packit 6c4009
  /* Non-zero is mantissa is zero.  */
Packit 6c4009
  int zero_mantissa;
Packit 6c4009
Packit 6c4009
  /* The leading digit before the decimal point.  */
Packit 6c4009
  char leading;
Packit 6c4009
Packit 6c4009
  /* Precision.  */
Packit 6c4009
  int precision = info->prec;
Packit 6c4009
Packit 6c4009
  /* Width.  */
Packit 6c4009
  int width = info->width;
Packit 6c4009
Packit 6c4009
  /* Number of characters written.  */
Packit 6c4009
  int done = 0;
Packit 6c4009
Packit 6c4009
  /* Nonzero if this is output on a wide character stream.  */
Packit 6c4009
  int wide = info->wide;
Packit 6c4009
Packit 6c4009
Packit 6c4009
  /* Figure out the decimal point character.  */
Packit 6c4009
  if (info->extra == 0)
Packit 6c4009
    {
Packit 6c4009
      decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
Packit 6c4009
      decimalwc = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      decimal = _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
Packit 6c4009
      decimalwc = _NL_CURRENT_WORD (LC_MONETARY,
Packit 6c4009
				    _NL_MONETARY_DECIMAL_POINT_WC);
Packit 6c4009
    }
Packit 6c4009
  /* The decimal point character must never be zero.  */
Packit 6c4009
  assert (*decimal != '\0' && decimalwc != L'\0');
Packit 6c4009
Packit 6c4009
#define PRINTF_FPHEX_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
	if (isupper (info->spec))					\
Packit 6c4009
	  {								\
Packit 6c4009
	    special = "NAN";						\
Packit 6c4009
	    wspecial = L"NAN";						\
Packit 6c4009
	  }								\
Packit 6c4009
	else								\
Packit 6c4009
	  {								\
Packit 6c4009
	    special = "nan";						\
Packit 6c4009
	    wspecial = L"nan";						\
Packit 6c4009
	  }								\
Packit 6c4009
      }									\
Packit 6c4009
    else								\
Packit 6c4009
      {									\
Packit 6c4009
	if (isinf (VAR))						\
Packit 6c4009
	  {								\
Packit 6c4009
	    if (isupper (info->spec))					\
Packit 6c4009
	      {								\
Packit 6c4009
		special = "INF";					\
Packit 6c4009
		wspecial = L"INF";					\
Packit 6c4009
	      }								\
Packit 6c4009
	    else							\
Packit 6c4009
	      {								\
Packit 6c4009
		special = "inf";					\
Packit 6c4009
		wspecial = L"inf";					\
Packit 6c4009
	      }								\
Packit 6c4009
	  }								\
Packit 6c4009
      }									\
Packit 6c4009
    negative = signbit (VAR);						\
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_FPHEX_FETCH (_Float128, fpnum.flt128)
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_FPHEX_FETCH (long double, fpnum.ldbl)
Packit 6c4009
  else
Packit 6c4009
#endif
Packit 6c4009
    PRINTF_FPHEX_FETCH (double, fpnum.dbl.d)
Packit 6c4009
Packit 6c4009
#undef PRINTF_FPHEX_FETCH
Packit 6c4009
Packit 6c4009
  if (special)
Packit 6c4009
    {
Packit 6c4009
      int width = info->width;
Packit 6c4009
Packit 6c4009
      if (negative || 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 (negative)
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
#if __HAVE_DISTINCT_FLOAT128
Packit 6c4009
  if (info->is_binary128)
Packit 6c4009
    PRINT_FPHEX_FLOAT128;
Packit 6c4009
  else
Packit 6c4009
#endif
Packit 6c4009
  if (info->is_long_double == 0 || sizeof (double) == sizeof (long double))
Packit 6c4009
    {
Packit 6c4009
      /* We have 52 bits of mantissa plus one implicit digit.  Since
Packit 6c4009
	 52 bits are representable without rest using hexadecimal
Packit 6c4009
	 digits we use only the implicit digits for the number before
Packit 6c4009
	 the decimal point.  */
Packit 6c4009
      unsigned long long int num;
Packit 6c4009
Packit 6c4009
      num = (((unsigned long long int) fpnum.dbl.ieee.mantissa0) << 32
Packit 6c4009
	     | fpnum.dbl.ieee.mantissa1);
Packit 6c4009
Packit 6c4009
      zero_mantissa = num == 0;
Packit 6c4009
Packit 6c4009
      if (sizeof (unsigned long int) > 6)
Packit 6c4009
	{
Packit 6c4009
	  wnumstr = _itowa_word (num, wnumbuf + (sizeof wnumbuf) / sizeof (wchar_t), 16,
Packit 6c4009
				 info->spec == 'A');
Packit 6c4009
	  numstr = _itoa_word (num, numbuf + sizeof numbuf, 16,
Packit 6c4009
			       info->spec == 'A');
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  wnumstr = _itowa (num, wnumbuf + sizeof wnumbuf / sizeof (wchar_t), 16,
Packit 6c4009
			    info->spec == 'A');
Packit 6c4009
	  numstr = _itoa (num, numbuf + sizeof numbuf, 16,
Packit 6c4009
			  info->spec == 'A');
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Fill with zeroes.  */
Packit 6c4009
      while (wnumstr > wnumbuf + (sizeof wnumbuf - 52) / sizeof (wchar_t))
Packit 6c4009
	{
Packit 6c4009
	  *--wnumstr = L'0';
Packit 6c4009
	  *--numstr = '0';
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1';
Packit 6c4009
Packit 6c4009
      exponent = fpnum.dbl.ieee.exponent;
Packit 6c4009
Packit 6c4009
      if (exponent == 0)
Packit 6c4009
	{
Packit 6c4009
	  if (zero_mantissa)
Packit 6c4009
	    expnegative = 0;
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      /* This is a denormalized number.  */
Packit 6c4009
	      expnegative = 1;
Packit 6c4009
	      exponent = IEEE754_DOUBLE_BIAS - 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else if (exponent >= IEEE754_DOUBLE_BIAS)
Packit 6c4009
	{
Packit 6c4009
	  expnegative = 0;
Packit 6c4009
	  exponent -= IEEE754_DOUBLE_BIAS;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  expnegative = 1;
Packit 6c4009
	  exponent = -(exponent - IEEE754_DOUBLE_BIAS);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
#ifdef PRINT_FPHEX_LONG_DOUBLE
Packit 6c4009
  else
Packit 6c4009
    PRINT_FPHEX_LONG_DOUBLE;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* Look for trailing zeroes.  */
Packit 6c4009
  if (! zero_mantissa)
Packit 6c4009
    {
Packit 6c4009
      wnumend = array_end (wnumbuf);
Packit 6c4009
      numend = array_end (numbuf);
Packit 6c4009
      while (wnumend[-1] == L'0')
Packit 6c4009
	{
Packit 6c4009
	  --wnumend;
Packit 6c4009
	  --numend;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      bool do_round_away = false;
Packit 6c4009
Packit 6c4009
      if (precision != -1 && precision < numend - numstr)
Packit 6c4009
	{
Packit 6c4009
	  char last_digit = precision > 0 ? numstr[precision - 1] : leading;
Packit 6c4009
	  char next_digit = numstr[precision];
Packit 6c4009
	  int last_digit_value = (last_digit >= 'A' && last_digit <= 'F'
Packit 6c4009
				  ? last_digit - 'A' + 10
Packit 6c4009
				  : (last_digit >= 'a' && last_digit <= 'f'
Packit 6c4009
				     ? last_digit - 'a' + 10
Packit 6c4009
				     : last_digit - '0'));
Packit 6c4009
	  int next_digit_value = (next_digit >= 'A' && next_digit <= 'F'
Packit 6c4009
				  ? next_digit - 'A' + 10
Packit 6c4009
				  : (next_digit >= 'a' && next_digit <= 'f'
Packit 6c4009
				     ? next_digit - 'a' + 10
Packit 6c4009
				     : next_digit - '0'));
Packit 6c4009
	  bool more_bits = ((next_digit_value & 7) != 0
Packit 6c4009
			    || precision + 1 < numend - numstr);
Packit 6c4009
	  int rounding_mode = get_rounding_mode ();
Packit 6c4009
	  do_round_away = round_away (negative, last_digit_value & 1,
Packit 6c4009
				      next_digit_value >= 8, more_bits,
Packit 6c4009
				      rounding_mode);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (precision == -1)
Packit 6c4009
	precision = numend - numstr;
Packit 6c4009
      else if (do_round_away)
Packit 6c4009
	{
Packit 6c4009
	  /* Round up.  */
Packit 6c4009
	  int cnt = precision;
Packit 6c4009
	  while (--cnt >= 0)
Packit 6c4009
	    {
Packit 6c4009
	      char ch = numstr[cnt];
Packit 6c4009
	      /* We assume that the digits and the letters are ordered
Packit 6c4009
		 like in ASCII.  This is true for the rest of GNU, too.  */
Packit 6c4009
	      if (ch == '9')
Packit 6c4009
		{
Packit 6c4009
		  wnumstr[cnt] = (wchar_t) info->spec;
Packit 6c4009
		  numstr[cnt] = info->spec;	/* This is tricky,
Packit 6c4009
						   think about it!  */
Packit 6c4009
		  break;
Packit 6c4009
		}
Packit 6c4009
	      else if (tolower (ch) < 'f')
Packit 6c4009
		{
Packit 6c4009
		  ++numstr[cnt];
Packit 6c4009
		  ++wnumstr[cnt];
Packit 6c4009
		  break;
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  numstr[cnt] = '0';
Packit 6c4009
		  wnumstr[cnt] = L'0';
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  if (cnt < 0)
Packit 6c4009
	    {
Packit 6c4009
	      /* The mantissa so far was fff...f  Now increment the
Packit 6c4009
		 leading digit.  Here it is again possible that we
Packit 6c4009
		 get an overflow.  */
Packit 6c4009
	      if (leading == '9')
Packit 6c4009
		leading = info->spec;
Packit 6c4009
	      else if (tolower (leading) < 'f')
Packit 6c4009
		++leading;
Packit 6c4009
	      else
Packit 6c4009
		{
Packit 6c4009
		  leading = '1';
Packit 6c4009
		  if (expnegative)
Packit 6c4009
		    {
Packit 6c4009
		      exponent -= 4;
Packit 6c4009
		      if (exponent <= 0)
Packit 6c4009
			{
Packit 6c4009
			  exponent = -exponent;
Packit 6c4009
			  expnegative = 0;
Packit 6c4009
			}
Packit 6c4009
		    }
Packit 6c4009
		  else
Packit 6c4009
		    exponent += 4;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (precision == -1)
Packit 6c4009
	precision = 0;
Packit 6c4009
      numend = numstr;
Packit 6c4009
      wnumend = wnumstr;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now we can compute the exponent string.  */
Packit 6c4009
  expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
Packit 6c4009
  wexpstr = _itowa_word (exponent,
Packit 6c4009
			 wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);
Packit 6c4009
Packit 6c4009
  /* Now we have all information to compute the size.  */
Packit 6c4009
  width -= ((negative || info->showsign || info->space)
Packit 6c4009
	    /* Sign.  */
Packit 6c4009
	    + 2    + 1 + 0 + precision + 1 + 1
Packit 6c4009
	    /* 0x    h   .   hhh         P   ExpoSign.  */
Packit 6c4009
	    + ((expbuf + sizeof expbuf) - expstr));
Packit 6c4009
	    /* Exponent.  */
Packit 6c4009
Packit 6c4009
  /* Count the decimal point.
Packit 6c4009
     A special case when the mantissa or the precision is zero and the `#'
Packit 6c4009
     is not given.  In this case we must not print the decimal point.  */
Packit 6c4009
  if (precision > 0 || info->alt)
Packit 6c4009
    width -= wide ? 1 : strlen (decimal);
Packit 6c4009
Packit 6c4009
  if (!info->left && info->pad != '0' && width > 0)
Packit 6c4009
    PADN (' ', width);
Packit 6c4009
Packit 6c4009
  if (negative)
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
  outchar ('0');
Packit 6c4009
  if ('X' - 'A' == 'x' - 'a')
Packit 6c4009
    outchar (info->spec + ('x' - 'a'));
Packit 6c4009
  else
Packit 6c4009
    outchar (info->spec == 'A' ? 'X' : 'x');
Packit 6c4009
Packit 6c4009
  if (!info->left && info->pad == '0' && width > 0)
Packit 6c4009
    PADN ('0', width);
Packit 6c4009
Packit 6c4009
  outchar (leading);
Packit 6c4009
Packit 6c4009
  if (precision > 0 || info->alt)
Packit 6c4009
    {
Packit 6c4009
      const wchar_t *wtmp = &decimalwc;
Packit 6c4009
      PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (precision > 0)
Packit 6c4009
    {
Packit 6c4009
      ssize_t tofill = precision - (numend - numstr);
Packit 6c4009
      PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
Packit 6c4009
      if (tofill > 0)
Packit 6c4009
	PADN ('0', tofill);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if ('P' - 'A' == 'p' - 'a')
Packit 6c4009
    outchar (info->spec + ('p' - 'a'));
Packit 6c4009
  else
Packit 6c4009
    outchar (info->spec == 'A' ? 'P' : 'p');
Packit 6c4009
Packit 6c4009
  outchar (expnegative ? '-' : '+');
Packit 6c4009
Packit 6c4009
  PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);
Packit 6c4009
Packit 6c4009
  if (info->left && info->pad != '0' && width > 0)
Packit 6c4009
    PADN (info->pad, width);
Packit 6c4009
Packit 6c4009
  return done;
Packit 6c4009
}