Ben Shi via llvm-dev
2020-Jul-11 11:48 UTC
[llvm-dev] Is there a simple way to check APInt multiplication overflow?
Hello, I have two APInt variables, which are got by ConstantSDNode::getAPIntValue(). Is there a simple way to know if their multiplication overflows? For example, if both APInt vars contain a int32_t, APInt a, b; Then I think if (a.getMinSignedBits() + b.getMinSignedBits() > 32) a*b must exceeds int32_t <=> overflow happens; Is my way OK? Ben Shi -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200711/f03a8973/attachment.html>
Roman Lebedev via llvm-dev
2020-Jul-11 11:53 UTC
[llvm-dev] Is there a simple way to check APInt multiplication overflow?
On Sat, Jul 11, 2020 at 2:49 PM Ben Shi via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > Hello, > > I have two APInt variables, which are got by ConstantSDNode::getAPIntValue(). > > Is there a simple way to know if their multiplication overflows?There's APInt::smul_ov(const APInt &RHS, bool &Overflow) https://github.com/llvm/llvm-project/blob/d7a05698efcfa6c596bcaadd8d5154612990f8f3/llvm/include/llvm/ADT/APInt.h#L1115-L1134> For example, if both APInt vars contain a int32_t, > APInt a, b; > Then I think > > if (a.getMinSignedBits() + b.getMinSignedBits() > 32) > a*b must exceeds int32_t <=> overflow happens; > > Is my way OK? > > Ben ShiRoman> _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev