1/* Print floating point number in hexadecimal notation according to ISO C99.
2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#define PRINT_FPHEX_LONG_DOUBLE \
20do { \
21 /* We have 105 bits of mantissa plus one implicit digit. Since \
22 106 bits are representable without rest using hexadecimal \
23 digits we use only the implicit digits for the number before \
24 the decimal point. */ \
25 unsigned long long int num0, num1; \
26 unsigned long long hi, lo; \
27 int ediff; \
28 union ibm_extended_long_double u; \
29 u.ld = fpnum.ldbl; \
30 \
31 assert (sizeof (long double) == 16); \
32 \
33 lo = ((long long)u.d[1].ieee.mantissa0 << 32) | u.d[1].ieee.mantissa1; \
34 hi = ((long long)u.d[0].ieee.mantissa0 << 32) | u.d[0].ieee.mantissa1; \
35 lo <<= 7; /* pre-shift lo to match ieee854. */ \
36 /* If the lower double is not a denormal or zero then set the hidden \
37 53rd bit. */ \
38 if (u.d[1].ieee.exponent != 0) \
39 lo |= (1ULL << (52 + 7)); \
40 else \
41 lo <<= 1; \
42 /* The lower double is normalized separately from the upper. We \
43 may need to adjust the lower manitissa to reflect this. */ \
44 ediff = u.d[0].ieee.exponent - u.d[1].ieee.exponent - 53; \
45 if (ediff > 63) \
46 lo = 0; \
47 else if (ediff > 0) \
48 lo = lo >> ediff; \
49 else if (ediff < 0) \
50 lo = lo << -ediff; \
51 if (u.d[0].ieee.negative != u.d[1].ieee.negative \
52 && lo != 0) \
53 { \
54 lo = (1ULL << 60) - lo; \
55 if (hi == 0L) \
56 { \
57 /* we have a borrow from the hidden bit, so shift left 1. */ \
58 hi = 0xffffffffffffeLL | (lo >> 59); \
59 lo = 0xfffffffffffffffLL & (lo << 1); \
60 u.d[0].ieee.exponent--; \
61 } \
62 else \
63 hi--; \
64 } \
65 num1 = (hi << 60) | lo; \
66 num0 = hi >> 4; \
67 \
68 zero_mantissa = (num0|num1) == 0; \
69 \
70 if (sizeof (unsigned long int) > 6) \
71 numstr = _itoa_word (num1, numbuf + sizeof numbuf, 16, \
72 info->spec == 'A'); \
73 else \
74 numstr = _itoa (num1, numbuf + sizeof numbuf, 16, \
75 info->spec == 'A'); \
76 \
77 while (numstr > numbuf + (sizeof numbuf - 64 / 4)) \
78 *--numstr = '0'; \
79 \
80 if (sizeof (unsigned long int) > 6) \
81 numstr = _itoa_word (num0, numstr, 16, info->spec == 'A'); \
82 else \
83 numstr = _itoa (num0, numstr, 16, info->spec == 'A'); \
84 \
85 /* Fill with zeroes. */ \
86 while (numstr > numbuf + (sizeof numbuf - 112 / 4)) \
87 *--numstr = '0'; \
88 \
89 leading = u.d[0].ieee.exponent == 0 ? '0' : '1'; \
90 \
91 exponent = u.d[0].ieee.exponent; \
92 \
93 if (exponent == 0) \
94 { \
95 if (zero_mantissa) \
96 expnegative = 0; \
97 else \
98 { \
99 /* This is a denormalized number. */ \
100 expnegative = 1; \
101 exponent = IEEE754_DOUBLE_BIAS - 1; \
102 } \
103 } \
104 else if (exponent >= IEEE754_DOUBLE_BIAS) \
105 { \
106 expnegative = 0; \
107 exponent -= IEEE754_DOUBLE_BIAS; \
108 } \
109 else \
110 { \
111 expnegative = 1; \
112 exponent = -(exponent - IEEE754_DOUBLE_BIAS); \
113 } \
114} while (0)
115
116#include <stdio-common/printf_fphex.c>
117

source code of glibc/sysdeps/ieee754/ldbl-128ibm/printf_fphex.c