1 /* Print floating point number in hexadecimal notation according to ISO C99.
2 Copyright (C) 1997-2002,2004,2006,2011 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 /* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
35 /* This defines make it possible to use the same code for GNU C library and
36 the GNU I/O library. */
38 #define PUT(f, s, n) _IO_sputn (f, s, n)
39 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_IO_padn) (f, c, n))
40 /* We use this file GNU C library and GNU I/O library. So make
43 #define putc(c, f) (wide \
44 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
45 #define size_t _IO_size_t
48 /* Macros for doing the actual output. */
53 register const int outc = (ch); \
54 if (putc (outc, fp) == EOF) \
59 #define PRINT(ptr, wptr, len) \
62 register size_t outlen = (len); \
64 while (outlen-- > 0) \
67 while (outlen-- > 0) \
71 #define PADN(ch, len) \
74 if (PAD (fp, ch, len) != len) \
81 # define MIN(a,b) ((a)<(b)?(a):(b))
86 __printf_fphex (FILE *fp
,
87 const struct printf_info
*info
,
88 const void *const *args
)
90 /* The floating-point value to output. */
93 union ieee754_double dbl
;
94 union ieee854_long_double ldbl
;
98 /* Locale-dependent representation of decimal point. */
102 /* "NaN" or "Inf" for the special cases. */
103 const char *special
= NULL
;
104 const wchar_t *wspecial
= NULL
;
106 /* Buffer for the generated number string for the mantissa. The
107 maximal size for the mantissa is 128 bits. */
116 /* The maximal exponent of two in decimal notation has 5 digits. */
124 /* Non-zero is mantissa is zero. */
127 /* The leading digit before the decimal point. */
131 int precision
= info
->prec
;
134 int width
= info
->width
;
136 /* Number of characters written. */
139 /* Nonzero if this is output on a wide character stream. */
140 int wide
= info
->wide
;
143 /* Figure out the decimal point character. */
144 if (info
->extra
== 0)
146 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
147 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
151 decimal
= _NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
);
152 decimalwc
= _NL_CURRENT_WORD (LC_MONETARY
,
153 _NL_MONETARY_DECIMAL_POINT_WC
);
155 /* The decimal point character must never be zero. */
156 assert (*decimal
!= '\0' && decimalwc
!= L
'\0');
159 /* Fetch the argument value. */
160 #ifndef __NO_LONG_DOUBLE_MATH
161 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
163 fpnum
.ldbl
.d
= *(const long double *) args
[0];
165 /* Check for special values: not a number or infinity. */
166 if (__isnanl (fpnum
.ldbl
.d
))
168 negative
= fpnum
.ldbl
.ieee
.negative
!= 0;
169 if (isupper (info
->spec
))
182 int res
= __isinfl (fpnum
.ldbl
.d
);
185 if (isupper (info
->spec
))
198 negative
= signbit (fpnum
.ldbl
.d
);
202 #endif /* no long double */
204 fpnum
.dbl
.d
= *(const double *) args
[0];
206 /* Check for special values: not a number or infinity. */
207 if (__isnan (fpnum
.dbl
.d
))
209 negative
= fpnum
.dbl
.ieee
.negative
!= 0;
210 if (isupper (info
->spec
))
223 int res
= __isinf (fpnum
.dbl
.d
);
226 if (isupper (info
->spec
))
239 negative
= signbit (fpnum
.dbl
.d
);
245 int width
= info
->width
;
247 if (negative
|| info
->showsign
|| info
->space
)
251 if (!info
->left
&& width
> 0)
256 else if (info
->showsign
)
258 else if (info
->space
)
261 PRINT (special
, wspecial
, 3);
263 if (info
->left
&& width
> 0)
269 if (info
->is_long_double
== 0 || sizeof (double) == sizeof (long double))
271 /* We have 52 bits of mantissa plus one implicit digit. Since
272 52 bits are representable without rest using hexadecimal
273 digits we use only the implicit digits for the number before
274 the decimal point. */
275 unsigned long long int num
;
277 num
= (((unsigned long long int) fpnum
.dbl
.ieee
.mantissa0
) << 32
278 | fpnum
.dbl
.ieee
.mantissa1
);
280 zero_mantissa
= num
== 0;
282 if (sizeof (unsigned long int) > 6)
284 wnumstr
= _itowa_word (num
, wnumbuf
+ (sizeof wnumbuf
) / sizeof (wchar_t), 16,
286 numstr
= _itoa_word (num
, numbuf
+ sizeof numbuf
, 16,
291 wnumstr
= _itowa (num
, wnumbuf
+ sizeof wnumbuf
/ sizeof (wchar_t), 16,
293 numstr
= _itoa (num
, numbuf
+ sizeof numbuf
, 16,
297 /* Fill with zeroes. */
298 while (wnumstr
> wnumbuf
+ (sizeof wnumbuf
- 52) / sizeof (wchar_t))
304 leading
= fpnum
.dbl
.ieee
.exponent
== 0 ? '0' : '1';
306 exponent
= fpnum
.dbl
.ieee
.exponent
;
314 /* This is a denormalized number. */
316 exponent
= IEEE754_DOUBLE_BIAS
- 1;
319 else if (exponent
>= IEEE754_DOUBLE_BIAS
)
322 exponent
-= IEEE754_DOUBLE_BIAS
;
327 exponent
= -(exponent
- IEEE754_DOUBLE_BIAS
);
330 #ifdef PRINT_FPHEX_LONG_DOUBLE
332 PRINT_FPHEX_LONG_DOUBLE
;
335 /* Look for trailing zeroes. */
338 wnumend
= &wnumbuf
[sizeof wnumbuf
/ sizeof wnumbuf
[0]];
339 numend
= &numbuf
[sizeof numbuf
/ sizeof numbuf
[0]];
340 while (wnumend
[-1] == L
'0')
347 precision
= numend
- numstr
;
348 else if (precision
< numend
- numstr
349 && (numstr
[precision
] > '8'
350 || (('A' < '0' || 'a' < '0')
351 && numstr
[precision
] < '0')
352 || (numstr
[precision
] == '8'
353 && (precision
+ 1 < numend
- numstr
356 && ((numstr
[precision
- 1] & 1)
357 ^ (isdigit (numstr
[precision
- 1]) == 0)))
360 ^ (isdigit (leading
) == 0)))))))
366 char ch
= numstr
[cnt
];
367 /* We assume that the digits and the letters are ordered
368 like in ASCII. This is true for the rest of GNU, too. */
371 wnumstr
[cnt
] = (wchar_t) info
->spec
;
372 numstr
[cnt
] = info
->spec
; /* This is tricky,
376 else if (tolower (ch
) < 'f')
390 /* The mantissa so far was fff...f Now increment the
391 leading digit. Here it is again possible that we
394 leading
= info
->spec
;
395 else if (tolower (leading
) < 'f')
405 exponent
= -exponent
;
423 /* Now we can compute the exponent string. */
424 expstr
= _itoa_word (exponent
, expbuf
+ sizeof expbuf
, 10, 0);
425 wexpstr
= _itowa_word (exponent
,
426 wexpbuf
+ sizeof wexpbuf
/ sizeof (wchar_t), 10, 0);
428 /* Now we have all information to compute the size. */
429 width
-= ((negative
|| info
->showsign
|| info
->space
)
431 + 2 + 1 + 0 + precision
+ 1 + 1
432 /* 0x h . hhh P ExpoSign. */
433 + ((expbuf
+ sizeof expbuf
) - expstr
));
436 /* Count the decimal point.
437 A special case when the mantissa or the precision is zero and the `#'
438 is not given. In this case we must not print the decimal point. */
439 if (precision
> 0 || info
->alt
)
440 width
-= wide
? 1 : strlen (decimal
);
442 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
447 else if (info
->showsign
)
449 else if (info
->space
)
453 if ('X' - 'A' == 'x' - 'a')
454 outchar (info
->spec
+ ('x' - 'a'));
456 outchar (info
->spec
== 'A' ? 'X' : 'x');
458 if (!info
->left
&& info
->pad
== '0' && width
> 0)
463 if (precision
> 0 || info
->alt
)
465 const wchar_t *wtmp
= &decimalwc
;
466 PRINT (decimal
, wtmp
, wide
? 1 : strlen (decimal
));
471 ssize_t tofill
= precision
- (numend
- numstr
);
472 PRINT (numstr
, wnumstr
, MIN (numend
- numstr
, precision
));
477 if ('P' - 'A' == 'p' - 'a')
478 outchar (info
->spec
+ ('p' - 'a'));
480 outchar (info
->spec
== 'A' ? 'P' : 'p');
482 outchar (expnegative
? '-' : '+');
484 PRINT (expstr
, wexpstr
, (expbuf
+ sizeof expbuf
) - expstr
);
486 if (info
->left
&& info
->pad
!= '0' && width
> 0)
487 PADN (info
->pad
, width
);