From patchwork Wed Sep 22 07:10:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 45274 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A9083858D39 for ; Wed, 22 Sep 2021 07:11:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A9083858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1632294663; bh=yJ+YvWQpyCa8yhSlzUkWgn7QQ0LVlu90hXdPrCOo004=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=vvxGJLpszwS2REip1qR1q/1M9MvppRfjFTkF0s4fvuJJNdt5nFKYJkRuOOPer3AKU /+TOe09JKMMUSBlBh/nUTUhlI8Hw2d+Qqw90xkrGhHOqqNcwTxLRl23dtSzwIlPSqU qK2t8mCto4fWVLmY7kxe7PGtf0x1Ex2Kg3byrMcA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 867703858422 for ; Wed, 22 Sep 2021 07:10:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 867703858422 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-207-Am8d9Pl-OeOYQ_VS8DmbXQ-1; Wed, 22 Sep 2021 03:10:28 -0400 X-MC-Unique: Am8d9Pl-OeOYQ_VS8DmbXQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E021E802C92 for ; Wed, 22 Sep 2021 07:10:27 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.176]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1200C60843 for ; Wed, 22 Sep 2021 07:10:26 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH 1/2] vfprintf: Handle floating-point cases outside of process_arg macro In-Reply-To: References: X-From-Line: 69b85ef47fc897703bb2c83a5fa54109a363e48b Mon Sep 17 00:00:00 2001 Message-Id: <69b85ef47fc897703bb2c83a5fa54109a363e48b.1632294469.git.fweimer@redhat.com> Date: Wed, 22 Sep 2021 09:10:25 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" A lot of the code is unique to the positional and non-positional code. Also unify the decimal and hexadecimal cases via the new helper function __printf_fp_spec. --- stdio-common/vfprintf-internal.c | 186 +++++++++++++------------------ 1 file changed, 75 insertions(+), 111 deletions(-) diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c index 3f3d1e148a..8e31a964d1 100644 --- a/stdio-common/vfprintf-internal.c +++ b/stdio-common/vfprintf-internal.c @@ -342,6 +342,18 @@ outstring_converted_wide_string (FILE *s, const OTHER_CHAR_T *src, int prec, return done; } +/* Calls __printf_fp or __printf_fphex based on the value of the + format specifier INFO->spec. */ +static inline int +__printf_fp_spec (FILE *fp, const struct printf_info *info, + const void *const *args) +{ + if (info->spec == 'a' || info->spec == 'A') + return __printf_fphex (fp, info, args); + else + return __printf_fp (fp, info, args); +} + /* For handling long_double and longlong we use the same flag. If `long' and `long long' are effectively the same type define it to zero. */ @@ -902,116 +914,6 @@ static const uint8_t jump_table[] = break; \ } \ \ - LABEL (form_float): \ - { \ - /* Floating-point number. This is handled by printf_fp.c. */ \ - const void *ptr; \ - int function_done; \ - \ - if (fspec == NULL) \ - { \ - if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \ - is_long_double = 0; \ - \ - struct printf_info info = { .prec = prec, \ - .width = width, \ - .spec = spec, \ - .is_long_double = is_long_double, \ - .is_short = is_short, \ - .is_long = is_long, \ - .alt = alt, \ - .space = space, \ - .left = left, \ - .showsign = showsign, \ - .group = group, \ - .pad = pad, \ - .extra = 0, \ - .i18n = use_outdigits, \ - .wide = sizeof (CHAR_T) != 1, \ - .is_binary128 = 0}; \ - \ - PARSE_FLOAT_VA_ARG_EXTENDED (info); \ - ptr = (const void *) &the_arg; \ - \ - function_done = __printf_fp (s, &info, &ptr); \ - } \ - else \ - { \ - ptr = (const void *) &args_value[fspec->data_arg]; \ - if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \ - { \ - fspec->data_arg_type = PA_DOUBLE; \ - fspec->info.is_long_double = 0; \ - } \ - SETUP_FLOAT128_INFO (fspec->info); \ - \ - function_done = __printf_fp (s, &fspec->info, &ptr); \ - } \ - \ - if (function_done < 0) \ - { \ - /* Error in print handler; up to handler to set errno. */ \ - done = -1; \ - goto all_done; \ - } \ - \ - done_add (function_done); \ - } \ - break; \ - \ - LABEL (form_floathex): \ - { \ - /* Floating point number printed as hexadecimal number. */ \ - const void *ptr; \ - int function_done; \ - \ - if (fspec == NULL) \ - { \ - if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \ - is_long_double = 0; \ - \ - struct printf_info info = { .prec = prec, \ - .width = width, \ - .spec = spec, \ - .is_long_double = is_long_double, \ - .is_short = is_short, \ - .is_long = is_long, \ - .alt = alt, \ - .space = space, \ - .left = left, \ - .showsign = showsign, \ - .group = group, \ - .pad = pad, \ - .extra = 0, \ - .wide = sizeof (CHAR_T) != 1, \ - .is_binary128 = 0}; \ - \ - PARSE_FLOAT_VA_ARG_EXTENDED (info); \ - ptr = (const void *) &the_arg; \ - \ - function_done = __printf_fphex (s, &info, &ptr); \ - } \ - else \ - { \ - ptr = (const void *) &args_value[fspec->data_arg]; \ - if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) \ - fspec->info.is_long_double = 0; \ - SETUP_FLOAT128_INFO (fspec->info); \ - \ - function_done = __printf_fphex (s, &fspec->info, &ptr); \ - } \ - \ - if (function_done < 0) \ - { \ - /* Error in print handler; up to handler to set errno. */ \ - done = -1; \ - goto all_done; \ - } \ - \ - done_add (function_done); \ - } \ - break; \ - \ LABEL (form_pointer): \ /* Generic pointer. */ \ { \ @@ -1646,6 +1548,45 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap, unsigned int mode_flags) process_arg (((struct printf_spec *) NULL)); process_string_arg (((struct printf_spec *) NULL)); + LABEL (form_float): + LABEL (form_floathex): + { + if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) + is_long_double = 0; + + struct printf_info info = + { + .prec = prec, + .width = width, + .spec = spec, + .is_long_double = is_long_double, + .is_short = is_short, + .is_long = is_long, + .alt = alt, + .space = space, + .left = left, + .showsign = showsign, + .group = group, + .pad = pad, + .extra = 0, + .i18n = use_outdigits, + .wide = sizeof (CHAR_T) != 1, + .is_binary128 = 0 + }; + + PARSE_FLOAT_VA_ARG_EXTENDED (info); + const void *ptr = &the_arg; + + int function_done = __printf_fp_spec (s, &info, &ptr); + if (function_done < 0) + { + done = -1; + goto all_done; + } + done_add (function_done); + } + break; + LABEL (form_unknown): if (spec == L_('\0')) { @@ -1901,7 +1842,6 @@ printf_positional (FILE *s, const CHAR_T *format, int readonly_format, unsigned long int word; } number; int base; - union printf_arg the_arg; CHAR_T *string; /* Pointer to argument string. */ /* Fill variables from values in struct. */ @@ -1996,6 +1936,30 @@ printf_positional (FILE *s, const CHAR_T *format, int readonly_format, process_arg ((&specs[nspecs_done])); process_string_arg ((&specs[nspecs_done])); + LABEL (form_float): + LABEL (form_floathex): + { + const void *ptr + = (const void *) &args_value[specs[nspecs_done].data_arg]; + if (__glibc_unlikely ((mode_flags & PRINTF_LDBL_IS_DBL) != 0)) + { + specs[nspecs_done].data_arg_type = PA_DOUBLE; + specs[nspecs_done].info.is_long_double = 0; + } + SETUP_FLOAT128_INFO (specs[nspecs_done].info); + + int function_done + = __printf_fp_spec (s, &specs[nspecs_done].info, &ptr); + if (function_done < 0) + { + /* Error in print handler; up to handler to set errno. */ + done = -1; + goto all_done; + } + done_add (function_done); + } + break; + LABEL (form_unknown): { unsigned int i;