Sign extend to a new width.
| 909 | |
| 910 | // Sign extend to a new width. |
| 911 | APInt APInt::sext(unsigned Width) const { |
| 912 | assert(Width > BitWidth && "Invalid APInt SignExtend request"); |
| 913 | |
| 914 | if (Width <= APINT_BITS_PER_WORD) |
| 915 | return APInt(Width, SignExtend64(U.VAL, BitWidth)); |
| 916 | |
| 917 | APInt Result(getMemory(getNumWords(Width)), Width); |
| 918 | |
| 919 | // Copy words. |
| 920 | std::memcpy(Result.U.pVal, getRawData(), getNumWords() * APINT_WORD_SIZE); |
| 921 | |
| 922 | // Sign extend the last word since there may be unused bits in the input. |
| 923 | Result.U.pVal[getNumWords() - 1] = |
| 924 | SignExtend64(Result.U.pVal[getNumWords() - 1], |
| 925 | ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1); |
| 926 | |
| 927 | // Fill with sign bits. |
| 928 | std::memset(Result.U.pVal + getNumWords(), isNegative() ? -1 : 0, |
| 929 | (Result.getNumWords() - getNumWords()) * APINT_WORD_SIZE); |
| 930 | Result.clearUnusedBits(); |
| 931 | return Result; |
| 932 | } |
| 933 | |
| 934 | // Zero extend to a new width. |
| 935 | APInt APInt::zext(unsigned width) const { |
no test coverage detected