1 /* Print floating point number in hexadecimal notation according to
3 Copyright (C) 1997, 1998, 1999 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. */
31 #include <locale/localeinfo.h>
33 /* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
36 /* This defines make it possible to use the same code for GNU C library and
37 the GNU I/O library. */
40 # define PUT(f, s, n) _IO_sputn (f, s, n)
41 # define PAD(f, c, n) _IO_padn (f, c, n)
42 /* We use this file GNU C library and GNU I/O library. So make
45 # define putc(c, f) _IO_putc_unlocked (c, f)
46 # define size_t _IO_size_t
47 # define FILE _IO_FILE
48 #else /* ! USE_IN_LIBIO */
49 # define PUT(f, s, n) fwrite (s, 1, n, f)
50 # define PAD(f, c, n) __printf_pad (f, c, n)
51 ssize_t __printf_pad
__P ((FILE *, char pad
, int n
)); /* In vfprintf.c. */
52 #endif /* USE_IN_LIBIO */
54 /* Macros for doing the actual output. */
59 register const int outc = (ch); \
60 if (putc (outc, fp) == EOF) \
65 #define PRINT(ptr, len) \
69 const char *cp = (ptr); \
70 while (outlen-- > 0) \
74 #define PADN(ch, len) \
77 if (PAD (fp, ch, len) != len) \
84 # define MIN(a,b) ((a)<(b)?(a):(b))
89 __printf_fphex (FILE *fp
,
90 const struct printf_info
*info
,
91 const void *const *args
)
93 /* The floating-point value to output. */
96 union ieee754_double dbl
;
97 union ieee854_long_double ldbl
;
101 /* Locale-dependent representation of decimal point. */
104 /* "NaN" or "Inf" for the special cases. */
105 const char *special
= NULL
;
107 /* Buffer for the generated number string for the mantissa. The
108 maximal size for the mantissa is 64 bits. */
114 /* The maximal exponent of two in decimal notation has 5 digits. */
120 /* Non-zero is mantissa is zero. */
123 /* The leading digit before the decimal point. */
127 int precision
= info
->prec
;
130 int width
= info
->width
;
132 /* Number of characters written. */
136 /* Figure out the decimal point character. */
137 if (info
->extra
== 0)
141 memset (&state
, '\0', sizeof (state
));
142 if (__mbrtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
143 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
)),
145 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
151 memset (&state
, '\0', sizeof (state
));
152 if (__mbrtowc (&decimal
, _NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
),
153 strlen (_NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
)),
155 decimal
= (wchar_t) *_NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
);
157 /* Give default value. */
158 if (decimal
== L
'\0')
162 /* Fetch the argument value. */
163 #ifndef __NO_LONG_DOUBLE_MATH
164 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
166 fpnum
.ldbl
.d
= *(const long double *) args
[0];
168 /* Check for special values: not a number or infinity. */
169 if (__isnanl (fpnum
.ldbl
.d
))
171 special
= isupper (info
->spec
) ? "NAN" : "nan";
176 if (__isinfl (fpnum
.ldbl
.d
))
177 special
= isupper (info
->spec
) ? "INF" : "inf";
179 negative
= signbit (fpnum
.ldbl
.d
);
183 #endif /* no long double */
185 fpnum
.dbl
.d
= *(const double *) args
[0];
187 /* Check for special values: not a number or infinity. */
188 if (__isnan (fpnum
.dbl
.d
))
190 special
= isupper (info
->spec
) ? "NAN" : "nan";
195 if (__isinf (fpnum
.dbl
.d
))
196 special
= isupper (info
->spec
) ? "INF" : "inf";
198 negative
= signbit (fpnum
.dbl
.d
);
204 int width
= info
->width
;
206 if (negative
|| info
->showsign
|| info
->space
)
210 if (!info
->left
&& width
> 0)
215 else if (info
->showsign
)
217 else if (info
->space
)
222 if (info
->left
&& width
> 0)
228 /* We are handling here only 64 and 80 bit IEEE foating point
230 if (info
->is_long_double
== 0 || sizeof (double) == sizeof (long double))
232 /* We have 52 bits of mantissa plus one implicit digit. Since
233 52 bits are representable without rest using hexadecimal
234 digits we use only the implicit digits for the number before
235 the decimal point. */
236 unsigned long long int num
;
238 num
= (((unsigned long long int) fpnum
.dbl
.ieee
.mantissa0
) << 32
239 | fpnum
.dbl
.ieee
.mantissa1
);
241 zero_mantissa
= num
== 0;
243 if (sizeof (unsigned long int) > 6)
244 numstr
= _itoa_word (num
, numbuf
+ sizeof numbuf
, 16,
247 numstr
= _itoa (num
, numbuf
+ sizeof numbuf
, 16,
250 /* Fill with zeroes. */
251 while (numstr
> numbuf
+ (sizeof numbuf
- 52 / 4))
254 leading
= fpnum
.dbl
.ieee
.exponent
== 0 ? '0' : '1';
256 exponent
= fpnum
.dbl
.ieee
.exponent
;
264 /* This is a denormalized number. */
266 exponent
= IEEE754_DOUBLE_BIAS
- 1;
269 else if (exponent
>= IEEE754_DOUBLE_BIAS
)
272 exponent
-= IEEE754_DOUBLE_BIAS
;
277 exponent
= -(exponent
- IEEE754_DOUBLE_BIAS
);
282 /* The "strange" 80 bit format on ix86 and m68k has an explicit
283 leading digit in the 64 bit mantissa. */
284 unsigned long long int num
;
286 assert (sizeof (long double) == 12);
288 num
= (((unsigned long long int) fpnum
.ldbl
.ieee
.mantissa0
) << 32
289 | fpnum
.ldbl
.ieee
.mantissa1
);
291 zero_mantissa
= num
== 0;
293 if (sizeof (unsigned long int) > 6)
294 numstr
= _itoa_word (num
, numbuf
+ sizeof numbuf
, 16,
297 numstr
= _itoa (num
, numbuf
+ sizeof numbuf
, 16, info
->spec
== 'A');
299 /* Fill with zeroes. */
300 while (numstr
> numbuf
+ (sizeof numbuf
- 64 / 4))
303 /* We use a full nibble for the leading digit. */
306 /* We have 3 bits from the mantissa in the leading nibble.
307 Therefore we are here using `IEEE854_LONG_DOUBLE_BIAS + 3'. */
308 exponent
= fpnum
.ldbl
.ieee
.exponent
;
316 /* This is a denormalized number. */
318 /* This is a hook for the m68k long double format, where the
319 exponent bias is the same for normalized and denormalized
321 #ifndef LONG_DOUBLE_DENORM_BIAS
322 # define LONG_DOUBLE_DENORM_BIAS (IEEE854_LONG_DOUBLE_BIAS - 1)
324 exponent
= LONG_DOUBLE_DENORM_BIAS
+ 3;
327 else if (exponent
>= IEEE854_LONG_DOUBLE_BIAS
+ 3)
330 exponent
-= IEEE854_LONG_DOUBLE_BIAS
+ 3;
335 exponent
= -(exponent
- (IEEE854_LONG_DOUBLE_BIAS
+ 3));
339 /* Look for trailing zeroes. */
342 numend
= numbuf
+ sizeof numbuf
;
343 while (numend
[-1] == '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 numstr
[cnt
] = info
->spec
; /* This is tricky,
375 else if (tolower (ch
) < 'f')
385 /* The mantissa so far was fff...f Now increment the
386 leading digit. Here it is again possible that we
389 leading
= info
->spec
;
390 else if (tolower (leading
) < 'f')
410 /* Now we can compute the exponent string. */
411 expstr
= _itoa_word (exponent
, expbuf
+ sizeof expbuf
, 10, 0);
413 /* Now we have all information to compute the size. */
414 width
-= ((negative
|| info
->showsign
|| info
->space
)
416 + 2 + 1 + 1 + precision
+ 1 + 1
417 /* 0x h . hhh P ExpoSign. */
418 + ((expbuf
+ sizeof expbuf
) - expstr
));
421 /* A special case when the mantissa or the precision is zero and the `#'
422 is not given. In this case we must not print the decimal point. */
423 if ((zero_mantissa
|| precision
== 0) && !info
->alt
)
424 ++width
; /* This nihilates the +1 for the decimal-point
425 character in the following equation. */
427 if (!info
->left
&& width
> 0)
432 else if (info
->showsign
)
434 else if (info
->space
)
438 outchar (info
->spec
== 'A' ? 'X' : 'x');
441 if ((!zero_mantissa
&& precision
> 0) || info
->alt
)
444 if (!zero_mantissa
&& precision
> 0)
446 PRINT (numstr
, MIN (numend
- numstr
, precision
));
447 if (precision
> numend
- numstr
)
448 PADN ('0', precision
- (numend
- numstr
));
451 if (info
->left
&& info
->pad
== '0' && width
> 0)
454 outchar (info
->spec
== 'A' ? 'P' : 'p');
456 outchar (expnegative
? '-' : '+');
458 PRINT (expstr
, (expbuf
+ sizeof expbuf
) - expstr
);
460 if (info
->left
&& info
->pad
!= '0' && width
> 0)
461 PADN (info
->pad
, width
);