| 
									
										
										
										
											2015-11-12 01:49:07 +01:00
										 |  |  | #include <tommath_private.h>
 | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | #ifdef BN_MP_DIV_C
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-08-05 01:24:44 +00:00
										 |  |  |  * LibTomMath is a library that provides multiple-precision | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |  * integer arithmetic as well as number theoretic functionality. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-08-05 01:24:44 +00:00
										 |  |  |  * The library was designed directly after the MPI library by | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |  * Michael Fromberger but has been written from scratch with | 
					
						
							|  |  |  |  * additional optimizations in place. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The library is free for all purposes without any express | 
					
						
							|  |  |  |  * guarantee it works. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-10-30 17:55:29 -04:00
										 |  |  |  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef BN_MP_DIV_SMALL
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* slower bit-bang division... also smaller */ | 
					
						
							|  |  |  | int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |    mp_int ta, tb, tq, q; | 
					
						
							|  |  |  |    int    res, n, n2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* is divisor zero ? */ | 
					
						
							| 
									
										
										
										
											2015-10-25 16:34:43 +01:00
										 |  |  |   if (mp_iszero (b) == MP_YES) { | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |     return MP_VAL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* if a < b then q=0, r = a */ | 
					
						
							|  |  |  |   if (mp_cmp_mag (a, b) == MP_LT) { | 
					
						
							|  |  |  |     if (d != NULL) { | 
					
						
							|  |  |  |       res = mp_copy (a, d); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       res = MP_OKAY; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (c != NULL) { | 
					
						
							|  |  |  |       mp_zero (c); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |   /* init our temps */ | 
					
						
							| 
									
										
										
										
											2015-10-30 17:49:09 -04:00
										 |  |  |   if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |      return res; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mp_set(&tq, 1); | 
					
						
							|  |  |  |   n = mp_count_bits(a) - mp_count_bits(b); | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |   if (((res = mp_abs(a, &ta)) != MP_OKAY) || | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:15 +01:00
										 |  |  |       ((res = mp_abs(b, &tb)) != MP_OKAY) || | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |       ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) || | 
					
						
							|  |  |  |       ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |       goto LBL_ERR; | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while (n-- >= 0) { | 
					
						
							|  |  |  |      if (mp_cmp(&tb, &ta) != MP_GT) { | 
					
						
							|  |  |  |         if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) || | 
					
						
							|  |  |  |             ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |            goto LBL_ERR; | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  |      if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) || | 
					
						
							|  |  |  |          ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |            goto LBL_ERR; | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |      } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* now q == quotient and ta == remainder */ | 
					
						
							|  |  |  |   n  = a->sign; | 
					
						
							| 
									
										
										
										
											2015-11-13 10:28:23 +01:00
										 |  |  |   n2 = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |   if (c != NULL) { | 
					
						
							|  |  |  |      mp_exch(c, &q); | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |      c->sign  = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2; | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (d != NULL) { | 
					
						
							|  |  |  |      mp_exch(d, &ta); | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |      d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n; | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  | LBL_ERR: | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  |    mp_clear_multi(&ta, &tb, &tq, &q, NULL); | 
					
						
							|  |  |  |    return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:15 +01:00
										 |  |  | /* integer signed division.
 | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |  * c*b + d == a [e.g. a/b, c=quotient, d=remainder] | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |  * HAC pp.598 Algorithm 14.20 | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:15 +01:00
										 |  |  |  * Note that the description in HAC is horribly | 
					
						
							|  |  |  |  * incomplete.  For example, it doesn't consider | 
					
						
							|  |  |  |  * the case where digits are removed from 'x' in | 
					
						
							|  |  |  |  * the inner loop.  It also doesn't consider the | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |  * case that y has fewer than three digits, etc.. | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:15 +01:00
										 |  |  |  * The overall algorithm is as described as | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |  * 14.20 from HAC but fixed to treat these cases. | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2003-12-24 18:59:22 +00:00
										 |  |  | int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2003-02-28 16:09:08 +00:00
										 |  |  |   mp_int  q, x, y, t1, t2; | 
					
						
							|  |  |  |   int     res, n, t, i, norm, neg; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* is divisor zero ? */ | 
					
						
							| 
									
										
										
										
											2015-10-25 16:34:43 +01:00
										 |  |  |   if (mp_iszero (b) == MP_YES) { | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     return MP_VAL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* if a < b then q=0, r = a */ | 
					
						
							|  |  |  |   if (mp_cmp_mag (a, b) == MP_LT) { | 
					
						
							|  |  |  |     if (d != NULL) { | 
					
						
							|  |  |  |       res = mp_copy (a, d); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       res = MP_OKAY; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (c != NULL) { | 
					
						
							|  |  |  |       mp_zero (c); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((res = mp_init_size (&q, a->used + 2)) != MP_OKAY) { | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   q.used = a->used + 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((res = mp_init (&t1)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_Q; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((res = mp_init (&t2)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_T1; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((res = mp_init_copy (&x, a)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_T2; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if ((res = mp_init_copy (&y, b)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_X; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* fix the sign */ | 
					
						
							|  |  |  |   neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; | 
					
						
							|  |  |  |   x.sign = y.sign = MP_ZPOS; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |   /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */ | 
					
						
							| 
									
										
										
										
											2003-03-29 18:16:01 +00:00
										 |  |  |   norm = mp_count_bits(&y) % DIGIT_BIT; | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  |   if (norm < (int)(DIGIT_BIT-1)) { | 
					
						
							| 
									
										
										
										
											2003-03-29 18:16:01 +00:00
										 |  |  |      norm = (DIGIT_BIT-1) - norm; | 
					
						
							|  |  |  |      if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |        goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-03-29 18:16:01 +00:00
										 |  |  |      } | 
					
						
							|  |  |  |      if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |        goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-03-29 18:16:01 +00:00
										 |  |  |      } | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |      norm = 0; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   /* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */ | 
					
						
							|  |  |  |   n = x.used - 1; | 
					
						
							|  |  |  |   t = y.used - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |   /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */ | 
					
						
							|  |  |  |   if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */ | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |     goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while (mp_cmp (&x, &y) != MP_LT) { | 
					
						
							|  |  |  |     ++(q.dp[n - t]); | 
					
						
							|  |  |  |     if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |       goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* reset y by shifting it back down */ | 
					
						
							|  |  |  |   mp_rshd (&y, n - t); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* step 3. for i from n down to (t + 1) */ | 
					
						
							|  |  |  |   for (i = n; i >= (t + 1); i--) { | 
					
						
							| 
									
										
										
										
											2003-08-29 14:06:56 +00:00
										 |  |  |     if (i > x.used) { | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |       continue; | 
					
						
							| 
									
										
										
										
											2003-08-29 14:06:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:15 +01:00
										 |  |  |     /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
 | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |      * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     if (x.dp[i] == y.dp[t]) { | 
					
						
							| 
									
										
										
										
											2015-10-17 18:28:05 +02:00
										 |  |  |       q.dp[(i - t) - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2003-02-28 16:09:08 +00:00
										 |  |  |       mp_word tmp; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |       tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT); | 
					
						
							|  |  |  |       tmp |= ((mp_word) x.dp[i - 1]); | 
					
						
							|  |  |  |       tmp /= ((mp_word) y.dp[t]); | 
					
						
							| 
									
										
										
										
											2015-10-11 19:01:04 +02:00
										 |  |  |       if (tmp > (mp_word) MP_MASK) { | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  |         tmp = MP_MASK; | 
					
						
							| 
									
										
										
										
											2015-10-11 19:01:04 +02:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-10-17 18:28:05 +02:00
										 |  |  |       q.dp[(i - t) - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:15 +01:00
										 |  |  |     /* while (q{i-t-1} * (yt * b + y{t-1})) >
 | 
					
						
							|  |  |  |              xi * b**2 + xi-1 * b + xi-2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        do q{i-t-1} -= 1; | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |     */ | 
					
						
							| 
									
										
										
										
											2015-10-17 18:28:05 +02:00
										 |  |  |     q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1) & MP_MASK; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     do { | 
					
						
							| 
									
										
										
										
											2015-10-17 18:28:05 +02:00
										 |  |  |       q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1) & MP_MASK; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       /* find left hand */ | 
					
						
							|  |  |  |       mp_zero (&t1); | 
					
						
							| 
									
										
										
										
											2015-11-13 10:28:23 +01:00
										 |  |  |       t1.dp[0] = ((t - 1) < 0) ? 0 : y.dp[t - 1]; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |       t1.dp[1] = y.dp[t]; | 
					
						
							|  |  |  |       t1.used = 2; | 
					
						
							| 
									
										
										
										
											2015-10-17 18:28:05 +02:00
										 |  |  |       if ((res = mp_mul_d (&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |         goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /* find right hand */ | 
					
						
							| 
									
										
										
										
											2015-11-13 10:28:23 +01:00
										 |  |  |       t2.dp[0] = ((i - 2) < 0) ? 0 : x.dp[i - 2]; | 
					
						
							|  |  |  |       t2.dp[1] = ((i - 1) < 0) ? 0 : x.dp[i - 1]; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |       t2.dp[2] = x.dp[i]; | 
					
						
							|  |  |  |       t2.used = 3; | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  |     } while (mp_cmp_mag(&t1, &t2) == MP_GT); | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |     /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */ | 
					
						
							| 
									
										
										
										
											2015-10-17 18:28:05 +02:00
										 |  |  |     if ((res = mp_mul_d (&y, q.dp[(i - t) - 1], &t1)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |       goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-17 18:28:05 +02:00
										 |  |  |     if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |       goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |       goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |     /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */ | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     if (x.sign == MP_NEG) { | 
					
						
							|  |  |  |       if ((res = mp_copy (&y, &t1)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |         goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-10-17 18:28:05 +02:00
										 |  |  |       if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |         goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |       if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) { | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  |         goto LBL_Y; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-17 18:28:05 +02:00
										 |  |  |       q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1UL) & MP_MASK; | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2003-05-17 12:33:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:15 +01:00
										 |  |  |   /* now q is the quotient and x is the remainder
 | 
					
						
							|  |  |  |    * [which we have to normalize] | 
					
						
							| 
									
										
										
										
											2003-06-19 10:04:50 +00:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   /* get sign before writing to c */ | 
					
						
							| 
									
										
										
										
											2015-11-13 10:28:23 +01:00
										 |  |  |   x.sign = (x.used == 0) ? MP_ZPOS : a->sign; | 
					
						
							| 
									
										
										
										
											2003-03-22 15:10:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   if (c != NULL) { | 
					
						
							|  |  |  |     mp_clamp (&q); | 
					
						
							|  |  |  |     mp_exch (&q, c); | 
					
						
							|  |  |  |     c->sign = neg; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (d != NULL) { | 
					
						
							| 
									
										
										
										
											2015-11-12 01:18:00 +01:00
										 |  |  |     if ((res = mp_div_2d (&x, norm, &x, NULL)) != MP_OKAY) { | 
					
						
							|  |  |  |       goto LBL_Y; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |     mp_exch (&x, d); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   res = MP_OKAY; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-23 02:40:37 +00:00
										 |  |  | LBL_Y:mp_clear (&y); | 
					
						
							|  |  |  | LBL_X:mp_clear (&x); | 
					
						
							|  |  |  | LBL_T2:mp_clear (&t2); | 
					
						
							|  |  |  | LBL_T1:mp_clear (&t1); | 
					
						
							|  |  |  | LBL_Q:mp_clear (&q); | 
					
						
							| 
									
										
										
										
											2003-02-28 16:08:34 +00:00
										 |  |  |   return res; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2004-10-29 22:07:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-08-01 16:37:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* $Source$ */ | 
					
						
							|  |  |  | /* $Revision$ */ | 
					
						
							|  |  |  | /* $Date$ */ |