1 /* Print size value using units for orders of magnitude.
2 Copyright (C) 1997-2016 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/>. */
28 /* This defines make it possible to use the same code for GNU C library and
29 the GNU I/O library. */
30 #define PUT(f, s, n) _IO_sputn (f, s, n)
31 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
32 /* We use this file GNU C library and GNU I/O library. So make
35 #define putc(c, f) (wide \
36 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
37 #define size_t _IO_size_t
40 /* Macros for doing the actual output. */
45 const int outc = (ch); \
46 if (putc (outc, fp) == EOF) \
51 #define PRINT(ptr, wptr, len) \
54 size_t outlen = (len); \
57 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
65 while (outlen-- > 0) \
68 while (outlen-- > 0) \
73 #define PADN(ch, len) \
76 if (PAD (fp, ch, len) != len) \
82 /* Prototype for helper functions. */
83 extern int __printf_fp (FILE *fp
, const struct printf_info
*info
,
84 const void *const *args
);
88 __printf_size (FILE *fp
, const struct printf_info
*info
,
89 const void *const *args
)
91 /* Units for the both formats. */
92 #define BINARY_UNITS " kmgtpezy"
93 #define DECIMAL_UNITS " KMGTPEZY"
94 static const char units
[2][sizeof (BINARY_UNITS
)] =
96 BINARY_UNITS
, /* For binary format. */
97 DECIMAL_UNITS
/* For decimal format. */
99 const char *tag
= units
[isupper (info
->spec
) != 0];
100 int divisor
= isupper (info
->spec
) ? 1000 : 1024;
102 /* The floating-point value to output. */
105 union ieee754_double dbl
;
109 const void *ptr
= &fpnum
;
113 /* "NaN" or "Inf" for the special cases. */
114 const char *special
= NULL
;
115 const wchar_t *wspecial
= NULL
;
117 struct printf_info fp_info
;
119 int wide
= info
->wide
;
121 /* Fetch the argument value. */
122 #ifndef __NO_LONG_DOUBLE_MATH
123 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
125 fpnum
.ldbl
= *(const long double *) args
[0];
127 /* Check for special values: not a number or infinity. */
128 if (isnan (fpnum
.ldbl
))
132 // is_neg = 0; Already zero
134 else if (isinf (fpnum
.ldbl
))
136 is_neg
= signbit (fpnum
.ldbl
);
141 while (fpnum
.ldbl
>= divisor
&& tag
[1] != '\0')
143 fpnum
.ldbl
/= divisor
;
148 #endif /* no long double */
150 fpnum
.dbl
.d
= *(const double *) args
[0];
152 /* Check for special values: not a number or infinity. */
153 if (isnan (fpnum
.dbl
.d
))
157 // is_neg = 0; Already zero
159 else if (isinf (fpnum
.dbl
.d
))
161 is_neg
= signbit (fpnum
.dbl
.d
);
166 while (fpnum
.dbl
.d
>= divisor
&& tag
[1] != '\0')
168 fpnum
.dbl
.d
/= divisor
;
175 int width
= info
->prec
> info
->width
? info
->prec
: info
->width
;
177 if (is_neg
|| info
->showsign
|| info
->space
)
181 if (!info
->left
&& width
> 0)
186 else if (info
->showsign
)
188 else if (info
->space
)
191 PRINT (special
, wspecial
, 3);
193 if (info
->left
&& width
> 0)
199 /* Prepare to print the number. We want to use `__printf_fp' so we
200 have to prepare a `printf_info' structure. */
203 fp_info
.prec
= info
->prec
< 0 ? 3 : info
->prec
;
206 if (fp_info
.left
&& fp_info
.pad
== L
' ')
208 /* We must do the padding ourself since the unit character must
209 be placed before the padding spaces. */
212 done
= __printf_fp (fp
, &fp_info
, &ptr
);
216 if (info
->width
> done
)
217 PADN (' ', info
->width
- done
);
222 /* We can let __printf_fp do all the printing and just add our
223 unit character afterwards. */
224 fp_info
.width
= info
->width
- 1;
226 done
= __printf_fp (fp
, &fp_info
, &ptr
);
233 ldbl_strong_alias (__printf_size
, printf_size
);
235 /* This is the function used by `vfprintf' to determine number and
236 type of the arguments. */
238 printf_size_info (const struct printf_info
*info
, size_t n
, int *argtypes
)
240 /* We need only one double or long double argument. */
242 argtypes
[0] = PA_DOUBLE
| (info
->is_long_double
? PA_FLAG_LONG_DOUBLE
: 0);