From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: from second.openwall.net (second.openwall.net [193.110.157.125]) by inbox.vuxu.org (Postfix) with SMTP id 13A62216A8 for ; Sat, 13 Apr 2024 01:33:06 +0200 (CEST) Received: (qmail 9224 invoked by uid 550); 12 Apr 2024 23:33:00 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 8149 invoked from network); 12 Apr 2024 23:32:59 -0000 Date: Fri, 12 Apr 2024 19:33:11 -0400 From: Rich Felker To: Peter Ammon Cc: musl@lists.openwall.com Message-ID: <20240412233310.GF4163@brightrain.aerifal.cx> References: <20240412195728.GE4163@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="bqPh76xD3yWylqqJ" Content-Disposition: inline In-Reply-To: <20240412195728.GE4163@brightrain.aerifal.cx> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] [PATCH] Fix printf hex float formatting with precision --bqPh76xD3yWylqqJ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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< > 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 --bqPh76xD3yWylqqJ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="pct_a.diff" 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; --bqPh76xD3yWylqqJ--