[PATCH] [ConstantRange] Add a smultiply method for signed multiply
Nick Lewycky
nlewycky at google.com
Sat Feb 21 13:05:32 PST 2015
On 21 February 2015 at 03:58, James Molloy <James.Molloy at arm.com> wrote:
> Hi Nick, all,
>
> Firstly apologies for not creating a unit test. I thought this was
> totally trivial and it turns out it’s completely not.
>
> When wrapping, it is sufficient for my use case to know that we’ve
> wrapped. I don’t care about what we represent a wrapped value as (where we
> pick the gap).
>
> Discounting wrapping for the moment, it seems to me that while
> multiplication is signedness-independent, minimum and maximum are not.
>
> [-2,0) * [-4,4)
> Consider the trivial case: i8 [254,0) * i8 [252,4). In signed
> arithmetic,we get:
> a[0] * b[0] = 8
> a[1] * b[1] = 0
> a[0] * b[1] = -8
> a[1] * b[0] = 0
>
> min: -8, max: 8
>
> In unsigned arithmetic we get:
> a[0] * b[0] = 8
> a[1] * b[1] = 0
> a[0] * b[1] = 248
> a[1] * b[0] = 0
>
> min: 8, max: 248 ( =-8 )
>
> So depending on the signedness of the minimum/maximum operation, we get
> different results. Therefore, we do need a signed version of multiply.
>
> That’s as far as my logic takes me, could someone please point out where
> I’ve gone wrong?
>
Fantastic example!
The total set of values is {0, 1, 2, 3, 4, 6, 8, 250, 252, 254, 255}. The
best range to cover that is [250, 9), but we calculate full-set because of
the truncation step.
The multiply function shouldn't have signed or unsigned variations in the
API because there is only one best resulting range either way, but you've
clearly identified a deficiency. ConstantRange::multiply could compute it
twice once with zext and unsigned min/max and again with sext and signed
min/max, then choose the one with the smaller range. Maybe there's an even
better algorithm.
Nick
> Cheers,
>
> James
>
> On 21 Feb 2015, at 02:11, Nick Lewycky <nlewycky at google.com> wrote:
>
> On 20 February 2015 at 17:49, Nick Lewycky <nlewycky at google.com> wrote:
>
>> On 20 February 2015 at 17:41, Sanjoy Das <sanjoy at playingwithpointers.com
>> > wrote:
>>
>>> > It's hard to do multiplication of ConstantRanges precisely, and
>>> multiply
>>> > does have a TODO requesting improvement. The problems start once you
>>> have
>>> > multiplies that can wrap. What you need to solve for is the max and
>>> min of
>>> > each of the multiplies (cartesian product) in modulo 2^n space.
>>> Because we
>>> > need to push the resulting set into a constant range, what it looks
>>> like is
>>> > "how far > 0 is the lowest value" and "how far < UINT_MAX is the
>>> highest
>>> > value", with no interesting information about the values in the middle.
>>>
>>> Just to make sure I understand you: this problem isn't really limited
>>> to cases that actually wrap -- i32 [1, 4) * i32 [3, 4) == i32 {3, 6,
>>> 9} and this structure is lost when we coerce the result to [3, 10).
>>>
>>
>> I wasn't considering that a problem. ConstantRange::multiply
>> today precisely produces the range [3, 10) in that case. There is no
>> improvement to be made.
>>
>> Consider i8 [1, 4) * i8 [9, 10).
>>
>
> Uhm, I meant to say "i8 [1, 86) * i8 [9, 10)". That's quite a typo.
>
> The best possible range is [1, 253), but our implementation will
>> return the full set. That's really hard to get right. We extend the bit
>> range to twice the bit range so that we can do the multiply algebraically,
>> then we call truncate. Truncate correctly detects that we cover the whole
>> span of integers in the bitwidth and returns the full set.
>>
>> Nick
>>
>> I wonder how crazy it is to have the notion of a first-class
>>> ConstantRange "expression" (i.e. remembering that a ConstantRange is
>>> i32 [1, 4) * i32 [3, 4) and not eagerly flattening it).
>>>
>>
>>
>>> -- Sanjoy
>>>
>>> >
>>> > Nick
>>> >
>>> >>
>>> >> > On 20 Feb 2015, at 23:48, Sanjoy Das <
>>> sanjoy at playingwithpointers.com>
>>> >> > wrote:
>>> >> >
>>> >> >> Actually, I don't think there is any need for a separate smultiply
>>> >> >> when we're working on two's complement. IMHO, the right solution
>>> is
>>> >> >> to just make ConstantRange::multiply smarter about handing cases
>>> like
>>> >> >> [-1,2) * [-1,2) etc.
>>> >> >>
>>> >> >> -- Sanjoy
>>> >> >>
>>> >> >> On Fri, Feb 20, 2015 at 3:40 PM, Sanjoy Das
>>> >> >> <sanjoy at playingwithpointers.com> wrote:
>>> >> >>> I don't think so:
>>> >> >>>
>>> >> >>> [-3,0) * [-3, 0) should be [1, 10) but using your formula I get
>>> [3,
>>> >> >>> <something>).
>>> >> >>>
>>> >> >>> On Fri, Feb 20, 2015 at 3:31 PM, Hal Finkel <hfinkel at anl.gov>
>>> wrote:
>>> >> >>>> ----- Original Message -----
>>> >> >>>>> From: "Sanjoy Das" <sanjoy at playingwithpointers.com>
>>> >> >>>>> To: reviews+D7789+public+ffa7549bb8a7b441 at reviews.llvm.org
>>> >> >>>>> Cc: "james molloy" <james.molloy at arm.com>, "Hal Finkel"
>>> >> >>>>> <hfinkel at anl.gov>, "Commit Messages and Patches for LLVM"
>>> >> >>>>> <llvm-commits at cs.uiuc.edu>
>>> >> >>>>> Sent: Friday, February 20, 2015 5:11:46 PM
>>> >> >>>>> Subject: Re: [PATCH] [ConstantRange] Add a smultiply method for
>>> >> >>>>> signed multiply
>>> >> >>>>>
>>> >> >>>>>> ConstantRange
>>> >> >>>>>> +ConstantRange::smultiply(const ConstantRange &Other) const {
>>> >> >>>>>> + // TODO: If either operand is a single element and the
>>> multiply
>>> >> >>>>>> is known to
>>> >> >>>>>> + // be non-wrapping, round the result min and max value to
>>> the
>>> >> >>>>>> appropriate
>>> >> >>>>>> + // multiple of that element. If wrapping is possible, at
>>> least
>>> >> >>>>>> adjust the
>>> >> >>>>>> + // range according to the greatest power-of-two factor of
>>> the
>>> >> >>>>>> single element.
>>> >> >>>>>> +
>>> >> >>>>>> + if (isEmptySet() || Other.isEmptySet())
>>> >> >>>>>> + return ConstantRange(getBitWidth(), /*isFullSet=*/false);
>>> >> >>>>>> +
>>> >> >>>>>> + APInt this_min = getSignedMin().sext(getBitWidth() * 2);
>>> >> >>>>>> + APInt this_max = getSignedMax().sext(getBitWidth() * 2);
>>> >> >>>>>> + APInt Other_min = Other.getSignedMin().sext(getBitWidth() *
>>> 2);
>>> >> >>>>>> + APInt Other_max = Other.getSignedMax().sext(getBitWidth() *
>>> 2);
>>> >> >>>>>> +
>>> >> >>>>>> + ConstantRange Result_zext = ConstantRange(this_min *
>>> Other_min,
>>> >> >>>>>> + this_max *
>>> Other_max +
>>> >> >>>>>> 1);
>>> >> >>>>>> + return Result_zext.truncate(getBitWidth());
>>> >> >>>>>
>>> >> >>>>> I'm not sure if this is correct: if we multiply [-1, 2) with
>>> >> >>>>> itself,
>>> >> >>>>> won't this return [1, 2)? Shouldn't [-1,2) * [-1,2) be [-1,2)?
>>> >> >>>>
>>> >> >>>> Indeed. The minimum should not be this_min * Other_min, but
>>> rather
>>> >> >>>> min(this_min * Other_max, this_max * Other_min), right?
>>> >> >>>>
>>> >> >>>> -Hal
>>> >> >>>>
>>> >> >>>>>
>>> >> >>>>> -- Sanjoy
>>> >> >>>>>
>>> >> >>>>
>>> >> >>>> --
>>> >> >>>> Hal Finkel
>>> >> >>>> Assistant Computational Scientist
>>> >> >>>> Leadership Computing Facility
>>> >> >>>> Argonne National Laboratory
>>> >> >>
>>> >> >
>>> >> >
>>> >> > -- IMPORTANT NOTICE: The contents of this email and any attachments
>>> are
>>> >> > confidential and may also be privileged. If you are not the intended
>>> >> > recipient, please notify the sender immediately and do not disclose
>>> the
>>> >> > contents to any other person, use it for any purpose, or store or
>>> copy the
>>> >> > information in any medium. Thank you.
>>> >> >
>>> >> > ARM Limited, Registered office 110 Fulbourn Road, Cambridge CB1 9NJ,
>>> >> > Registered in England & Wales, Company No: 2557590
>>> >> > ARM Holdings plc, Registered office 110 Fulbourn Road, Cambridge CB1
>>> >> > 9NJ, Registered in England & Wales, Company No: 2548782
>>> >> >
>>> >>
>>> >> _______________________________________________
>>> >> llvm-commits mailing list
>>> >> llvm-commits at cs.uiuc.edu
>>> >> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>>>
>>
>
> -- IMPORTANT NOTICE: The contents of this email and any attachments are
> confidential and may also be privileged. If you are not the intended
> recipient, please notify the sender immediately and do not disclose the
> contents to any other person, use it for any purpose, or store or copy the
> information in any medium. Thank you.
>
> ARM Limited, Registered office 110 Fulbourn Road, Cambridge CB1 9NJ,
> Registered in England & Wales, Company No: 2557590
> ARM Holdings plc, Registered office 110 Fulbourn Road, Cambridge CB1 9NJ,
> Registered in England & Wales, Company No: 2548782
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150221/93406653/attachment.html>
More information about the llvm-commits
mailing list