From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 32047 invoked from network); 2 Jan 2024 21:08:17 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 2 Jan 2024 21:08:17 -0000 Received: (qmail 11342 invoked by uid 550); 2 Jan 2024 21:07:01 -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 11316 invoked from network); 2 Jan 2024 21:07:01 -0000 Date: Tue, 2 Jan 2024 16:08:16 -0500 From: Rich Felker To: Florian Weimer Cc: John M , musl@lists.openwall.com Message-ID: <20240102210816.GM4163@brightrain.aerifal.cx> References: <20240101134309.GN1427497@port70.net> <20240101174752.GL4163@brightrain.aerifal.cx> <87plyk11n4.fsf@oldenburg.str.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87plyk11n4.fsf@oldenburg.str.redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] Wrong rounding in printf when precision is not set to max 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