q /= DIGIT(b, USED(b) - 1);
q /= b_msd;
mp_err   s_mp_div(mp_int *a, mp_int *b){  mp_int   quot, rem, t;  mp_word  q;  mp_err   res;  mp_digit d;  int      ix;  if(mp_cmp_z(b) == 0)    return MP_RANGE;  /* Shortcut if b is power of two */  if((ix = s_mp_ispow2(b)) >= 0) {    mp_copy(a, b);  /* need this for remainder */    s_mp_div_2d(a, (mp_digit)ix);    s_mp_mod_2d(b, (mp_digit)ix);    return MP_OKAY;  }  /* Allocate space to store the quotient */  if((res = mp_init_size(&quot, USED(a))) != MP_OKAY)    return res;  /* A working temporary for division     */  if((res = mp_init_size(&t, USED(a))) != MP_OKAY)    goto T;  /* Allocate space for the remainder     */  if((res = mp_init_size(&rem, USED(a))) != MP_OKAY)    goto REM;  /* Normalize to optimize guessing       */  d = s_mp_norm(a, b);  /* Perform the division itself...woo!   */  ix = USED(a) - 1;  while(ix >= 0) {    /* Find a partial substring of a which is at least b */    while(s_mp_cmp(&rem, b) < 0 && ix >= 0) {      if((res = s_mp_lshd(&rem, 1)) != MP_OKAY) 	goto CLEANUP;      if((res = s_mp_lshd(&quot, 1)) != MP_OKAY)	goto CLEANUP;      DIGIT(&rem, 0) = DIGIT(a, ix);      s_mp_clamp(&rem);      --ix;    }    /* If we didn't find one, we're finished dividing    */    if(s_mp_cmp(&rem, b) < 0)       break;        /* Compute a guess for the next quotient digit       */    q = DIGIT(&rem, USED(&rem) - 1);    if(USED(&rem) > 1)      q = (q << DIGIT_BIT) | DIGIT(&rem, USED(&rem) - 2);    q /= DIGIT(b, USED(b) - 1);    if(q == RADIX)      --q;    /* See what that multiplies out to                   */    mp_copy(b, &t);    if((res = s_mp_mul_d(&t, q)) != MP_OKAY)      goto CLEANUP;    /*        If it's too big, back it off.  We should not have to do this       more than once, or, in rare cases, twice.  Knuth describes a       method by which this could be reduced to a maximum of once, but       I didn't implement that here.     */    while(s_mp_cmp(&t, &rem) > 0) {      --q;      s_mp_sub(&t, b);    }    /* At this point, q should be the right next digit   */    if((res = s_mp_sub(&rem, &t)) != MP_OKAY)      goto CLEANUP;    /*      Include the digit in the quotient.  We allocated enough memory      for any quotient we could ever possibly get, so we should not      have to check for failures here     */    DIGIT(&quot, 0) = q;  }  /* Denormalize remainder                */  if(d != 1) {    if((res = s_mp_div_d(&rem, d, NULL)) != MP_OKAY)      goto CLEANUP;  }  s_mp_clamp(&quot);  s_mp_clamp(&rem);  /* Copy quotient back to output         */  s_mp_exch(&quot, a);    /* Copy remainder back to output        */  s_mp_exch(&rem, b);CLEANUP:  mp_clear(&rem);REM:  mp_clear(&t);T:  mp_clear(&quot);  return res;} /* end s_mp_div() */