static int[] divide(int quot[],int quotLength,int a[],int aLength,int b[],int bLength){
  int normA[]=new int[aLength + 1];
  int normB[]=new int[bLength + 1];
  int normBLength=bLength;
  int divisorShift=TBigDecimal.numberOfLeadingZeros(b[bLength - 1]);
  if (divisorShift != 0) {
    TBitLevel.shiftLeft(normB,b,0,divisorShift);
    TBitLevel.shiftLeft(normA,a,0,divisorShift);
  }
 else {
    System.arraycopy(a,0,normA,0,aLength);
    System.arraycopy(b,0,normB,0,bLength);
  }
  int firstDivisorDigit=normB[normBLength - 1];
  int i=quotLength - 1;
  int j=aLength;
  while (i >= 0) {
    int guessDigit=0;
    if (normA[j] == firstDivisorDigit) {
      guessDigit=-1;
    }
 else {
      long product=(((normA[j] & 0xffffffffL) << 32) + (normA[j - 1] & 0xffffffffL));
      long res=TDivision.divideLongByInt(product,firstDivisorDigit);
      guessDigit=(int)res;
      int rem=(int)(res >> 32);
      if (guessDigit != 0) {
        long leftHand=0;
        long rightHand=0;
        boolean rOverflowed=false;
        guessDigit++;
        do {
          guessDigit--;
          if (rOverflowed) {
            break;
          }
          leftHand=(guessDigit & 0xffffffffL) * (normB[normBLength - 2] & 0xffffffffL);
          rightHand=((long)rem << 32) + (normA[j - 2] & 0xffffffffL);
          long longR=(rem & 0xffffffffL) + (firstDivisorDigit & 0xffffffffL);
          if (TBigDecimal.numberOfLeadingZeros((int)(longR >>> 32)) < 32) {
            rOverflowed=true;
          }
 else {
            rem=(int)longR;
          }
        }
 while (((leftHand ^ 0x8000000000000000L) > (rightHand ^ 0x8000000000000000L)));
      }
    }
    if (guessDigit != 0) {
      int borrow=TDivision.multiplyAndSubtract(normA,j - normBLength,normB,normBLength,guessDigit);
      if (borrow != 0) {
        guessDigit--;
        long carry=0;
        for (int k=0; k < normBLength; k++) {
          carry+=(normA[j - normBLength + k] & 0xffffffffL) + (normB[k] & 0xffffffffL);
          normA[j - normBLength + k]=(int)carry;
          carry>>>=32;
        }
      }
    }
    if (quot != null) {
      quot[i]=guessDigit;
    }
    j--;
    i--;
  }
  if (divisorShift != 0) {
    TBitLevel.shiftRight(normB,normBLength,normA,0,divisorShift);
    return normB;
  }
  System.arraycopy(normA,0,normB,0,bLength);
  return normA;
}

static int [ ] divide ( int quot [ ] , int quotLength , int a [ ] , int aLength , int b [ ] , int bLength ) { int normA [ ] = new int [ aLength + 1 ] ; int normB [ ] = new int [ bLength + 1 ] ; int normBLength = bLength ; int divisorShift = TBigDecimal . numberOfLeadingZeros ( b [ bLength - 1 ] ) ; if ( divisorShift != 0 ) { TBitLevel . shiftLeft ( normB , b , 0 , divisorShift ) ; TBitLevel . shiftLeft ( normA , a , 0 , divisorShift ) ; } else { System . arraycopy ( a , 0 , normA , 0 , aLength ) ; System . arraycopy ( b , 0 , normB , 0 , bLength ) ; } int firstDivisorDigit = normB [ normBLength - 1 ] ; int i = quotLength - 1 ; int j = aLength ; while ( i >= 0 ) { int guessDigit = 0 ; if ( normA [ j ] == firstDivisorDigit ) { guessDigit = - 1 ; } else { long product = ( ( ( normA [ j ] & 0xffffffffL ) << 32 ) + ( normA [ j - 1 ] & 0xffffffffL ) ) ; long res = TDivision . divideLongByInt ( product , firstDivisorDigit ) ; guessDigit = ( int ) res ; int rem = ( int ) ( res > > 32 ) ; if ( guessDigit != 0 ) { long leftHand = 0 ; long rightHand = 0 ; boolean rOverflowed = false ; guessDigit ++ ; do { guessDigit -- ; if ( rOverflowed ) { break ; } leftHand = ( guessDigit & 0xffffffffL ) * ( normB [ normBLength - 2 ] & 0xffffffffL ) ; rightHand = ( ( long ) rem << 32 ) + ( normA [ j - 2 ] & 0xffffffffL ) ; long longR = ( rem & 0xffffffffL ) + ( firstDivisorDigit & 0xffffffffL ) ; if ( TBigDecimal . numberOfLeadingZeros ( ( int ) ( longR > > > 32 ) ) < 32 ) { rOverflowed = true ; } else { rem = ( int ) longR ; } } while ( ( ( leftHand ^ 0x8000000000000000L ) > ( rightHand ^ 0x8000000000000000L ) ) ) ; } } if ( guessDigit != 0 ) { int borrow = TDivision . multiplyAndSubtract ( normA , j - normBLength , normB , normBLength , guessDigit ) ; if ( borrow != 0 ) { guessDigit -- ; long carry = 0 ; for ( int k = 0 ; k < normBLength ; k ++ ) { carry += ( normA [ j - normBLength + k ] & 0xffffffffL ) + ( normB [ k ] & 0xffffffffL ) ; normA [ j - normBLength + k ] = ( int ) carry ; carry >>>= 32 ; } } } if ( quot != null ) { quot [ i ] = guessDigit ; } j -- ; i -- ; } if ( divisorShift != 0 ) { TBitLevel . shiftRight ( normB , normBLength , normA , 0 , divisorShift ) ; return normB ; } System . arraycopy ( normA , 0 , normB , 0 , bLength ) ; return normA ; }
divides the array ' a ' by the array ' b ' and gets the quotient and the remainder . implements the knuth ' s division algorithm . see d . knuth , the art of computer programming , vol . 2 . steps d1 - d8 correspond the steps in the algorithm description .
