With -fno-trapping-math we can tolerate the spurious floating point
exceptions so we don't need to care the additional elements.
gcc/
* config/loongarch/loongarch-protos.h
(loongarch_adjust_scalar_input_for_simd): New prototype.
* config/loongarch/loongarch.cc
(loongarch_adjust_scalar_input_for_simd): Implement.
* config/loongarch/simd.md (<simd_frint_pattern><mode>2): Use
subreg instead of broadcasting the element if
-fno-trapping-math.
gcc/testsuite/
* gcc.target/loongarch/roundeven-notrap.c: New test.
---
gcc/config/loongarch/loongarch-protos.h | 1 +
gcc/config/loongarch/loongarch.cc | 15 +++++++++++++
gcc/config/loongarch/simd.md | 21 ++++++++++++-------
.../gcc.target/loongarch/roundeven-notrap.c | 14 +++++++++++++
4 files changed, 43 insertions(+), 8 deletions(-)
create mode 100644 gcc/testsuite/gcc.target/loongarch/roundeven-notrap.c
diff --git a/gcc/config/loongarch/loongarch-protos.h
b/gcc/config/loongarch/loongarch-protos.h
index 0289409226f..52aec1fd1b7 100644
--- a/gcc/config/loongarch/loongarch-protos.h
+++ b/gcc/config/loongarch/loongarch-protos.h
@@ -236,4 +236,5 @@ extern void get_feature_mask_for_version (tree,
loongarch_fmv_feature_mask *,
auto_vec<unsigned int> *);
extern int loongarch_compare_version_priority (tree, tree);
extern void loongarch_output_asm_load_canary (rtx reg, rtx canary, rtx tmp);
+extern rtx loongarch_adjust_scalar_input_for_simd (rtx op, machine_mode);
#endif /* ! GCC_LOONGARCH_PROTOS_H */
diff --git a/gcc/config/loongarch/loongarch.cc
b/gcc/config/loongarch/loongarch.cc
index 55c9e59bdf4..7789b0b964c 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -12244,6 +12244,21 @@ loongarch_output_asm_load_canary (rtx reg, rtx canary,
rtx tmp)
output_asm_insn ("ld.%v0\t%0,%0,0", op);
}
+rtx
+loongarch_adjust_scalar_input_for_simd (rtx op, machine_mode vmode)
+{
+ machine_mode mode = GET_MODE_INNER (vmode);
+
+ if (!flag_trapping_math)
+ return force_lowpart_subreg (vmode, op, mode);
+ else
+ {
+ rtx tmp = gen_reg_rtx (vmode);
+ emit_move_insn (tmp, gen_vec_duplicate (vmode, op));
+ return tmp;
+ }
+}
+
/* Initialize the GCC target structure. */
#undef TARGET_ASM_ALIGNED_HI_OP
#define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
diff --git a/gcc/config/loongarch/simd.md b/gcc/config/loongarch/simd.md
index 0ac1ecab56d..557b8a70bf4 100644
--- a/gcc/config/loongarch/simd.md
+++ b/gcc/config/loongarch/simd.md
@@ -329,8 +329,9 @@ (define_expand "nearbyint<mode>2"
;; much faster than calling a libc function: on LA464 and LA664 there is a
;; 3x ~ 5x speed up.
;;
-;; Note that a vreplvei instruction is needed or we'll also operate on the
-;; junk in high bits of the vector register and produce random FP exceptions.
+;; Note that a vreplvei instruction is needed with -ftrapping-math or we'll
+;; also operate on the junk in high bits of the vector register and produce
+;; random FP exceptions.
(define_int_iterator LSX_SCALAR_FRINT
[UNSPEC_FRINTRP
@@ -342,14 +343,18 @@ (define_mode_attr VLSX_FOR_FMODE [(DF "V2DF") (SF
"V4SF")])
(define_expand "<simd_frint_pattern><mode>2"
[(set (match_dup 2)
- (vec_duplicate:<VLSX_FOR_FMODE>
- (match_operand:ANYF 1 "register_operand")))
- (set (match_dup 2)
- (unspec:<VLSX_FOR_FMODE> [(match_dup 2)] LSX_SCALAR_FRINT))
+ (unspec:<VLSX_FOR_FMODE>
+ [(match_operand:ANYF 1 "register_operand")] LSX_SCALAR_FRINT))
(set (match_operand:ANYF 0 "register_operand")
- (vec_select:ANYF (match_dup 2) (parallel [(const_int 0)])))]
+ (subreg:ANYF (match_dup 2) 0))]
"ISA_HAS_LSX && (flag_fp_int_builtin_inexact || !flag_trapping_math)"
- "operands[2] = gen_reg_rtx (<VLSX_FOR_FMODE>mode);")
+ {
+ machine_mode vmode = <VLSX_FOR_FMODE>mode;
+
+ operands[2] = gen_reg_rtx (vmode);
+ operands[1] = loongarch_adjust_scalar_input_for_simd (operands[1],
+ vmode);
+ })
;; <x>vftint.{/rp/rz/rm}
(define_insn
diff --git a/gcc/testsuite/gcc.target/loongarch/roundeven-notrap.c
b/gcc/testsuite/gcc.target/loongarch/roundeven-notrap.c
new file mode 100644
index 00000000000..e38fd763fe2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/roundeven-notrap.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=loongarch64 -mabi=lp64d -mlsx -fno-trapping-math
-fno-math-errno" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/*
+**t:
+** vfrintrne.d \$vr0,\$vr0
+** jr \$r1
+*/
+double
+t (double x)
+{
+ return __builtin_roundeven(x);
+}
--
2.56.1