| 1690 | } |
| 1691 | |
| 1692 | APInt APInt::urem(const APInt &RHS) const { |
| 1693 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
| 1694 | if (isSingleWord()) { |
| 1695 | assert(RHS.U.VAL != 0 && "Remainder by zero?"); |
| 1696 | return APInt(BitWidth, U.VAL % RHS.U.VAL); |
| 1697 | } |
| 1698 | |
| 1699 | // Get some facts about the LHS |
| 1700 | unsigned lhsWords = getNumWords(getActiveBits()); |
| 1701 | |
| 1702 | // Get some facts about the RHS |
| 1703 | unsigned rhsBits = RHS.getActiveBits(); |
| 1704 | unsigned rhsWords = getNumWords(rhsBits); |
| 1705 | assert(rhsWords && "Performing remainder operation by zero ???"); |
| 1706 | |
| 1707 | // Check the degenerate cases |
| 1708 | if (lhsWords == 0) |
| 1709 | // 0 % Y ===> 0 |
| 1710 | return APInt(BitWidth, 0); |
| 1711 | if (rhsBits == 1) |
| 1712 | // X % 1 ===> 0 |
| 1713 | return APInt(BitWidth, 0); |
| 1714 | if (lhsWords < rhsWords || this->ult(RHS)) |
| 1715 | // X % Y ===> X, iff X < Y |
| 1716 | return *this; |
| 1717 | if (*this == RHS) |
| 1718 | // X % X == 0; |
| 1719 | return APInt(BitWidth, 0); |
| 1720 | if (lhsWords == 1) |
| 1721 | // All high words are zero, just use native remainder |
| 1722 | return APInt(BitWidth, U.pVal[0] % RHS.U.pVal[0]); |
| 1723 | |
| 1724 | // We have to compute it the hard way. Invoke the Knuth divide algorithm. |
| 1725 | APInt Remainder(BitWidth, 0); |
| 1726 | divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, nullptr, Remainder.U.pVal); |
| 1727 | return Remainder; |
| 1728 | } |
| 1729 | |
| 1730 | uint64_t APInt::urem(uint64_t RHS) const { |
| 1731 | assert(RHS != 0 && "Remainder by zero?"); |
no test coverage detected