public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Aldy Hernandez <aldyh@redhat.com>
To: GCC patches <gcc-patches@gcc.gnu.org>
Cc: Andrew MacLeod <amacleod@redhat.com>, Aldy Hernandez <aldyh@redhat.com>
Subject: [COMMITTED] Share common ordered comparison code with UN*_EXPR.
Date: Tue, 11 Oct 2022 15:51:34 +0200	[thread overview]
Message-ID: <20221011135136.369644-2-aldyh@redhat.com> (raw)
In-Reply-To: <20221011135136.369644-1-aldyh@redhat.com>

Most unordered comparisons can use the result from the ordered
version, if the operands are known not to be NAN or if the result is
true.

gcc/ChangeLog:

	* range-op-float.cc (class foperator_unordered_lt): New.
	(class foperator_relop_unknown): Remove
	(class foperator_unordered_le): New.
	(class foperator_unordered_gt): New.
	(class foperator_unordered_ge): New.
	(class foperator_unordered_equal): New.
	(floating_op_table::floating_op_table): Replace all UN_EXPR
	entries with their appropriate fop_unordered_* counterpart.
---
 gcc/range-op-float.cc | 140 ++++++++++++++++++++++++++++++++++++++----
 1 file changed, 128 insertions(+), 12 deletions(-)

diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc
index 3cf117d8931..8dd4bcc70c0 100644
--- a/gcc/range-op-float.cc
+++ b/gcc/range-op-float.cc
@@ -1132,24 +1132,140 @@ foperator_ordered::op1_range (frange &r, tree type,
   return true;
 }
 
-// Placeholder for unimplemented relational operators.
+class foperator_unordered_lt : public range_operator_float
+{
+  using range_operator_float::fold_range;
+public:
+  bool fold_range (irange &r, tree type,
+		   const frange &op1, const frange &op2,
+		   relation_kind rel) const final override
+  {
+    if (op1.known_isnan () || op2.known_isnan ())
+      {
+	r = range_true (type);
+	return true;
+      }
+    if (!fop_lt.fold_range (r, type, op1, op2, rel))
+      return false;
+    // The result is the same as the ordered version when the
+    // comparison is true or when the operands cannot be NANs.
+    if (finite_operands_p (op1, op2) || r == range_true (type))
+      return true;
+    else
+      {
+	r = range_true_and_false (type);
+	return true;
+      }
+  }
+} fop_unordered_lt;
 
-class foperator_relop_unknown : public range_operator_float
+class foperator_unordered_le : public range_operator_float
 {
   using range_operator_float::fold_range;
+public:
+  bool fold_range (irange &r, tree type,
+		   const frange &op1, const frange &op2,
+		   relation_kind rel) const final override
+  {
+    if (op1.known_isnan () || op2.known_isnan ())
+      {
+	r = range_true (type);
+	return true;
+      }
+    if (!fop_le.fold_range (r, type, op1, op2, rel))
+      return false;
+    // The result is the same as the ordered version when the
+    // comparison is true or when the operands cannot be NANs.
+    if (finite_operands_p (op1, op2) || r == range_true (type))
+      return true;
+    else
+      {
+	r = range_true_and_false (type);
+	return true;
+      }
+  }
+} fop_unordered_le;
 
+class foperator_unordered_gt : public range_operator_float
+{
+  using range_operator_float::fold_range;
 public:
   bool fold_range (irange &r, tree type,
 		   const frange &op1, const frange &op2,
-		   relation_kind) const final override
+		   relation_kind rel) const final override
   {
     if (op1.known_isnan () || op2.known_isnan ())
-      r = range_true (type);
+      {
+	r = range_true (type);
+	return true;
+      }
+    if (!fop_gt.fold_range (r, type, op1, op2, rel))
+      return false;
+    // The result is the same as the ordered version when the
+    // comparison is true or when the operands cannot be NANs.
+    if (finite_operands_p (op1, op2) || r == range_true (type))
+      return true;
     else
-      r.set_varying (type);
-    return true;
+      {
+	r = range_true_and_false (type);
+	return true;
+      }
+  }
+} fop_unordered_gt;
+
+class foperator_unordered_ge : public range_operator_float
+{
+  using range_operator_float::fold_range;
+public:
+  bool fold_range (irange &r, tree type,
+		   const frange &op1, const frange &op2,
+		   relation_kind rel) const final override
+  {
+    if (op1.known_isnan () || op2.known_isnan ())
+      {
+	r = range_true (type);
+	return true;
+      }
+    if (!fop_ge.fold_range (r, type, op1, op2, rel))
+      return false;
+    // The result is the same as the ordered version when the
+    // comparison is true or when the operands cannot be NANs.
+    if (finite_operands_p (op1, op2) || r == range_true (type))
+      return true;
+    else
+      {
+	r = range_true_and_false (type);
+	return true;
+      }
+  }
+} fop_unordered_ge;
+
+class foperator_unordered_equal : public range_operator_float
+{
+  using range_operator_float::fold_range;
+public:
+  bool fold_range (irange &r, tree type,
+		   const frange &op1, const frange &op2,
+		   relation_kind rel) const final override
+  {
+    if (op1.known_isnan () || op2.known_isnan ())
+      {
+	r = range_true (type);
+	return true;
+      }
+    if (!fop_equal.fold_range (r, type, op1, op2, rel))
+      return false;
+    // The result is the same as the ordered version when the
+    // comparison is true or when the operands cannot be NANs.
+    if (finite_operands_p (op1, op2) || r == range_true (type))
+      return true;
+    else
+      {
+	r = range_true_and_false (type);
+	return true;
+      }
   }
-} fop_unordered_relop_unknown;
+} fop_unordered_equal;
 
 
 // Instantiate a range_op_table for floating point operations.
@@ -1174,11 +1290,11 @@ floating_op_table::floating_op_table ()
   set (LE_EXPR, fop_le);
   set (GT_EXPR, fop_gt);
   set (GE_EXPR, fop_ge);
-  set (UNLE_EXPR, fop_unordered_relop_unknown);
-  set (UNLT_EXPR, fop_unordered_relop_unknown);
-  set (UNGE_EXPR, fop_unordered_relop_unknown);
-  set (UNGT_EXPR, fop_unordered_relop_unknown);
-  set (UNEQ_EXPR, fop_unordered_relop_unknown);
+  set (UNLE_EXPR, fop_unordered_le);
+  set (UNLT_EXPR, fop_unordered_lt);
+  set (UNGE_EXPR, fop_unordered_ge);
+  set (UNGT_EXPR, fop_unordered_gt);
+  set (UNEQ_EXPR, fop_unordered_equal);
   set (ORDERED_EXPR, fop_ordered);
   set (UNORDERED_EXPR, fop_unordered);
 }
-- 
2.37.3


  reply	other threads:[~2022-10-11 13:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-11 13:51 [COMMITTED] Move TRUE case first in range-op.cc Aldy Hernandez
2022-10-11 13:51 ` Aldy Hernandez [this message]
2022-10-11 13:51 ` [COMMITTED] Implement op1_range operators for unordered comparisons Aldy Hernandez
2022-10-11 13:57   ` Aldy Hernandez
2022-10-11 13:51 ` [COMMITTED] Implement ABS_EXPR operator for frange Aldy Hernandez

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=20221011135136.369644-2-aldyh@redhat.com \
    --to=aldyh@redhat.com \
    --cc=amacleod@redhat.com \
    --cc=gcc-patches@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).