public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "joseph at codesourcery dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libquadmath/105101] incorrect rounding for sqrtq
Date: Fri, 10 Jun 2022 17:40:25 +0000	[thread overview]
Message-ID: <bug-105101-4-MP7v6EQkJR@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105101-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105101

--- Comment #18 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
libquadmath is essentially legacy code.  People working directly in C 
should be using the C23 _Float128 interfaces and *f128 functions, as in 
current glibc, rather than libquadmath interfaces (unless their code needs 
to support old glibc or non-glibc C libraries that don't support _Float128 
in C23 Annex H).  It would be desirable to make GCC generate *f128 calls 
when appropriate from Fortran code using this format as well; see 
<https://gcc.gnu.org/pipermail/gcc-patches/2021-September/578937.html> for 
more discussion of the different cases involved.

Most of libquadmath is derived from code in glibc - some of it can now be 
updated from the glibc code automatically (see update-quadmath.py), other 
parts can't (although it would certainly be desirable to extend 
update-quadmath.py to cover that other code as well).  See the commit 
message for commit 4239f144ce50c94f2c6cc232028f167b6ebfd506 for a more 
detailed discussion of what code comes from glibc and what is / is not 
automatically handled by update-quadmath.py.  Since update-quadmath.py 
hasn't been run for a while, it might need changes to work with more 
recent changes to the glibc code.

sqrtq.c is one of the files not based on glibc code.  That's probably 
because glibc didn't have a convenient generic implementation of binary128 
sqrt to use when libquadmath was added - it has soft-fp implementations 
used for various architectures, but those require sfp-machine.h for each 
architecture (which maybe we do in fact have in libgcc for each relevant 
architecture, but it's an extra complication).  Certainly making it 
possible to use code from glibc for binary128 sqrt would be a good idea, 
but while we aren't doing that, it should also be OK to improve sqrtq 
locally in libquadmath.

The glibc functions for this format are generally *not* optimized for 
speed yet (this includes the soft-fp-based versions of sqrt).  Note that 
what's best for speed may depend a lot on whether the architecture has 
hardware support for binary128 arithmetic; if it has such support, it's 
more likely an implementation based on binary128 floating-point operations 
is efficient; if it doesn't, direct use of integer arithmetic, without 
lots of intermediate packing / unpacking into the binary128 format, is 
likely to be more efficient.  See the discussion starting at 
<https://sourceware.org/pipermail/libc-alpha/2020-June/thread.html#115229> 
for more on this - glibc is a better place for working on most optimized 
function implementations than GCC.  See also 
<https://core-math.gitlabpages.inria.fr/> - those functions are aiming to 
be correctly rounding, which is *not* a goal for most glibc libm 
functions, but are still quite likely to be faster than the existing 
non-optimized functions in glibc.

fma is a particularly tricky case because it *is* required to be correctly 
rounding, in all rounding modes, and correct rounding implies correct 
exceptions, *and* correct exceptions for fma includes getting right the 
architecture-specific choice of whether tininess is detected before or 
after rounding.

Correct exceptions for sqrt are simpler, but to be correct for glibc it 
still needs to avoid spurious "inexact" exceptions - for example, from the 
use of double in intermediate computations in your version (see the 
optimized feholdexcept / fesetenv operations used in glibc for cases where 
exceptions from intermediate computations are to be discarded).

For functions that aren't required to be correctly rounding, the glibc 
manual discusses the accuracy goals (including on exceptions, e.g. 
avoiding spurious "underflow" exceptions from intermediate computations 
for results where the rounded result returned is not consistent with 
rounding a tiny, inexact value).

  parent reply	other threads:[~2022-06-10 17:40 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-29 19:44 [Bug libquadmath/105101] New: " tkoenig at gcc dot gnu.org
2022-03-30 11:34 ` [Bug libquadmath/105101] " fxcoudert at gcc dot gnu.org
2022-03-30 12:15 ` jakub at gcc dot gnu.org
2022-03-30 16:28 ` kargl at gcc dot gnu.org
2022-03-30 16:31 ` kargl at gcc dot gnu.org
2022-04-02 19:33 ` already5chosen at yahoo dot com
2022-04-09 10:09 ` tkoenig at gcc dot gnu.org
2022-04-09 10:23 ` jakub at gcc dot gnu.org
2022-04-09 10:25 ` tkoenig at gcc dot gnu.org
2022-04-09 15:50 ` sgk at troutmask dot apl.washington.edu
2022-04-15 13:49 ` already5chosen at yahoo dot com
2022-04-16 22:54 ` already5chosen at yahoo dot com
2022-04-18 23:57 ` already5chosen at yahoo dot com
2022-04-20 12:57 ` already5chosen at yahoo dot com
2022-04-21 15:09 ` already5chosen at yahoo dot com
2022-06-09  7:52 ` tkoenig at gcc dot gnu.org
2022-06-09  8:03 ` jakub at gcc dot gnu.org
2022-06-10 16:16 ` already5chosen at yahoo dot com
2022-06-10 16:18 ` already5chosen at yahoo dot com
2022-06-10 17:40 ` joseph at codesourcery dot com [this message]
2022-06-11 21:15 ` already5chosen at yahoo dot com
2022-06-13 17:59 ` joseph at codesourcery dot com
2022-06-13 20:05 ` already5chosen at yahoo dot com
2022-06-13 20:20 ` joseph at codesourcery dot com
2022-06-13 20:56 ` already5chosen at yahoo dot com
2022-06-13 22:34 ` joseph at codesourcery dot com

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=bug-105101-4-MP7v6EQkJR@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).