mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Jens Gustedt <Jens.Gustedt@inria.fr>
Cc: musl@lists.openwall.com
Subject: Re: [musl] [C23 printf 3/3] C23: implement the wfN length modifiers for printf
Date: Fri, 26 May 2023 16:33:59 -0400	[thread overview]
Message-ID: <20230526203358.GO4163@brightrain.aerifal.cx> (raw)
In-Reply-To: <b3bd20b161442150ed0e72f98858c5e2cb83625a.1685129854.git.Jens.Gustedt@inria.fr>

On Fri, May 26, 2023 at 09:41:04PM +0200, Jens Gustedt wrote:
> Musl only has a difference between fixed-width and fastest-width
> integer types for N == 16. And even here all architectures have made
> the same choice, namely mapping to 32 bit types.
> ---
>  src/stdio/vfprintf.c  | 9 ++++++++-
>  src/stdio/vfwprintf.c | 9 ++++++++-
>  2 files changed, 16 insertions(+), 2 deletions(-)
> 
> diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
> index 1a516663..9f02a594 100644
> --- a/src/stdio/vfprintf.c
> +++ b/src/stdio/vfprintf.c
> @@ -529,9 +529,16 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg,
>  			ps=st;
>  			st=states[st]S(*s++);
>  			if (st == WPRE) {
> +				// See if "fast" is requested. Difference is only
> +				// relevant for a fast type of minimum width 16.
> +				int fast16 = HPRE;
> +				if (*s == 'f') {
> +					fast16 = BARE;
> +					++s;
> +				}
>  				switch (getint(&s)) {
>  				case 8:  st = HHPRE; goto wpre;
> -				case 16: st = HPRE; goto wpre;
> +				case 16: st = fast16; goto wpre;
>  				case 32: st = BARE; goto wpre;
>  #if UINTPTR_MAX >= UINT64_MAX
>  				case 64: st = LPRE; goto wpre;
> diff --git a/src/stdio/vfwprintf.c b/src/stdio/vfwprintf.c
> index 4320761a..4e9ce63a 100644
> --- a/src/stdio/vfwprintf.c
> +++ b/src/stdio/vfwprintf.c
> @@ -244,9 +244,16 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
>  			ps=st;
>  			st=states[st]S(*s++);
>  			if (st == WPRE) {
> +				// See if "fast" is requested. Difference is only
> +				// relevant for a fast type of minimum width 16.
> +				int fast16 = HPRE;
> +				if (*s == 'f') {
> +					fast16 = BARE;
> +					++s;
> +				}
>  				switch (getint(&s)) {
>  				case 8:  st = HHPRE; goto wpre;
> -				case 16: st = HPRE; goto wpre;
> +				case 16: st = fast16; goto wpre;
>  				case 32: st = BARE; goto wpre;
>  #if UINTPTR_MAX >= UINT64_MAX
>  				case 64: st = LPRE; goto wpre;
> -- 
> 2.34.1

It may just work to have w8, w16, wf8, and wf16 all resolve to BARE
unconditionally, as the default promitions for variadic functions
force these all to be passed as int. In the current code, for h and hh
prefixes we cast down and discard any high bits, but unless the caller
invoked UB by passing an argument with mismatched type, the cast is
guaranteed not to change the value.

Rich

  reply	other threads:[~2023-05-26 20:34 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-26 19:41 [musl] [C23 printf 0/3] Jens Gustedt
2023-05-26 19:41 ` [musl] [C23 printf 1/3] C23: implement the b and B printf specifiers Jens Gustedt
2023-05-26 19:41 ` [musl] [C23 printf 2/3] C23: implement the wN length specifiers for printf Jens Gustedt
2023-05-26 20:31   ` Rich Felker
2023-05-26 20:51     ` Jₑₙₛ Gustedt
2023-05-26 21:03       ` Rich Felker
2023-05-29  7:14         ` Jₑₙₛ Gustedt
2023-05-29 15:46           ` Rich Felker
2023-05-29 19:21             ` Jₑₙₛ Gustedt
2023-05-30  1:48               ` Rich Felker
2023-05-30  6:46                 ` Jₑₙₛ Gustedt
2023-05-30 17:28                   ` Rich Felker
2023-05-30 18:00                     ` Rich Felker
2023-05-31  7:59                     ` Jₑₙₛ Gustedt
2023-06-02 14:29                 ` Rich Felker
2023-06-02 14:55                   ` Jₑₙₛ Gustedt
2023-06-02 15:07                     ` Rich Felker
2023-05-26 19:41 ` [musl] [C23 printf 3/3] C23: implement the wfN length modifiers " Jens Gustedt
2023-05-26 20:33   ` Rich Felker [this message]
2023-05-26 21:00     ` Jₑₙₛ Gustedt
     [not found] <cover.1685429467.git.Jens.Gustedt@inria.fr>
2023-05-30  6:55 ` Jens Gustedt
2023-05-31 14:04 [musl] [C23 printf 0/3] to be replaced Jens Gustedt
2023-05-31 14:04 ` [musl] [C23 printf 3/3] C23: implement the wfN length modifiers for printf Jens Gustedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230526203358.GO4163@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=Jens.Gustedt@inria.fr \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).