mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Florian Weimer <fweimer@redhat.com>
Cc: John M <johnm@braincalibration.de>, musl@lists.openwall.com
Subject: Re: [musl] Wrong rounding in printf when precision is not set to max
Date: Tue, 2 Jan 2024 16:08:16 -0500	[thread overview]
Message-ID: <20240102210816.GM4163@brightrain.aerifal.cx> (raw)
In-Reply-To: <87plyk11n4.fsf@oldenburg.str.redhat.com>

On Tue, Jan 02, 2024 at 11:16:15AM +0100, Florian Weimer wrote:
> * Rich Felker:
> 
> > Note that, even then, there are lots of gotchas. The x87's "single"
> > and "double" modes are not actually IEEE single and double, but
> > nonstandard types with the IEEE significand precision but excess
> > exponent range that's truncated via double-rounding whenever the
> > intermediate is spilled to memory.
> 
> Doesn't setting the control word avoid double rounding?

No. The first rounding is of the exact mathematical result to
x87-single (23-bit significand, 16-bit exponent) or x87-double (52-bit
significand, 16-bit exponent), then there's a second rounding on store
to IEEE single or IEEE double. The result differs on overflow or
underflow. For example:

- a+b-c (including special case a+b-b) can be finite in x87-double
  (even after final store), but infinite in IEEE double.

- a+b-c (including special case a+b-b) can be exact in x87-double, but
  incur rounding in IEEE double due to a+b becoming denormal.

If you can be certain no intermediates will ever overflow or underflow
(and no denormals appear in inputs), then I believe x87 reduced
precision modes can faithfully emulate IEEE arithmetic. But this is a
nontrivial condition to satisfy in general.

Rich

      reply	other threads:[~2024-01-02 21:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-01  9:11 John M
2024-01-01 13:43 ` Szabolcs Nagy
2024-01-01 17:47   ` Rich Felker
2024-01-02 10:16     ` Florian Weimer
2024-01-02 21:08       ` Rich Felker [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=20240102210816.GM4163@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=fweimer@redhat.com \
    --cc=johnm@braincalibration.de \
    --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).