Remove "[Add new features here]" for 2.27
[glibc.git] / stdio-common / printf_fphex.c
blob004d2aa7164b4175969f582b432616d71cb2eb5e
1 /* Print floating point number in hexadecimal notation according to ISO C99.
2 Copyright (C) 1997-2017 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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <ctype.h>
21 #include <ieee754.h>
22 #include <math.h>
23 #include <printf.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <wchar.h>
28 #include <_itoa.h>
29 #include <_itowa.h>
30 #include <locale/localeinfo.h>
31 #include <stdbool.h>
32 #include <rounding-mode.h>
34 #if __HAVE_DISTINCT_FLOAT128
35 # include "ieee754_float128.h"
36 # include <ldbl-128/printf_fphex_macros.h>
37 # define PRINT_FPHEX_FLOAT128 \
38 PRINT_FPHEX (_Float128, fpnum.flt128, ieee854_float128, \
39 IEEE854_FLOAT128_BIAS)
40 #endif
42 /* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
43 #include <assert.h>
45 /* This defines make it possible to use the same code for GNU C library and
46 the GNU I/O library. */
47 #include <libioP.h>
48 #define PUT(f, s, n) _IO_sputn (f, s, n)
49 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
50 /* We use this file GNU C library and GNU I/O library. So make
51 names equal. */
52 #undef putc
53 #define putc(c, f) (wide \
54 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
55 #define size_t _IO_size_t
56 #define FILE _IO_FILE
58 /* Macros for doing the actual output. */
60 #define outchar(ch) \
61 do \
62 { \
63 const int outc = (ch); \
64 if (putc (outc, fp) == EOF) \
65 return -1; \
66 ++done; \
67 } while (0)
69 #define PRINT(ptr, wptr, len) \
70 do \
71 { \
72 size_t outlen = (len); \
73 if (wide) \
74 while (outlen-- > 0) \
75 outchar (*wptr++); \
76 else \
77 while (outlen-- > 0) \
78 outchar (*ptr++); \
79 } while (0)
81 #define PADN(ch, len) \
82 do \
83 { \
84 if (PAD (fp, ch, len) != len) \
85 return -1; \
86 done += len; \
87 } \
88 while (0)
90 #ifndef MIN
91 # define MIN(a,b) ((a)<(b)?(a):(b))
92 #endif
95 int
96 __printf_fphex (FILE *fp,
97 const struct printf_info *info,
98 const void *const *args)
100 /* The floating-point value to output. */
101 union
103 union ieee754_double dbl;
104 long double ldbl;
105 #if __HAVE_DISTINCT_FLOAT128
106 _Float128 flt128;
107 #endif
109 fpnum;
111 /* Locale-dependent representation of decimal point. */
112 const char *decimal;
113 wchar_t decimalwc;
115 /* "NaN" or "Inf" for the special cases. */
116 const char *special = NULL;
117 const wchar_t *wspecial = NULL;
119 /* Buffer for the generated number string for the mantissa. The
120 maximal size for the mantissa is 128 bits. */
121 char numbuf[32];
122 char *numstr;
123 char *numend;
124 wchar_t wnumbuf[32];
125 wchar_t *wnumstr;
126 wchar_t *wnumend;
127 int negative;
129 /* The maximal exponent of two in decimal notation has 5 digits. */
130 char expbuf[5];
131 char *expstr;
132 wchar_t wexpbuf[5];
133 wchar_t *wexpstr;
134 int expnegative;
135 int exponent;
137 /* Non-zero is mantissa is zero. */
138 int zero_mantissa;
140 /* The leading digit before the decimal point. */
141 char leading;
143 /* Precision. */
144 int precision = info->prec;
146 /* Width. */
147 int width = info->width;
149 /* Number of characters written. */
150 int done = 0;
152 /* Nonzero if this is output on a wide character stream. */
153 int wide = info->wide;
156 /* Figure out the decimal point character. */
157 if (info->extra == 0)
159 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
160 decimalwc = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
162 else
164 decimal = _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
165 decimalwc = _NL_CURRENT_WORD (LC_MONETARY,
166 _NL_MONETARY_DECIMAL_POINT_WC);
168 /* The decimal point character must never be zero. */
169 assert (*decimal != '\0' && decimalwc != L'\0');
171 #define PRINTF_FPHEX_FETCH(FLOAT, VAR) \
173 (VAR) = *(const FLOAT *) args[0]; \
175 /* Check for special values: not a number or infinity. */ \
176 if (isnan (VAR)) \
178 if (isupper (info->spec)) \
180 special = "NAN"; \
181 wspecial = L"NAN"; \
183 else \
185 special = "nan"; \
186 wspecial = L"nan"; \
189 else \
191 if (isinf (VAR)) \
193 if (isupper (info->spec)) \
195 special = "INF"; \
196 wspecial = L"INF"; \
198 else \
200 special = "inf"; \
201 wspecial = L"inf"; \
205 negative = signbit (VAR); \
208 /* Fetch the argument value. */
209 #if __HAVE_DISTINCT_FLOAT128
210 if (info->is_binary128)
211 PRINTF_FPHEX_FETCH (_Float128, fpnum.flt128)
212 else
213 #endif
214 #ifndef __NO_LONG_DOUBLE_MATH
215 if (info->is_long_double && sizeof (long double) > sizeof (double))
216 PRINTF_FPHEX_FETCH (long double, fpnum.ldbl)
217 else
218 #endif
219 PRINTF_FPHEX_FETCH (double, fpnum.dbl.d)
221 #undef PRINTF_FPHEX_FETCH
223 if (special)
225 int width = info->width;
227 if (negative || info->showsign || info->space)
228 --width;
229 width -= 3;
231 if (!info->left && width > 0)
232 PADN (' ', width);
234 if (negative)
235 outchar ('-');
236 else if (info->showsign)
237 outchar ('+');
238 else if (info->space)
239 outchar (' ');
241 PRINT (special, wspecial, 3);
243 if (info->left && width > 0)
244 PADN (' ', width);
246 return done;
249 #if __HAVE_DISTINCT_FLOAT128
250 if (info->is_binary128)
251 PRINT_FPHEX_FLOAT128;
252 else
253 #endif
254 if (info->is_long_double == 0 || sizeof (double) == sizeof (long double))
256 /* We have 52 bits of mantissa plus one implicit digit. Since
257 52 bits are representable without rest using hexadecimal
258 digits we use only the implicit digits for the number before
259 the decimal point. */
260 unsigned long long int num;
262 num = (((unsigned long long int) fpnum.dbl.ieee.mantissa0) << 32
263 | fpnum.dbl.ieee.mantissa1);
265 zero_mantissa = num == 0;
267 if (sizeof (unsigned long int) > 6)
269 wnumstr = _itowa_word (num, wnumbuf + (sizeof wnumbuf) / sizeof (wchar_t), 16,
270 info->spec == 'A');
271 numstr = _itoa_word (num, numbuf + sizeof numbuf, 16,
272 info->spec == 'A');
274 else
276 wnumstr = _itowa (num, wnumbuf + sizeof wnumbuf / sizeof (wchar_t), 16,
277 info->spec == 'A');
278 numstr = _itoa (num, numbuf + sizeof numbuf, 16,
279 info->spec == 'A');
282 /* Fill with zeroes. */
283 while (wnumstr > wnumbuf + (sizeof wnumbuf - 52) / sizeof (wchar_t))
285 *--wnumstr = L'0';
286 *--numstr = '0';
289 leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1';
291 exponent = fpnum.dbl.ieee.exponent;
293 if (exponent == 0)
295 if (zero_mantissa)
296 expnegative = 0;
297 else
299 /* This is a denormalized number. */
300 expnegative = 1;
301 exponent = IEEE754_DOUBLE_BIAS - 1;
304 else if (exponent >= IEEE754_DOUBLE_BIAS)
306 expnegative = 0;
307 exponent -= IEEE754_DOUBLE_BIAS;
309 else
311 expnegative = 1;
312 exponent = -(exponent - IEEE754_DOUBLE_BIAS);
315 #ifdef PRINT_FPHEX_LONG_DOUBLE
316 else
317 PRINT_FPHEX_LONG_DOUBLE;
318 #endif
320 /* Look for trailing zeroes. */
321 if (! zero_mantissa)
323 wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]];
324 numend = &numbuf[sizeof numbuf / sizeof numbuf[0]];
325 while (wnumend[-1] == L'0')
327 --wnumend;
328 --numend;
331 bool do_round_away = false;
333 if (precision != -1 && precision < numend - numstr)
335 char last_digit = precision > 0 ? numstr[precision - 1] : leading;
336 char next_digit = numstr[precision];
337 int last_digit_value = (last_digit >= 'A' && last_digit <= 'F'
338 ? last_digit - 'A' + 10
339 : (last_digit >= 'a' && last_digit <= 'f'
340 ? last_digit - 'a' + 10
341 : last_digit - '0'));
342 int next_digit_value = (next_digit >= 'A' && next_digit <= 'F'
343 ? next_digit - 'A' + 10
344 : (next_digit >= 'a' && next_digit <= 'f'
345 ? next_digit - 'a' + 10
346 : next_digit - '0'));
347 bool more_bits = ((next_digit_value & 7) != 0
348 || precision + 1 < numend - numstr);
349 int rounding_mode = get_rounding_mode ();
350 do_round_away = round_away (negative, last_digit_value & 1,
351 next_digit_value >= 8, more_bits,
352 rounding_mode);
355 if (precision == -1)
356 precision = numend - numstr;
357 else if (do_round_away)
359 /* Round up. */
360 int cnt = precision;
361 while (--cnt >= 0)
363 char ch = numstr[cnt];
364 /* We assume that the digits and the letters are ordered
365 like in ASCII. This is true for the rest of GNU, too. */
366 if (ch == '9')
368 wnumstr[cnt] = (wchar_t) info->spec;
369 numstr[cnt] = info->spec; /* This is tricky,
370 think about it! */
371 break;
373 else if (tolower (ch) < 'f')
375 ++numstr[cnt];
376 ++wnumstr[cnt];
377 break;
379 else
381 numstr[cnt] = '0';
382 wnumstr[cnt] = L'0';
385 if (cnt < 0)
387 /* The mantissa so far was fff...f Now increment the
388 leading digit. Here it is again possible that we
389 get an overflow. */
390 if (leading == '9')
391 leading = info->spec;
392 else if (tolower (leading) < 'f')
393 ++leading;
394 else
396 leading = '1';
397 if (expnegative)
399 exponent -= 4;
400 if (exponent <= 0)
402 exponent = -exponent;
403 expnegative = 0;
406 else
407 exponent += 4;
412 else
414 if (precision == -1)
415 precision = 0;
416 numend = numstr;
417 wnumend = wnumstr;
420 /* Now we can compute the exponent string. */
421 expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
422 wexpstr = _itowa_word (exponent,
423 wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);
425 /* Now we have all information to compute the size. */
426 width -= ((negative || info->showsign || info->space)
427 /* Sign. */
428 + 2 + 1 + 0 + precision + 1 + 1
429 /* 0x h . hhh P ExpoSign. */
430 + ((expbuf + sizeof expbuf) - expstr));
431 /* Exponent. */
433 /* Count the decimal point.
434 A special case when the mantissa or the precision is zero and the `#'
435 is not given. In this case we must not print the decimal point. */
436 if (precision > 0 || info->alt)
437 width -= wide ? 1 : strlen (decimal);
439 if (!info->left && info->pad != '0' && width > 0)
440 PADN (' ', width);
442 if (negative)
443 outchar ('-');
444 else if (info->showsign)
445 outchar ('+');
446 else if (info->space)
447 outchar (' ');
449 outchar ('0');
450 if ('X' - 'A' == 'x' - 'a')
451 outchar (info->spec + ('x' - 'a'));
452 else
453 outchar (info->spec == 'A' ? 'X' : 'x');
455 if (!info->left && info->pad == '0' && width > 0)
456 PADN ('0', width);
458 outchar (leading);
460 if (precision > 0 || info->alt)
462 const wchar_t *wtmp = &decimalwc;
463 PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
466 if (precision > 0)
468 ssize_t tofill = precision - (numend - numstr);
469 PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
470 if (tofill > 0)
471 PADN ('0', tofill);
474 if ('P' - 'A' == 'p' - 'a')
475 outchar (info->spec + ('p' - 'a'));
476 else
477 outchar (info->spec == 'A' ? 'P' : 'p');
479 outchar (expnegative ? '-' : '+');
481 PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);
483 if (info->left && info->pad != '0' && width > 0)
484 PADN (info->pad, width);
486 return done;