Update.
[glibc.git] / stdio-common / printf_fphex.c
blob4099bf2dd162b9b111145c757b099b9bb4ddf4f0
1 /* Print floating point number in hexadecimal notation according to
2 ISO C 9X.
3 Copyright (C) 1997, 1998 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 "_itoa.h"
30 #include <locale/localeinfo.h>
32 /* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
33 #include <assert.h>
35 /* This defines make it possible to use the same code for GNU C library and
36 the GNU I/O library. */
37 #ifdef USE_IN_LIBIO
38 # include <libioP.h>
39 # define PUT(f, s, n) _IO_sputn (f, s, n)
40 # define PAD(f, c, n) _IO_padn (f, c, n)
41 /* We use this file GNU C library and GNU I/O library. So make
42 names equal. */
43 # undef putc
44 # define putc(c, f) _IO_putc_unlocked (c, f)
45 # define size_t _IO_size_t
46 # define FILE _IO_FILE
47 #else /* ! USE_IN_LIBIO */
48 # define PUT(f, s, n) fwrite (s, 1, n, f)
49 # define PAD(f, c, n) __printf_pad (f, c, n)
50 ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c. */
51 #endif /* USE_IN_LIBIO */
53 /* Macros for doing the actual output. */
55 #define outchar(ch) \
56 do \
57 { \
58 register const int outc = (ch); \
59 if (putc (outc, fp) == EOF) \
60 return -1; \
61 ++done; \
62 } while (0)
64 #define PRINT(ptr, len) \
65 do \
66 { \
67 int outlen = (len); \
68 const char *cp = (ptr); \
69 while (outlen-- > 0) \
70 outchar (*cp++); \
71 } while (0)
73 #define PADN(ch, len) \
74 do \
75 { \
76 if (PAD (fp, ch, len) != len) \
77 return -1; \
78 done += len; \
79 } \
80 while (0)
82 #ifndef MIN
83 # define MIN(a,b) ((a)<(b)?(a):(b))
84 #endif
87 int
88 __printf_fphex (FILE *fp,
89 const struct printf_info *info,
90 const void *const *args)
92 /* The floating-point value to output. */
93 union
95 union ieee754_double dbl;
96 union ieee854_long_double ldbl;
98 fpnum;
100 /* Locale-dependent representation of decimal point. */
101 wchar_t decimal;
103 /* "NaN" or "Inf" for the special cases. */
104 const char *special = NULL;
106 /* Buffer for the generated number string for the mantissa. The
107 maximal size for the mantissa is 64 bits. */
108 char numbuf[16];
109 char *numstr;
110 char *numend;
111 int negative;
113 /* The maximal exponent of two in decimal notation has 5 digits. */
114 char expbuf[5];
115 char *expstr;
116 int expnegative;
117 int exponent;
119 /* Non-zero is mantissa is zero. */
120 int zero_mantissa;
122 /* The leading digit before the decimal point. */
123 char leading;
125 /* Precision. */
126 int precision = info->prec;
128 /* Width. */
129 int width = info->width;
131 /* Number of characters written. */
132 int done = 0;
135 /* Figure out the decimal point character. */
136 if (info->extra == 0)
138 if (mbtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT),
139 strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT))) <= 0)
140 decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
142 else
144 if (mbtowc (&decimal, _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT),
145 strlen (_NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT))) <= 0)
146 decimal = (wchar_t) *_NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
148 /* Give default value. */
149 if (decimal == L'\0')
150 decimal = L'.';
153 /* Fetch the argument value. */
154 if (info->is_long_double && sizeof (long double) > sizeof (double))
156 fpnum.ldbl.d = *(const long double *) args[0];
158 /* Check for special values: not a number or infinity. */
159 if (__isnanl (fpnum.ldbl.d))
161 special = isupper (info->spec) ? "NAN" : "nan";
162 negative = 0;
164 else
166 if (__isinfl (fpnum.ldbl.d))
167 special = isupper (info->spec) ? "INF" : "inf";
169 negative = signbit (fpnum.ldbl.d);
172 else
174 fpnum.dbl.d = *(const double *) args[0];
176 /* Check for special values: not a number or infinity. */
177 if (__isnan (fpnum.dbl.d))
179 special = isupper (info->spec) ? "NAN" : "nan";
180 negative = 0;
182 else
184 if (__isinf (fpnum.dbl.d))
185 special = isupper (info->spec) ? "INF" : "inf";
187 negative = signbit (fpnum.dbl.d);
191 if (special)
193 int width = info->width;
195 if (negative || info->showsign || info->space)
196 --width;
197 width -= 3;
199 if (!info->left && width > 0)
200 PADN (' ', width);
202 if (negative)
203 outchar ('-');
204 else if (info->showsign)
205 outchar ('+');
206 else if (info->space)
207 outchar (' ');
209 PRINT (special, 3);
211 if (info->left && width > 0)
212 PADN (' ', width);
214 return done;
217 /* We are handling here only 64 and 80 bit IEEE foating point
218 numbers. */
219 if (info->is_long_double == 0 || sizeof (double) == sizeof (long double))
221 /* We have 52 bits of mantissa plus one implicit digit. Since
222 52 bits are representable without rest using hexadecimal
223 digits we use only the implicit digits for the number before
224 the decimal point. */
225 unsigned long long int num;
227 num = (((unsigned long long int) fpnum.dbl.ieee.mantissa0) << 32
228 | fpnum.dbl.ieee.mantissa1);
230 zero_mantissa = num == 0;
232 if (sizeof (unsigned long int) > 6)
233 numstr = _itoa_word (num, numbuf + sizeof numbuf, 16,
234 info->spec == 'A');
235 else
236 numstr = _itoa (num, numbuf + sizeof numbuf, 16,
237 info->spec == 'A');
239 /* Fill with zeroes. */
240 while (numstr > numbuf + (sizeof numbuf - 52 / 4))
241 *--numstr = '0';
243 leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1';
245 exponent = fpnum.dbl.ieee.exponent;
247 if (exponent == 0)
249 if (zero_mantissa)
250 expnegative = 0;
251 else
253 /* This is a denormalized number. */
254 expnegative = 1;
255 exponent = -(1 - IEEE754_DOUBLE_BIAS);
258 else if (exponent >= IEEE754_DOUBLE_BIAS)
260 expnegative = 0;
261 exponent -= IEEE754_DOUBLE_BIAS;
263 else
265 expnegative = 1;
266 exponent = -(exponent - IEEE754_DOUBLE_BIAS);
269 else
271 /* The "strange" 80 bit format on ix86 and m68k has an explicit
272 leading digit in the 64 bit mantissa. */
273 unsigned long long int num;
275 assert (sizeof (long double) == 12);
277 num = (((unsigned long long int) fpnum.ldbl.ieee.mantissa0) << 32
278 | fpnum.ldbl.ieee.mantissa1);
280 zero_mantissa = num == 0;
282 if (sizeof (unsigned long int) > 6)
283 numstr = _itoa_word (num, numbuf + sizeof numbuf, 16,
284 info->spec == 'A');
285 else
286 numstr = _itoa (num, numbuf + sizeof numbuf, 16, info->spec == 'A');
288 /* Fill with zeroes. */
289 while (numstr > numbuf + (sizeof numbuf - 64 / 4))
290 *--numstr = '0';
292 /* We use a full nibble for the leading digit. */
293 leading = *numstr++;
295 /* We have 3 bits from the mantissa in the leading nibble.
296 Therefore we are here using `IEEE854_LONG_DOUBLE_BIAS + 3'. */
297 exponent = fpnum.ldbl.ieee.exponent;
299 if (exponent == 0)
301 if (zero_mantissa)
302 expnegative = 0;
303 else
305 /* This is a denormalized number. */
306 expnegative = 1;
307 exponent = -(1 - (IEEE854_LONG_DOUBLE_BIAS + 3));
310 else if (exponent >= IEEE854_LONG_DOUBLE_BIAS + 3)
312 expnegative = 0;
313 exponent -= IEEE854_LONG_DOUBLE_BIAS + 2;
315 else
317 expnegative = 1;
318 exponent = -(exponent - (IEEE854_LONG_DOUBLE_BIAS + 3));
322 /* Look for trailing zeroes. */
323 if (! zero_mantissa)
325 numend = numbuf + sizeof numbuf;
326 while (numend[-1] == '0')
327 --numend;
329 if (precision == -1)
330 precision = numend - numstr;
331 else if (precision < numend - numstr
332 && (numstr[precision] > '8'
333 || (('A' < '0' || 'a' < '0')
334 && numstr[precision] < '0')
335 || (numstr[precision] == '8'
336 && (precision + 1 < numend - numstr
337 /* Round to even. */
338 || (precision > 0
339 && ((numstr[precision - 1] & 1)
340 ^ (isdigit (numstr[precision - 1]) == 0)))
341 || (precision == 0
342 && ((leading & 1)
343 ^ (isdigit (leading) == 0)))))))
345 /* Round up. */
346 int cnt = precision;
347 while (--cnt >= 0)
349 char ch = numstr[cnt];
350 /* We assume that the digits and the letters are ordered
351 like in ASCII. This is true for the rest of GNU, too. */
352 if (ch == '9')
354 numstr[cnt] = info->spec; /* This is tricky,
355 think about it! */
356 break;
358 else if (tolower (ch) < 'f')
360 ++numstr[cnt];
361 break;
363 else
364 numstr[cnt] = '0';
366 if (cnt < 0)
368 /* The mantissa so far was fff...f Now increment the
369 leading digit. Here it is again possible that we
370 get an overflow. */
371 if (leading == '9')
372 leading = info->spec;
373 else if (tolower (leading) < 'f')
374 ++leading;
375 else
377 leading = 1;
378 if (expnegative)
380 exponent += 4;
381 if (exponent >= 0)
382 expnegative = 0;
384 else
385 exponent += 4;
390 else
391 numend = numstr;
393 /* Now we can compute the exponent string. */
394 expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
396 /* Now we have all information to compute the size. */
397 width -= ((negative || info->showsign || info->space)
398 /* Sign. */
399 + 2 + 1 + 1 + precision + 1 + 1
400 /* 0x h . hhh P ExpoSign. */
401 + ((expbuf + sizeof expbuf) - expstr));
402 /* Exponent. */
404 /* A special case if when the mantissa is zero and the `#' is not
405 given. In this case we must not print the decimal point. */
406 if (zero_mantissa && precision == 0 && !info->alt)
407 ++width; /* This nihilates the +1 for the decimal-point
408 character in the following equation. */
410 if (!info->left && width > 0)
411 PADN (' ', width);
413 if (negative)
414 outchar ('-');
415 else if (info->showsign)
416 outchar ('+');
417 else if (info->space)
418 outchar (' ');
420 outchar ('0');
421 outchar (info->spec == 'A' ? 'X' : 'x');
422 outchar (leading);
424 if (!zero_mantissa || precision > 0 || info->alt)
425 outchar (decimal);
427 if (!zero_mantissa || precision > 0)
429 PRINT (numstr, MIN (numend - numstr, precision));
430 if (precision > numend - numstr)
431 PADN ('0', precision - (numend - numstr));
434 if (info->left && info->pad == '0' && width > 0)
435 PADN ('0', width);
437 outchar (info->spec == 'A' ? 'P' : 'p');
439 outchar (expnegative ? '-' : '+');
441 PRINT (expstr, (expbuf + sizeof expbuf) - expstr);
443 if (info->left && info->pad != '0' && width > 0)
444 PADN (info->pad, width);
446 return done;