1/* Print floating point number in hexadecimal notation according to ISO C99.
2 Copyright (C) 1997-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
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.
10
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.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <ctype.h>
21#include <ieee754.h>
22#include <math.h>
23#include <printf.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <string.h>
27#include <wchar.h>
28#include <_itoa.h>
29#include <_itowa.h>
30#include <locale/localeinfo.h>
31#include <stdbool.h>
32#include <rounding-mode.h>
33
34#if __HAVE_DISTINCT_FLOAT128
35# include "ieee754_float128.h"
36# include <ldbl-128/printf_fphex_macros.h>
37# define PRINT_FPHEX_FLOAT128 \
38 PRINT_FPHEX (_Float128, fpnum.flt128, ieee854_float128, \
39 IEEE854_FLOAT128_BIAS)
40#endif
41
42/* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
43#include <assert.h>
44
45/* This defines make it possible to use the same code for GNU C library and
46 the GNU I/O library. */
47#include <libioP.h>
48#define PUT(f, s, n) _IO_sputn (f, s, n)
49#define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
50/* We use this file GNU C library and GNU I/O library. So make
51 names equal. */
52#undef putc
53#define putc(c, f) (wide \
54 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
55#define size_t _IO_size_t
56#define FILE _IO_FILE
57
58/* Macros for doing the actual output. */
59
60#define outchar(ch) \
61 do \
62 { \
63 const int outc = (ch); \
64 if (putc (outc, fp) == EOF) \
65 return -1; \
66 ++done; \
67 } while (0)
68
69#define PRINT(ptr, wptr, len) \
70 do \
71 { \
72 size_t outlen = (len); \
73 if (wide) \
74 while (outlen-- > 0) \
75 outchar (*wptr++); \
76 else \
77 while (outlen-- > 0) \
78 outchar (*ptr++); \
79 } while (0)
80
81#define PADN(ch, len) \
82 do \
83 { \
84 if (PAD (fp, ch, len) != len) \
85 return -1; \
86 done += len; \
87 } \
88 while (0)
89
90#ifndef MIN
91# define MIN(a,b) ((a)<(b)?(a):(b))
92#endif
93
94
95int
96__printf_fphex (FILE *fp,
97 const struct printf_info *info,
98 const void *const *args)
99{
100 /* The floating-point value to output. */
101 union
102 {
103 union ieee754_double dbl;
104 long double ldbl;
105#if __HAVE_DISTINCT_FLOAT128
106 _Float128 flt128;
107#endif
108 }
109 fpnum;
110
111 /* Locale-dependent representation of decimal point. */
112 const char *decimal;
113 wchar_t decimalwc;
114
115 /* "NaN" or "Inf" for the special cases. */
116 const char *special = NULL;
117 const wchar_t *wspecial = NULL;
118
119 /* Buffer for the generated number string for the mantissa. The
120 maximal size for the mantissa is 128 bits. */
121 char numbuf[32];
122 char *numstr;
123 char *numend;
124 wchar_t wnumbuf[32];
125 wchar_t *wnumstr;
126 wchar_t *wnumend;
127 int negative;
128
129 /* The maximal exponent of two in decimal notation has 5 digits. */
130 char expbuf[5];
131 char *expstr;
132 wchar_t wexpbuf[5];
133 wchar_t *wexpstr;
134 int expnegative;
135 int exponent;
136
137 /* Non-zero is mantissa is zero. */
138 int zero_mantissa;
139
140 /* The leading digit before the decimal point. */
141 char leading;
142
143 /* Precision. */
144 int precision = info->prec;
145
146 /* Width. */
147 int width = info->width;
148
149 /* Number of characters written. */
150 int done = 0;
151
152 /* Nonzero if this is output on a wide character stream. */
153 int wide = info->wide;
154
155
156 /* Figure out the decimal point character. */
157 if (info->extra == 0)
158 {
159 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
160 decimalwc = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
161 }
162 else
163 {
164 decimal = _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
165 decimalwc = _NL_CURRENT_WORD (LC_MONETARY,
166 _NL_MONETARY_DECIMAL_POINT_WC);
167 }
168 /* The decimal point character must never be zero. */
169 assert (*decimal != '\0' && decimalwc != L'\0');
170
171#define PRINTF_FPHEX_FETCH(FLOAT, VAR) \
172 { \
173 (VAR) = *(const FLOAT *) args[0]; \
174 \
175 /* Check for special values: not a number or infinity. */ \
176 if (isnan (VAR)) \
177 { \
178 if (isupper (info->spec)) \
179 { \
180 special = "NAN"; \
181 wspecial = L"NAN"; \
182 } \
183 else \
184 { \
185 special = "nan"; \
186 wspecial = L"nan"; \
187 } \
188 } \
189 else \
190 { \
191 if (isinf (VAR)) \
192 { \
193 if (isupper (info->spec)) \
194 { \
195 special = "INF"; \
196 wspecial = L"INF"; \
197 } \
198 else \
199 { \
200 special = "inf"; \
201 wspecial = L"inf"; \
202 } \
203 } \
204 } \
205 negative = signbit (VAR); \
206 }
207
208 /* Fetch the argument value. */
209#if __HAVE_DISTINCT_FLOAT128
210 if (info->is_binary128)
211 PRINTF_FPHEX_FETCH (_Float128, fpnum.flt128)
212 else
213#endif
214#ifndef __NO_LONG_DOUBLE_MATH
215 if (info->is_long_double && sizeof (long double) > sizeof (double))
216 PRINTF_FPHEX_FETCH (long double, fpnum.ldbl)
217 else
218#endif
219 PRINTF_FPHEX_FETCH (double, fpnum.dbl.d)
220
221#undef PRINTF_FPHEX_FETCH
222
223 if (special)
224 {
225 int width = info->width;
226
227 if (negative || info->showsign || info->space)
228 --width;
229 width -= 3;
230
231 if (!info->left && width > 0)
232 PADN (' ', width);
233
234 if (negative)
235 outchar ('-');
236 else if (info->showsign)
237 outchar ('+');
238 else if (info->space)
239 outchar (' ');
240
241 PRINT (special, wspecial, 3);
242
243 if (info->left && width > 0)
244 PADN (' ', width);
245
246 return done;
247 }
248
249#if __HAVE_DISTINCT_FLOAT128
250 if (info->is_binary128)
251 PRINT_FPHEX_FLOAT128;
252 else
253#endif
254 if (info->is_long_double == 0 || sizeof (double) == sizeof (long double))
255 {
256 /* We have 52 bits of mantissa plus one implicit digit. Since
257 52 bits are representable without rest using hexadecimal
258 digits we use only the implicit digits for the number before
259 the decimal point. */
260 unsigned long long int num;
261
262 num = (((unsigned long long int) fpnum.dbl.ieee.mantissa0) << 32
263 | fpnum.dbl.ieee.mantissa1);
264
265 zero_mantissa = num == 0;
266
267 if (sizeof (unsigned long int) > 6)
268 {
269 wnumstr = _itowa_word (num, wnumbuf + (sizeof wnumbuf) / sizeof (wchar_t), 16,
270 info->spec == 'A');
271 numstr = _itoa_word (num, numbuf + sizeof numbuf, 16,
272 info->spec == 'A');
273 }
274 else
275 {
276 wnumstr = _itowa (num, wnumbuf + sizeof wnumbuf / sizeof (wchar_t), 16,
277 info->spec == 'A');
278 numstr = _itoa (num, numbuf + sizeof numbuf, 16,
279 info->spec == 'A');
280 }
281
282 /* Fill with zeroes. */
283 while (wnumstr > wnumbuf + (sizeof wnumbuf - 52) / sizeof (wchar_t))
284 {
285 *--wnumstr = L'0';
286 *--numstr = '0';
287 }
288
289 leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1';
290
291 exponent = fpnum.dbl.ieee.exponent;
292
293 if (exponent == 0)
294 {
295 if (zero_mantissa)
296 expnegative = 0;
297 else
298 {
299 /* This is a denormalized number. */
300 expnegative = 1;
301 exponent = IEEE754_DOUBLE_BIAS - 1;
302 }
303 }
304 else if (exponent >= IEEE754_DOUBLE_BIAS)
305 {
306 expnegative = 0;
307 exponent -= IEEE754_DOUBLE_BIAS;
308 }
309 else
310 {
311 expnegative = 1;
312 exponent = -(exponent - IEEE754_DOUBLE_BIAS);
313 }
314 }
315#ifdef PRINT_FPHEX_LONG_DOUBLE
316 else
317 PRINT_FPHEX_LONG_DOUBLE;
318#endif
319
320 /* Look for trailing zeroes. */
321 if (! zero_mantissa)
322 {
323 wnumend = &wnumbuf[sizeof wnumbuf / sizeof wnumbuf[0]];
324 numend = &numbuf[sizeof numbuf / sizeof numbuf[0]];
325 while (wnumend[-1] == L'0')
326 {
327 --wnumend;
328 --numend;
329 }
330
331 bool do_round_away = false;
332
333 if (precision != -1 && precision < numend - numstr)
334 {
335 char last_digit = precision > 0 ? numstr[precision - 1] : leading;
336 char next_digit = numstr[precision];
337 int last_digit_value = (last_digit >= 'A' && last_digit <= 'F'
338 ? last_digit - 'A' + 10
339 : (last_digit >= 'a' && last_digit <= 'f'
340 ? last_digit - 'a' + 10
341 : last_digit - '0'));
342 int next_digit_value = (next_digit >= 'A' && next_digit <= 'F'
343 ? next_digit - 'A' + 10
344 : (next_digit >= 'a' && next_digit <= 'f'
345 ? next_digit - 'a' + 10
346 : next_digit - '0'));
347 bool more_bits = ((next_digit_value & 7) != 0
348 || precision + 1 < numend - numstr);
349 int rounding_mode = get_rounding_mode ();
350 do_round_away = round_away (negative, last_digit_value & 1,
351 next_digit_value >= 8, more_bits,
352 rounding_mode);
353 }
354
355 if (precision == -1)
356 precision = numend - numstr;
357 else if (do_round_away)
358 {
359 /* Round up. */
360 int cnt = precision;
361 while (--cnt >= 0)
362 {
363 char ch = numstr[cnt];
364 /* We assume that the digits and the letters are ordered
365 like in ASCII. This is true for the rest of GNU, too. */
366 if (ch == '9')
367 {
368 wnumstr[cnt] = (wchar_t) info->spec;
369 numstr[cnt] = info->spec; /* This is tricky,
370 think about it! */
371 break;
372 }
373 else if (tolower (ch) < 'f')
374 {
375 ++numstr[cnt];
376 ++wnumstr[cnt];
377 break;
378 }
379 else
380 {
381 numstr[cnt] = '0';
382 wnumstr[cnt] = L'0';
383 }
384 }
385 if (cnt < 0)
386 {
387 /* The mantissa so far was fff...f Now increment the
388 leading digit. Here it is again possible that we
389 get an overflow. */
390 if (leading == '9')
391 leading = info->spec;
392 else if (tolower (leading) < 'f')
393 ++leading;
394 else
395 {
396 leading = '1';
397 if (expnegative)
398 {
399 exponent -= 4;
400 if (exponent <= 0)
401 {
402 exponent = -exponent;
403 expnegative = 0;
404 }
405 }
406 else
407 exponent += 4;
408 }
409 }
410 }
411 }
412 else
413 {
414 if (precision == -1)
415 precision = 0;
416 numend = numstr;
417 wnumend = wnumstr;
418 }
419
420 /* Now we can compute the exponent string. */
421 expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
422 wexpstr = _itowa_word (exponent,
423 wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);
424
425 /* Now we have all information to compute the size. */
426 width -= ((negative || info->showsign || info->space)
427 /* Sign. */
428 + 2 + 1 + 0 + precision + 1 + 1
429 /* 0x h . hhh P ExpoSign. */
430 + ((expbuf + sizeof expbuf) - expstr));
431 /* Exponent. */
432
433 /* Count the decimal point.
434 A special case when the mantissa or the precision is zero and the `#'
435 is not given. In this case we must not print the decimal point. */
436 if (precision > 0 || info->alt)
437 width -= wide ? 1 : strlen (decimal);
438
439 if (!info->left && info->pad != '0' && width > 0)
440 PADN (' ', width);
441
442 if (negative)
443 outchar ('-');
444 else if (info->showsign)
445 outchar ('+');
446 else if (info->space)
447 outchar (' ');
448
449 outchar ('0');
450 if ('X' - 'A' == 'x' - 'a')
451 outchar (info->spec + ('x' - 'a'));
452 else
453 outchar (info->spec == 'A' ? 'X' : 'x');
454
455 if (!info->left && info->pad == '0' && width > 0)
456 PADN ('0', width);
457
458 outchar (leading);
459
460 if (precision > 0 || info->alt)
461 {
462 const wchar_t *wtmp = &decimalwc;
463 PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
464 }
465
466 if (precision > 0)
467 {
468 ssize_t tofill = precision - (numend - numstr);
469 PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
470 if (tofill > 0)
471 PADN ('0', tofill);
472 }
473
474 if ('P' - 'A' == 'p' - 'a')
475 outchar (info->spec + ('p' - 'a'));
476 else
477 outchar (info->spec == 'A' ? 'P' : 'p');
478
479 outchar (expnegative ? '-' : '+');
480
481 PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);
482
483 if (info->left && info->pad != '0' && width > 0)
484 PADN (info->pad, width);
485
486 return done;
487}
488