1 /* Print floating point number in hexadecimal notation according to
3 Copyright (C) 1997 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
29 #include "../locale/localeinfo.h"
31 /* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
34 /* This defines make it possible to use the same code for GNU C library and
35 the GNU I/O library. */
38 # define PUT(f, s, n) _IO_sputn (f, s, n)
39 # define PAD(f, c, n) _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) _IO_putc_unlocked (c, f)
44 # define size_t _IO_size_t
45 # define FILE _IO_FILE
46 #else /* ! USE_IN_LIBIO */
47 # define PUT(f, s, n) fwrite (s, 1, n, f)
48 # define PAD(f, c, n) __printf_pad (f, c, n)
49 ssize_t __printf_pad
__P ((FILE *, char pad
, int n
)); /* In vfprintf.c. */
50 #endif /* USE_IN_LIBIO */
52 /* Macros for doing the actual output. */
57 register const int outc = (ch); \
58 if (putc (outc, fp) == EOF) \
63 #define PRINT(ptr, len) \
67 const char *cp = (ptr); \
68 while (outlen-- > 0) \
72 #define PADN(ch, len) \
75 if (PAD (fp, ch, len) != len) \
82 # define MIN(a,b) ((a)<(b)?(a):(b))
87 __printf_fphex (FILE *fp
,
88 const struct printf_info
*info
,
89 const void *const *args
)
91 /* The floating-point value to output. */
94 union ieee754_double dbl
;
95 union ieee854_long_double ldbl
;
99 /* Locale-dependent representation of decimal point. */
102 /* "NaN" or "Inf" for the special cases. */
103 const char *special
= NULL
;
105 /* Buffer for the generated number string for the mantissa. The
106 maximal size for the mantissa is 64 bits. */
112 /* The maximal exponent of two in decimal notation has 5 digits. */
118 /* Non-zero is mantissa is zero. */
121 /* The leading digit before the decimal point. */
125 int precision
= info
->prec
;
128 int width
= info
->width
;
130 /* Number of characters written. */
134 /* Figure out the decimal point character. */
135 if (info
->extra
== 0)
137 if (mbtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
138 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
))) <= 0)
139 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
143 if (mbtowc (&decimal
, _NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
),
144 strlen (_NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
))) <= 0)
145 decimal
= (wchar_t) *_NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
);
147 /* Give default value. */
148 if (decimal
== L
'\0')
152 /* Fetch the argument value. */
153 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
155 fpnum
.ldbl
.d
= *(const long double *) args
[0];
157 /* Check for special values: not a number or infinity. */
158 if (__isnanl (fpnum
.ldbl
.d
))
160 special
= isupper (info
->spec
) ? "NAN" : "nan";
165 if (__isinfl (fpnum
.ldbl
.d
))
166 special
= isupper (info
->spec
) ? "INF" : "inf";
168 negative
= fpnum
.ldbl
.d
< 0;
173 fpnum
.dbl
.d
= *(const double *) args
[0];
175 /* Check for special values: not a number or infinity. */
176 if (__isnan (fpnum
.dbl
.d
))
178 special
= isupper (info
->spec
) ? "NAN" : "nan";
183 if (__isinf (fpnum
.dbl
.d
))
184 special
= isupper (info
->spec
) ? "INF" : "inf";
186 negative
= fpnum
.dbl
.d
< 0;
192 int width
= info
->prec
> info
->width
? info
->prec
: info
->width
;
194 if (negative
|| info
->showsign
|| info
->space
)
198 if (!info
->left
&& width
> 0)
203 else if (info
->showsign
)
205 else if (info
->space
)
210 if (info
->left
&& width
> 0)
216 /* We are handling here only 64 and 80 bit IEEE foating point
218 if (info
->is_long_double
== 0 || sizeof (double) == sizeof (long double))
220 /* We have 52 bits of mantissa plus one implicit digit. Since
221 52 bits are representable without rest using hexadecimal
222 digits we use only the implicit digits for the number before
223 the decimal point. */
224 unsigned long long int num
;
226 num
= (((unsigned long long int) fpnum
.dbl
.ieee
.mantissa0
) << 32
227 | fpnum
.dbl
.ieee
.mantissa1
);
229 zero_mantissa
= num
== 0;
231 if (sizeof (unsigned long int) > 6)
232 numstr
= _itoa_word (num
, numbuf
+ sizeof numbuf
, 16,
235 numstr
= _itoa (num
, numbuf
+ sizeof numbuf
, 16,
238 /* Fill with zeroes. */
239 while (numstr
> numbuf
+ (sizeof numbuf
- 13)) /* 52 ÷ 4 = 13 */
242 leading
= fpnum
.dbl
.ieee
.exponent
== 0 ? '0' : '1';
244 exponent
= fpnum
.dbl
.ieee
.exponent
;
246 if ((exponent
!= 0 && exponent
< IEEE754_DOUBLE_BIAS
)
247 || (exponent
== 0 && !zero_mantissa
))
250 exponent
= abs (exponent
- IEEE754_DOUBLE_BIAS
);
256 exponent
-= IEEE754_DOUBLE_BIAS
;
261 /* The "strange" 80 bit format on ix86 and m68k has an explicit
262 leading digit in the 64 bit mantissa. */
263 unsigned long long int num
;
265 assert (sizeof (long double) == 12);
267 num
= (((unsigned long long int) fpnum
.ldbl
.ieee
.mantissa0
) << 32
268 | fpnum
.ldbl
.ieee
.mantissa1
);
270 zero_mantissa
= num
== 0;
272 if (sizeof (unsigned long int) > 6)
273 numstr
= _itoa_word (num
, numbuf
+ sizeof numbuf
, 16,
276 numstr
= _itoa (num
, numbuf
+ sizeof numbuf
, 16, info
->spec
== 'A');
278 /* We use a full nibble for the leading digit. */
281 /* Fill with zeroes. */
282 while (numstr
> numbuf
+ (sizeof numbuf
- 15)) /* 60 ÷ 4 = 15 */
285 /* We have 3 bits from the mantissa in the leading nibble. */
286 exponent
= fpnum
.ldbl
.ieee
.exponent
- 3;
288 if ((exponent
!= 0 && exponent
< IEEE854_LONG_DOUBLE_BIAS
)
289 || (exponent
== 0 && !zero_mantissa
))
292 exponent
= abs (exponent
- IEEE854_LONG_DOUBLE_BIAS
);
298 exponent
-= IEEE854_LONG_DOUBLE_BIAS
;
302 /* Look for trailing zeroes. */
305 numend
= numbuf
+ sizeof numbuf
;
306 while (numend
[-1] == '0')
310 precision
= numend
- numstr
;
311 else if (precision
< numend
- numstr
312 && (numstr
[precision
] > 5
313 || (numstr
[precision
] == 5
314 && (precision
+ 1 < numend
- numstr
317 && ((numstr
[precision
- 1] & 1)
318 ^ (isdigit (numstr
[precision
- 1]) == 0)))
321 ^ (isdigit (leading
) == 0)))))))
327 char ch
= numstr
[cnt
];
328 /* We assume that the digits and the letters are ordered
329 like in ASCII. This is true for the rest of GNU, too. */
332 numstr
[cnt
] = info
->spec
; /* This is tricky,
336 else if (tolower (ch
) < 'f')
346 /* The mantissa so far was fff...f Now increment the
347 leading digit. Here it is again possible that we
350 leading
= info
->spec
;
351 else if (tolower (leading
) < 'f')
371 /* Now we can compute the exponent string. */
372 expstr
= _itoa_word (exponent
, expbuf
+ sizeof expbuf
, 10, 0);
374 /* Now we have all information to compute the size. */
375 width
-= ((negative
|| info
->showsign
|| info
->space
)
377 + 2 + 1 + 1 + precision
+ 1 + 1
378 /* 0x h . hhh P ExpoSign. */
379 + ((expbuf
+ sizeof expbuf
) - expstr
));
382 /* A special case if when the mantissa is zero and the `#' is not
383 given. In this case we must not print the decimal point. */
384 if (zero_mantissa
&& precision
== 0 && !info
->alt
)
385 ++width
; /* This nihilates the +1 for the decimal-point
386 character in the following equation. */
388 if (!info
->left
&& width
> 0)
393 else if (info
->showsign
)
395 else if (info
->space
)
399 outchar (info
->spec
== 'A' ? 'X' : 'x');
402 if (!zero_mantissa
|| precision
> 0 || info
->alt
)
405 if (!zero_mantissa
|| precision
> 0)
407 PRINT (numstr
, MIN (numend
- numstr
, precision
));
408 if (precision
> numend
- numstr
)
409 PADN ('0', precision
- (numend
- numstr
));
412 if (info
->left
&& info
->pad
== '0' && width
> 0)
415 outchar (info
->spec
== 'A' ? 'P' : 'p');
417 outchar (expnegative
? '-' : '+');
419 PRINT (expstr
, (expbuf
+ sizeof expbuf
) - expstr
);
421 if (info
->left
&& info
->pad
!= '0' && width
> 0)
422 PADN (info
->pad
, width
);