public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Implement range-op entry for __builtin_copysign.
@ 2022-10-14 15:08 Aldy Hernandez
  2022-10-14 16:06 ` Jakub Jelinek
  0 siblings, 1 reply; 2+ messages in thread
From: Aldy Hernandez @ 2022-10-14 15:08 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: GCC patches, Andrew MacLeod, Aldy Hernandez

[Jakub, while we're on the subject of signed zeros and copysign, does
this look OK to you?]

copysign(MAGNITUDE, SIGN) is implemented as the absolute of MAGNITUDE,
with SIGN applied.  If the sign of "SIGN" cannot be determined, we
return a range of [-MAGNITUDE, +MAGNITUDE].

gcc/ChangeLog:

	* gimple-range-op.cc (class cfn_copysign): New.
	(gimple_range_op_handler::maybe_builtin_call): Add
	CFN_BUILT_IN_COPYSIGN*.
---
 gcc/gimple-range-op.cc | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc
index 527893f66af..8137308f32e 100644
--- a/gcc/gimple-range-op.cc
+++ b/gcc/gimple-range-op.cc
@@ -342,6 +342,38 @@ public:
   }
 } op_cfn_signbit;
 
+// Implement range operator for CFN_BUILT_IN_COPYSIGN
+class cfn_copysign : public range_operator_float
+{
+public:
+  using range_operator_float::fold_range;
+  virtual bool fold_range (frange &r, tree type, const frange &lh,
+			   const frange &rh, relation_kind) const override
+  {
+    frange neg;
+    range_op_handler abs_op (ABS_EXPR, type);
+    range_op_handler neg_op (NEGATE_EXPR, type);
+    if (!abs_op || !abs_op.fold_range (r, type, lh, frange (type)))
+      return false;
+    if (!neg_op || !neg_op.fold_range (neg, type, r, frange (type)))
+      return false;
+
+    bool signbit;
+    if (rh.signbit_p (signbit))
+      {
+	// If the sign is negative, flip the result from ABS,
+	// otherwise leave things positive.
+	if (signbit)
+	  r = neg;
+      }
+    else
+      // If the sign is unknown, keep the positive and negative
+      // alternatives.
+      r.union_ (neg);
+    return true;
+  }
+} op_cfn_copysign;
+
 // Implement range operator for CFN_BUILT_IN_TOUPPER and CFN_BUILT_IN_TOLOWER.
 class cfn_toupper_tolower : public range_operator
 {
@@ -762,6 +794,13 @@ gimple_range_op_handler::maybe_builtin_call ()
       m_valid = true;
       break;
 
+    CASE_FLT_FN (CFN_BUILT_IN_COPYSIGN):
+      m_op1 = gimple_call_arg (call, 0);
+      m_op2 = gimple_call_arg (call, 1);
+      m_float = &op_cfn_copysign;
+      m_valid = true;
+      break;
+
     case CFN_BUILT_IN_TOUPPER:
     case CFN_BUILT_IN_TOLOWER:
       // Only proceed If the argument is compatible with the LHS.
-- 
2.37.3


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

* Re: [PATCH] Implement range-op entry for __builtin_copysign.
  2022-10-14 15:08 [PATCH] Implement range-op entry for __builtin_copysign Aldy Hernandez
@ 2022-10-14 16:06 ` Jakub Jelinek
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Jelinek @ 2022-10-14 16:06 UTC (permalink / raw)
  To: Aldy Hernandez; +Cc: GCC patches, Andrew MacLeod

On Fri, Oct 14, 2022 at 05:08:51PM +0200, Aldy Hernandez wrote:
> [Jakub, while we're on the subject of signed zeros and copysign, does
> this look OK to you?]
> 
> copysign(MAGNITUDE, SIGN) is implemented as the absolute of MAGNITUDE,
> with SIGN applied.  If the sign of "SIGN" cannot be determined, we
> return a range of [-MAGNITUDE, +MAGNITUDE].
> 
> gcc/ChangeLog:
> 
> 	* gimple-range-op.cc (class cfn_copysign): New.
> 	(gimple_range_op_handler::maybe_builtin_call): Add
> 	CFN_BUILT_IN_COPYSIGN*.
> ---
>  gcc/gimple-range-op.cc | 39 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 39 insertions(+)
> 
> diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc
> index 527893f66af..8137308f32e 100644
> --- a/gcc/gimple-range-op.cc
> +++ b/gcc/gimple-range-op.cc
> @@ -342,6 +342,38 @@ public:
>    }
>  } op_cfn_signbit;
>  
> +// Implement range operator for CFN_BUILT_IN_COPYSIGN
> +class cfn_copysign : public range_operator_float
> +{
> +public:
> +  using range_operator_float::fold_range;
> +  virtual bool fold_range (frange &r, tree type, const frange &lh,
> +			   const frange &rh, relation_kind) const override
> +  {
> +    frange neg;
> +    range_op_handler abs_op (ABS_EXPR, type);
> +    range_op_handler neg_op (NEGATE_EXPR, type);
> +    if (!abs_op || !abs_op.fold_range (r, type, lh, frange (type)))
> +      return false;
> +    if (!neg_op || !neg_op.fold_range (neg, type, r, frange (type)))
> +      return false;
> +
> +    bool signbit;
> +    if (rh.signbit_p (signbit))
> +      {
> +	// If the sign is negative, flip the result from ABS,
> +	// otherwise leave things positive.
> +	if (signbit)
> +	  r = neg;
> +      }
> +    else
> +      // If the sign is unknown, keep the positive and negative
> +      // alternatives.
> +      r.union_ (neg);
> +    return true;
> +  }
> +} op_cfn_copysign;
> +
>  // Implement range operator for CFN_BUILT_IN_TOUPPER and CFN_BUILT_IN_TOLOWER.
>  class cfn_toupper_tolower : public range_operator
>  {
> @@ -762,6 +794,13 @@ gimple_range_op_handler::maybe_builtin_call ()
>        m_valid = true;
>        break;
>  
> +    CASE_FLT_FN (CFN_BUILT_IN_COPYSIGN):

    CASE_CFN_COPYSIGN_ALL:

instead.
Otherwise LGTM.

> +      m_op1 = gimple_call_arg (call, 0);
> +      m_op2 = gimple_call_arg (call, 1);
> +      m_float = &op_cfn_copysign;
> +      m_valid = true;
> +      break;
> +
>      case CFN_BUILT_IN_TOUPPER:
>      case CFN_BUILT_IN_TOLOWER:
>        // Only proceed If the argument is compatible with the LHS.
> -- 
> 2.37.3

	Jakub


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

end of thread, other threads:[~2022-10-14 16:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-14 15:08 [PATCH] Implement range-op entry for __builtin_copysign Aldy Hernandez
2022-10-14 16:06 ` Jakub Jelinek

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