| 1597 | } |
| 1598 | |
| 1599 | APInt APInt::udiv(const APInt &RHS) const { |
| 1600 | assert(BitWidth == RHS.BitWidth && "Bit widths must be the same"); |
| 1601 | |
| 1602 | // First, deal with the easy case |
| 1603 | if (isSingleWord()) { |
| 1604 | assert(RHS.U.VAL != 0 && "Divide by zero?"); |
| 1605 | return APInt(BitWidth, U.VAL / RHS.U.VAL); |
| 1606 | } |
| 1607 | |
| 1608 | // Get some facts about the LHS and RHS number of bits and words |
| 1609 | unsigned lhsWords = getNumWords(getActiveBits()); |
| 1610 | unsigned rhsBits = RHS.getActiveBits(); |
| 1611 | unsigned rhsWords = getNumWords(rhsBits); |
| 1612 | assert(rhsWords && "Divided by zero???"); |
| 1613 | |
| 1614 | // Deal with some degenerate cases |
| 1615 | if (!lhsWords) |
| 1616 | // 0 / X ===> 0 |
| 1617 | return APInt(BitWidth, 0); |
| 1618 | if (rhsBits == 1) |
| 1619 | // X / 1 ===> X |
| 1620 | return *this; |
| 1621 | if (lhsWords < rhsWords || this->ult(RHS)) |
| 1622 | // X / Y ===> 0, iff X < Y |
| 1623 | return APInt(BitWidth, 0); |
| 1624 | if (*this == RHS) |
| 1625 | // X / X ===> 1 |
| 1626 | return APInt(BitWidth, 1); |
| 1627 | if (lhsWords == 1) // rhsWords is 1 if lhsWords is 1. |
| 1628 | // All high words are zero, just use native divide |
| 1629 | return APInt(BitWidth, this->U.pVal[0] / RHS.U.pVal[0]); |
| 1630 | |
| 1631 | // We have to compute it the hard way. Invoke the Knuth divide algorithm. |
| 1632 | APInt Quotient(BitWidth, 0); // to hold result. |
| 1633 | divide(U.pVal, lhsWords, RHS.U.pVal, rhsWords, Quotient.U.pVal, nullptr); |
| 1634 | return Quotient; |
| 1635 | } |
| 1636 | |
| 1637 | APInt APInt::udiv(uint64_t RHS) const { |
| 1638 | assert(RHS != 0 && "Divide by zero?"); |
no test coverage detected