Hi guys, I just noticed that the LLVM has some node for signed/unsigned type( like udiv, sdiv), but why the ADD, SUB do not have the counter part sadd, uadd? best kevin
On Tue, Feb 17, 2015 at 10:41 AM, kewuzhang <kewu.zhang at amd.com> wrote:> Hi guys, > > I just noticed that the LLVM has some node for signed/unsigned type( like udiv, sdiv), but why the ADD, SUB do not have the counter part sadd, uadd?That's just how 2-s complement integers work -- signed and unsigned addition / subtraction are the same at the bit-level (but signed and unsigned division are not). -- Sanjoy
Hi Kevin, On 17 February 2015 at 10:41, kewuzhang <kewu.zhang at amd.com> wrote:> I just noticed that the LLVM has some node for signed/unsigned type( like udiv, sdiv), but why the ADD, SUB do not have the counter part sadd, uadd?That's because in 2s complement arithmetic the bit pattern of the result doesn't depend on whether the operation is signed (unlike multiplication & division). Cheers. Tim.
Thank you very much! best keivn On Feb 17, 2015, at 2:01 PM, Tim Northover <t.p.northover at gmail.com> wrote:> Hi Kevin, > > On 17 February 2015 at 10:41, kewuzhang <kewu.zhang at amd.com> wrote: >> I just noticed that the LLVM has some node for signed/unsigned type( like udiv, sdiv), but why the ADD, SUB do not have the counter part sadd, uadd? > > That's because in 2s complement arithmetic the bit pattern of the > result doesn't depend on whether the operation is signed (unlike > multiplication & division). > > Cheers. > > Tim.
So if the overflow happens for either one of the case, the return value will be implementation dependent? best kevin On Feb 17, 2015, at 2:01 PM, Tim Northover <t.p.northover at gmail.com> wrote:> Hi Kevin, > > On 17 February 2015 at 10:41, kewuzhang <kewu.zhang at amd.com> wrote: >> I just noticed that the LLVM has some node for signed/unsigned type( like udiv, sdiv), but why the ADD, SUB do not have the counter part sadd, uadd? > > That's because in 2s complement arithmetic the bit pattern of the > result doesn't depend on whether the operation is signed (unlike > multiplication & division). > > Cheers. > > Tim.
A hypothetical 'uadd' would have the exact same behavior as 'sadd' when using two's complement. We need both 'udiv' and 'sdiv' because dividing acts differently with negative numbers. For example, dividing by 1 by 0b11111111111111111111111111111111 gives -1 for signed division and '0' for unsigned division. On Tue, Feb 17, 2015 at 10:41 AM, kewuzhang <kewu.zhang at amd.com> wrote:> Hi guys, > > I just noticed that the LLVM has some node for signed/unsigned type( like > udiv, sdiv), but why the ADD, SUB do not have the counter part sadd, uadd? > > best > > kevin > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150217/a73dd732/attachment.html>