[APInt] Add a public typedef for the internal type of APInt use it instead of integerPart. Make APINT_BITS_PER_WORD and APINT_WORD_SIZE public.

This patch is one step to attempt to unify the main APInt interface and the tc functions used by APFloat.

This patch adds a WordType to APInt and uses that in all the tc functions. I've added temporary typedefs to APFloat to alias it to integerPart to keep the patch size down. I'll work on removing that in a future patch.

In future patches I hope to reuse the tc functions to implement some of the main APInt functionality.

I may remove APINT_ from BITS_PER_WORD and WORD_SIZE constants so that we don't have the repetitive APInt::APINT_ externally.

Differential Revision: https://reviews.llvm.org/D31523

llvm-svn: 299341
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index 904e9bf..9778628 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -37,6 +37,10 @@
 
 using namespace llvm;
 
+// TODO: Remove these and use APInt qualified types directly.
+typedef APInt::WordType integerPart;
+const unsigned int integerPartWidth = APInt::APINT_BITS_PER_WORD;
+
 /// A macro used to combine two fcCategory enums into one key which can be used
 /// in a switch statement to classify how the interaction of two APFloat's
 /// categories affects an operation.
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 0617c89..6f5df52 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -700,7 +700,7 @@
 unsigned APInt::countLeadingZerosSlowCase() const {
   unsigned Count = 0;
   for (int i = getNumWords()-1; i >= 0; --i) {
-    integerPart V = pVal[i];
+    uint64_t V = pVal[i];
     if (V == 0)
       Count += APINT_BITS_PER_WORD;
     else {
@@ -2313,43 +2313,44 @@
 
 // Assumed by lowHalf, highHalf, partMSB and partLSB.  A fairly safe
 // and unrestricting assumption.
-static_assert(integerPartWidth % 2 == 0, "Part width must be divisible by 2!");
+static_assert(APInt::APINT_BITS_PER_WORD % 2 == 0,
+              "Part width must be divisible by 2!");
 
 /* Some handy functions local to this file.  */
 
 /* Returns the integer part with the least significant BITS set.
    BITS cannot be zero.  */
-static inline integerPart lowBitMask(unsigned bits) {
-  assert(bits != 0 && bits <= integerPartWidth);
+static inline APInt::WordType lowBitMask(unsigned bits) {
+  assert(bits != 0 && bits <= APInt::APINT_BITS_PER_WORD);
 
-  return ~(integerPart) 0 >> (integerPartWidth - bits);
+  return ~(APInt::WordType) 0 >> (APInt::APINT_BITS_PER_WORD - bits);
 }
 
 /* Returns the value of the lower half of PART.  */
-static inline integerPart lowHalf(integerPart part) {
-  return part & lowBitMask(integerPartWidth / 2);
+static inline APInt::WordType lowHalf(APInt::WordType part) {
+  return part & lowBitMask(APInt::APINT_BITS_PER_WORD / 2);
 }
 
 /* Returns the value of the upper half of PART.  */
-static inline integerPart highHalf(integerPart part) {
-  return part >> (integerPartWidth / 2);
+static inline APInt::WordType highHalf(APInt::WordType part) {
+  return part >> (APInt::APINT_BITS_PER_WORD / 2);
 }
 
 /* Returns the bit number of the most significant set bit of a part.
    If the input number has no bits set -1U is returned.  */
-static unsigned partMSB(integerPart value) {
+static unsigned partMSB(APInt::WordType value) {
   return findLastSet(value, ZB_Max);
 }
 
 /* Returns the bit number of the least significant set bit of a
    part.  If the input number has no bits set -1U is returned.  */
-static unsigned partLSB(integerPart value) {
+static unsigned partLSB(APInt::WordType value) {
   return findFirstSet(value, ZB_Max);
 }
 
 /* Sets the least significant part of a bignum to the input value, and
    zeroes out higher parts.  */
-void APInt::tcSet(integerPart *dst, integerPart part, unsigned parts) {
+void APInt::tcSet(WordType *dst, WordType part, unsigned parts) {
   assert(parts > 0);
 
   dst[0] = part;
@@ -2358,13 +2359,13 @@
 }
 
 /* Assign one bignum to another.  */
-void APInt::tcAssign(integerPart *dst, const integerPart *src, unsigned parts) {
+void APInt::tcAssign(WordType *dst, const WordType *src, unsigned parts) {
   for (unsigned i = 0; i < parts; i++)
     dst[i] = src[i];
 }
 
 /* Returns true if a bignum is zero, false otherwise.  */
-bool APInt::tcIsZero(const integerPart *src, unsigned parts) {
+bool APInt::tcIsZero(const WordType *src, unsigned parts) {
   for (unsigned i = 0; i < parts; i++)
     if (src[i])
       return false;
@@ -2373,30 +2374,30 @@
 }
 
 /* Extract the given bit of a bignum; returns 0 or 1.  */
-int APInt::tcExtractBit(const integerPart *parts, unsigned bit) {
-  return (parts[bit / integerPartWidth] &
-          ((integerPart) 1 << bit % integerPartWidth)) != 0;
+int APInt::tcExtractBit(const WordType *parts, unsigned bit) {
+  return (parts[bit / APINT_BITS_PER_WORD] &
+          ((WordType) 1 << bit % APINT_BITS_PER_WORD)) != 0;
 }
 
 /* Set the given bit of a bignum. */
-void APInt::tcSetBit(integerPart *parts, unsigned bit) {
-  parts[bit / integerPartWidth] |= (integerPart) 1 << (bit % integerPartWidth);
+void APInt::tcSetBit(WordType *parts, unsigned bit) {
+  parts[bit / APINT_BITS_PER_WORD] |= (WordType) 1 << (bit % APINT_BITS_PER_WORD);
 }
 
 /* Clears the given bit of a bignum. */
-void APInt::tcClearBit(integerPart *parts, unsigned bit) {
-  parts[bit / integerPartWidth] &=
-    ~((integerPart) 1 << (bit % integerPartWidth));
+void APInt::tcClearBit(WordType *parts, unsigned bit) {
+  parts[bit / APINT_BITS_PER_WORD] &=
+    ~((WordType) 1 << (bit % APINT_BITS_PER_WORD));
 }
 
 /* Returns the bit number of the least significant set bit of a
    number.  If the input number has no bits set -1U is returned.  */
-unsigned APInt::tcLSB(const integerPart *parts, unsigned n) {
+unsigned APInt::tcLSB(const WordType *parts, unsigned n) {
   for (unsigned i = 0; i < n; i++) {
     if (parts[i] != 0) {
       unsigned lsb = partLSB(parts[i]);
 
-      return lsb + i * integerPartWidth;
+      return lsb + i * APINT_BITS_PER_WORD;
     }
   }
 
@@ -2405,14 +2406,14 @@
 
 /* Returns the bit number of the most significant set bit of a number.
    If the input number has no bits set -1U is returned.  */
-unsigned APInt::tcMSB(const integerPart *parts, unsigned n) {
+unsigned APInt::tcMSB(const WordType *parts, unsigned n) {
   do {
     --n;
 
     if (parts[n] != 0) {
       unsigned msb = partMSB(parts[n]);
 
-      return msb + n * integerPartWidth;
+      return msb + n * APINT_BITS_PER_WORD;
     }
   } while (n);
 
@@ -2424,28 +2425,28 @@
    the least significant bit of DST.  All high bits above srcBITS in
    DST are zero-filled.  */
 void
-APInt::tcExtract(integerPart *dst, unsigned dstCount, const integerPart *src,
+APInt::tcExtract(WordType *dst, unsigned dstCount, const WordType *src,
                  unsigned srcBits, unsigned srcLSB) {
-  unsigned dstParts = (srcBits + integerPartWidth - 1) / integerPartWidth;
+  unsigned dstParts = (srcBits + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
   assert(dstParts <= dstCount);
 
-  unsigned firstSrcPart = srcLSB / integerPartWidth;
+  unsigned firstSrcPart = srcLSB / APINT_BITS_PER_WORD;
   tcAssign (dst, src + firstSrcPart, dstParts);
 
-  unsigned shift = srcLSB % integerPartWidth;
+  unsigned shift = srcLSB % APINT_BITS_PER_WORD;
   tcShiftRight (dst, dstParts, shift);
 
-  /* We now have (dstParts * integerPartWidth - shift) bits from SRC
+  /* We now have (dstParts * APINT_BITS_PER_WORD - shift) bits from SRC
      in DST.  If this is less that srcBits, append the rest, else
      clear the high bits.  */
-  unsigned n = dstParts * integerPartWidth - shift;
+  unsigned n = dstParts * APINT_BITS_PER_WORD - shift;
   if (n < srcBits) {
-    integerPart mask = lowBitMask (srcBits - n);
+    WordType mask = lowBitMask (srcBits - n);
     dst[dstParts - 1] |= ((src[firstSrcPart + dstParts] & mask)
-                          << n % integerPartWidth);
+                          << n % APINT_BITS_PER_WORD);
   } else if (n > srcBits) {
-    if (srcBits % integerPartWidth)
-      dst[dstParts - 1] &= lowBitMask (srcBits % integerPartWidth);
+    if (srcBits % APINT_BITS_PER_WORD)
+      dst[dstParts - 1] &= lowBitMask (srcBits % APINT_BITS_PER_WORD);
   }
 
   /* Clear high parts.  */
@@ -2454,12 +2455,12 @@
 }
 
 /* DST += RHS + C where C is zero or one.  Returns the carry flag.  */
-integerPart APInt::tcAdd(integerPart *dst, const integerPart *rhs,
-                         integerPart c, unsigned parts) {
+APInt::WordType APInt::tcAdd(WordType *dst, const WordType *rhs,
+                             WordType c, unsigned parts) {
   assert(c <= 1);
 
   for (unsigned i = 0; i < parts; i++) {
-    integerPart l = dst[i];
+    WordType l = dst[i];
     if (c) {
       dst[i] += rhs[i] + 1;
       c = (dst[i] <= l);
@@ -2473,13 +2474,12 @@
 }
 
 /* DST -= RHS + C where C is zero or one.  Returns the carry flag.  */
-integerPart APInt::tcSubtract(integerPart *dst, const integerPart *rhs,
-                              integerPart c, unsigned parts)
-{
+APInt::WordType APInt::tcSubtract(WordType *dst, const WordType *rhs,
+                                  WordType c, unsigned parts) {
   assert(c <= 1);
 
   for (unsigned i = 0; i < parts; i++) {
-    integerPart l = dst[i];
+    WordType l = dst[i];
     if (c) {
       dst[i] -= rhs[i] + 1;
       c = (dst[i] >= l);
@@ -2493,7 +2493,7 @@
 }
 
 /* Negate a bignum in-place.  */
-void APInt::tcNegate(integerPart *dst, unsigned parts) {
+void APInt::tcNegate(WordType *dst, unsigned parts) {
   tcComplement(dst, parts);
   tcIncrement(dst, parts);
 }
@@ -2509,8 +2509,8 @@
     DSTPARTS parts of the result, and if all of the omitted higher
     parts were zero return zero, otherwise overflow occurred and
     return one.  */
-int APInt::tcMultiplyPart(integerPart *dst, const integerPart *src,
-                          integerPart multiplier, integerPart carry,
+int APInt::tcMultiplyPart(WordType *dst, const WordType *src,
+                          WordType multiplier, WordType carry,
                           unsigned srcParts, unsigned dstParts,
                           bool add) {
   /* Otherwise our writes of DST kill our later reads of SRC.  */
@@ -2522,7 +2522,7 @@
 
   unsigned i;
   for (i = 0; i < n; i++) {
-    integerPart low, mid, high, srcPart;
+    WordType low, mid, high, srcPart;
 
       /* [ LOW, HIGH ] = MULTIPLIER * SRC[i] + DST[i] + CARRY.
 
@@ -2543,14 +2543,14 @@
 
       mid = lowHalf(srcPart) * highHalf(multiplier);
       high += highHalf(mid);
-      mid <<= integerPartWidth / 2;
+      mid <<= APINT_BITS_PER_WORD / 2;
       if (low + mid < low)
         high++;
       low += mid;
 
       mid = highHalf(srcPart) * lowHalf(multiplier);
       high += highHalf(mid);
-      mid <<= integerPartWidth / 2;
+      mid <<= APINT_BITS_PER_WORD / 2;
       if (low + mid < low)
         high++;
       low += mid;
@@ -2599,8 +2599,8 @@
    is filled with the least significant parts of the result.  Returns
    one if overflow occurred, otherwise zero.  DST must be disjoint
    from both operands.  */
-int APInt::tcMultiply(integerPart *dst, const integerPart *lhs,
-                      const integerPart *rhs, unsigned parts) {
+int APInt::tcMultiply(WordType *dst, const WordType *lhs,
+                      const WordType *rhs, unsigned parts) {
   assert(dst != lhs && dst != rhs);
 
   int overflow = 0;
@@ -2617,8 +2617,8 @@
    operands.  No overflow occurs.  DST must be disjoint from both
    operands.  Returns the number of parts required to hold the
    result.  */
-unsigned APInt::tcFullMultiply(integerPart *dst, const integerPart *lhs,
-                               const integerPart *rhs, unsigned lhsParts,
+unsigned APInt::tcFullMultiply(WordType *dst, const WordType *lhs,
+                               const WordType *rhs, unsigned lhsParts,
                                unsigned rhsParts) {
   /* Put the narrower number on the LHS for less loops below.  */
   if (lhsParts > rhsParts) {
@@ -2647,8 +2647,8 @@
    use by the routine; its contents need not be initialized and are
    destroyed.  LHS, REMAINDER and SCRATCH must be distinct.
 */
-int APInt::tcDivide(integerPart *lhs, const integerPart *rhs,
-                    integerPart *remainder, integerPart *srhs,
+int APInt::tcDivide(WordType *lhs, const WordType *rhs,
+                    WordType *remainder, WordType *srhs,
                     unsigned parts) {
   assert(lhs != remainder && lhs != srhs && remainder != srhs);
 
@@ -2656,9 +2656,9 @@
   if (shiftCount == 0)
     return true;
 
-  shiftCount = parts * integerPartWidth - shiftCount;
-  unsigned n = shiftCount / integerPartWidth;
-  integerPart mask = (integerPart) 1 << (shiftCount % integerPartWidth);
+  shiftCount = parts * APINT_BITS_PER_WORD - shiftCount;
+  unsigned n = shiftCount / APINT_BITS_PER_WORD;
+  WordType mask = (WordType) 1 << (shiftCount % APINT_BITS_PER_WORD);
 
   tcAssign(srhs, rhs, parts);
   tcShiftLeft(srhs, parts, shiftCount);
@@ -2681,7 +2681,7 @@
       shiftCount--;
       tcShiftRight(srhs, parts, 1);
       if ((mask >>= 1) == 0) {
-        mask = (integerPart) 1 << (integerPartWidth - 1);
+        mask = (WordType) 1 << (APINT_BITS_PER_WORD - 1);
         n--;
       }
   }
@@ -2691,14 +2691,14 @@
 
 /* Shift a bignum left COUNT bits in-place.  Shifted in bits are zero.
    There are no restrictions on COUNT.  */
-void APInt::tcShiftLeft(integerPart *dst, unsigned parts, unsigned count) {
+void APInt::tcShiftLeft(WordType *dst, unsigned parts, unsigned count) {
   if (count) {
     /* Jump is the inter-part jump; shift is is intra-part shift.  */
-    unsigned jump = count / integerPartWidth;
-    unsigned shift = count % integerPartWidth;
+    unsigned jump = count / APINT_BITS_PER_WORD;
+    unsigned shift = count % APINT_BITS_PER_WORD;
 
     while (parts > jump) {
-      integerPart part;
+      WordType part;
 
       parts--;
 
@@ -2708,7 +2708,7 @@
       if (shift) {
         part <<= shift;
         if (parts >= jump + 1)
-          part |= dst[parts - jump - 1] >> (integerPartWidth - shift);
+          part |= dst[parts - jump - 1] >> (APINT_BITS_PER_WORD - shift);
       }
 
       dst[parts] = part;
@@ -2721,16 +2721,16 @@
 
 /* Shift a bignum right COUNT bits in-place.  Shifted in bits are
    zero.  There are no restrictions on COUNT.  */
-void APInt::tcShiftRight(integerPart *dst, unsigned parts, unsigned count) {
+void APInt::tcShiftRight(WordType *dst, unsigned parts, unsigned count) {
   if (count) {
     /* Jump is the inter-part jump; shift is is intra-part shift.  */
-    unsigned jump = count / integerPartWidth;
-    unsigned shift = count % integerPartWidth;
+    unsigned jump = count / APINT_BITS_PER_WORD;
+    unsigned shift = count % APINT_BITS_PER_WORD;
 
     /* Perform the shift.  This leaves the most significant COUNT bits
        of the result at zero.  */
     for (unsigned i = 0; i < parts; i++) {
-      integerPart part;
+      WordType part;
 
       if (i + jump >= parts) {
         part = 0;
@@ -2739,7 +2739,7 @@
         if (shift) {
           part >>= shift;
           if (i + jump + 1 < parts)
-            part |= dst[i + jump + 1] << (integerPartWidth - shift);
+            part |= dst[i + jump + 1] << (APINT_BITS_PER_WORD - shift);
         }
       }
 
@@ -2749,31 +2749,31 @@
 }
 
 /* Bitwise and of two bignums.  */
-void APInt::tcAnd(integerPart *dst, const integerPart *rhs, unsigned parts) {
+void APInt::tcAnd(WordType *dst, const WordType *rhs, unsigned parts) {
   for (unsigned i = 0; i < parts; i++)
     dst[i] &= rhs[i];
 }
 
 /* Bitwise inclusive or of two bignums.  */
-void APInt::tcOr(integerPart *dst, const integerPart *rhs, unsigned parts) {
+void APInt::tcOr(WordType *dst, const WordType *rhs, unsigned parts) {
   for (unsigned i = 0; i < parts; i++)
     dst[i] |= rhs[i];
 }
 
 /* Bitwise exclusive or of two bignums.  */
-void APInt::tcXor(integerPart *dst, const integerPart *rhs, unsigned parts) {
+void APInt::tcXor(WordType *dst, const WordType *rhs, unsigned parts) {
   for (unsigned i = 0; i < parts; i++)
     dst[i] ^= rhs[i];
 }
 
 /* Complement a bignum in-place.  */
-void APInt::tcComplement(integerPart *dst, unsigned parts) {
+void APInt::tcComplement(WordType *dst, unsigned parts) {
   for (unsigned i = 0; i < parts; i++)
     dst[i] = ~dst[i];
 }
 
 /* Comparison (unsigned) of two bignums.  */
-int APInt::tcCompare(const integerPart *lhs, const integerPart *rhs,
+int APInt::tcCompare(const WordType *lhs, const WordType *rhs,
                      unsigned parts) {
   while (parts) {
     parts--;
@@ -2787,7 +2787,7 @@
 }
 
 /* Increment a bignum in-place, return the carry flag.  */
-integerPart APInt::tcIncrement(integerPart *dst, unsigned parts) {
+APInt::WordType APInt::tcIncrement(WordType *dst, unsigned parts) {
   unsigned i;
   for (i = 0; i < parts; i++)
     if (++dst[i] != 0)
@@ -2797,7 +2797,7 @@
 }
 
 /* Decrement a bignum in-place, return the borrow flag.  */
-integerPart APInt::tcDecrement(integerPart *dst, unsigned parts) {
+APInt::WordType APInt::tcDecrement(WordType *dst, unsigned parts) {
   for (unsigned i = 0; i < parts; i++) {
     // If the current word is non-zero, then the decrement has no effect on the
     // higher-order words of the integer and no borrow can occur. Exit early.
@@ -2811,16 +2811,16 @@
 
 /* Set the least significant BITS bits of a bignum, clear the
    rest.  */
-void APInt::tcSetLeastSignificantBits(integerPart *dst, unsigned parts,
+void APInt::tcSetLeastSignificantBits(WordType *dst, unsigned parts,
                                       unsigned bits) {
   unsigned i = 0;
-  while (bits > integerPartWidth) {
-    dst[i++] = ~(integerPart) 0;
-    bits -= integerPartWidth;
+  while (bits > APINT_BITS_PER_WORD) {
+    dst[i++] = ~(WordType) 0;
+    bits -= APINT_BITS_PER_WORD;
   }
 
   if (bits)
-    dst[i++] = ~(integerPart) 0 >> (integerPartWidth - bits);
+    dst[i++] = ~(WordType) 0 >> (APINT_BITS_PER_WORD - bits);
 
   while (i < parts)
     dst[i++] = 0;