From: Markus Wichmann <nullplan@gmx.net>
To: musl@lists.openwall.com
Cc: Michal Terepeta <michalt@google.com>,
t5y-external <t5y-external@google.com>
Subject: Re: [musl] Wrong formatting of doubles?
Date: Thu, 3 Jul 2025 06:31:07 +0200 [thread overview]
Message-ID: <aGYHi-GMMiYari-w@voyager> (raw)
In-Reply-To: <20250702145823.GJ1827@brightrain.aerifal.cx>
Am Wed, Jul 02, 2025 at 10:58:23AM -0400 schrieb Rich Felker:
> On Tue, Jul 01, 2025 at 09:46:10PM -0400, Rich Felker wrote:
> > static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t, int ps)
> > {
> > - int bufsize = (ps==BIGLPRE)
> > - ? (LDBL_MANT_DIG+28)/29 + 1 + // mantissa expansion
> > - (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9 // exponent expansion
> > - : (DBL_MANT_DIG+28)/29 + 1 +
> > - (DBL_MAX_EXP+DBL_MANT_DIG+28+8)/9;
> > + int max_mant_dig = (ps==BIGLPRE) ? LDBL_MANT_DIG : DBL_MANT_DIG;
> > + int max_exp = (ps==BIGLPRE) ? LDBL_MAX_EXP : DBL_MAX_EXP;
> > + int max_mant_slots = (max_mant_dig+28)/29 + 1;
> > + int max_exp_slots = (max_exp+max_mant_dig+28+8)/9;
> > + int bufsize = max_mant_slots + max_exp_slots;
> > uint32_t big[bufsize];
> > uint32_t *a, *d, *r, *z;
> > int e2=0, e, i, j, l;
> > @@ -266,7 +266,7 @@ static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t, int ps)
> > if (y) y *= 0x1p28, e2-=28;
> >
> > if (e2<0) a=r=z=big;
> > - else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
> > + else a=r=z=big+sizeof(big)/sizeof(*big) - max_mant_slots - 1;
> >
> > do {
> > *z = y;
> > --
> > 2.21.0
> >
>
> While nothing should change in the case of negative exponents, I also
> did an empirical check with DBL_TRUE_MIN to check the space for
> denormals, and we have 5 slots to spare.
That tracks with my calculations. I would just look at it from the
result: The buffer only needs to be big enough to contain the largest
end result, right? The largest decimal expansion occurs at TRUE_MIN,
which is 2^(MIN_EXP - MANT_DIG), which has MANT_DIG - MIN_EXP decimal
places. So you need enough slots for that many digits, +1 for the
pre-radix slot you always have, so in the end
bufsize = (mant_dig - min_exp + 8)/9 + 1
And that results in a little bit less than what you calculate.
type | yours | mine
-----|-------|-----
ld64 | 126 | 121
ld80 | 1835 | 1828
ld128| 1842 | 1833
>
> The multiplication by 0x1p28 above is handled by the +28 in the
> expression for max_exp_slots.
>
See, that is an intermediate result. The multiplication is not big
enough to make the mantissa expansion require more pre-radix slots, so
it doesn't change how much space is needed. It only changes how many
slots the mantissa expansion might take, which is not important to
printing TRUE_MIN. It is important to printing numbers with positive
exponents, but only for keeping the right distance from the end of the
buffer.
Ciao,
Markus
prev parent reply other threads:[~2025-07-03 4:31 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-01 5:55 Wrong formatting of doubles? Michal Terepeta
2025-07-01 14:22 ` [musl] " Rich Felker
2025-07-01 16:19 ` Rich Felker
2025-07-01 16:37 ` Rich Felker
2025-07-01 17:02 ` Jeffrey Walton
2025-07-01 17:22 ` [musl] " Markus Wichmann
2025-07-01 17:34 ` Rich Felker
2025-07-02 1:46 ` Rich Felker
2025-07-02 7:56 ` [musl] " Michal Terepeta
2025-07-08 7:43 ` Michal Terepeta
2025-07-08 16:22 ` [musl] " Rich Felker
2025-07-02 14:58 ` Rich Felker
2025-07-03 4:31 ` Markus Wichmann [this message]
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=aGYHi-GMMiYari-w@voyager \
--to=nullplan@gmx.net \
--cc=michalt@google.com \
--cc=musl@lists.openwall.com \
--cc=t5y-external@google.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).