glibc/include/printf.h
Florian Weimer e88b9f0e5c stdio-common: Convert vfprintf and related functions to buffers
vfprintf is entangled with vfwprintf (of course), __printf_fp,
__printf_fphex, __vstrfmon_l_internal, and the strfrom family of
functions.  The latter use the internal snprintf functionality,
so vsnprintf is converted as well.

The simples conversion is __printf_fphex, followed by
__vstrfmon_l_internal and __printf_fp, and finally
__vfprintf_internal and __vfwprintf_internal.  __vsnprintf_internal
and strfrom* are mostly consuming the new interfaces, so they
are comparatively simple.

__printf_fp is a public symbol, so the FILE *-based interface
had to preserved.

The __printf_fp rewrite does not change the actual binary-to-decimal
conversion algorithm, and digits are still not emitted directly to
the target buffer.  However, the staging buffer now uses bytes
instead of wide characters, and one buffer copy is eliminated.

The changes are at least performance-neutral in my testing.
Floating point printing and snprintf improved measurably, so that
this Lua script

  for i=1,5000000 do
      print(i, i * math.pi)
  end

runs about 5% faster for me.  To preserve fprintf performance for
a simple "%d" format, this commit has some logic changes under
LABEL (unsigned_number) to avoid additional function calls.  There
are certainly some very easy performance improvements here: binary,
octal and hexadecimal formatting can easily avoid the temporary work
buffer (the number of digits can be computed ahead-of-time using one
of the __builtin_clz* built-ins). Decimal formatting can use a
specialized version of _itoa_word for base 10.

The existing (inconsistent) width handling between strfmon and printf
is preserved here.  __print_fp_buffer_1 would have to use
__translated_number_width to achieve ISO conformance for printf.

Test expectations in libio/tst-vtables-common.c are adjusted because
the internal staging buffer merges all virtual function calls into
one.

In general, stack buffer usage is greatly reduced, particularly for
unbuffered input streams.  __printf_fp can still use a large buffer
in binary128 mode for %g, though.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2022-12-19 18:56:54 +01:00

96 lines
3.2 KiB
C

#ifndef _PRINTF_H
/* Workaround PR90731 with GCC 9 when using ldbl redirects in C++. */
#include <bits/floatn.h>
#if defined __cplusplus && __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
# if __GNUC_PREREQ (9, 0) && !__GNUC_PREREQ (9, 3)
# pragma GCC system_header
# endif
#endif
#include <stdio-common/printf.h>
# ifndef _ISOMAC
/* Internal interfaces for registered specifiers. */
extern printf_arginfo_size_function **__printf_arginfo_table attribute_hidden;
extern printf_function **__printf_function_table attribute_hidden;
extern printf_va_arg_function **__printf_va_arg_table attribute_hidden;
int __register_printf_specifier (int, printf_function,
printf_arginfo_size_function);
libc_hidden_proto (__register_printf_specifier)
/* The various kinds of arguments that can be passed to printf. */
union printf_arg
{
wchar_t pa_wchar;
int pa_int;
long int pa_long_int;
long long int pa_long_long_int;
unsigned int pa_u_int;
unsigned long int pa_u_long_int;
unsigned long long int pa_u_long_long_int;
double pa_double;
long double pa_long_double;
#if __HAVE_FLOAT128_UNLIKE_LDBL
_Float128 pa_float128;
#endif
const char *pa_string;
const wchar_t *pa_wstring;
void *pa_pointer;
void *pa_user;
};
/* Invoke a registered printf callback. Called from vfprintf and vfwprintf. */
int __printf_function_invoke (void *, printf_function callback,
union printf_arg *args_value,
size_t ndata_args,
struct printf_info *info) attribute_hidden;
int __wprintf_function_invoke (void *, printf_function callback,
union printf_arg *args_value,
size_t ndata_args,
struct printf_info *info) attribute_hidden;
#include <bits/types/locale_t.h>
/* Returns the width (as for printf, in bytes) of the converted ASCII
number in the characters in the range [FIRST, LAST). The range
must only contain ASCII digits. The caller is responsible for
avoiding overflow.
This function is used during non-wide digit translation. Wide
digit translate produces one wide character per ASCII digit,
so the width is simply LAST - FIRST. */
int __translated_number_width (locale_t loc,
const char *first, const char *last)
attribute_hidden;
struct __printf_buffer;
void __printf_buffer (struct __printf_buffer *buf, const char *format,
va_list ap, unsigned int mode_flags);
struct __wprintf_buffer;
void __wprintf_buffer (struct __wprintf_buffer *buf, const wchar_t *format,
va_list ap, unsigned int mode_flags);
extern int __printf_fp (FILE *, const struct printf_info *,
const void *const *);
libc_hidden_proto (__printf_fp)
void __printf_fphex_l_buffer (struct __printf_buffer *, locale_t,
const struct printf_info *,
const void *const *) attribute_hidden;
void __printf_fp_l_buffer (struct __printf_buffer *, locale_t,
const struct printf_info *,
const void *const *) attribute_hidden;
struct __wprintf_buffer;
void __wprintf_fphex_l_buffer (struct __wprintf_buffer *, locale_t,
const struct printf_info *,
const void *const *) attribute_hidden;
void __wprintf_fp_l_buffer (struct __wprintf_buffer *, locale_t,
const struct printf_info *,
const void *const *) attribute_hidden;
# endif /* !_ISOMAC */
#endif