<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, May 19, 2014 at 11:52 PM, Marshall Clow <span dir="ltr"><<a href="mailto:mclow.lists@gmail.com" target="_blank">mclow.lists@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class=""><br>
On May 15, 2014, at 9:42 AM, suyog sarda <<a href="mailto:sardask01@gmail.com">sardask01@gmail.com</a>> wrote:<br>
<br>
> Hi,<br>
><br>
> Attaching patch for checking if Signed Addition overflows.<br>
> This patch implements two things :<br>
><br>
> 1. If we know one number is positive and another is negative, we return true as signed addition of two opposite signed numbers will never overflow.<br>
><br>
> 2. Implemented TODO : If one of the operands only has one non-zero bit, and if the other operand has a known-zero bit in a more significant place than it (not including the sign bit) the ripple may go up to and fill the zero, but won't change the sign.<br>

> e.x -  (x & ~4) + 1<br>
> We make sure that we are ignoring 0 at MSB.<br>
<br>
</div>I suspect that you are thinking too hard about this.<br>
a + b will overflow (both positive) if<br>
        MAX_VALUE - a < b<br>
<br>
where MAX_VALUE is the maximum possible positive value for that type.<br>
<br>
Both negative can be handled similarly.<br>
<br>
Bonus is that this doesn’t require the assumption of twos-complement representation.<br></blockquote><div><br></div><div>This will work only if we know that both the operands are known constant. The attached patch works for example like (X & ~4) + 1 given in TODO where one of the operand can be variable.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
[ If course, if we don’t have MAX_VALUE for the type, or the compiler can’t do math on that type, then my suggestion won’t work. ]<br></blockquote><div> </div><div>I am not sure if we have MAX_VALUE. In my view, if we are handling cases where one of the operand can be variable, MAX_VALUE won't work in that case.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
— Marshall<br>
<div class=""> <br></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">
<br>
> 3 Test case included :<br>
><br>
> 1. To check if 0 in one number is at more significant place than 1 in second number. We emit 'add nsw' as we are sure there will be no overflow as the ripple will die at that zero.<br>
> 2. If 0 is at less significant place than one, then emit normal 'add' as we are not sure if the ripple will die before reaching Sign bit.<br>
> 3. IF the two numbers are of opposite sign, we emit 'add nsw' as we are sure that it will never overflow.<br>
><br>
> Please help in reviewing the patch.<br>
><br>
> --<br>
> With regards,<br>
> Suyog Sarda<br>
</div>> <AddOverFlow.patch>_______________________________________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
<br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>With regards,<br>Suyog Sarda<br>
</div></div>