what is the parameter bool in APInt(unsigned numBits, uint64_t val, bool isSigned = false) used for? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of what is the parameter bool in APInt(unsigned numBits, uint64_t val, bool isSigned = false) used for? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am curious about the third paramter, the boolean isSigned in APInt(unsigned numBits, uint64_t val, bool isSigned = false) from LLVMs llvm/ADT/APInt.h header.

No matter what I set it to, the result of functions like getActiveBits() or getMinSignedBits() does not change at all.

Furthermore if I want get an signed/unsigned value, I use getSExtValue() or getZExtValue().

The value of isSigned does not matter to them either.

So when will isSigned matter?

Answer

TL;DR: isSigned is only important for numBits > 64.

The sizeof(val) is only 64 bits. Imagine you want to store a signed value in a integer with a size > 64 bits.

  • If the value is negativ, all high bits must be set to 1 since negative values are typically stored as two’s complement.

  • If the value is positive, all high bits must be set to 0

Example

Let’s assume you want to store -1 in 128 bits.

The binary representation of uint64_t val holding -1 is

1111111111111111111111111111111111111111111111111111111111111111

This are only 64 ones, so there are 64 bits left to be filled. Without the isSigned value, it would be impossible to know whether these bits should be ones, resulting in -1 or zeros, resulting in 18446744073709551615.

Long explanation

A look a the source code reveals, that isSigned is only used under certain circumstances:

APInt(unsigned numBits, uint64_t val, bool isSigned = false)
     : BitWidth(numBits) {
   assert(BitWidth && "bitwidth too small");
   if (isSingleWord()) {
       U.VAL = val;
       clearUnusedBits();
   } else {
       initSlowCase(val, isSigned);
   }
}

According to its function header, isSingleWord

returns true if the number of bits <= 64, false otherwise.

Therefore the line

if (isSingleWord()) {

checks if the storage for the value occupies more memory than val itself.

If numBits is larger than 64, APInt::initSlowCase gets called:

void APInt::initSlowCase(uint64_t val, bool isSigned) {
   U.pVal = getClearedMemory(getNumWords());
   U.pVal[0] = val;
   if (isSigned && int64_t(val) < 0)
       for (unsigned i = 1; i < getNumWords(); ++i)
           U.pVal[i] = WORDTYPE_MAX;
   clearUnusedBits();
}

This function copies the value from the val variable and fills the bits to numBits.

That is necessary, because begative values are stored as two’s complement. If isSigned is set and val is a negative value, all bits of the high words are set to ones.

We are here to answer your question about what is the parameter bool in APInt(unsigned numBits, uint64_t val, bool isSigned = false) used for? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji