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
31 #include <locale/localeinfo.h>
33 /* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
36 int __printf_fphex (FILE *fp
, const struct printf_info
*info
,
37 const void *const *args
);
39 /* This defines make it possible to use the same code for GNU C library and
40 the GNU I/O library. */
43 # define PUT(f, s, n) _IO_sputn (f, s, n)
44 # define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
45 /* We use this file GNU C library and GNU I/O library. So make
48 # define putc(c, f) (wide \
49 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
50 # define size_t _IO_size_t
51 # define FILE _IO_FILE
52 #else /* ! USE_IN_LIBIO */
53 # define PUT(f, s, n) fwrite (s, 1, n, f)
54 # define PAD(f, c, n) __printf_pad (f, c, n)
55 ssize_t __printf_pad
__P ((FILE *, char pad
, int n
)); /* In vfprintf.c. */
56 #endif /* USE_IN_LIBIO */
58 /* Macros for doing the actual output. */
63 register const int outc = (ch); \
64 if (putc (outc, fp) == EOF) \
69 #define PRINT(ptr, wptr, len) \
72 register 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 #if defined(__x86_64__) || defined(__INTEL__)
98 /* sysdeps/x86_64/fpu/printf_fphex.c */
100 #ifndef LONG_DOUBLE_DENORM_BIAS
101 # define LONG_DOUBLE_DENORM_BIAS (IEEE854_LONG_DOUBLE_BIAS - 1)
104 #define PRINT_FPHEX_LONG_DOUBLE \
106 /* The "strange" 80 bit format on ix86 and m68k has an explicit \
107 leading digit in the 64 bit mantissa. */ \
108 unsigned long long int num; \
111 num = (((unsigned long long int) fpnum.ldbl.ieee.mantissa0) << 32 \
112 | fpnum.ldbl.ieee.mantissa1); \
114 zero_mantissa = num == 0; \
116 if (sizeof (unsigned long int) > 6) \
118 numstr = _itoa_word (num, numbuf + sizeof numbuf, 16, \
119 info->spec == 'A'); \
120 wnumstr = _itowa_word (num, \
121 wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),\
122 16, info->spec == 'A'); \
126 numstr = _itoa (num, numbuf + sizeof numbuf, 16, info->spec == 'A');\
127 wnumstr = _itowa (num, \
128 wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t), \
129 16, info->spec == 'A'); \
132 /* Fill with zeroes. */ \
133 while (numstr > numbuf + (sizeof numbuf - 64 / 4)) \
139 /* We use a full nibble for the leading digit. */ \
140 leading = *numstr++; \
142 /* We have 3 bits from the mantissa in the leading nibble. \
143 Therefore we are here using `IEEE854_LONG_DOUBLE_BIAS + 3'. */ \
144 exponent = fpnum.ldbl.ieee.exponent; \
152 /* This is a denormalized number. */ \
154 /* This is a hook for the m68k long double format, where the \
155 exponent bias is the same for normalized and denormalized \
157 exponent = LONG_DOUBLE_DENORM_BIAS + 3; \
160 else if (exponent >= IEEE854_LONG_DOUBLE_BIAS + 3) \
163 exponent -= IEEE854_LONG_DOUBLE_BIAS + 3; \
168 exponent = -(exponent - (IEEE854_LONG_DOUBLE_BIAS + 3)); \
172 #endif /* __x86_64__ || __INTEL__ */
176 __printf_fphex (FILE *fp
,
177 const struct printf_info
*info
,
178 const void *const *args
)
180 /* The floating-point value to output. */
183 union ieee754_double dbl
;
184 union ieee854_long_double ldbl
;
188 /* Locale-dependent representation of decimal point. */
192 /* "NaN" or "Inf" for the special cases. */
193 const char *special
= NULL
;
194 const wchar_t *wspecial
= NULL
;
196 /* Buffer for the generated number string for the mantissa. The
197 maximal size for the mantissa is 128 bits. */
202 wchar_t *wnumstr
=L
"";
206 /* The maximal exponent of two in decimal notation has 5 digits. */
214 /* Non-zero is mantissa is zero. */
215 int zero_mantissa
= 1;
217 /* The leading digit before the decimal point. */
221 int precision
= info
->prec
;
224 int width
= info
->width
;
226 /* Number of characters written. */
229 /* Nonzero if this is output on a wide character stream. */
230 int wide
= info
->wide
;
233 /* Figure out the decimal point character. */
234 if (info
->extra
== 0)
236 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
237 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
241 decimal
= _NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
);
242 decimalwc
= _NL_CURRENT_WORD (LC_MONETARY
,
243 _NL_MONETARY_DECIMAL_POINT_WC
);
245 /* The decimal point character must never be zero. */
246 assert (*decimal
!= '\0' && decimalwc
!= L
'\0');
249 /* Fetch the argument value. */
250 #ifndef __NO_LONG_DOUBLE_MATH
251 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
253 fpnum
.ldbl
.d
= *(const long double *) args
[0];
255 /* Check for special values: not a number or infinity. */
256 if (__isnanl (fpnum
.ldbl
.d
))
258 if (isupper (info
->spec
))
272 if (__isinfl (fpnum
.ldbl
.d
))
274 if (isupper (info
->spec
))
286 negative
= signbit (fpnum
.ldbl
.d
);
290 #endif /* no long double */
292 fpnum
.dbl
.d
= *(const double *) args
[0];
294 /* Check for special values: not a number or infinity. */
295 if (__isnan (fpnum
.dbl
.d
))
297 if (isupper (info
->spec
))
311 if (__isinf (fpnum
.dbl
.d
))
313 if (isupper (info
->spec
))
325 negative
= signbit (fpnum
.dbl
.d
);
331 int width
= info
->width
;
333 if (negative
|| info
->showsign
|| info
->space
)
337 if (!info
->left
&& width
> 0)
342 else if (info
->showsign
)
344 else if (info
->space
)
347 PRINT (special
, wspecial
, 3);
349 if (info
->left
&& width
> 0)
355 if (info
->is_long_double
== 0 || sizeof (double) == sizeof (long double))
357 /* We have 52 bits of mantissa plus one implicit digit. Since
358 52 bits are representable without rest using hexadecimal
359 digits we use only the implicit digits for the number before
360 the decimal point. */
361 unsigned long long int num
;
363 num
= (((unsigned long long int) fpnum
.dbl
.ieee
.mantissa0
) << 32
364 | fpnum
.dbl
.ieee
.mantissa1
);
366 zero_mantissa
= num
== 0;
368 if (sizeof (unsigned long int) > 6)
370 wnumstr
= _itowa_word (num
, wnumbuf
+ (sizeof wnumbuf
) / sizeof (wchar_t), 16,
372 numstr
= _itoa_word (num
, numbuf
+ sizeof numbuf
, 16,
377 wnumstr
= _itowa (num
, wnumbuf
+ sizeof wnumbuf
/ sizeof (wchar_t), 16,
379 numstr
= _itoa (num
, numbuf
+ sizeof numbuf
, 16,
383 /* Fill with zeroes. */
384 while (wnumstr
> wnumbuf
+ (sizeof wnumbuf
- 52) / sizeof (wchar_t))
390 leading
= fpnum
.dbl
.ieee
.exponent
== 0 ? '0' : '1';
392 exponent
= fpnum
.dbl
.ieee
.exponent
;
400 /* This is a denormalized number. */
402 exponent
= IEEE754_DOUBLE_BIAS
- 1;
405 else if (exponent
>= IEEE754_DOUBLE_BIAS
)
408 exponent
-= IEEE754_DOUBLE_BIAS
;
413 exponent
= -(exponent
- IEEE754_DOUBLE_BIAS
);
416 #ifdef PRINT_FPHEX_LONG_DOUBLE
418 PRINT_FPHEX_LONG_DOUBLE
;
421 /* Look for trailing zeroes. */
424 wnumend
= &wnumbuf
[sizeof wnumbuf
/ sizeof wnumbuf
[0]];
425 numend
= &numbuf
[sizeof numbuf
/ sizeof numbuf
[0]];
426 while (wnumend
[-1] == L
'0')
433 precision
= numend
- numstr
;
434 else if (precision
< numend
- numstr
435 && (numstr
[precision
] > '8'
436 || (('A' < '0' || 'a' < '0')
437 && numstr
[precision
] < '0')
438 || (numstr
[precision
] == '8'
439 && (precision
+ 1 < numend
- numstr
442 && ((numstr
[precision
- 1] & 1)
443 ^ (isdigit (numstr
[precision
- 1]) == 0)))
446 ^ (isdigit (leading
) == 0)))))))
452 char ch
= numstr
[cnt
];
453 /* We assume that the digits and the letters are ordered
454 like in ASCII. This is true for the rest of GNU, too. */
457 wnumstr
[cnt
] = (wchar_t) info
->spec
;
458 numstr
[cnt
] = info
->spec
; /* This is tricky,
462 else if (tolower (ch
) < 'f')
476 /* The mantissa so far was fff...f Now increment the
477 leading digit. Here it is again possible that we
480 leading
= info
->spec
;
481 else if (tolower (leading
) < 'f')
491 exponent
= -exponent
;
509 /* Now we can compute the exponent string. */
510 expstr
= _itoa_word (exponent
, expbuf
+ sizeof expbuf
, 10, 0);
511 wexpstr
= _itowa_word (exponent
,
512 wexpbuf
+ sizeof wexpbuf
/ sizeof (wchar_t), 10, 0);
514 /* Now we have all information to compute the size. */
515 width
-= ((negative
|| info
->showsign
|| info
->space
)
517 + 2 + 1 + 0 + precision
+ 1 + 1
518 /* 0x h . hhh P ExpoSign. */
519 + ((expbuf
+ sizeof expbuf
) - expstr
));
522 /* Count the decimal point.
523 A special case when the mantissa or the precision is zero and the `#'
524 is not given. In this case we must not print the decimal point. */
525 if (precision
> 0 || info
->alt
)
526 width
-= wide
? 1 : strlen (decimal
);
528 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
533 else if (info
->showsign
)
535 else if (info
->space
)
539 if ('X' - 'A' == 'x' - 'a')
540 outchar (info
->spec
+ ('x' - 'a'));
542 outchar (info
->spec
== 'A' ? 'X' : 'x');
544 if (!info
->left
&& info
->pad
== '0' && width
> 0)
549 if (precision
> 0 || info
->alt
)
551 const wchar_t *wtmp
= &decimalwc
;
552 PRINT (decimal
, wtmp
, wide
? 1 : strlen (decimal
));
557 ssize_t tofill
= precision
- (numend
- numstr
);
558 PRINT (numstr
, wnumstr
, MIN (numend
- numstr
, precision
));
563 if ('P' - 'A' == 'p' - 'a')
564 outchar (info
->spec
+ ('p' - 'a'));
566 outchar (info
->spec
== 'A' ? 'P' : 'p');
568 outchar (expnegative
? '-' : '+');
570 PRINT (expstr
, wexpstr
, (expbuf
+ sizeof expbuf
) - expstr
);
572 if (info
->left
&& info
->pad
!= '0' && width
> 0)
573 PADN (info
->pad
, width
);