2.9
[glibc/nacl-glibc.git] / sysdeps / ieee754 / ldbl-128ibm / printf_fphex.c
blobb2ad25e31f03abca6d930566562e2d2d62248f34
1 /* Print floating point number in hexadecimal notation according to ISO C99.
2 Copyright (C) 1997,1998,1999,2000,2001,2002,2004,2006,2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #define PRINT_FPHEX_LONG_DOUBLE \
23 do { \
24 /* We have 105 bits of mantissa plus one implicit digit. Since \
25 106 bits are representable without rest using hexadecimal \
26 digits we use only the implicit digits for the number before \
27 the decimal point. */ \
28 unsigned long long int num0, num1; \
29 unsigned long long hi, lo; \
30 int ediff; \
31 union ibm_extended_long_double eldbl; \
32 eldbl.d = fpnum.ldbl.d; \
34 assert (sizeof (long double) == 16); \
36 lo = ((long long)eldbl.ieee.mantissa2 << 32) | eldbl.ieee.mantissa3; \
37 hi = ((long long)eldbl.ieee.mantissa0 << 32) | eldbl.ieee.mantissa1; \
38 lo <<= 7; /* pre-shift lo to match ieee854. */ \
39 /* If the lower double is not a denomal or zero then set the hidden \
40 53rd bit. */ \
41 if (eldbl.ieee.exponent2 != 0) \
42 lo |= (1ULL << (52 + 7)); \
43 else \
44 lo <<= 1; \
45 /* The lower double is normalized separately from the upper. We \
46 may need to adjust the lower manitissa to reflect this. */ \
47 ediff = eldbl.ieee.exponent - eldbl.ieee.exponent2; \
48 if (ediff > 53 + 63) \
49 lo = 0; \
50 else if (ediff > 53) \
51 lo = lo >> (ediff - 53); \
52 else if (eldbl.ieee.exponent2 == 0 && ediff < 53) \
53 lo = lo << (53 - ediff); \
54 if (eldbl.ieee.negative != eldbl.ieee.negative2 \
55 && (eldbl.ieee.exponent2 != 0 || lo != 0L)) \
56 { \
57 lo = (1ULL << 60) - lo; \
58 if (hi == 0L) \
59 { \
60 /* we have a borrow from the hidden bit, so shift left 1. */ \
61 hi = 0xffffffffffffeLL | (lo >> 59); \
62 lo = 0xfffffffffffffffLL & (lo << 1); \
63 eldbl.ieee.exponent--; \
64 } \
65 else \
66 hi--; \
67 } \
68 num1 = (hi << 60) | lo; \
69 num0 = hi >> 4; \
71 zero_mantissa = (num0|num1) == 0; \
73 if (sizeof (unsigned long int) > 6) \
74 { \
75 numstr = _itoa_word (num1, numbuf + sizeof numbuf, 16, \
76 info->spec == 'A'); \
77 wnumstr = _itowa_word (num1, \
78 wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t),\
79 16, info->spec == 'A'); \
80 } \
81 else \
82 { \
83 numstr = _itoa (num1, numbuf + sizeof numbuf, 16, \
84 info->spec == 'A'); \
85 wnumstr = _itowa (num1, \
86 wnumbuf + sizeof (wnumbuf) / sizeof (wchar_t), \
87 16, info->spec == 'A'); \
88 } \
90 while (numstr > numbuf + (sizeof numbuf - 64 / 4)) \
91 { \
92 *--numstr = '0'; \
93 *--wnumstr = L'0'; \
94 } \
96 if (sizeof (unsigned long int) > 6) \
97 { \
98 numstr = _itoa_word (num0, numstr, 16, info->spec == 'A'); \
99 wnumstr = _itowa_word (num0, wnumstr, 16, info->spec == 'A'); \
101 else \
103 numstr = _itoa (num0, numstr, 16, info->spec == 'A'); \
104 wnumstr = _itowa (num0, wnumstr, 16, info->spec == 'A'); \
107 /* Fill with zeroes. */ \
108 while (numstr > numbuf + (sizeof numbuf - 112 / 4)) \
110 *--numstr = '0'; \
111 *--wnumstr = L'0'; \
114 leading = eldbl.ieee.exponent == 0 ? '0' : '1'; \
116 exponent = eldbl.ieee.exponent; \
118 if (exponent == 0) \
120 if (zero_mantissa) \
121 expnegative = 0; \
122 else \
124 /* This is a denormalized number. */ \
125 expnegative = 1; \
126 exponent = IBM_EXTENDED_LONG_DOUBLE_BIAS - 1; \
129 else if (exponent >= IBM_EXTENDED_LONG_DOUBLE_BIAS) \
131 expnegative = 0; \
132 exponent -= IBM_EXTENDED_LONG_DOUBLE_BIAS; \
134 else \
136 expnegative = 1; \
137 exponent = -(exponent - IBM_EXTENDED_LONG_DOUBLE_BIAS); \
139 } while (0)
141 #include <stdio-common/printf_fphex.c>