| 920 | // B[NB] -------- divisor |
| 921 | |
| 922 | void Divide(word *R, word *Q, word *T, const word *A, unsigned int NA, const word *B, unsigned int NB) |
| 923 | { |
| 924 | assert(NA && NB && NA%2==0 && NB%2==0); |
| 925 | assert(B[NB-1] || B[NB-2]); |
| 926 | assert(NB <= NA); |
| 927 | |
| 928 | // set up temporary work space |
| 929 | word *const TA=T; |
| 930 | word *const TB=T+NA+2; |
| 931 | word *const TP=T+NA+2+NB; |
| 932 | |
| 933 | // copy B into TB and normalize it so that TB has highest bit set to 1 |
| 934 | unsigned shiftWords = (B[NB-1]==0); |
| 935 | TB[0] = TB[NB-1] = 0; |
| 936 | CopyWords(TB+shiftWords, B, NB-shiftWords); |
| 937 | unsigned shiftBits = WORD_BITS - BitPrecision(TB[NB-1]); |
| 938 | assert(shiftBits < WORD_BITS); |
| 939 | ShiftWordsLeftByBits(TB, NB, shiftBits); |
| 940 | |
| 941 | // copy A into TA and normalize it |
| 942 | TA[0] = TA[NA] = TA[NA+1] = 0; |
| 943 | CopyWords(TA+shiftWords, A, NA); |
| 944 | ShiftWordsLeftByBits(TA, NA+2, shiftBits); |
| 945 | |
| 946 | if (TA[NA+1]==0 && TA[NA] <= 1) |
| 947 | { |
| 948 | Q[NA-NB+1] = Q[NA-NB] = 0; |
| 949 | while (TA[NA] || Compare(TA+NA-NB, TB, NB) >= 0) |
| 950 | { |
| 951 | TA[NA] -= Subtract(TA+NA-NB, TA+NA-NB, TB, NB); |
| 952 | ++Q[NA-NB]; |
| 953 | } |
| 954 | } |
| 955 | else |
| 956 | { |
| 957 | NA+=2; |
| 958 | assert(Compare(TA+NA-NB, TB, NB) < 0); |
| 959 | } |
| 960 | |
| 961 | word B0 = TB[NB-2] + 1; |
| 962 | word B1 = TB[NB-1] + (B0==0); |
| 963 | |
| 964 | // start reducing TA mod TB, 2 words at a time |
| 965 | for (unsigned i=NA-2; i>=NB; i-=2) |
| 966 | { |
| 967 | AtomicDivide(Q[i-NB], Q[i-NB+1], TA+i-2, B0, B1); |
| 968 | CorrectQuotientEstimate(TA+i-NB, TP, Q[i-NB], Q[i-NB+1], TB, NB); |
| 969 | } |
| 970 | |
| 971 | // copy TA into R, and denormalize it |
| 972 | CopyWords(R, TA+shiftWords, NB); |
| 973 | ShiftWordsRightByBits(R, NB, shiftBits); |
| 974 | } |
| 975 | |
| 976 | static inline unsigned int EvenWordCount(const word *X, unsigned int N) |
| 977 | { |
no test coverage detected