range-op-float: Improve multiplication reverse operation

Message ID Y43dD0/eSrUNx/8Z@tucnak
State Unresolved
Headers
Series range-op-float: Improve multiplication reverse operation |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Dec. 5, 2022, 11:59 a.m. UTC
  On Mon, Dec 05, 2022 at 10:54:41AM +0100, Aldy Hernandez wrote:
> > Do you mind if I try that incrementally and only if it doesn't make the
> > code too large/too unreadable?
> 
> Sure.  And don't feel obligated to implement it either.  range-ops is a
> never ending pit of possible optimizations.  We found that out quite early
> in the design.
> 
> If you don't get to it, could you at least add a comment?

So like this for multiplication op1/2_range if it passes bootstrap/regtest?
For division I'll need to go to a drawing board...

2022-12-05  Jakub Jelinek  <jakub@redhat.com>

	* range-op-float.cc (zero_to_max_range): New function.
	(foperator_plus::op1_range): If lhs can't be INF nor NAN,
	op1 can't be INF.



	Jakub
  

Comments

Aldy Hernandez Dec. 5, 2022, 1:29 p.m. UTC | #1
On 12/5/22 12:59, Jakub Jelinek wrote:
> On Mon, Dec 05, 2022 at 10:54:41AM +0100, Aldy Hernandez wrote:
>>> Do you mind if I try that incrementally and only if it doesn't make the
>>> code too large/too unreadable?
>>
>> Sure.  And don't feel obligated to implement it either.  range-ops is a
>> never ending pit of possible optimizations.  We found that out quite early
>> in the design.
>>
>> If you don't get to it, could you at least add a comment?
> 
> So like this for multiplication op1/2_range if it passes bootstrap/regtest?
> For division I'll need to go to a drawing board...

Sure, looks good to me.

> 
> 2022-12-05  Jakub Jelinek  <jakub@redhat.com>
> 
> 	* range-op-float.cc (zero_to_max_range): New function.
> 	(foperator_plus::op1_range): If lhs can't be INF nor NAN,
> 	op1 can't be INF.
> 
> --- gcc/range-op-float.cc.jj	2022-12-05 11:17:34.900573272 +0100
> +++ gcc/range-op-float.cc	2022-12-05 12:32:30.286753929 +0100
> @@ -2004,6 +2004,29 @@ zero_to_inf_range (REAL_VALUE_TYPE &lb,
>       }
>   }
>   
> +// Set [lb, ub] to [-MAX, -0], [-MAX, +MAX] or [+0, +MAX] depending on
> +// signbit_known.
> +static void
> +zero_to_max_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, tree type,
> +		   int signbit_known)

My apologies for having to passs around [lb, ub] everywhere.  At least 
for rv_fold(), I'd like to rewrite it next year as:

void rv_fold (frange &r, tree type, ...);

...instead of having to pass LB, UB, and maybe_nan as references.  All 
the information in LB, UB, and maybe_nan can fit nicely in an frange. 
This is what we do in irange (wi_fold).  It also sets us up nicely for 
when we have subranges, and we can union the result.

I'll clean it up early next release.
Aldy

> +{
> +  if (signbit_known > 0)
> +    {
> +      lb = dconst0;
> +      ub = real_max_representable (type);
> +    }
> +  else if (signbit_known < 0)
> +    {
> +      lb = real_min_representable (type);
> +      ub = real_value_negate (&dconst0);
> +    }
> +  else
> +    {
> +      lb = real_min_representable (type);
> +      ub = real_max_representable (type);
> +    }
> +}
> +
>   class foperator_plus : public range_operator_float
>   {
>     using range_operator_float::op1_range;
> @@ -2159,7 +2182,14 @@ public:
>   	// and perhaps if it can be NAN or not.
>   	REAL_VALUE_TYPE lb, ub;
>   	int signbit_known = signbit_known_p (lhs_lb, lhs_ub, op2_lb, op2_ub);
> -	zero_to_inf_range (lb, ub, signbit_known);
> +	// If lhs can't be +-INF nor NAN, then op1 can't be +-INF -
> +	// +-INF * anything is either +-INF or NAN (if op2 is +-0 or NAN).
> +	if (!real_isinf (&lhs_lb)
> +	    && !real_isinf (&lhs_ub)
> +	    && !lhs.maybe_isnan ())
> +	  zero_to_max_range (lb, ub, type, signbit_known);
> +	else
> +	  zero_to_inf_range (lb, ub, signbit_known);
>   	r.set (type, lb, ub);
>         }
>       // Otherwise, if op2 is a singleton INF and lhs doesn't include INF,
> 
> 
> 	Jakub
>
  

Patch

--- gcc/range-op-float.cc.jj	2022-12-05 11:17:34.900573272 +0100
+++ gcc/range-op-float.cc	2022-12-05 12:32:30.286753929 +0100
@@ -2004,6 +2004,29 @@  zero_to_inf_range (REAL_VALUE_TYPE &lb,
     }
 }
 
+// Set [lb, ub] to [-MAX, -0], [-MAX, +MAX] or [+0, +MAX] depending on
+// signbit_known.
+static void
+zero_to_max_range (REAL_VALUE_TYPE &lb, REAL_VALUE_TYPE &ub, tree type,
+		   int signbit_known)
+{
+  if (signbit_known > 0)
+    {
+      lb = dconst0;
+      ub = real_max_representable (type);
+    }
+  else if (signbit_known < 0)
+    {
+      lb = real_min_representable (type);
+      ub = real_value_negate (&dconst0);
+    }
+  else
+    {
+      lb = real_min_representable (type);
+      ub = real_max_representable (type);
+    }
+}
+
 class foperator_plus : public range_operator_float
 {
   using range_operator_float::op1_range;
@@ -2159,7 +2182,14 @@  public:
 	// and perhaps if it can be NAN or not.
 	REAL_VALUE_TYPE lb, ub;
 	int signbit_known = signbit_known_p (lhs_lb, lhs_ub, op2_lb, op2_ub);
-	zero_to_inf_range (lb, ub, signbit_known);
+	// If lhs can't be +-INF nor NAN, then op1 can't be +-INF -
+	// +-INF * anything is either +-INF or NAN (if op2 is +-0 or NAN).
+	if (!real_isinf (&lhs_lb)
+	    && !real_isinf (&lhs_ub)
+	    && !lhs.maybe_isnan ())
+	  zero_to_max_range (lb, ub, type, signbit_known);
+	else
+	  zero_to_inf_range (lb, ub, signbit_known);
 	r.set (type, lb, ub);
       }
     // Otherwise, if op2 is a singleton INF and lhs doesn't include INF,