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/>. */
30 #include <locale/localeinfo.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)
42 /* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
45 /* This defines make it possible to use the same code for GNU C library and
46 the GNU I/O library. */
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
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
58 /* Macros for doing the actual output. */
63 const int outc = (ch); \
64 if (putc (outc, fp) == EOF) \
69 #define PRINT(ptr, wptr, len) \
72 size_t outlen = (len); \
74 while (outlen-- > 0) \
77 while (outlen-- > 0) \
81 #define PADN(ch, len) \
84 if (PAD (fp, ch, len) != len) \
91 # define MIN(a,b) ((a)<(b)?(a):(b))
96 __printf_fphex (FILE *fp
,
97 const struct printf_info
*info
,
98 const void *const *args
)
100 /* The floating-point value to output. */
103 union ieee754_double dbl
;
105 #if __HAVE_DISTINCT_FLOAT128
111 /* Locale-dependent representation of decimal point. */
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. */
129 /* The maximal exponent of two in decimal notation has 5 digits. */
137 /* Non-zero is mantissa is zero. */
140 /* The leading digit before the decimal point. */
144 int precision
= info
->prec
;
147 int width
= info
->width
;
149 /* Number of characters written. */
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
);
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. */ \
178 if (isupper (info->spec)) \
193 if (isupper (info->spec)) \
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
)
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
)
219 PRINTF_FPHEX_FETCH (double, fpnum
.dbl
.d
)
221 #undef PRINTF_FPHEX_FETCH
225 int width
= info
->width
;
227 if (negative
|| info
->showsign
|| info
->space
)
231 if (!info
->left
&& width
> 0)
236 else if (info
->showsign
)
238 else if (info
->space
)
241 PRINT (special
, wspecial
, 3);
243 if (info
->left
&& width
> 0)
249 #if __HAVE_DISTINCT_FLOAT128
250 if (info
->is_binary128
)
251 PRINT_FPHEX_FLOAT128
;
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,
271 numstr
= _itoa_word (num
, numbuf
+ sizeof numbuf
, 16,
276 wnumstr
= _itowa (num
, wnumbuf
+ sizeof wnumbuf
/ sizeof (wchar_t), 16,
278 numstr
= _itoa (num
, numbuf
+ sizeof numbuf
, 16,
282 /* Fill with zeroes. */
283 while (wnumstr
> wnumbuf
+ (sizeof wnumbuf
- 52) / sizeof (wchar_t))
289 leading
= fpnum
.dbl
.ieee
.exponent
== 0 ? '0' : '1';
291 exponent
= fpnum
.dbl
.ieee
.exponent
;
299 /* This is a denormalized number. */
301 exponent
= IEEE754_DOUBLE_BIAS
- 1;
304 else if (exponent
>= IEEE754_DOUBLE_BIAS
)
307 exponent
-= IEEE754_DOUBLE_BIAS
;
312 exponent
= -(exponent
- IEEE754_DOUBLE_BIAS
);
315 #ifdef PRINT_FPHEX_LONG_DOUBLE
317 PRINT_FPHEX_LONG_DOUBLE
;
320 /* Look for trailing zeroes. */
323 wnumend
= &wnumbuf
[sizeof wnumbuf
/ sizeof wnumbuf
[0]];
324 numend
= &numbuf
[sizeof numbuf
/ sizeof numbuf
[0]];
325 while (wnumend
[-1] == L
'0')
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
,
356 precision
= numend
- numstr
;
357 else if (do_round_away
)
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. */
368 wnumstr
[cnt
] = (wchar_t) info
->spec
;
369 numstr
[cnt
] = info
->spec
; /* This is tricky,
373 else if (tolower (ch
) < 'f')
387 /* The mantissa so far was fff...f Now increment the
388 leading digit. Here it is again possible that we
391 leading
= info
->spec
;
392 else if (tolower (leading
) < 'f')
402 exponent
= -exponent
;
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
)
428 + 2 + 1 + 0 + precision
+ 1 + 1
429 /* 0x h . hhh P ExpoSign. */
430 + ((expbuf
+ sizeof expbuf
) - expstr
));
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)
444 else if (info
->showsign
)
446 else if (info
->space
)
450 if ('X' - 'A' == 'x' - 'a')
451 outchar (info
->spec
+ ('x' - 'a'));
453 outchar (info
->spec
== 'A' ? 'X' : 'x');
455 if (!info
->left
&& info
->pad
== '0' && width
> 0)
460 if (precision
> 0 || info
->alt
)
462 const wchar_t *wtmp
= &decimalwc
;
463 PRINT (decimal
, wtmp
, wide
? 1 : strlen (decimal
));
468 ssize_t tofill
= precision
- (numend
- numstr
);
469 PRINT (numstr
, wnumstr
, MIN (numend
- numstr
, precision
));
474 if ('P' - 'A' == 'p' - 'a')
475 outchar (info
->spec
+ ('p' - 'a'));
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
);