#include <ctype.h>
#include <ieee754.h>
#include <math.h>
#include <printf.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <_itoa.h>
#include <_itowa.h>
#include <locale/localeinfo.h>
#include <stdbool.h>
#include <rounding-mode.h>
#include <printf_fphex.h>
#include <assert.h>
#include <libioP.h>
#define PUT(f, s, n) _IO_sputn (f, s, n)
#define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
#undef putc
#define putc(c, f) (wide \
? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
#define size_t _IO_size_t
#define FILE _IO_FILE
#define outchar(ch) \
do \
{ \
const int outc = (ch); \
if (putc (outc, fp) == EOF) \
return -1; \
++done; \
} while (0)
#define PRINT(ptr, wptr, len) \
do \
{ \
size_t outlen = (len); \
if (wide) \
while (outlen-- > 0) \
outchar (*wptr++); \
else \
while (outlen-- > 0) \
outchar (*ptr++); \
} while (0)
#define PADN(ch, len) \
do \
{ \
if (PAD (fp, ch, len) != len) \
return -1; \
done += len; \
} \
while (0)
#ifndef MIN
# define MIN(a,b) ((a)<(b)?(a):(b))
#endif
int
__printf_fphex (FILE *fp,
const struct printf_info *info,
const void *const *args)
{
union
{
union ieee754_double dbl;
long double ldbl;
}
fpnum;
const char *decimal;
wchar_t decimalwc;
const char *special = NULL;
const wchar_t *wspecial = NULL;
char numbuf[32];
char *numstr;
char *numend;
wchar_t wnumbuf[32];
wchar_t *wnumstr;
wchar_t *wnumend;
int negative;
char expbuf[5];
char *expstr;
wchar_t wexpbuf[5];
wchar_t *wexpstr;
int expnegative;
int exponent;
int zero_mantissa;
char leading;
int precision = info->prec;
int width = info->width;
int done = 0;
int wide = info->wide;
bool do_round_away;
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);
}
assert (*decimal != '\0' && decimalwc != L'\0');
#ifndef __NO_LONG_DOUBLE_MATH
if (info->is_long_double && sizeof (long double) > sizeof (double))
{
fpnum.ldbl = *(const long double *) args[0];
if (isnan (fpnum.ldbl))
{
if (isupper (info->spec))
{
special = "NAN";
wspecial = L"NAN";
}
else
{
special = "nan";
wspecial = L"nan";
}
}
else
{
if (isinf (fpnum.ldbl))
{
if (isupper (info->spec))
{
special = "INF";
wspecial = L"INF";
}
else
{
special = "inf";
wspecial = L"inf";
}
}
}
negative = signbit (fpnum.ldbl);
}
else
#endif
{
fpnum.dbl.d = *(const double *) args[0];
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))
{
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');
}
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
{
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
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);
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)
{
int cnt = precision;
while (--cnt >= 0)
{
char ch = numstr[cnt];
if (ch == '9')
{
wnumstr[cnt] = (wchar_t) info->spec;
numstr[cnt] = info->spec;
break;
}
else if (tolower (ch) < 'f')
{
++numstr[cnt];
++wnumstr[cnt];
break;
}
else
{
numstr[cnt] = '0';
wnumstr[cnt] = L'0';
}
}
if (cnt < 0)
{
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;
}
expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
wexpstr = _itowa_word (exponent,
wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);
width -= ((negative || info->showsign || info->space)
+ 2 + 1 + 0 + precision + 1 + 1
+ ((expbuf + sizeof expbuf) - expstr));
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;
}