public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] MIPS: Fix wrong MSA FP vector negation
@ 2024-02-04 18:00 Xi Ruoyao
  2024-02-05  1:56 ` YunQiang Su
  0 siblings, 1 reply; 3+ messages in thread
From: Xi Ruoyao @ 2024-02-04 18:00 UTC (permalink / raw)
  To: gcc-patches; +Cc: YunQiang Su, Jeff Law, Xi Ruoyao

We expanded (neg x) to (minus const0 x) for MSA FP vectors, this is
wrong because -0.0 is not 0 - 0.0.  This causes some Python tests to
fail when Python is built with MSA enabled.

Use the bnegi.df instructions to simply reverse the sign bit instead.

gcc/ChangeLog:

	* config/mips/mips-msa.md (elmsgnbit): New define_mode_attr.
	(neg<mode>2): Change the mode iterator from MSA to IMSA because
	in FP arithmetic we cannot use (0 - x) for -x.
	(neg<mode>2): New define_insn to implement FP vector negation,
	using a bnegi instruction to negate the sign bit.
---

Bootstrapped and regtested on mips64el-linux-gnuabi64.  Ok for trunk
and/or release branches?

 gcc/config/mips/mips-msa.md | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/gcc/config/mips/mips-msa.md b/gcc/config/mips/mips-msa.md
index 83d9a08e360..920161ed1d8 100644
--- a/gcc/config/mips/mips-msa.md
+++ b/gcc/config/mips/mips-msa.md
@@ -231,6 +231,10 @@ (define_mode_attr bitimm
    (V4SI  "uimm5")
    (V2DI  "uimm6")])
 
+;; The index of sign bit in FP vector elements.
+(define_mode_attr elmsgnbit [(V2DF "63") (V4DF "63")
+			     (V4SF "31") (V8SF "31")])
+
 (define_expand "vec_init<mode><unitmode>"
   [(match_operand:MSA 0 "register_operand")
    (match_operand:MSA 1 "")]
@@ -597,9 +601,9 @@ (define_expand "abs<mode>2"
 })
 
 (define_expand "neg<mode>2"
-  [(set (match_operand:MSA 0 "register_operand")
-	(minus:MSA (match_dup 2)
-		   (match_operand:MSA 1 "register_operand")))]
+  [(set (match_operand:IMSA 0 "register_operand")
+	(minus:IMSA (match_dup 2)
+		   (match_operand:IMSA 1 "register_operand")))]
   "ISA_HAS_MSA"
 {
   rtx reg = gen_reg_rtx (<MODE>mode);
@@ -607,6 +611,14 @@ (define_expand "neg<mode>2"
   operands[2] = reg;
 })
 
+(define_insn "neg<mode>2"
+  [(set (match_operand:FMSA 0 "register_operand" "=f")
+	(neg (match_operand:FMSA 1 "register_operand" "f")))]
+  "ISA_HAS_MSA"
+  "bnegi.<msafmt>\t%w0,%w1,<elmsgnbit>"
+  [(set_attr "type" "simd_bit")
+   (set_attr "mode" "<MODE>")])
+
 (define_expand "msa_ldi<mode>"
   [(match_operand:IMSA 0 "register_operand")
    (match_operand 1 "const_imm10_operand")]
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] MIPS: Fix wrong MSA FP vector negation
  2024-02-04 18:00 [PATCH] MIPS: Fix wrong MSA FP vector negation Xi Ruoyao
@ 2024-02-05  1:56 ` YunQiang Su
  2024-02-05 12:25   ` Pushed: " Xi Ruoyao
  0 siblings, 1 reply; 3+ messages in thread
From: YunQiang Su @ 2024-02-05  1:56 UTC (permalink / raw)
  To: Xi Ruoyao; +Cc: gcc-patches, Jeff Law

Xi Ruoyao <xry111@xry111.site> 于2024年2月5日周一 02:01写道:
>
> We expanded (neg x) to (minus const0 x) for MSA FP vectors, this is
> wrong because -0.0 is not 0 - 0.0.  This causes some Python tests to
> fail when Python is built with MSA enabled.
>
> Use the bnegi.df instructions to simply reverse the sign bit instead.
>
> gcc/ChangeLog:
>
>         * config/mips/mips-msa.md (elmsgnbit): New define_mode_attr.
>         (neg<mode>2): Change the mode iterator from MSA to IMSA because
>         in FP arithmetic we cannot use (0 - x) for -x.
>         (neg<mode>2): New define_insn to implement FP vector negation,
>         using a bnegi instruction to negate the sign bit.
> ---
>
> Bootstrapped and regtested on mips64el-linux-gnuabi64.  Ok for trunk
> and/or release branches?
>
>  gcc/config/mips/mips-msa.md | 18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)
>

LGTM, while I guess that we also need a test case.

> diff --git a/gcc/config/mips/mips-msa.md b/gcc/config/mips/mips-msa.md
> index 83d9a08e360..920161ed1d8 100644
> --- a/gcc/config/mips/mips-msa.md
> +++ b/gcc/config/mips/mips-msa.md
> @@ -231,6 +231,10 @@ (define_mode_attr bitimm
>     (V4SI  "uimm5")
>     (V2DI  "uimm6")])
>
> +;; The index of sign bit in FP vector elements.
> +(define_mode_attr elmsgnbit [(V2DF "63") (V4DF "63")
> +                            (V4SF "31") (V8SF "31")])
> +
>  (define_expand "vec_init<mode><unitmode>"
>    [(match_operand:MSA 0 "register_operand")
>     (match_operand:MSA 1 "")]
> @@ -597,9 +601,9 @@ (define_expand "abs<mode>2"
>  })
>
>  (define_expand "neg<mode>2"
> -  [(set (match_operand:MSA 0 "register_operand")
> -       (minus:MSA (match_dup 2)
> -                  (match_operand:MSA 1 "register_operand")))]
> +  [(set (match_operand:IMSA 0 "register_operand")
> +       (minus:IMSA (match_dup 2)
> +                  (match_operand:IMSA 1 "register_operand")))]
>    "ISA_HAS_MSA"
>  {
>    rtx reg = gen_reg_rtx (<MODE>mode);
> @@ -607,6 +611,14 @@ (define_expand "neg<mode>2"
>    operands[2] = reg;
>  })
>
> +(define_insn "neg<mode>2"
> +  [(set (match_operand:FMSA 0 "register_operand" "=f")
> +       (neg (match_operand:FMSA 1 "register_operand" "f")))]
> +  "ISA_HAS_MSA"
> +  "bnegi.<msafmt>\t%w0,%w1,<elmsgnbit>"
> +  [(set_attr "type" "simd_bit")
> +   (set_attr "mode" "<MODE>")])
> +
>  (define_expand "msa_ldi<mode>"
>    [(match_operand:IMSA 0 "register_operand")
>     (match_operand 1 "const_imm10_operand")]
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Pushed: [PATCH] MIPS: Fix wrong MSA FP vector negation
  2024-02-05  1:56 ` YunQiang Su
@ 2024-02-05 12:25   ` Xi Ruoyao
  0 siblings, 0 replies; 3+ messages in thread
From: Xi Ruoyao @ 2024-02-05 12:25 UTC (permalink / raw)
  To: YunQiang Su; +Cc: gcc-patches, Jeff Law

On Mon, 2024-02-05 at 09:56 +0800, YunQiang Su wrote:
> Xi Ruoyao <xry111@xry111.site> 于2024年2月5日周一 02:01写道:
> > 
> > We expanded (neg x) to (minus const0 x) for MSA FP vectors, this is
> > wrong because -0.0 is not 0 - 0.0.  This causes some Python tests to
> > fail when Python is built with MSA enabled.
> > 
> > Use the bnegi.df instructions to simply reverse the sign bit instead.
> > 
> > gcc/ChangeLog:
> > 
> >          * config/mips/mips-msa.md (elmsgnbit): New define_mode_attr.
> >          (neg<mode>2): Change the mode iterator from MSA to IMSA because
> >          in FP arithmetic we cannot use (0 - x) for -x.
> >          (neg<mode>2): New define_insn to implement FP vector negation,
> >          using a bnegi instruction to negate the sign bit.
> > ---
> > 
> > Bootstrapped and regtested on mips64el-linux-gnuabi64.  Ok for trunk
> > and/or release branches?
> > 
> >   gcc/config/mips/mips-msa.md | 18 +++++++++++++++---
> >   1 file changed, 15 insertions(+), 3 deletions(-)
> > 
> 
> LGTM, while I guess that we also need a test case.

Pushed to trunk and release branches, with a following obvious fix:

diff --git a/gcc/config/mips/mips-msa.md b/gcc/config/mips/mips-msa.md
index 920161ed1d8..779157f2a0c 100644
--- a/gcc/config/mips/mips-msa.md
+++ b/gcc/config/mips/mips-msa.md
@@ -613,7 +613,7 @@ (define_expand "neg<mode>2"
 
 (define_insn "neg<mode>2"
   [(set (match_operand:FMSA 0 "register_operand" "=f")
-       (neg (match_operand:FMSA 1 "register_operand" "f")))]
+       (neg:FMSA (match_operand:FMSA 1 "register_operand" "f")))]
   "ISA_HAS_MSA"
   "bnegi.<msafmt>\t%w0,%w1,<elmsgnbit>"
   [(set_attr "type" "simd_bit")

I'll write a test case for gcc.dg/vect later (now I have to do
$SOME_REAL_LIFE_THING...)

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-02-05 12:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-04 18:00 [PATCH] MIPS: Fix wrong MSA FP vector negation Xi Ruoyao
2024-02-05  1:56 ` YunQiang Su
2024-02-05 12:25   ` Pushed: " Xi Ruoyao

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).