1 /* Print size value using units for orders of magnitude.
2 Copyright (C) 1997-2024 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/>. */
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))
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. */
37 const int outc = (ch); \
38 if (putc (outc, fp) == EOF) \
43 #define PRINT(ptr, wptr, len) \
46 size_t outlen = (len); \
49 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
57 while (outlen-- > 0) \
60 while (outlen-- > 0) \
65 #define PADN(ch, len) \
68 if (PAD (fp, ch, len) != len) \
74 /* Prototype for helper functions. */
75 extern int __printf_fp (FILE *fp
, const struct printf_info
*info
,
76 const void *const *args
);
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. */
97 union ieee754_double dbl
;
99 #if __HAVE_DISTINCT_FLOAT128
104 const void *ptr
= &fpnum
;
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
;
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. */ \
125 /* is_neg = 0; Already zero */ \
127 else if (isinf (VAR)) \
129 is_neg = signbit (VAR); \
134 while ((VAR) >= divisor && tag[1] != '\0') \
141 /* Fetch the argument value. */
142 #if __HAVE_DISTINCT_FLOAT128
143 if (info
->is_binary128
)
144 PRINTF_SIZE_FETCH (_Float128
, fpnum
.f128
)
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
)
152 PRINTF_SIZE_FETCH (double, fpnum
.dbl
.d
)
154 #undef PRINTF_SIZE_FETCH
158 int width
= info
->prec
> info
->width
? info
->prec
: info
->width
;
160 if (is_neg
|| info
->showsign
|| info
->space
)
164 if (!info
->left
&& width
> 0)
169 else if (info
->showsign
)
171 else if (info
->space
)
174 PRINT (special
, wspecial
, 3);
176 if (info
->left
&& width
> 0)
182 /* Prepare to print the number. We want to use `__printf_fp' so we
183 have to prepare a `printf_info' structure. */
186 fp_info
.prec
= info
->prec
< 0 ? 3 : info
->prec
;
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. */
195 done
= __printf_fp (fp
, &fp_info
, &ptr
);
199 if (info
->width
> done
)
200 PADN (' ', info
->width
- done
);
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
);
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. */
225 argtypes
[0] = PA_DOUBLE
| (info
->is_long_double
? PA_FLAG_LONG_DOUBLE
: 0);