+void APInt::udivrem(const APInt &LHS, const APInt &RHS,
+ APInt &Quotient, APInt &Remainder) {
+ // Get some size facts about the dividend and divisor
+ uint32_t lhsBits = LHS.getActiveBits();
+ uint32_t lhsWords = !lhsBits ? 0 : (APInt::whichWord(lhsBits - 1) + 1);
+ uint32_t rhsBits = RHS.getActiveBits();
+ uint32_t rhsWords = !rhsBits ? 0 : (APInt::whichWord(rhsBits - 1) + 1);
+
+ // Check the degenerate cases
+ if (lhsWords == 0) {
+ Quotient = 0; // 0 / Y ===> 0
+ Remainder = 0; // 0 % Y ===> 0
+ return;
+ }
+
+ if (lhsWords < rhsWords || LHS.ult(RHS)) {
+ Quotient = 0; // X / Y ===> 0, iff X < Y
+ Remainder = LHS; // X % Y ===> X, iff X < Y
+ return;
+ }
+
+ if (LHS == RHS) {
+ Quotient = 1; // X / X ===> 1
+ Remainder = 0; // X % X ===> 0;
+ return;
+ }
+
+ if (lhsWords == 1 && rhsWords == 1) {
+ // There is only one word to consider so use the native versions.
+ if (LHS.isSingleWord()) {
+ Quotient = APInt(LHS.getBitWidth(), LHS.VAL / RHS.VAL);
+ Remainder = APInt(LHS.getBitWidth(), LHS.VAL % RHS.VAL);
+ } else {
+ Quotient = APInt(LHS.getBitWidth(), LHS.pVal[0] / RHS.pVal[0]);
+ Remainder = APInt(LHS.getBitWidth(), LHS.pVal[0] % RHS.pVal[0]);
+ }
+ return;
+ }
+
+ // Okay, lets do it the long way
+ divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
+}
+