Use correct signedness in default implementations of wcscmp and wmemcmp
[glibc.git] / stdio-common / printf_fphex.c
blob6cb71414f5d90f0286c16f0db981283cde893f41
1 /* Print floating point number in hexadecimal notation according to ISO C99.
2 Copyright (C) 1997-2002,2004,2006,2011 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
19 02111-1307 USA. */
21 #include <ctype.h>
22 #include <ieee754.h>
23 #include <math.h>
24 #include <printf.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <wchar.h>
29 #include "_itoa.h"
30 #include "_itowa.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 #include <libioP.h>
39 #define PUT(f, s, n) _IO_sputn (f, s, n)
40 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_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) (wide \
45 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
46 #define size_t _IO_size_t
47 #define FILE _IO_FILE
49 /* Macros for doing the actual output. */
51 #define outchar(ch) \
52 do \
53 { \
54 register const int outc = (ch); \
55 if (putc (outc, fp) == EOF) \
56 return -1; \
57 ++done; \
58 } while (0)
60 #define PRINT(ptr, wptr, len) \
61 do \
62 { \
63 register size_t outlen = (len); \
64 if (wide) \
65 while (outlen-- > 0) \
66 outchar (*wptr++); \
67 else \
68 while (outlen-- > 0) \
69 outchar (*ptr++); \
70 } while (0)
72 #define PADN(ch, len) \
73 do \
74 { \
75 if (PAD (fp, ch, len) != len) \
76 return -1; \
77 done += len; \
78 } \
79 while (0)
81 #ifndef MIN
82 # define MIN(a,b) ((a)<(b)?(a):(b))
83 #endif
86 int
87 __printf_fphex (FILE *fp,
88 const struct printf_info *info,
89 const void *const *args)
91 /* The floating-point value to output. */
92 union
94 union ieee754_double dbl;
95 union ieee854_long_double ldbl;
97 fpnum;
99 /* Locale-dependent representation of decimal point. */
100 const char *decimal;
101 wchar_t decimalwc;
103 /* "NaN" or "Inf" for the special cases. */
104 const char *special = NULL;
105 const wchar_t *wspecial = NULL;
107 /* Buffer for the generated number string for the mantissa. The
108 maximal size for the mantissa is 128 bits. */
109 char numbuf[32];
110 char *numstr;
111 char *numend;
112 wchar_t wnumbuf[32];
113 wchar_t *wnumstr;
114 wchar_t *wnumend;
115 int negative;
117 /* The maximal exponent of two in decimal notation has 5 digits. */
118 char expbuf[5];
119 char *expstr;
120 wchar_t wexpbuf[5];
121 wchar_t *wexpstr;
122 int expnegative;
123 int exponent;
125 /* Non-zero is mantissa is zero. */
126 int zero_mantissa;
128 /* The leading digit before the decimal point. */
129 char leading;
131 /* Precision. */
132 int precision = info->prec;
134 /* Width. */
135 int width = info->width;
137 /* Number of characters written. */
138 int done = 0;
140 /* Nonzero if this is output on a wide character stream. */
141 int wide = info->wide;
144 /* Figure out the decimal point character. */
145 if (info->extra == 0)
147 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
148 decimalwc = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
150 else
152 decimal = _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
153 decimalwc = _NL_CURRENT_WORD (LC_MONETARY,
154 _NL_MONETARY_DECIMAL_POINT_WC);
156 /* The decimal point character must never be zero. */
157 assert (*decimal != '\0' && decimalwc != L'\0');
160 /* Fetch the argument value. */
161 #ifndef __NO_LONG_DOUBLE_MATH
162 if (info->is_long_double && sizeof (long double) > sizeof (double))
164 fpnum.ldbl.d = *(const long double *) args[0];
166 /* Check for special values: not a number or infinity. */
167 if (__isnanl (fpnum.ldbl.d))
169 negative = fpnum.ldbl.ieee.negative != 0;
170 if (isupper (info->spec))
172 special = "NAN";
173 wspecial = L"NAN";
175 else
177 special = "nan";
178 wspecial = L"nan";
181 else
183 int res = __isinfl (fpnum.ldbl.d);
184 if (res)
186 if (isupper (info->spec))
188 special = "INF";
189 wspecial = L"INF";
191 else
193 special = "inf";
194 wspecial = L"inf";
196 negative = res < 0;
198 else
199 negative = signbit (fpnum.ldbl.d);
202 else
203 #endif /* no long double */
205 fpnum.dbl.d = *(const double *) args[0];
207 /* Check for special values: not a number or infinity. */
208 if (__isnan (fpnum.dbl.d))
210 negative = fpnum.dbl.ieee.negative != 0;
211 if (isupper (info->spec))
213 special = "NAN";
214 wspecial = L"NAN";
216 else
218 special = "nan";
219 wspecial = L"nan";
222 else
224 int res = __isinf (fpnum.dbl.d);
225 if (res)
227 if (isupper (info->spec))
229 special = "INF";
230 wspecial = L"INF";
232 else
234 special = "inf";
235 wspecial = L"inf";
237 negative = res < 0;
239 else
240 negative = signbit (fpnum.dbl.d);
244 if (special)
246 int width = info->width;
248 if (negative || info->showsign || info->space)
249 --width;
250 width -= 3;
252 if (!info->left && width > 0)
253 PADN (' ', width);
255 if (negative)
256 outchar ('-');
257 else if (info->showsign)
258 outchar ('+');
259 else if (info->space)
260 outchar (' ');
262 PRINT (special, wspecial, 3);
264 if (info->left && width > 0)
265 PADN (' ', width);
267 return done;
270 if (info->is_long_double == 0 || sizeof (double) == sizeof (long double))
272 /* We have 52 bits of mantissa plus one implicit digit. Since
273 52 bits are representable without rest using hexadecimal
274 digits we use only the implicit digits for the number before
275 the decimal point. */
276 unsigned long long int num;
278 num = (((unsigned long long int) fpnum.dbl.ieee.mantissa0) << 32
279 | fpnum.dbl.ieee.mantissa1);
281 zero_mantissa = num == 0;
283 if (sizeof (unsigned long int) > 6)
285 wnumstr = _itowa_word (num, wnumbuf + (sizeof wnumbuf) / sizeof (wchar_t), 16,
286 info->spec == 'A');
287 numstr = _itoa_word (num, numbuf + sizeof numbuf, 16,
288 info->spec == 'A');
290 else
292 wnumstr = _itowa (num, wnumbuf + sizeof wnumbuf / sizeof (wchar_t), 16,
293 info->spec == 'A');
294 numstr = _itoa (num, numbuf + sizeof numbuf, 16,
295 info->spec == 'A');
298 /* Fill with zeroes. */
299 while (wnumstr > wnumbuf + (sizeof wnumbuf - 52) / sizeof (wchar_t))
301 *--wnumstr = L'0';
302 *--numstr = '0';
305 leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1';
307 exponent = fpnum.dbl.ieee.exponent;
309 if (exponent == 0)
311 if (zero_mantissa)
312 expnegative = 0;
313 else
315 /* This is a denormalized number. */
316 expnegative = 1;
317 exponent = IEEE754_DOUBLE_BIAS - 1;
320 else if (exponent >= IEEE754_DOUBLE_BIAS)
322 expnegative = 0;
323 exponent -= IEEE754_DOUBLE_BIAS;
325 else
327 expnegative = 1;
328 exponent = -(exponent - IEEE754_DOUBLE_BIAS);
331 #ifdef PRINT_FPHEX_LONG_DOUBLE
332 else
333 PRINT_FPHEX_LONG_DOUBLE;
334 #endif
336 /* Look for trailing zeroes. */
337 if (! zero_mantissa)
339 wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]];
340 numend = &numbuf[sizeof numbuf / sizeof numbuf[0]];
341 while (wnumend[-1] == L'0')
343 --wnumend;
344 --numend;
347 if (precision == -1)
348 precision = numend - numstr;
349 else if (precision < numend - numstr
350 && (numstr[precision] > '8'
351 || (('A' < '0' || 'a' < '0')
352 && numstr[precision] < '0')
353 || (numstr[precision] == '8'
354 && (precision + 1 < numend - numstr
355 /* Round to even. */
356 || (precision > 0
357 && ((numstr[precision - 1] & 1)
358 ^ (isdigit (numstr[precision - 1]) == 0)))
359 || (precision == 0
360 && ((leading & 1)
361 ^ (isdigit (leading) == 0)))))))
363 /* Round up. */
364 int cnt = precision;
365 while (--cnt >= 0)
367 char ch = numstr[cnt];
368 /* We assume that the digits and the letters are ordered
369 like in ASCII. This is true for the rest of GNU, too. */
370 if (ch == '9')
372 wnumstr[cnt] = (wchar_t) info->spec;
373 numstr[cnt] = info->spec; /* This is tricky,
374 think about it! */
375 break;
377 else if (tolower (ch) < 'f')
379 ++numstr[cnt];
380 ++wnumstr[cnt];
381 break;
383 else
385 numstr[cnt] = '0';
386 wnumstr[cnt] = L'0';
389 if (cnt < 0)
391 /* The mantissa so far was fff...f Now increment the
392 leading digit. Here it is again possible that we
393 get an overflow. */
394 if (leading == '9')
395 leading = info->spec;
396 else if (tolower (leading) < 'f')
397 ++leading;
398 else
400 leading = '1';
401 if (expnegative)
403 exponent -= 4;
404 if (exponent <= 0)
406 exponent = -exponent;
407 expnegative = 0;
410 else
411 exponent += 4;
416 else
418 if (precision == -1)
419 precision = 0;
420 numend = numstr;
421 wnumend = wnumstr;
424 /* Now we can compute the exponent string. */
425 expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
426 wexpstr = _itowa_word (exponent,
427 wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);
429 /* Now we have all information to compute the size. */
430 width -= ((negative || info->showsign || info->space)
431 /* Sign. */
432 + 2 + 1 + 0 + precision + 1 + 1
433 /* 0x h . hhh P ExpoSign. */
434 + ((expbuf + sizeof expbuf) - expstr));
435 /* Exponent. */
437 /* Count the decimal point.
438 A special case when the mantissa or the precision is zero and the `#'
439 is not given. In this case we must not print the decimal point. */
440 if (precision > 0 || info->alt)
441 width -= wide ? 1 : strlen (decimal);
443 if (!info->left && info->pad != '0' && width > 0)
444 PADN (' ', width);
446 if (negative)
447 outchar ('-');
448 else if (info->showsign)
449 outchar ('+');
450 else if (info->space)
451 outchar (' ');
453 outchar ('0');
454 if ('X' - 'A' == 'x' - 'a')
455 outchar (info->spec + ('x' - 'a'));
456 else
457 outchar (info->spec == 'A' ? 'X' : 'x');
459 if (!info->left && info->pad == '0' && width > 0)
460 PADN ('0', width);
462 outchar (leading);
464 if (precision > 0 || info->alt)
466 const wchar_t *wtmp = &decimalwc;
467 PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
470 if (precision > 0)
472 ssize_t tofill = precision - (numend - numstr);
473 PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
474 if (tofill > 0)
475 PADN ('0', tofill);
478 if ('P' - 'A' == 'p' - 'a')
479 outchar (info->spec + ('p' - 'a'));
480 else
481 outchar (info->spec == 'A' ? 'P' : 'p');
483 outchar (expnegative ? '-' : '+');
485 PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);
487 if (info->left && info->pad != '0' && width > 0)
488 PADN (info->pad, width);
490 return done;