[llvm-dev] Behaviour of APInt

Sam Parker via llvm-dev llvm-dev at lists.llvm.org
Thu Jan 31 04:56:38 PST 2019


I'm having trouble understanding how APInts should be used.

The APInt documentation states that it 'is a functional replacement for common
case unsigned integer type', but I'm not seeing this because the internal logic
is that the value is always treated as negative if the most significant bit is
set.

I'm interested in an add or sub that could be using a negative value. I have the
following snippet of code to demostrate the issue:

APInt Positive(8, 128, false);
APInt Negative(8, -128, true);
LLVM_DEBUG(dbgs() << "Positive: " << Positive << "\n");
LLVM_DEBUG(dbgs() << "Negative: " << Negative << "\n");
LLVM_DEBUG(dbgs() << "0 + Positive = " << 0 + Positive << "\n");
LLVM_DEBUG(dbgs() << "0 - Positive = " << 0 - Positive << "\n");
LLVM_DEBUG(dbgs() << "0 + Negative = " << 0 + Negative << "\n");
LLVM_DEBUG(dbgs() << "0 - Negative = " << 0 - Negative << "\n");

The output is:

Positive: -128
Negative: -128
0 + Positive = -128
0 - Positive = -128
0 + Negative = -128
0 - Negative = -128

I know there are operators for when the sign matters, but from my example,
either my understanding or the functionality is broken. If an abstract
structure exists, why does the MSB still represent the sign? Especially
when it's supposed to be an unsigned type!


Thanks,

Sam


Sam Parker

Compilation Tools Engineer | Arm

. . . . . . . . . . . . . . . . . . . . . . . . . . .

Arm.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190131/198cc934/attachment.html>


More information about the llvm-dev mailing list