1 /* Print floating point number in hexadecimal notation according to ISO C99.
2 Copyright (C) 1997-2002,2004,2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include "quadmath-printf.h"
33 /* Macros for doing the actual output. */
38 register const int outc = (ch); \
39 if (PUTC (outc, fp) == EOF) \
44 #define PRINT(ptr, wptr, len) \
47 register size_t outlen = (len); \
49 while (outlen-- > 0) \
52 while (outlen-- > 0) \
56 #define PADN(ch, len) \
59 if (PAD (fp, ch, len) != len) \
68 __quadmath_printf_fphex (struct __quadmath_printf_file *fp,
69 const struct printf_info *info,
70 const void *const *args)
72 /* The floating-point value to output. */
73 ieee854_float128 fpnum;
75 /* Locale-dependent representation of decimal point. */
79 /* "NaN" or "Inf" for the special cases. */
80 const char *special = NULL;
81 const wchar_t *wspecial = NULL;
83 /* Buffer for the generated number string for the mantissa. The
84 maximal size for the mantissa is 128 bits. */
93 /* The maximal exponent of two in decimal notation has 5 digits. */
101 /* Non-zero is mantissa is zero. */
104 /* The leading digit before the decimal point. */
108 int precision = info->prec;
111 int width = info->width;
113 /* Number of characters written. */
116 /* Nonzero if this is output on a wide character stream. */
117 int wide = info->wide;
119 /* Figure out the decimal point character. */
120 #ifdef USE_NL_LANGINFO
121 if (info->extra == 0)
122 decimal = nl_langinfo (DECIMAL_POINT);
125 decimal = nl_langinfo (MON_DECIMAL_POINT);
126 if (*decimal == '\0')
127 decimal = nl_langinfo (DECIMAL_POINT);
129 /* The decimal point character must never be zero. */
130 assert (*decimal != '\0');
131 #elif defined USE_LOCALECONV
132 const struct lconv *lc = localeconv ();
133 if (info->extra == 0)
134 decimal = lc->decimal_point;
137 decimal = lc->mon_decimal_point;
138 if (decimal == NULL || *decimal == '\0')
139 decimal = lc->decimal_point;
141 if (decimal == NULL || *decimal == '\0')
146 #ifdef USE_NL_LANGINFO_WC
147 if (info->extra == 0)
148 decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
151 decimalwc = nl_langinfo_wc (_NL_MONETARY_DECIMAL_POINT_WC);
152 if (decimalwc == L_('\0'))
153 decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
155 /* The decimal point character must never be zero. */
156 assert (decimalwc != L_('\0'));
161 /* Fetch the argument value. */
163 fpnum.value = **(const __float128 **) args[0];
165 /* Check for special values: not a number or infinity. */
166 if (isnanq (fpnum.value))
168 negative = fpnum.ieee.negative != 0;
169 if (isupper (info->spec))
172 wspecial = L_("NAN");
177 wspecial = L_("nan");
182 if (isinfq (fpnum.value))
184 if (isupper (info->spec))
187 wspecial = L_("INF");
192 wspecial = L_("inf");
196 negative = signbitq (fpnum.value);
202 int width = info->width;
204 if (negative || info->showsign || info->space)
208 if (!info->left && width > 0)
213 else if (info->showsign)
215 else if (info->space)
218 PRINT (special, wspecial, 3);
220 if (info->left && width > 0)
227 /* We have 112 bits of mantissa plus one implicit digit. Since
228 112 bits are representable without rest using hexadecimal
229 digits we use only the implicit digits for the number before
230 the decimal point. */
233 assert (sizeof (long double) == 16);
235 num0 = fpnum.ieee.mant_high;
236 num1 = fpnum.ieee.mant_low;
238 zero_mantissa = (num0|num1) == 0;
240 if (sizeof (unsigned long int) > 6)
242 numstr = _itoa_word (num1, numbuf + sizeof numbuf, 16,
244 wnumstr = _itowa_word (num1,
245 wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),
246 16, info->spec == 'A');
250 numstr = _itoa (num1, numbuf + sizeof numbuf, 16,
252 wnumstr = _itowa (num1,
253 wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),
254 16, info->spec == 'A');
257 while (numstr > numbuf + (sizeof numbuf - 64 / 4))
260 *--wnumstr = L_('0');
263 if (sizeof (unsigned long int) > 6)
265 numstr = _itoa_word (num0, numstr, 16, info->spec == 'A');
266 wnumstr = _itowa_word (num0, wnumstr, 16, info->spec == 'A');
270 numstr = _itoa (num0, numstr, 16, info->spec == 'A');
271 wnumstr = _itowa (num0, wnumstr, 16, info->spec == 'A');
274 /* Fill with zeroes. */
275 while (numstr > numbuf + (sizeof numbuf - 112 / 4))
278 *--wnumstr = L_('0');
281 leading = fpnum.ieee.exponent == 0 ? '0' : '1';
283 exponent = fpnum.ieee.exponent;
291 /* This is a denormalized number. */
293 exponent = IEEE854_FLOAT128_BIAS - 1;
296 else if (exponent >= IEEE854_FLOAT128_BIAS)
299 exponent -= IEEE854_FLOAT128_BIAS;
304 exponent = -(exponent - IEEE854_FLOAT128_BIAS);
308 /* Look for trailing zeroes. */
311 wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]];
312 numend = &numbuf[sizeof numbuf / sizeof numbuf[0]];
313 while (wnumend[-1] == L_('0'))
320 precision = numend - numstr;
321 else if (precision < numend - numstr
322 && (numstr[precision] > '8'
323 || (('A' < '0' || 'a' < '0')
324 && numstr[precision] < '0')
325 || (numstr[precision] == '8'
326 && (precision + 1 < numend - numstr
329 && ((numstr[precision - 1] & 1)
330 ^ (isdigit (numstr[precision - 1]) == 0)))
333 ^ (isdigit (leading) == 0)))))))
339 char ch = numstr[cnt];
340 /* We assume that the digits and the letters are ordered
341 like in ASCII. This is true for the rest of GNU, too. */
344 wnumstr[cnt] = (wchar_t) info->spec;
345 numstr[cnt] = info->spec; /* This is tricky,
349 else if (tolower (ch) < 'f')
358 wnumstr[cnt] = L_('0');
363 /* The mantissa so far was fff...f Now increment the
364 leading digit. Here it is again possible that we
367 leading = info->spec;
368 else if (tolower (leading) < 'f')
378 exponent = -exponent;
396 /* Now we can compute the exponent string. */
397 expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
398 wexpstr = _itowa_word (exponent,
399 wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);
401 /* Now we have all information to compute the size. */
402 width -= ((negative || info->showsign || info->space)
404 + 2 + 1 + 0 + precision + 1 + 1
405 /* 0x h . hhh P ExpoSign. */
406 + ((expbuf + sizeof expbuf) - expstr));
409 /* Count the decimal point.
410 A special case when the mantissa or the precision is zero and the `#'
411 is not given. In this case we must not print the decimal point. */
412 if (precision > 0 || info->alt)
413 width -= wide ? 1 : strlen (decimal);
415 if (!info->left && info->pad != '0' && width > 0)
420 else if (info->showsign)
422 else if (info->space)
426 if ('X' - 'A' == 'x' - 'a')
427 outchar (info->spec + ('x' - 'a'));
429 outchar (info->spec == 'A' ? 'X' : 'x');
431 if (!info->left && info->pad == '0' && width > 0)
436 if (precision > 0 || info->alt)
438 const wchar_t *wtmp = &decimalwc;
439 PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
444 ssize_t tofill = precision - (numend - numstr);
445 PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
450 if ('P' - 'A' == 'p' - 'a')
451 outchar (info->spec + ('p' - 'a'));
453 outchar (info->spec == 'A' ? 'P' : 'p');
455 outchar (expnegative ? '-' : '+');
457 PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);
459 if (info->left && info->pad != '0' && width > 0)
460 PADN (info->pad, width);