mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Peter Ammon <corydoras@ridiculousfish.com>
Cc: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] Fix printf hex float formatting with precision
Date: Fri, 12 Apr 2024 19:33:11 -0400	[thread overview]
Message-ID: <20240412233310.GF4163@brightrain.aerifal.cx> (raw)
In-Reply-To: <20240412195728.GE4163@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 900 bytes --]

On Fri, Apr 12, 2024 at 03:57:28PM -0400, Rich Felker wrote:
> On Thu, Apr 11, 2024 at 06:17:25PM -0700, Peter Ammon wrote:
> > if (p>=0 && p<(LDBL_MANT_DIG-1+3)/4) {
> >     int re = LDBL_MANT_DIG-1-(p*4);
> >     long double round = 1ULL<<re;
> 
> This expression overflows. re can be as large as 108 but 1ULL has
> 64-bit type.
> 
> There's probably some reasonable way to write it that works for
> reasonable sizes of long double (I think it's safe to assume IEEE quad
> is the max that will ever be supported), but I presume I wrote it with
> the original inefficient loop to be fully general to arbitrary
> precision.
> 
> It could just be written to use scalbn, I think. At the time I
> probably was trying to avoid dependency on a libm that could have been
> separate from libc (bad historical thing). IIRC there was a time when
> frexp was not used either.

Does the attached look ok?

Rich

[-- Attachment #2: pct_a.diff --]
[-- Type: text/plain, Size: 652 bytes --]

diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
index 497c5e19..dc648e7e 100644
--- a/src/stdio/vfprintf.c
+++ b/src/stdio/vfprintf.c
@@ -211,18 +211,11 @@ static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
 	if (y) e2--;
 
 	if ((t|32)=='a') {
-		long double round = 8.0;
-		int re;
-
 		if (t&32) prefix += 9;
 		pl += 2;
 
-		if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
-		else re=LDBL_MANT_DIG/4-1-p;
-
-		if (re) {
-			round *= 1<<(LDBL_MANT_DIG%4);
-			while (re--) round*=16;
+		if (p>=0 && p<(LDBL_MANT_DIG-1+3)/4) {
+			double round = scalbn(1, LDBL_MANT_DIG-1-(p*4));
 			if (*prefix=='-') {
 				y=-y;
 				y-=round;

  reply	other threads:[~2024-04-12 23:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-12  1:17 Peter Ammon
2024-04-12  2:34 ` Rich Felker
2024-04-12 19:57 ` Rich Felker
2024-04-12 23:33   ` Rich Felker [this message]
2024-04-13 16:00     ` Peter Ammon

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=20240412233310.GF4163@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=corydoras@ridiculousfish.com \
    --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).