With -fno-trapping-math we can tolerate the spurious INEXACT exception
so a frint instruction can be used instead of calling the libm function.

gcc/

        * config/loongarch/loongarch.md (nearbyint<mode>2): New
        define_expand.
        * config/loongarch/simd.md (nearbyint<mode>2): New
        define_expand.

gcc/testsuite

        * gcc.target/loongarch/nearbyint-notrap.c: New test.
---
 gcc/config/loongarch/loongarch.md                  |  7 +++++++
 gcc/config/loongarch/simd.md                       |  7 +++++++
 .../gcc.target/loongarch/nearbyint-notrap.c        | 14 ++++++++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/loongarch/nearbyint-notrap.c

diff --git a/gcc/config/loongarch/loongarch.md 
b/gcc/config/loongarch/loongarch.md
index be5e9f19a67..8eef348af29 100644
--- a/gcc/config/loongarch/loongarch.md
+++ b/gcc/config/loongarch/loongarch.md
@@ -2969,6 +2969,13 @@ (define_insn "rint<mode>2"
   [(set_attr "type" "fcvt")
    (set_attr "mode" "<MODE>")])
 
+;; With -fno-trapping-math we can tolerate spurious INEXACT exception.
+(define_expand "nearbyint<mode>2"
+  [(set (match_operand:ANYF 0 "register_operand" "=f")
+       (unspec:ANYF [(match_operand:ANYF 1 "register_operand" "f")]
+                     UNSPEC_FRINT))]
+  "TARGET_64BIT && !flag_trapping_math")
+
 ;; Convert floating-point numbers to integers
 (define_insn "*<lrint_pattern><ANYF:mode><ANYFI:mode>2"
   [(set (match_operand:ANYFI 0 "register_operand" "=f")
diff --git a/gcc/config/loongarch/simd.md b/gcc/config/loongarch/simd.md
index 5d49145109c..0ac1ecab56d 100644
--- a/gcc/config/loongarch/simd.md
+++ b/gcc/config/loongarch/simd.md
@@ -316,6 +316,13 @@ (define_expand "ftrunc<mode>2"
                     UNSPEC_FRINTRZ))]
   "")
 
+;; With -fno-trapping-math we can tolerate spurious INEXACT exception.
+(define_expand "nearbyint<mode>2"
+  [(set (match_operand:FVEC 0 "register_operand" "=f")
+       (unspec:FVEC [(match_operand:FVEC 1 "register_operand" "f")]
+                     UNSPEC_FRINT))]
+  "TARGET_64BIT && !flag_trapping_math")
+
 ;; Use LSX for scalar ceil/floor/trunc/roundeven when -mlsx and -ffp-int-
 ;; builtin-inexact.  The base FP instruction set lacks these operations.
 ;; Yes we are wasting 50% or even 75% of the CPU horsepower, but it's still
diff --git a/gcc/testsuite/gcc.target/loongarch/nearbyint-notrap.c 
b/gcc/testsuite/gcc.target/loongarch/nearbyint-notrap.c
new file mode 100644
index 00000000000..390894418e5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/nearbyint-notrap.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mdouble-float -fno-trapping-math" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/*
+**t:
+**     frint.d \$f0,\$f0
+**     jr      \$r1
+*/
+double
+t (double x)
+{
+  return __builtin_nearbyint(x);
+}
-- 
2.56.1

Reply via email to