public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: Junxian Zhu <zhujunxian@oss.cipunited.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 2/2] MIPS: Hard-float rounding instructions support
Date: Wed, 31 Jan 2024 17:08:36 +0800	[thread overview]
Message-ID: <7d6ec9e35a9bbf55df7ba2ae0b228a30ed6349b7.camel@xry111.site> (raw)
In-Reply-To: <5f0f4c83-f6d8-4af3-8cce-e12cd5314da1@oss.cipunited.com>

On Wed, 2024-01-31 at 11:28 +0800, Junxian Zhu wrote:
> 在 2023/12/26 16:29, Xi Ruoyao 写道:
> > On Tue, 2023-12-26 at 10:37 +0800, Junxian Zhu wrote:
> > > 在 2023/12/25 18:51, Xi Ruoyao 写道:
> > > > On Mon, 2023-12-25 at 18:35 +0800, Junxian Zhu wrote:
> > > > 
> > > > /* snip */
> > > > 
> > > > > +/*
> > > > > + * ceil(x)
> > > > > + * Return x rounded toward -inf to integral value
> > > > > + * Method:
> > > > > + *	Bit twiddling.
> > > > > + */
> > > > > +
> > > > > +#if ((__mips_fpr == 64) && (__mips_hard_float == 1) &&
> > > > > ((__mips == 32 && __mips_isa_rev > 1) || __mips == 64))
> > > > > +#include <sys/regdef.h>
> > > > > +#include <sysdep.h>
> > > > > +#include <libm-alias-double.h>
> > > > > +
> > > > > +ENTRY(__ceil)
> > > > > +	.set push
> > > > > +	.set noreorder
> > > > > +	.set noat
> > > > > +# $f0=ret, $f12=double, a0=int64/int32_h, a1=int32_l,
> > > > > a2=sign, a3=exp
> > > > > +#if __mips == 64
> > > > > +	dmfc1   a0, $f12 # assign int64
> > > > > +#else
> > > > > +	mfhc1   a0, $f12 # assign int64
> > > > > +#endif
> > > > > +	cfc1    t0, $f26
> > > > > +	ceil.l.d    $f0, $f12
> > > > No, C23 does not allow this function to raise an INEXACT
> > > > exception, but
> > > > ceil.l.d will do so.
> > > > 
> > > > Such optimizations should be performed in GCC which can be
> > > > controlled by
> > > > the programmer with -std=c23 and/or -f[no-]fp-int-builtin-
> > > > inexact, not
> > > > in Glibc where we cannot know if the programmer wants to deviate
> > > > from
> > > > C23.
> > > The cfc1 instruction will backup float point exception status
> > > before
> > > running ceil.l.d, and the following ctc1 will restore float point
> > > exception status to avoid INEXACT exception raised by ceil.l.d.
> > > It's the
> > > same way like what have been done in s_ceil.S for i386.
> > Still incorrect because when the Enable field of FCSR contains
> > INEXACT a
> > SIGFPE will be immediately delivered and there is no way to
> > recover.  A
> > demonstration:
> > 
> > #define _GNU_SOURCE
> > #include <stdio.h>
> > #include <fenv.h>
> > 
> > int main()
> > {
> >    printf("%d\n", feenableexcept(FE_INEXACT));
> > 
> >    double data = 114.514;
> >    long control;
> >    asm("cfc1\t%1,$f26\n\t"
> >        "ceil.l.d\t%0,%0\n\t"
> >        "cvt.d.l\t%0,%0\n\t"
> >        "ctc1\t%1,$f26": "+f"(data), "=r"(control));
> >    printf("%.15f\n", data);
> >    return 0;
> > }
> > 
> > On i386 the fnstenv instruction also masks out all the FP exceptions
> > so
> > this is not a problem.  See commit 26b0bf96000a.
> 
> I made some similar code to test and found that sqrt.fmt on MIPS will 
> raise INEXACT exception too.
> 
> So should we mask all the FP exceptions while compiling MIPS 
> SQRT{F}_BUILTIN in GCC ? (for C23)

Why?

    volatile double x = 2;
    sqrt(x); 

should definitely raise an INEXACT exception.  Unless explicitly stated
otherwise, all operations producing a result different than the
"infinite precision operation" should do it.  For the conversion
operations they are just "explicitly stated otherwise."

But if sqrt.fmt is raising it for

    volatile double x = 4;
    sqrt(x); 

it's obviously wrong, and I'd say such an exception is nonsense even
regardless of the standard so we should just not use this instruction.

Something not so obvious is: the standard explicitly says

    volatile double x = __builtin_inf();
    sqrt(x); 

should **not** raise an INEXACT exception.  So if you are raising
INEXACT for this you should only use sqrt.fmt when you can prove the
operand is finite (for example when -ffinite-math-only, or using some
information from the VRP pass).

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

  parent reply	other threads:[~2024-01-31  9:08 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-25 10:35 [PATCH 0/2] Add hard-float rounding instructions support for MIPS architecture Junxian Zhu
2023-12-25 10:35 ` [PATCH 1/2] limb-alias-double.h: Fix missing semicolon Junxian Zhu
2023-12-25 10:35 ` [PATCH 2/2] MIPS: Hard-float rounding instructions support Junxian Zhu
2023-12-25 10:51   ` Xi Ruoyao
2023-12-26  2:37     ` Junxian Zhu
2023-12-26  8:29       ` Xi Ruoyao
2023-12-26 20:12         ` Adhemerval Zanella Netto
2023-12-26 21:50           ` Xi Ruoyao
2023-12-26 22:50             ` Xi Ruoyao
2023-12-27 13:25               ` Adhemerval Zanella Netto
2024-01-02 10:08               ` Junxian Zhu
2024-01-02  9:43         ` Junxian Zhu
2024-01-02  9:57           ` Xi Ruoyao
     [not found]         ` <5f0f4c83-f6d8-4af3-8cce-e12cd5314da1@oss.cipunited.com>
2024-01-31  9:08           ` Xi Ruoyao [this message]
2024-01-25 13:58     ` Junxian Zhu
2024-01-25 14:37       ` Xi Ruoyao
2023-12-25 12:36   ` YunQiang Su
2023-12-26  2:48     ` Junxian Zhu
2024-01-02  9:51     ` Junxian Zhu
2023-12-29  1:00   ` Joseph Myers

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=7d6ec9e35a9bbf55df7ba2ae0b228a30ed6349b7.camel@xry111.site \
    --to=xry111@xry111.site \
    --cc=libc-alpha@sourceware.org \
    --cc=zhujunxian@oss.cipunited.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.
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).