diff mbox

[wide-int] Add a fast path for multiplication by 0

Message ID 87li07s0um.fsf@talisman.default
State New
Headers show

Commit Message

Richard Sandiford Nov. 29, 2013, 3:07 p.m. UTC
Richard Biener <richard.guenther@gmail.com> writes:
> On Fri, Nov 29, 2013 at 12:14 PM, Richard Sandiford
> <rdsandiford@googlemail.com> wrote:
>> In the fold-const.ii testcase, well over half of the mul_internal calls
>> were for multiplication by 0 (106038 out of 169355).  This patch adds
>> an early-out for that.
>>
>> Tested on x86_64-linux-gnu.  OK to install?
>
> Ok.  Did you check how many of the remaining are multiplies by 1?

Turns out to be 9685, which is probably enough to justify a special case.

Tested on x86_64-linux-gnu.  OK to install?

Thanks,
Richard

Comments

Richard Biener Nov. 30, 2013, 12:05 p.m. UTC | #1
Richard Sandiford <rdsandiford@googlemail.com> wrote:
>Richard Biener <richard.guenther@gmail.com> writes:
>> On Fri, Nov 29, 2013 at 12:14 PM, Richard Sandiford
>> <rdsandiford@googlemail.com> wrote:
>>> In the fold-const.ii testcase, well over half of the mul_internal
>calls
>>> were for multiplication by 0 (106038 out of 169355).  This patch
>adds
>>> an early-out for that.
>>>
>>> Tested on x86_64-linux-gnu.  OK to install?
>>
>> Ok.  Did you check how many of the remaining are multiplies by 1?
>
>Turns out to be 9685, which is probably enough to justify a special
>case.
>
>Tested on x86_64-linux-gnu.  OK to install?

Ok.  I assume we already have a special.case for division by 1?

Thanks,
Richard.

>Thanks,
>Richard
>
>
>Index: gcc/wide-int.cc
>===================================================================
>--- gcc/wide-int.cc	2013-11-29 15:04:41.177142418 +0000
>+++ gcc/wide-int.cc	2013-11-29 15:05:36.482424592 +0000
>@@ -1296,6 +1296,20 @@ wi::mul_internal (HOST_WIDE_INT *val, co
>       return 1;
>     }
> 
>+  /* Handle multiplications by 1.  */
>+  if (op1len == 1 && op1[0] == 1)
>+    {
>+      for (i = 0; i < op2len; i++)
>+	val[i] = op2[i];
>+      return op2len;
>+    }
>+  if (op2len == 1 && op2[0] == 1)
>+    {
>+      for (i = 0; i < op1len; i++)
>+	val[i] = op1[i];
>+      return op1len;
>+    }
>+
>   /* If we need to check for overflow, we can only do half wide
>      multiplies quickly because we need to look at the top bits to
>      check for the overflow.  */
Richard Sandiford Dec. 1, 2013, 10:35 a.m. UTC | #2
Richard Biener <richard.guenther@gmail.com> writes:
> Richard Sandiford <rdsandiford@googlemail.com> wrote:
>>Richard Biener <richard.guenther@gmail.com> writes:
>>> On Fri, Nov 29, 2013 at 12:14 PM, Richard Sandiford
>>> <rdsandiford@googlemail.com> wrote:
>>>> In the fold-const.ii testcase, well over half of the mul_internal
>>calls
>>>> were for multiplication by 0 (106038 out of 169355).  This patch
>>adds
>>>> an early-out for that.
>>>>
>>>> Tested on x86_64-linux-gnu.  OK to install?
>>>
>>> Ok.  Did you check how many of the remaining are multiplies by 1?
>>
>>Turns out to be 9685, which is probably enough to justify a special
>>case.
>>
>>Tested on x86_64-linux-gnu.  OK to install?
>
> Ok.  I assume we already have a special.case for division by 1?

I don't think it's really worth having one there.  If the inputs fit
into single HWIs then the output does too, so we can just use a host
division.

Thanks,
Richard
diff mbox

Patch

Index: gcc/wide-int.cc
===================================================================
--- gcc/wide-int.cc	2013-11-29 15:04:41.177142418 +0000
+++ gcc/wide-int.cc	2013-11-29 15:05:36.482424592 +0000
@@ -1296,6 +1296,20 @@  wi::mul_internal (HOST_WIDE_INT *val, co
       return 1;
     }
 
+  /* Handle multiplications by 1.  */
+  if (op1len == 1 && op1[0] == 1)
+    {
+      for (i = 0; i < op2len; i++)
+	val[i] = op2[i];
+      return op2len;
+    }
+  if (op2len == 1 && op2[0] == 1)
+    {
+      for (i = 0; i < op1len; i++)
+	val[i] = op1[i];
+      return op1len;
+    }
+
   /* If we need to check for overflow, we can only do half wide
      multiplies quickly because we need to look at the top bits to
      check for the overflow.  */