/* Temporary storage: Needs n limbs for the quotient, at qp. tp must point to an area large enough for the resulting cofactor, plus one limb extra. All in all, 2N + 1 if N is a bound for both inputs and outputs. */ mp_size_t mpn_gcdext_subdiv_step (mp_ptr gp, mp_size_t *gn, mp_ptr up, mp_size_t *usizep, mp_ptr ap, mp_ptr bp, mp_size_t n, mp_ptr u0, mp_ptr u1, mp_size_t *unp, mp_ptr qp, mp_ptr tp) { mp_size_t an, bn, un; mp_size_t qn; mp_size_t u0n; int swapped; an = bn = n; ASSERT (an > 0); ASSERT (ap[an-1] > 0 || bp[an-1] > 0); MPN_NORMALIZE (ap, an); MPN_NORMALIZE (bp, bn); un = *unp; swapped = 0; if (UNLIKELY (an == 0)) { return_b: MPN_COPY (gp, bp, bn); *gn = bn; MPN_NORMALIZE (u0, un); MPN_COPY (up, u0, un); *usizep = swapped ? un : -un; return 0; } else if (UNLIKELY (bn == 0)) { MPN_COPY (gp, ap, an); *gn = an; MPN_NORMALIZE (u1, un); MPN_COPY (up, u1, un); *usizep = swapped ? -un : un; return 0; } /* Arrange so that a > b, subtract an -= bn, and maintain normalization. */ if (an < bn) { MPN_PTR_SWAP (ap, an, bp, bn); MP_PTR_SWAP (u0, u1); swapped ^= 1; } else if (an == bn) { int c; MPN_CMP (c, ap, bp, an); if (UNLIKELY (c == 0)) { MPN_COPY (gp, ap, an); *gn = an; /* Must return the smallest cofactor, +u1 or -u0 */ MPN_CMP (c, u0, u1, un); ASSERT (c != 0 || (un == 1 && u0[0] == 1 && u1[0] == 1)); if (c < 0) { MPN_NORMALIZE (u0, un); MPN_COPY (up, u0, un); swapped ^= 1; } else { MPN_NORMALIZE_NOT_ZERO (u1, un); MPN_COPY (up, u1, un); } *usizep = swapped ? -un : un; return 0; } else if (c < 0) { MP_PTR_SWAP (ap, bp); MP_PTR_SWAP (u0, u1); swapped ^= 1; } } /* Reduce a -= b, u1 += u0 */ ASSERT_NOCARRY (mpn_sub (ap, ap, an, bp, bn)); MPN_NORMALIZE (ap, an); ASSERT (an > 0); u1[un] = mpn_add_n (u1, u1, u0, un); un += (u1[un] > 0); /* Arrange so that a > b, and divide a = q b + r */ if (an < bn) { MPN_PTR_SWAP (ap, an, bp, bn); MP_PTR_SWAP (u0, u1); swapped ^= 1; } else if (an == bn) { int c; MPN_CMP (c, ap, bp, an); if (UNLIKELY (c == 0)) goto return_b; else if (c < 0) { MP_PTR_SWAP (ap, bp); MP_PTR_SWAP (u0, u1); swapped ^= 1; } } /* Reduce a -= q b, u1 += q u0 */ qn = an - bn + 1; mpn_tdiv_qr (qp, ap, 0, ap, an, bp, bn); if (mpn_zero_p (ap, bn)) goto return_b; n = bn; /* Update u1 += q u0 */ u0n = un; MPN_NORMALIZE (u0, u0n); if (u0n > 0) { qn -= (qp[qn - 1] == 0); if (qn > u0n) mpn_mul (tp, qp, qn, u0, u0n); else mpn_mul (tp, u0, u0n, qp, qn); if (qn + u0n > un) { mp_size_t u1n = un; un = qn + u0n; un -= (tp[un-1] == 0); u1[un] = mpn_add (u1, tp, un, u1, u1n); } else { u1[un] = mpn_add (u1, u1, un, tp, qn + u0n); } un += (u1[un] > 0); } *unp = un; return n; }
/* Perform a few steps, using some of mpn_nhgcd2, subtraction and division. Reduces the size by almost one limb or more, but never below the given size s. Return new size for a and b, or 0 if no more steps are possible. M = NULL is allowed, if M is not needed. Needs temporary space for division, n + 1 limbs, and for ngcd_matrix1_vector, n limbs. */ mp_size_t mpn_ngcd_step (mp_size_t n, mp_ptr ap, mp_ptr bp, mp_size_t s, struct ngcd_matrix *M, mp_ptr tp) { struct ngcd_matrix1 M1; mp_limb_t mask; mp_limb_t ah, al, bh, bl; mp_size_t an, bn, qn; mp_ptr qp; mp_ptr rp; int col; ASSERT (n > s); mask = ap[n-1] | bp[n-1]; ASSERT (mask > 0); if (n == s + 1) { if (mask < 4) goto subtract; ah = ap[n-1]; al = ap[n-2]; bh = bp[n-1]; bl = bp[n-2]; } else if (mask & GMP_NUMB_HIGHBIT) { ah = ap[n-1]; al = ap[n-2]; bh = bp[n-1]; bl = bp[n-2]; } else { int shift; count_leading_zeros (shift, mask); ah = MPN_EXTRACT_LIMB (shift, ap[n-1], ap[n-2]); al = MPN_EXTRACT_LIMB (shift, ap[n-2], ap[n-3]); bh = MPN_EXTRACT_LIMB (shift, bp[n-1], bp[n-2]); bl = MPN_EXTRACT_LIMB (shift, bp[n-2], bp[n-3]); } /* Try an mpn_nhgcd2 step */ if (mpn_nhgcd2 (ah, al, bh, bl, &M1)) { /* Multiply M <- M * M1 */ if (M) ngcd_matrix_mul_1 (M, &M1); /* Multiply M1^{-1} (a;b) */ return mpn_ngcd_matrix1_vector (&M1, n, ap, bp, tp); } subtract: /* There are two ways in which mpn_nhgcd2 can fail. Either one of ah and bh was too small, or ah, bh were (almost) equal. Perform one subtraction step (for possible cancellation of high limbs), followed by one division. */ /* Since we must ensure that #(a-b) > s, we handle cancellation of high limbs explicitly up front. (FIXME: Or is it better to just subtract, normalize, and use an addition to undo if it turns out the the difference is too small?) */ for (an = n; an > s; an--) if (ap[an-1] != bp[an-1]) break; if (an == s) return 0; /* Maintain a > b. When needed, swap a and b, and let col keep track of how to update M. */ if (ap[an-1] > bp[an-1]) { /* a is largest. In the subtraction step, we need to update column 1 of M */ col = 1; } else { MP_PTR_SWAP (ap, bp); col = 0; } bn = n; MPN_NORMALIZE (bp, bn); if (bn <= s) return 0; /* We have #a, #b > s. When is it possible that #(a-b) < s? For cancellation to happen, the numbers must be of the form a = x + 1, 0, ..., 0, al b = x , GMP_NUMB_MAX, ..., GMP_NUMB_MAX, bl where al, bl denotes the least significant k limbs. If al < bl, then #(a-b) < k, and if also high(al) != 0, high(bl) != GMP_NUMB_MAX, then #(a-b) = k. If al >= bl, then #(a-b) = k + 1. */ if (ap[an-1] == bp[an-1] + 1) { mp_size_t k; int c; for (k = an-1; k > s; k--) if (ap[k-1] != 0 || bp[k-1] != GMP_NUMB_MAX) break; MPN_CMP (c, ap, bp, k); if (c < 0) { mp_limb_t cy; /* The limbs from k and up are cancelled. */ if (k == s) return 0; cy = mpn_sub_n (ap, ap, bp, k); ASSERT (cy == 1); an = k; } else { ASSERT_NOCARRY (mpn_sub_n (ap, ap, bp, k)); ap[k] = 1; an = k + 1; } } else ASSERT_NOCARRY (mpn_sub_n (ap, ap, bp, an)); ASSERT (an > s); ASSERT (ap[an-1] > 0); ASSERT (bn > s); ASSERT (bp[bn-1] > 0); if (M) ngcd_matrix_update_1 (M, col); if (an < bn) { MPN_PTR_SWAP (ap, an, bp, bn); col ^= 1; } else if (an == bn) { int c; MPN_CMP (c, ap, bp, an); if (c < 0) { MP_PTR_SWAP (ap, bp); col ^= 1; } } /* Divide a / b. Store first the quotient (qn limbs) and then the remainder (bn limbs) starting at tp. */ qn = an + 1 - bn; qp = tp; rp = tp + qn; /* FIXME: We could use an approximate division, that may return a too small quotient, and only guarantess that the size of r is almost the size of b. */ mpn_tdiv_qr (qp, rp, 0, ap, an, bp, bn); qn -= (qp[qn -1] == 0); /* Normalize remainder */ an = bn; for ( ; an > s; an--) if (rp[an-1] > 0) break; if (an > s) /* Include leading zero limbs */ MPN_COPY (ap, rp, bn); else { /* Quotient is too large */ mp_limb_t cy; cy = mpn_add (ap, bp, bn, rp, an); if (cy > 0) { ASSERT (bn < n); ap[bn] = cy; bp[bn] = 0; bn++; } MPN_DECR_U (qp, qn, 1); qn -= (qp[qn-1] == 0); } if (qn > 0 && M) ngcd_matrix_update_q (M, qp, qn, col); return bn; }
mp_size_t mpn_ngcdext_subdiv_step (mp_ptr gp, mp_size_t *gn, mp_ptr s0p, mp_ptr u0, mp_ptr u1, mp_size_t *un, mp_ptr ap, mp_ptr bp, mp_size_t n, mp_ptr tp) { /* Called when nhgcd or mpn_nhgcd2 has failed. Then either one of a or b is very small, or the difference is very small. Perform one subtraction followed by one division. */ mp_size_t an, bn, cy, qn, qn2, u0n, u1n; int negate = 0; int c; ASSERT (n > 0); ASSERT (ap[n-1] > 0 || bp[n-1] > 0); /* See to what extend ap and bp are the same */ for (an = n; an > 0; an--) if (ap[an-1] != bp[an-1]) break; if (an == 0) { /* ap OR bp is the gcd, two possible normalisations u1 or -u0, pick the smallest */ MPN_COPY (gp, ap, n); (*gn) = n; MPN_CMP(c, u1, u0, *un); if (c <= 0) // u1 is smallest { MPN_NORMALIZE(u1, (*un)); MPN_COPY (s0p, u1, (*un)); } else // -u0 is smallest { MPN_NORMALIZE(u0, (*un)); MPN_COPY (s0p, u0, (*un)); (*un) = -(*un); } return 0; } if (ap[an-1] < bp[an-1]) /* swap so that ap >= bp */ { MP_PTR_SWAP (ap, bp); MP_PTR_SWAP (u0, u1); negate = ~negate; } bn = n; MPN_NORMALIZE (bp, bn); if (bn == 0) { /* ap is the gcd */ MPN_COPY (gp, ap, n); MPN_NORMALIZE(u1, (*un)); MPN_COPY (s0p, u1, (*un)); if (negate) (*un) = -(*un); (*gn) = n; return 0; } ASSERT_NOCARRY (mpn_sub_n (ap, ap, bp, an)); /* ap -= bp, u1 += u0 */ MPN_NORMALIZE (ap, an); ASSERT (an > 0); cy = mpn_add_n(u1, u1, u0, *un); if (cy) u1[(*un)++] = cy; if (an < bn) /* make an >= bn */ { MPN_PTR_SWAP (ap, an, bp, bn); MP_PTR_SWAP(u0, u1); negate = ~negate; } else if (an == bn) { MPN_CMP (c, ap, bp, an); if (c < 0) { MP_PTR_SWAP (ap, bp); MP_PTR_SWAP(u0, u1); negate = ~negate; } else if (c == 0) /* gcd is ap OR bp */ { /* this case seems to never occur it should happen only if ap = 2*bp */ MPN_COPY (gp, ap, an); (*gn) = an; /* As the gcd is ap OR bp, there are two possible cofactors here u1 or -u0, and we want the least of the two. */ MPN_CMP(c, u1, u0, *un); if (c < 0) // u1 is less { MPN_NORMALIZE(u1, (*un)); MPN_COPY (s0p, u1, (*un)); if (negate) (*un) = -(*un); } else if (c > 0) // -u0 is less { MPN_NORMALIZE(u0, (*un)); MPN_COPY (s0p, u0, (*un)); if (!negate) (*un) = -(*un); } else // same { MPN_NORMALIZE(u0, (*un)); MPN_COPY (s0p, u0, (*un)); } return 0; } } ASSERT (an >= bn); qn = an - bn + 1; mpn_tdiv_qr (tp, ap, 0, ap, an, bp, bn); /* ap -= q * bp, u1 += q * u0 */ /* Normalizing seems to be the simplest way to test if the remainder is zero. */ an = bn; MPN_NORMALIZE (ap, an); if (an == 0) { /* this case never seems to occur*/ /* gcd = bp */ MPN_COPY (gp, bp, bn); MPN_NORMALIZE(u0, (*un)); MPN_COPY (s0p, u0, (*un)); if (!negate) (*un) = -(*un); (*gn) = bn; return 0; } qn2 = qn; u0n = (*un); MPN_NORMALIZE (tp, qn2); MPN_NORMALIZE (u0, u0n); if (u0n > 0) { if (qn2 > u0n) mpn_mul(tp + qn, tp, qn2, u0, u0n); else mpn_mul(tp + qn, u0, u0n, tp, qn2); u0n += qn2; MPN_NORMALIZE(tp + qn, u0n); if ((*un) >= u0n) { cy = mpn_add(u1, u1, (*un), tp + qn, u0n); if (cy) u1[(*un)++] = cy; } else { cy = mpn_add(u1, tp + qn, u0n, u1, (*un)); (*un) = u0n; if (cy) u1[(*un)++] = cy; } } return bn; }