AArch64: Sort makefile entries
[glibc.git] / stdio-common / printf_size.c
blobfa3bf29fb47f13b196c2de93247793e2c175bc95
1 /* Print size value using units for orders of magnitude.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <ctype.h>
20 #include <ieee754.h>
21 #include <math.h>
22 #include <printf.h>
23 #include <libioP.h>
25 #define PUT(f, s, n) _IO_sputn (f, s, n)
26 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
27 #undef putc
28 #define putc(c, f) (wide \
29 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
32 /* Macros for doing the actual output. */
34 #define outchar(ch) \
35 do \
36 { \
37 const int outc = (ch); \
38 if (putc (outc, fp) == EOF) \
39 return -1; \
40 ++done; \
41 } while (0)
43 #define PRINT(ptr, wptr, len) \
44 do \
45 { \
46 size_t outlen = (len); \
47 if (len > 20) \
48 { \
49 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
50 return -1; \
51 ptr += outlen; \
52 done += outlen; \
53 } \
54 else \
55 { \
56 if (wide) \
57 while (outlen-- > 0) \
58 outchar (*wptr++); \
59 else \
60 while (outlen-- > 0) \
61 outchar (*ptr++); \
62 } \
63 } while (0)
65 #define PADN(ch, len) \
66 do \
67 { \
68 if (PAD (fp, ch, len) != len) \
69 return -1; \
70 done += len; \
71 } \
72 while (0)
74 /* Prototype for helper functions. */
75 extern int __printf_fp (FILE *fp, const struct printf_info *info,
76 const void *const *args);
79 int
80 __printf_size (FILE *fp, const struct printf_info *info,
81 const void *const *args)
83 /* Units for the both formats. */
84 #define BINARY_UNITS " kmgtpezy"
85 #define DECIMAL_UNITS " KMGTPEZY"
86 static const char units[2][sizeof (BINARY_UNITS)] =
88 BINARY_UNITS, /* For binary format. */
89 DECIMAL_UNITS /* For decimal format. */
91 const char *tag = units[isupper (info->spec) != 0];
92 int divisor = isupper (info->spec) ? 1000 : 1024;
94 /* The floating-point value to output. */
95 union
97 union ieee754_double dbl;
98 long double ldbl;
99 #if __HAVE_DISTINCT_FLOAT128
100 _Float128 f128;
101 #endif
103 fpnum;
104 const void *ptr = &fpnum;
106 int is_neg = 0;
108 /* "NaN" or "Inf" for the special cases. */
109 const char *special = NULL;
110 const wchar_t *wspecial = NULL;
112 struct printf_info fp_info;
113 int done = 0;
114 int wide = info->wide;
116 #define PRINTF_SIZE_FETCH(FLOAT, VAR) \
118 (VAR) = *(const FLOAT *) args[0]; \
120 /* Check for special values: not a number or infinity. */ \
121 if (isnan (VAR)) \
123 special = "nan"; \
124 wspecial = L"nan"; \
125 /* is_neg = 0; Already zero */ \
127 else if (isinf (VAR)) \
129 is_neg = signbit (VAR); \
130 special = "inf"; \
131 wspecial = L"inf"; \
133 else \
134 while ((VAR) >= divisor && tag[1] != '\0') \
136 (VAR) /= divisor; \
137 ++tag; \
141 /* Fetch the argument value. */
142 #if __HAVE_DISTINCT_FLOAT128
143 if (info->is_binary128)
144 PRINTF_SIZE_FETCH (_Float128, fpnum.f128)
145 else
146 #endif
147 #ifndef __NO_LONG_DOUBLE_MATH
148 if (info->is_long_double && sizeof (long double) > sizeof (double))
149 PRINTF_SIZE_FETCH (long double, fpnum.ldbl)
150 else
151 #endif
152 PRINTF_SIZE_FETCH (double, fpnum.dbl.d)
154 #undef PRINTF_SIZE_FETCH
156 if (special)
158 int width = info->prec > info->width ? info->prec : info->width;
160 if (is_neg || info->showsign || info->space)
161 --width;
162 width -= 3;
164 if (!info->left && width > 0)
165 PADN (' ', width);
167 if (is_neg)
168 outchar ('-');
169 else if (info->showsign)
170 outchar ('+');
171 else if (info->space)
172 outchar (' ');
174 PRINT (special, wspecial, 3);
176 if (info->left && width > 0)
177 PADN (' ', width);
179 return done;
182 /* Prepare to print the number. We want to use `__printf_fp' so we
183 have to prepare a `printf_info' structure. */
184 fp_info = *info;
185 fp_info.spec = 'f';
186 fp_info.prec = info->prec < 0 ? 3 : info->prec;
187 fp_info.wide = wide;
189 if (fp_info.left && fp_info.pad == L' ')
191 /* We must do the padding ourself since the unit character must
192 be placed before the padding spaces. */
193 fp_info.width = 0;
195 done = __printf_fp (fp, &fp_info, &ptr);
196 if (done > 0)
198 outchar (*tag);
199 if (info->width > done)
200 PADN (' ', info->width - done);
203 else
205 /* We can let __printf_fp do all the printing and just add our
206 unit character afterwards. */
207 fp_info.width = info->width - 1;
209 done = __printf_fp (fp, &fp_info, &ptr);
210 if (done > 0)
211 outchar (*tag);
214 return done;
216 ldbl_strong_alias (__printf_size, printf_size);
218 /* This is the function used by `vfprintf' to determine number and
219 type of the arguments. */
221 printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
223 /* We need only one double or long double argument. */
224 if (n >= 1)
225 argtypes[0] = PA_DOUBLE | (info->is_long_double ? PA_FLAG_LONG_DOUBLE : 0);
227 return 1;