1 /* Print floating point number in hexadecimal notation according to ISO C99.
2 Copyright (C) 1997-2012 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, see
18 <http://www.gnu.org/licenses/>. */
28 #include "quadmath-rounding-mode.h"
29 #include "quadmath-printf.h"
34 /* Macros for doing the actual output. */
39 register const int outc = (ch); \
40 if (PUTC (outc, fp) == EOF) \
45 #define PRINT(ptr, wptr, len) \
48 register size_t outlen = (len); \
50 while (outlen-- > 0) \
53 while (outlen-- > 0) \
57 #define PADN(ch, len) \
60 if (PAD (fp, ch, len) != len) \
69 __quadmath_printf_fphex (struct __quadmath_printf_file
*fp
,
70 const struct printf_info
*info
,
71 const void *const *args
)
73 /* The floating-point value to output. */
74 ieee854_float128 fpnum
;
76 /* Locale-dependent representation of decimal point. */
80 /* "NaN" or "Inf" for the special cases. */
81 const char *special
= NULL
;
82 const wchar_t *wspecial
= NULL
;
84 /* Buffer for the generated number string for the mantissa. The
85 maximal size for the mantissa is 128 bits. */
94 /* The maximal exponent of two in decimal notation has 5 digits. */
102 /* Non-zero is mantissa is zero. */
105 /* The leading digit before the decimal point. */
109 int precision
= info
->prec
;
112 int width
= info
->width
;
114 /* Number of characters written. */
117 /* Nonzero if this is output on a wide character stream. */
118 int wide
= info
->wide
;
122 /* Figure out the decimal point character. */
123 #ifdef USE_NL_LANGINFO
124 if (info
->extra
== 0)
125 decimal
= nl_langinfo (DECIMAL_POINT
);
128 decimal
= nl_langinfo (MON_DECIMAL_POINT
);
129 if (*decimal
== '\0')
130 decimal
= nl_langinfo (DECIMAL_POINT
);
132 /* The decimal point character must never be zero. */
133 assert (*decimal
!= '\0');
134 #elif defined USE_LOCALECONV
135 const struct lconv
*lc
= localeconv ();
136 if (info
->extra
== 0)
137 decimal
= lc
->decimal_point
;
140 decimal
= lc
->mon_decimal_point
;
141 if (decimal
== NULL
|| *decimal
== '\0')
142 decimal
= lc
->decimal_point
;
144 if (decimal
== NULL
|| *decimal
== '\0')
149 #ifdef USE_NL_LANGINFO_WC
150 if (info
->extra
== 0)
151 decimalwc
= nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC
);
154 decimalwc
= nl_langinfo_wc (_NL_MONETARY_DECIMAL_POINT_WC
);
155 if (decimalwc
== L_('\0'))
156 decimalwc
= nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC
);
158 /* The decimal point character must never be zero. */
159 assert (decimalwc
!= L_('\0'));
164 /* Fetch the argument value. */
166 memcpy (&fpnum
.value
, *(const void *const *) args
[0],
167 sizeof (fpnum
.value
));
169 /* Check for special values: not a number or infinity. */
170 if (isnanq (fpnum
.value
))
172 negative
= fpnum
.ieee
.negative
!= 0;
173 if (isupper (info
->spec
))
176 wspecial
= L_("NAN");
181 wspecial
= L_("nan");
186 if (isinfq (fpnum
.value
))
188 if (isupper (info
->spec
))
191 wspecial
= L_("INF");
196 wspecial
= L_("inf");
200 negative
= signbitq (fpnum
.value
);
206 int width
= info
->width
;
208 if (negative
|| info
->showsign
|| info
->space
)
212 if (!info
->left
&& width
> 0)
217 else if (info
->showsign
)
219 else if (info
->space
)
222 PRINT (special
, wspecial
, 3);
224 if (info
->left
&& width
> 0)
231 /* We have 112 bits of mantissa plus one implicit digit. Since
232 112 bits are representable without rest using hexadecimal
233 digits we use only the implicit digits for the number before
234 the decimal point. */
237 assert (sizeof (long double) == 16);
239 num0
= (((unsigned long long int) fpnum
.ieee
.mantissa0
) << 32
240 | fpnum
.ieee
.mantissa1
);
241 num1
= (((unsigned long long int) fpnum
.ieee
.mantissa2
) << 32
242 | fpnum
.ieee
.mantissa3
);
244 zero_mantissa
= (num0
|num1
) == 0;
246 if (sizeof (unsigned long int) > 6)
248 numstr
= _itoa_word (num1
, numbuf
+ sizeof numbuf
, 16,
250 wnumstr
= _itowa_word (num1
,
251 wnumbuf
+ sizeof (wnumbuf
) / sizeof (wchar_t),
252 16, info
->spec
== 'A');
256 numstr
= _itoa (num1
, numbuf
+ sizeof numbuf
, 16,
258 wnumstr
= _itowa (num1
,
259 wnumbuf
+ sizeof (wnumbuf
) / sizeof (wchar_t),
260 16, info
->spec
== 'A');
263 while (numstr
> numbuf
+ (sizeof numbuf
- 64 / 4))
266 *--wnumstr
= L_('0');
269 if (sizeof (unsigned long int) > 6)
271 numstr
= _itoa_word (num0
, numstr
, 16, info
->spec
== 'A');
272 wnumstr
= _itowa_word (num0
, wnumstr
, 16, info
->spec
== 'A');
276 numstr
= _itoa (num0
, numstr
, 16, info
->spec
== 'A');
277 wnumstr
= _itowa (num0
, wnumstr
, 16, info
->spec
== 'A');
280 /* Fill with zeroes. */
281 while (numstr
> numbuf
+ (sizeof numbuf
- 112 / 4))
283 *--wnumstr
= L_('0');
287 leading
= fpnum
.ieee
.exponent
== 0 ? '0' : '1';
289 exponent
= fpnum
.ieee
.exponent
;
297 /* This is a denormalized number. */
299 exponent
= IEEE854_FLOAT128_BIAS
- 1;
302 else if (exponent
>= IEEE854_FLOAT128_BIAS
)
305 exponent
-= IEEE854_FLOAT128_BIAS
;
310 exponent
= -(exponent
- IEEE854_FLOAT128_BIAS
);
314 /* Look for trailing zeroes. */
317 wnumend
= &wnumbuf
[sizeof wnumbuf
/ sizeof wnumbuf
[0]];
318 numend
= &numbuf
[sizeof numbuf
/ sizeof numbuf
[0]];
319 while (wnumend
[-1] == L_('0'))
325 do_round_away
= false;
327 if (precision
!= -1 && precision
< numend
- numstr
)
329 char last_digit
= precision
> 0 ? numstr
[precision
- 1] : leading
;
330 char next_digit
= numstr
[precision
];
331 int last_digit_value
= (last_digit
>= 'A' && last_digit
<= 'F'
332 ? last_digit
- 'A' + 10
333 : (last_digit
>= 'a' && last_digit
<= 'f'
334 ? last_digit
- 'a' + 10
335 : last_digit
- '0'));
336 int next_digit_value
= (next_digit
>= 'A' && next_digit
<= 'F'
337 ? next_digit
- 'A' + 10
338 : (next_digit
>= 'a' && next_digit
<= 'f'
339 ? next_digit
- 'a' + 10
340 : next_digit
- '0'));
341 bool more_bits
= ((next_digit_value
& 7) != 0
342 || precision
+ 1 < numend
- numstr
);
344 int rounding_mode
= get_rounding_mode ();
345 do_round_away
= round_away (negative
, last_digit_value
& 1,
346 next_digit_value
>= 8, more_bits
,
352 precision
= numend
- numstr
;
353 else if (do_round_away
)
359 char ch
= numstr
[cnt
];
360 /* We assume that the digits and the letters are ordered
361 like in ASCII. This is true for the rest of GNU, too. */
364 wnumstr
[cnt
] = (wchar_t) info
->spec
;
365 numstr
[cnt
] = info
->spec
; /* This is tricky,
369 else if (tolower (ch
) < 'f')
378 wnumstr
[cnt
] = L_('0');
383 /* The mantissa so far was fff...f Now increment the
384 leading digit. Here it is again possible that we
387 leading
= info
->spec
;
388 else if (tolower (leading
) < 'f')
398 exponent
= -exponent
;
416 /* Now we can compute the exponent string. */
417 expstr
= _itoa_word (exponent
, expbuf
+ sizeof expbuf
, 10, 0);
418 wexpstr
= _itowa_word (exponent
,
419 wexpbuf
+ sizeof wexpbuf
/ sizeof (wchar_t), 10, 0);
421 /* Now we have all information to compute the size. */
422 width
-= ((negative
|| info
->showsign
|| info
->space
)
424 + 2 + 1 + 0 + precision
+ 1 + 1
425 /* 0x h . hhh P ExpoSign. */
426 + ((expbuf
+ sizeof expbuf
) - expstr
));
429 /* Count the decimal point.
430 A special case when the mantissa or the precision is zero and the `#'
431 is not given. In this case we must not print the decimal point. */
432 if (precision
> 0 || info
->alt
)
433 width
-= wide
? 1 : strlen (decimal
);
435 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
440 else if (info
->showsign
)
442 else if (info
->space
)
446 if ('X' - 'A' == 'x' - 'a')
447 outchar (info
->spec
+ ('x' - 'a'));
449 outchar (info
->spec
== 'A' ? 'X' : 'x');
451 if (!info
->left
&& info
->pad
== '0' && width
> 0)
456 if (precision
> 0 || info
->alt
)
458 const wchar_t *wtmp
= &decimalwc
;
459 PRINT (decimal
, wtmp
, wide
? 1 : strlen (decimal
));
464 ssize_t tofill
= precision
- (numend
- numstr
);
465 PRINT (numstr
, wnumstr
, MIN (numend
- numstr
, precision
));
470 if ('P' - 'A' == 'p' - 'a')
471 outchar (info
->spec
+ ('p' - 'a'));
473 outchar (info
->spec
== 'A' ? 'P' : 'p');
475 outchar (expnegative
? '-' : '+');
477 PRINT (expstr
, wexpstr
, (expbuf
+ sizeof expbuf
) - expstr
);
479 if (info
->left
&& info
->pad
!= '0' && width
> 0)
480 PADN (info
->pad
, width
);