1 /* Print size value using units for orders of magnitude.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5 Based on a proposal by Larry McVoy <lm@sgi.com>.
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, see
19 <http://www.gnu.org/licenses/>. */
27 #define PUT(f, s, n) _IO_sputn (f, s, n)
28 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
30 #define putc(c, f) (wide \
31 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
34 /* Macros for doing the actual output. */
39 const int outc = (ch); \
40 if (putc (outc, fp) == EOF) \
45 #define PRINT(ptr, wptr, len) \
48 size_t outlen = (len); \
51 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
59 while (outlen-- > 0) \
62 while (outlen-- > 0) \
67 #define PADN(ch, len) \
70 if (PAD (fp, ch, len) != len) \
76 /* Prototype for helper functions. */
77 extern int __printf_fp (FILE *fp
, const struct printf_info
*info
,
78 const void *const *args
);
82 __printf_size (FILE *fp
, const struct printf_info
*info
,
83 const void *const *args
)
85 /* Units for the both formats. */
86 #define BINARY_UNITS " kmgtpezy"
87 #define DECIMAL_UNITS " KMGTPEZY"
88 static const char units
[2][sizeof (BINARY_UNITS
)] =
90 BINARY_UNITS
, /* For binary format. */
91 DECIMAL_UNITS
/* For decimal format. */
93 const char *tag
= units
[isupper (info
->spec
) != 0];
94 int divisor
= isupper (info
->spec
) ? 1000 : 1024;
96 /* The floating-point value to output. */
99 union ieee754_double dbl
;
101 #if __HAVE_DISTINCT_FLOAT128
106 const void *ptr
= &fpnum
;
110 /* "NaN" or "Inf" for the special cases. */
111 const char *special
= NULL
;
112 const wchar_t *wspecial
= NULL
;
114 struct printf_info fp_info
;
116 int wide
= info
->wide
;
118 #define PRINTF_SIZE_FETCH(FLOAT, VAR) \
120 (VAR) = *(const FLOAT *) args[0]; \
122 /* Check for special values: not a number or infinity. */ \
127 /* is_neg = 0; Already zero */ \
129 else if (isinf (VAR)) \
131 is_neg = signbit (VAR); \
136 while ((VAR) >= divisor && tag[1] != '\0') \
143 /* Fetch the argument value. */
144 #if __HAVE_DISTINCT_FLOAT128
145 if (info
->is_binary128
)
146 PRINTF_SIZE_FETCH (_Float128
, fpnum
.f128
)
149 #ifndef __NO_LONG_DOUBLE_MATH
150 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
151 PRINTF_SIZE_FETCH (long double, fpnum
.ldbl
)
154 PRINTF_SIZE_FETCH (double, fpnum
.dbl
.d
)
156 #undef PRINTF_SIZE_FETCH
160 int width
= info
->prec
> info
->width
? info
->prec
: info
->width
;
162 if (is_neg
|| info
->showsign
|| info
->space
)
166 if (!info
->left
&& width
> 0)
171 else if (info
->showsign
)
173 else if (info
->space
)
176 PRINT (special
, wspecial
, 3);
178 if (info
->left
&& width
> 0)
184 /* Prepare to print the number. We want to use `__printf_fp' so we
185 have to prepare a `printf_info' structure. */
188 fp_info
.prec
= info
->prec
< 0 ? 3 : info
->prec
;
191 if (fp_info
.left
&& fp_info
.pad
== L
' ')
193 /* We must do the padding ourself since the unit character must
194 be placed before the padding spaces. */
197 done
= __printf_fp (fp
, &fp_info
, &ptr
);
201 if (info
->width
> done
)
202 PADN (' ', info
->width
- done
);
207 /* We can let __printf_fp do all the printing and just add our
208 unit character afterwards. */
209 fp_info
.width
= info
->width
- 1;
211 done
= __printf_fp (fp
, &fp_info
, &ptr
);
218 ldbl_strong_alias (__printf_size
, printf_size
);
220 /* This is the function used by `vfprintf' to determine number and
221 type of the arguments. */
223 printf_size_info (const struct printf_info
*info
, size_t n
, int *argtypes
)
225 /* We need only one double or long double argument. */
227 argtypes
[0] = PA_DOUBLE
| (info
->is_long_double
? PA_FLAG_LONG_DOUBLE
: 0);