Update.
[glibc.git] / sysdeps / generic / printf_fphex.c
blob00dd8eed1d5e17a2ab746ac088db5160f6aabf9f
1 /* Print floating point number in hexadecimal notation according to
2 ISO C 9X.
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. */
22 #include <ctype.h>
23 #include <ieee754.h>
24 #include <math.h>
25 #include <printf.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <wchar.h>
30 #include "_itoa.h"
31 #include <locale/localeinfo.h>
33 /* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
34 #include <assert.h>
36 /* This defines make it possible to use the same code for GNU C library and
37 the GNU I/O library. */
38 #ifdef USE_IN_LIBIO
39 # include <libioP.h>
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
43 names equal. */
44 # undef putc
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. */
56 #define outchar(ch) \
57 do \
58 { \
59 register const int outc = (ch); \
60 if (putc (outc, fp) == EOF) \
61 return -1; \
62 ++done; \
63 } while (0)
65 #define PRINT(ptr, len) \
66 do \
67 { \
68 int outlen = (len); \
69 const char *cp = (ptr); \
70 while (outlen-- > 0) \
71 outchar (*cp++); \
72 } while (0)
74 #define PADN(ch, len) \
75 do \
76 { \
77 if (PAD (fp, ch, len) != len) \
78 return -1; \
79 done += len; \
80 } \
81 while (0)
83 #ifndef MIN
84 # define MIN(a,b) ((a)<(b)?(a):(b))
85 #endif
88 int
89 __printf_fphex (FILE *fp,
90 const struct printf_info *info,
91 const void *const *args)
93 /* The floating-point value to output. */
94 union
96 union ieee754_double dbl;
97 union ieee854_long_double ldbl;
99 fpnum;
101 /* Locale-dependent representation of decimal point. */
102 wchar_t decimal;
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. */
109 char numbuf[16];
110 char *numstr;
111 char *numend;
112 int negative;
114 /* The maximal exponent of two in decimal notation has 5 digits. */
115 char expbuf[5];
116 char *expstr;
117 int expnegative;
118 int exponent;
120 /* Non-zero is mantissa is zero. */
121 int zero_mantissa;
123 /* The leading digit before the decimal point. */
124 char leading;
126 /* Precision. */
127 int precision = info->prec;
129 /* Width. */
130 int width = info->width;
132 /* Number of characters written. */
133 int done = 0;
136 /* Figure out the decimal point character. */
137 if (info->extra == 0)
139 mbstate_t state;
141 memset (&state, '\0', sizeof (state));
142 if (__mbrtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT),
143 strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT)),
144 &state) <= 0)
145 decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
147 else
149 mbstate_t state;
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)),
154 &state) <= 0)
155 decimal = (wchar_t) *_NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
157 /* Give default value. */
158 if (decimal == L'\0')
159 decimal = L'.';
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";
172 negative = 0;
174 else
176 if (__isinfl (fpnum.ldbl.d))
177 special = isupper (info->spec) ? "INF" : "inf";
179 negative = signbit (fpnum.ldbl.d);
182 else
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";
191 negative = 0;
193 else
195 if (__isinf (fpnum.dbl.d))
196 special = isupper (info->spec) ? "INF" : "inf";
198 negative = signbit (fpnum.dbl.d);
202 if (special)
204 int width = info->width;
206 if (negative || info->showsign || info->space)
207 --width;
208 width -= 3;
210 if (!info->left && width > 0)
211 PADN (' ', width);
213 if (negative)
214 outchar ('-');
215 else if (info->showsign)
216 outchar ('+');
217 else if (info->space)
218 outchar (' ');
220 PRINT (special, 3);
222 if (info->left && width > 0)
223 PADN (' ', width);
225 return done;
228 /* We are handling here only 64 and 80 bit IEEE foating point
229 numbers. */
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,
245 info->spec == 'A');
246 else
247 numstr = _itoa (num, numbuf + sizeof numbuf, 16,
248 info->spec == 'A');
250 /* Fill with zeroes. */
251 while (numstr > numbuf + (sizeof numbuf - 52 / 4))
252 *--numstr = '0';
254 leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1';
256 exponent = fpnum.dbl.ieee.exponent;
258 if (exponent == 0)
260 if (zero_mantissa)
261 expnegative = 0;
262 else
264 /* This is a denormalized number. */
265 expnegative = 1;
266 exponent = IEEE754_DOUBLE_BIAS - 1;
269 else if (exponent >= IEEE754_DOUBLE_BIAS)
271 expnegative = 0;
272 exponent -= IEEE754_DOUBLE_BIAS;
274 else
276 expnegative = 1;
277 exponent = -(exponent - IEEE754_DOUBLE_BIAS);
280 else
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,
295 info->spec == 'A');
296 else
297 numstr = _itoa (num, numbuf + sizeof numbuf, 16, info->spec == 'A');
299 /* Fill with zeroes. */
300 while (numstr > numbuf + (sizeof numbuf - 64 / 4))
301 *--numstr = '0';
303 /* We use a full nibble for the leading digit. */
304 leading = *numstr++;
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;
310 if (exponent == 0)
312 if (zero_mantissa)
313 expnegative = 0;
314 else
316 /* This is a denormalized number. */
317 expnegative = 1;
318 /* This is a hook for the m68k long double format, where the
319 exponent bias is the same for normalized and denormalized
320 numbers. */
321 #ifndef LONG_DOUBLE_DENORM_BIAS
322 # define LONG_DOUBLE_DENORM_BIAS (IEEE854_LONG_DOUBLE_BIAS - 1)
323 #endif
324 exponent = LONG_DOUBLE_DENORM_BIAS + 3;
327 else if (exponent >= IEEE854_LONG_DOUBLE_BIAS + 3)
329 expnegative = 0;
330 exponent -= IEEE854_LONG_DOUBLE_BIAS + 3;
332 else
334 expnegative = 1;
335 exponent = -(exponent - (IEEE854_LONG_DOUBLE_BIAS + 3));
339 /* Look for trailing zeroes. */
340 if (! zero_mantissa)
342 numend = numbuf + sizeof numbuf;
343 while (numend[-1] == '0')
344 --numend;
346 if (precision == -1)
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
354 /* Round to even. */
355 || (precision > 0
356 && ((numstr[precision - 1] & 1)
357 ^ (isdigit (numstr[precision - 1]) == 0)))
358 || (precision == 0
359 && ((leading & 1)
360 ^ (isdigit (leading) == 0)))))))
362 /* Round up. */
363 int cnt = precision;
364 while (--cnt >= 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. */
369 if (ch == '9')
371 numstr[cnt] = info->spec; /* This is tricky,
372 think about it! */
373 break;
375 else if (tolower (ch) < 'f')
377 ++numstr[cnt];
378 break;
380 else
381 numstr[cnt] = '0';
383 if (cnt < 0)
385 /* The mantissa so far was fff...f Now increment the
386 leading digit. Here it is again possible that we
387 get an overflow. */
388 if (leading == '9')
389 leading = info->spec;
390 else if (tolower (leading) < 'f')
391 ++leading;
392 else
394 leading = 1;
395 if (expnegative)
397 exponent += 4;
398 if (exponent >= 0)
399 expnegative = 0;
401 else
402 exponent += 4;
407 else
408 numend = numstr;
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)
415 /* Sign. */
416 + 2 + 1 + 1 + precision + 1 + 1
417 /* 0x h . hhh P ExpoSign. */
418 + ((expbuf + sizeof expbuf) - expstr));
419 /* Exponent. */
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)
428 PADN (' ', width);
430 if (negative)
431 outchar ('-');
432 else if (info->showsign)
433 outchar ('+');
434 else if (info->space)
435 outchar (' ');
437 outchar ('0');
438 outchar (info->spec == 'A' ? 'X' : 'x');
439 outchar (leading);
441 if ((!zero_mantissa && precision > 0) || info->alt)
442 outchar (decimal);
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)
452 PADN ('0', width);
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);
463 return done;