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