/* Inputs are unsigned. */ static int abs_sub_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n) { int c; MPN_CMP (c, ap, bp, n); if (c >= 0) { mpn_sub_n (rp, ap, bp, n); return 0; } else { mpn_sub_n (rp, bp, ap, n); return 1; } }
void _tc4_add(mp_ptr rp, mp_size_t * rn, mp_srcptr r1, mp_size_t r1n, mp_srcptr r2, mp_size_t r2n) { mp_limb_t cy; mp_size_t s1 = ABS(r1n); mp_size_t s2 = ABS(r2n); if (!s1) { *rn = 0; } else if (!s2) { if (rp != r1) MPN_COPY(rp, r1, s1); *rn = r1n; } else if ((r1n ^ r2n) >= 0) { *rn = r1n; cy = mpn_add(rp, r1, s1, r2, s2); if (cy) { rp[s1] = cy; if ((*rn) < 0) (*rn)--; else (*rn)++; } } else { mp_size_t ct; if (s1 != s2) ct = 1; else MPN_CMP(ct, r1, r2, s1); if (!ct) *rn = 0; else if (ct > 0) { mpn_sub(rp, r1, s1, r2, s2); *rn = s1; MPN_NORMALIZE(rp, (*rn)); if (r1n < 0) *rn = -(*rn); } else { mpn_sub_n(rp, r2, r1, s1); *rn = s1; MPN_NORMALIZE(rp, (*rn)); if (r1n > 0) *rn = -(*rn); } } }
int mpz_cmpabs (mpz_srcptr u, mpz_srcptr v) { mp_size_t usize, vsize, dsize; mp_srcptr up, vp; int cmp; usize = ABSIZ (u); vsize = ABSIZ (v); dsize = usize - vsize; if (dsize != 0) return dsize; up = PTR(u); vp = PTR(v); MPN_CMP (cmp, up, vp, usize); return cmp; }
mpz_cmp (mpz_srcptr u, mpz_srcptr v) #endif { mp_size_t usize, vsize, dsize, asize; mp_srcptr up, vp; int cmp; usize = SIZ(u); vsize = SIZ(v); dsize = usize - vsize; if (dsize != 0) return dsize; asize = ABS (usize); up = PTR(u); vp = PTR(v); MPN_CMP (cmp, up, vp, asize); return (usize >= 0 ? cmp : -cmp); }
/* 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_gcdext (mp_ptr gp, mp_ptr up, mp_size_t *usizep, mp_ptr ap, mp_size_t an, mp_ptr bp, mp_size_t n) { mp_size_t talloc; mp_size_t scratch; mp_size_t matrix_scratch; mp_size_t ualloc = n + 1; mp_size_t un; mp_ptr u0; mp_ptr u1; mp_ptr tp; TMP_DECL; ASSERT (an >= n); ASSERT (n > 0); TMP_MARK; /* FIXME: Check for small sizes first, before setting up temporary storage etc. */ talloc = MPN_GCDEXT_LEHMER_N_ITCH(n); /* For initial division */ scratch = an - n + 1; if (scratch > talloc) talloc = scratch; if (ABOVE_THRESHOLD (n, GCDEXT_DC_THRESHOLD)) { /* For hgcd loop. */ mp_size_t hgcd_scratch; mp_size_t update_scratch; mp_size_t p1 = CHOOSE_P_1 (n); mp_size_t p2 = CHOOSE_P_2 (n); mp_size_t min_p = MIN(p1, p2); mp_size_t max_p = MAX(p1, p2); matrix_scratch = MPN_HGCD_MATRIX_INIT_ITCH (n - min_p); hgcd_scratch = mpn_hgcd_itch (n - min_p); update_scratch = max_p + n - 1; scratch = matrix_scratch + MAX(hgcd_scratch, update_scratch); if (scratch > talloc) talloc = scratch; /* Final mpn_gcdext_lehmer_n call. Need space for u and for copies of a and b. */ scratch = MPN_GCDEXT_LEHMER_N_ITCH (GCDEXT_DC_THRESHOLD) + 3*GCDEXT_DC_THRESHOLD; if (scratch > talloc) talloc = scratch; /* Cofactors u0 and u1 */ talloc += 2*(n+1); } tp = TMP_ALLOC_LIMBS(talloc); if (an > n) { mpn_tdiv_qr (tp, ap, 0, ap, an, bp, n); if (mpn_zero_p (ap, n)) { MPN_COPY (gp, bp, n); *usizep = 0; TMP_FREE; return n; } } if (BELOW_THRESHOLD (n, GCDEXT_DC_THRESHOLD)) { mp_size_t gn = mpn_gcdext_lehmer_n(gp, up, usizep, ap, bp, n, tp); TMP_FREE; return gn; } MPN_ZERO (tp, 2*ualloc); u0 = tp; tp += ualloc; u1 = tp; tp += ualloc; { /* For the first hgcd call, there are no u updates, and it makes some sense to use a different choice for p. */ /* FIXME: We could trim use of temporary storage, since u0 and u1 are not used yet. For the hgcd call, we could swap in the u0 and u1 pointers for the relevant matrix elements. */ struct hgcd_matrix M; mp_size_t p = CHOOSE_P_1 (n); mp_size_t nn; mpn_hgcd_matrix_init (&M, n - p, tp); nn = mpn_hgcd (ap + p, bp + p, n - p, &M, tp + matrix_scratch); if (nn > 0) { ASSERT (M.n <= (n - p - 1)/2); ASSERT (M.n + p <= (p + n - 1) / 2); /* Temporary storage 2 (p + M->n) <= p + n - 1 */ n = mpn_hgcd_matrix_adjust (&M, p + nn, ap, bp, p, tp + matrix_scratch); MPN_COPY (u0, M.p[1][0], M.n); MPN_COPY (u1, M.p[1][1], M.n); un = M.n; while ( (u0[un-1] | u1[un-1] ) == 0) un--; } else { /* mpn_hgcd 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 gn; mp_size_t updated_un = 1; u1[0] = 1; /* Temporary storage 2n + 1 */ n = mpn_gcdext_subdiv_step (gp, &gn, up, usizep, ap, bp, n, u0, u1, &updated_un, tp, tp + n); if (n == 0) { TMP_FREE; return gn; } un = updated_un; ASSERT (un < ualloc); } } while (ABOVE_THRESHOLD (n, GCDEXT_DC_THRESHOLD)) { struct hgcd_matrix M; mp_size_t p = CHOOSE_P_2 (n); mp_size_t nn; mpn_hgcd_matrix_init (&M, n - p, tp); nn = mpn_hgcd (ap + p, bp + p, n - p, &M, tp + matrix_scratch); if (nn > 0) { mp_ptr t0; t0 = tp + matrix_scratch; ASSERT (M.n <= (n - p - 1)/2); ASSERT (M.n + p <= (p + n - 1) / 2); /* Temporary storage 2 (p + M->n) <= p + n - 1 */ n = mpn_hgcd_matrix_adjust (&M, p + nn, ap, bp, p, t0); /* By the same analysis as for mpn_hgcd_matrix_mul */ ASSERT (M.n + un <= ualloc); /* FIXME: This copying could be avoided by some swapping of * pointers. May need more temporary storage, though. */ MPN_COPY (t0, u0, un); /* Temporary storage ualloc */ un = hgcd_mul_matrix_vector (&M, u0, t0, u1, un, t0 + un); ASSERT (un < ualloc); ASSERT ( (u0[un-1] | u1[un-1]) > 0); } else { /* mpn_hgcd 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 gn; mp_size_t updated_un = un; /* Temporary storage 2n + 1 */ n = mpn_gcdext_subdiv_step (gp, &gn, up, usizep, ap, bp, n, u0, u1, &updated_un, tp, tp + n); if (n == 0) { TMP_FREE; return gn; } un = updated_un; ASSERT (un < ualloc); } } if (UNLIKELY (mpn_cmp (ap, bp, n) == 0)) { /* Must return the smallest cofactor, +u1 or -u0 */ int c; MPN_COPY (gp, ap, n); MPN_CMP (c, u0, u1, un); ASSERT (c != 0); if (c < 0) { MPN_NORMALIZE (u0, un); MPN_COPY (up, u0, un); *usizep = -un; } else { MPN_NORMALIZE_NOT_ZERO (u1, un); MPN_COPY (up, u1, un); *usizep = un; } TMP_FREE; return n; } else if (mpn_zero_p (u0, un)) { mp_size_t gn; ASSERT (un == 1); ASSERT (u1[0] == 1); /* g = u a + v b = (u u1 - v u0) A + (...) B = u A + (...) B */ gn = mpn_gcdext_lehmer_n (gp, up, usizep, ap, bp, n, tp); TMP_FREE; return gn; } else { /* We have A = ... a + ... b B = u0 a + u1 b a = u1 A + ... B b = -u0 A + ... B with bounds |u0|, |u1| <= B / min(a, b) Compute g = u a + v b = (u u1 - v u0) A + (...) B Here, u, v are bounded by |u| <= b, |v| <= a */ mp_size_t u0n; mp_size_t u1n; mp_size_t lehmer_un; mp_size_t lehmer_vn; mp_size_t gn; mp_ptr lehmer_up; mp_ptr lehmer_vp; int negate; lehmer_up = tp; tp += n; /* Call mpn_gcdext_lehmer_n with copies of a and b. */ MPN_COPY (tp, ap, n); MPN_COPY (tp + n, bp, n); gn = mpn_gcdext_lehmer_n (gp, lehmer_up, &lehmer_un, tp, tp + n, n, tp + 2*n); u0n = un; MPN_NORMALIZE (u0, u0n); if (lehmer_un == 0) { /* u == 0 ==> v = g / b == 1 ==> g = - u0 A + (...) B */ MPN_COPY (up, u0, u0n); *usizep = -u0n; TMP_FREE; return gn; } lehmer_vp = tp; /* Compute v = (g - u a) / b */ lehmer_vn = compute_v (lehmer_vp, ap, bp, n, gp, gn, lehmer_up, lehmer_un, tp + n + 1); if (lehmer_un > 0) negate = 0; else { lehmer_un = -lehmer_un; negate = 1; } u1n = un; MPN_NORMALIZE (u1, u1n); /* It's possible that u0 = 1, u1 = 0 */ if (u1n == 0) { ASSERT (un == 1); ASSERT (u0[0] == 1); /* u1 == 0 ==> u u1 + v u0 = v */ MPN_COPY (up, lehmer_vp, lehmer_vn); *usizep = negate ? lehmer_vn : - lehmer_vn; TMP_FREE; return gn; } ASSERT (lehmer_un + u1n <= ualloc); ASSERT (lehmer_vn + u0n <= ualloc); /* Now u0, u1, u are non-zero. We may still have v == 0 */ /* Compute u u0 */ if (lehmer_un <= u1n) /* Should be the common case */ mpn_mul (up, u1, u1n, lehmer_up, lehmer_un); else mpn_mul (up, lehmer_up, lehmer_un, u1, u1n); un = u1n + lehmer_un; un -= (up[un - 1] == 0); if (lehmer_vn > 0) { mp_limb_t cy; /* Overwrites old u1 value */ if (lehmer_vn <= u0n) /* Should be the common case */ mpn_mul (u1, u0, u0n, lehmer_vp, lehmer_vn); else mpn_mul (u1, lehmer_vp, lehmer_vn, u0, u0n); u1n = u0n + lehmer_vn; u1n -= (u1[u1n - 1] == 0); if (u1n <= un) { cy = mpn_add (up, up, un, u1, u1n); } else { cy = mpn_add (up, u1, u1n, up, un); un = u1n; } up[un] = cy; un += (cy != 0); ASSERT (un < ualloc); } *usizep = negate ? -un : un; TMP_FREE; return gn; } }
/* 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; }
int main (int argc, char **argv) { gmp_randstate_ptr rands; mp_ptr ap, rp, pp, scratch; int count = COUNT; unsigned i; TMP_DECL; TMP_MARK; if (argc > 1) { char *end; count = strtol (argv[1], &end, 0); if (*end || count <= 0) { fprintf (stderr, "Invalid test count: %s.\n", argv[1]); return 1; } } tests_start (); rands = RANDS; ap = TMP_ALLOC_LIMBS (MAX_LIMBS); rp = TMP_ALLOC_LIMBS (MAX_LIMBS); pp = TMP_ALLOC_LIMBS (MAX_LIMBS); scratch = TMP_ALLOC_LIMBS (3*MAX_LIMBS); /* For mpn_powlo */ for (i = 0; i < count; i++) { mp_size_t n; mp_limb_t k; int c; n = 1 + gmp_urandomm_ui (rands, MAX_LIMBS); if (i & 1) mpn_random2 (ap, n); else mpn_random (ap, n); ap[0] |= 1; if (i < 100) k = 3 + 2*i; else { mpn_random (&k, 1); if (k < 3) k = 3; else k |= 1; } mpn_broot (rp, ap, n, k); mpn_powlo (pp, rp, &k, 1, n, scratch); MPN_CMP (c, ap, pp, n); if (c != 0) { gmp_fprintf (stderr, "mpn_broot returned bad result: %u limbs\n", (unsigned) n); gmp_fprintf (stderr, "k = %Mx\n", k); gmp_fprintf (stderr, "a = %Nx\n", ap, n); gmp_fprintf (stderr, "r = %Nx\n", rp, n); gmp_fprintf (stderr, "r^n = %Nx\n", pp, n); abort (); } } TMP_FREE; tests_end (); return 0; }
void mpz_urandomm (mpz_ptr rop, gmp_randstate_t rstate, mpz_srcptr n) { mp_ptr rp, np, nlast; mp_size_t nbits, size; int count; int pow2; int cmp; TMP_DECL; size = ABSIZ (n); if (size == 0) DIVIDE_BY_ZERO; nlast = &PTR (n)[size - 1]; /* Detect whether n is a power of 2. */ pow2 = POW2_P (*nlast); if (pow2 != 0) for (np = PTR (n); np < nlast; np++) if (*np != 0) { pow2 = 0; /* Mark n as `not a power of two'. */ break; } count_leading_zeros (count, *nlast); nbits = size * GMP_NUMB_BITS - (count - GMP_NAIL_BITS) - pow2; if (nbits == 0) /* nbits == 0 means that n was == 1. */ { SIZ (rop) = 0; return; } TMP_MARK; np = PTR (n); if (rop == n) { mp_ptr tp; tp = TMP_ALLOC_LIMBS (size); MPN_COPY (tp, np, size); np = tp; } /* Here the allocated size can be one too much if n is a power of (2^GMP_NUMB_BITS) but it's convenient for using mpn_cmp below. */ rp = MPZ_REALLOC (rop, size); /* Clear last limb to prevent the case in which size is one too much. */ rp[size - 1] = 0; count = MAX_URANDOMM_ITER; /* Set iteration count limit. */ do { _gmp_rand (rp, rstate, nbits); MPN_CMP (cmp, rp, np, size); } while (cmp >= 0 && --count != 0); if (count == 0) /* Too many iterations; return result mod n == result - n */ mpn_sub_n (rp, rp, np, size); MPN_NORMALIZE (rp, size); SIZ (rop) = size; TMP_FREE; }
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; }
mp_size_t mpn_gcdext (mp_ptr gp, mp_ptr s0p, mp_size_t *s0size, mp_ptr ap, mp_size_t an, mp_ptr bp, mp_size_t n) { mp_size_t init_scratch, orig_n = n; mp_size_t scratch, un, u0n, u1n; mp_limb_t t; mp_ptr tp, u0, u1; int swapped = 0; struct ngcd_matrix M; mp_size_t p; mp_size_t nn; mp_limb_signed_t a; int c; TMP_DECL; ASSERT (an >= n); if (an == 1) { if (!n) { /* shouldn't ever occur, but we include for completeness */ gp[0] = ap[0]; s0p[0] = 1; *s0size = 1; return 1; } gp[0] = mpn_gcdinv_1(&a, ap[0], bp[0]); if (a < (mp_limb_signed_t) 0) { s0p[0] = -a; (*s0size) = -1; } else { s0p[0] = a; (*s0size) = 1 - (s0p[0] == 0); } return 1; } init_scratch = MPN_NGCD_MATRIX_INIT_ITCH (n-P_SIZE(n)); scratch = mpn_nhgcd_itch ((n+1)/2); /* Space needed for mpn_ngcd_matrix_adjust */ if (scratch < 2*n) scratch = 2*n; if (scratch < an - n + 1) /* the first division can sometimes be selfish!! */ scratch = an - n + 1; /* Space needed for cofactor adjust */ scratch = MAX(scratch, 2*(n+1) + P_SIZE(n) + 1); TMP_MARK; if (5*n + 2 + MPN_GCD_LEHMER_N_ITCH(n) > init_scratch + scratch) tp = TMP_ALLOC_LIMBS (7*n+4+MPN_GCD_LEHMER_N_ITCH(n)); /* 2n+2 for u0, u1, 5*n+2 + MPN_GCD_LEHMER_N_ITCH(n) for Lehmer and copies of ap and bp and s (and finally 3*n+1 for t and get_t) */ else tp = TMP_ALLOC_LIMBS (2*(n+1) + init_scratch + scratch); if (an > n) { mp_ptr qp = tp; mpn_tdiv_qr (qp, ap, 0, ap, an, bp, n); an = n; MPN_NORMALIZE (ap, an); if (an == 0) { MPN_COPY (gp, bp, n); TMP_FREE; (*s0size) = 0; return n; } } if (BELOW_THRESHOLD (n, GCDEXT_THRESHOLD)) { n = mpn_ngcdext_lehmer (gp, s0p, s0size, ap, bp, n, tp); TMP_FREE; return n; } u0 = tp; /* Cofactor space */ u1 = tp + n + 1; MPN_ZERO(tp, 2*(n+1)); tp += 2*(n+1); /* First iteration, setup u0 and u1 */ p = P_SIZE(n); mpn_ngcd_matrix_init (&M, n - p, tp); ASSERT(tp + init_scratch > M.p[1][1] + M.n); nn = mpn_nhgcd (ap + p, bp + p, n - p, &M, tp + init_scratch); if (nn > 0) { n = mpn_ngcd_matrix_adjust (&M, p + nn, ap, bp, p, tp + init_scratch); /* (ap'', bp'')^T = M^-1(ap', bp')^T and (ap', bp') = (1*ap + ?*bp, 0*ap + ?*bp) We let u0 be minus the factor of ap appearing in the expression for bp'' and u1 be the factor of ap appearing in the expression for ap'' */ MPN_COPY(u0, M.p[1][0], M.n); MPN_COPY(u1, M.p[1][1], M.n); un = M.n; while ((u0[un-1] == 0) && (u1[un-1] == 0)) un--; /* normalise u0, u1, both cannot be zero as det = 1*/ } else { mp_size_t gn; un = 1; u0[0] = 0; /* bp = 0*ap + ?*bp, thus u0 = -0 */ u1[0] = 1; /* ap = 1*ap + ?*bp, thus u1 = 1 */ n = mpn_ngcdext_subdiv_step (gp, &gn, s0p, u0, u1, &un, ap, bp, n, tp); if (n == 0) { /* never observed to occur */ (*s0size) = un; ASSERT(s0p[*s0size - 1] != 0); TMP_FREE; return gn; } } while (ABOVE_THRESHOLD (n, GCDEXT_THRESHOLD)) { struct ngcd_matrix M; mp_size_t p = P_SIZE(n); mp_size_t nn; mpn_ngcd_matrix_init (&M, n - p, tp); nn = mpn_nhgcd (ap + p, bp + p, n - p, &M, tp + init_scratch); if (nn > 0) { n = mpn_ngcd_matrix_adjust (&M, p + nn, ap, bp, p, tp + init_scratch); ngcdext_cofactor_adjust(u0, u1, &un, &M, tp + init_scratch); /* (ap'', bp'')^T = M^-1(ap', bp')^T and (ap', bp') = (u1*ap + ?*bp, -u0*ap + ?*bp) So we need u0' = -(-c*u1 + a*-u0) = a*u0 + c*u1 and we need u1' = (d*u1 -b*-u0) = b*u0 + d*u1 */ ASSERT(un <= orig_n + 1); } else { mp_size_t gn; n = mpn_ngcdext_subdiv_step (gp, &gn, s0p, u0, u1, &un, ap, bp, n, tp); ASSERT(un <= orig_n + 1); if (n == 0) { (*s0size) = un; ASSERT(((*s0size) == 0) || (s0p[ABS(*s0size) - 1] != 0)); TMP_FREE; return gn; } } } ASSERT (ap[n-1] > 0 || bp[n-1] > 0); ASSERT (u0[un-1] > 0 || u1[un-1] > 0); if (ap[n-1] < bp[n-1]) { MP_PTR_SWAP (ap, bp); MP_PTR_SWAP (u0, u1); swapped = 1; } an = n; /* {ap, an} and {bp, bn} are normalised, {ap, an} >= {bp, bn} */ MPN_NORMALIZE (bp, n); if (n == 0) { /* If bp == 0 then gp = ap with cofactor u1 If we swapped then cofactor is -u1 This case never seems to happen */ MPN_COPY (gp, ap, an); MPN_NORMALIZE(u1, un); MPN_COPY(s0p, u1, un); (*s0size) = un; if (swapped) (*s0size) = -(*s0size); TMP_FREE; return an; } /* If at this point we have s*ap' + t*bp' = gp where gp is the gcd and (ap', bp') = (u1*ap + ?*bp, -u0*ap + ?*bp) then gp = s*u1*ap - t*u0*ap + ?*bp and the cofactor we want is (s*u1-t*u0). First there is the special case u0 = 0, u1 = 1 in which case we do not need to compute t... */ ASSERT(u1 + un <= tp); u0n = un; MPN_NORMALIZE(u0, u0n); /* {u0, u0n} is now normalised */ if (u0n == 0) /* u1 = 1 case is rare*/ { mp_size_t gn; gn = mpn_ngcdext_lehmer (gp, s0p, s0size, ap, bp, n, tp); if (swapped) (*s0size) = -(*s0size); TMP_FREE; return gn; } else { /* Compute final gcd. */ mp_size_t gn, sn, tn; mp_ptr s, t; mp_limb_t cy; int negate = 0; /* Save an, bn first as gcdext destroys inputs */ s = tp; tp += an; MPN_COPY(tp, ap, an); MPN_COPY(tp + an, bp, an); if (mpn_cmp(tp, tp + an, an) == 0) { /* gcd is tp or tp + an return smallest cofactor, either -u0 or u1 */ gn = an; MPN_NORMALIZE(tp, gn); MPN_COPY(gp, tp, gn); MPN_CMP(c, u0, u1, un); if (c < (mp_limb_signed_t) 0) { MPN_COPY(s0p, u0, u0n); (*s0size) = -u0n; } else { MPN_NORMALIZE(u1, un); MPN_COPY(s0p, u1, un); (*s0size) = un; } TMP_FREE; return gn; } gn = mpn_ngcdext_lehmer (gp, s, &sn, tp, tp + an, an, tp + 2*an); /* Special case, s == 0, t == 1, cofactor = -u0 case is rare*/ if (sn == 0) { MPN_COPY(s0p, u0, u0n); (*s0size) = -u0n; if (swapped) (*s0size) = -(*s0size); TMP_FREE; return gn; } /* We'll need the other cofactor t = (gp - s*ap)/bp */ t = tp; tp += (an + 1); gcdext_get_t(t, &tn, gp, gn, ap, an, bp, n, s, sn, tp); ASSERT((tn == 0) || (t[tn - 1] > 0)); /* {t, tn} is normalised */ ASSERT(tn <= an + 1); /* We want to compute s*u1 - t*u0, so if s is negative t will be positive, so we'd be dealing with negative numbers. We fix that here. */ if (sn < 0) { sn = -sn; negate = 1; } /* Now we can deal with the special case u1 = 0 */ u1n = un; MPN_NORMALIZE(u1, u1n); /* {u1, u1n} is now normalised */ if (u1n == 0) /* case is rare */ { MPN_COPY(s0p, t, tn); (*s0size) = -tn; if (swapped ^ negate) (*s0size) = -(*s0size); TMP_FREE; return gn; } /* t may be zero, but we need to compute s*u1 anyway */ if (sn >= u1n) mpn_mul(s0p, s, sn, u1, u1n); else mpn_mul(s0p, u1, u1n, s, sn); (*s0size) = sn + u1n; (*s0size) -= (s0p[sn + u1n - 1] == 0); ASSERT(s0p[*s0size - 1] > 0); /* {s0p, *s0size} is normalised now */ if (tn == 0) /* case is rare */ { if (swapped ^ negate) (*s0size) = -(*s0size); TMP_FREE; return gn; } /* Now compute the rest of the cofactor, t*u0 and subtract it We're done with u1 and s which happen to be consecutive, so use that space */ ASSERT(u1 + tn + u0n <= t); if (tn > u0n) mpn_mul(u1, t, tn, u0, u0n); else mpn_mul(u1, u0, u0n, t, tn); u1n = tn + u0n; u1n -= (u1[tn + u0n - 1] == 0); ASSERT(u1[u1n - 1] > 0); /* Recall t is now negated so s*u1 - t*u0 involves an *addition* */ if ((*s0size) >= u1n) { cy = mpn_add(s0p, s0p, *s0size, u1, u1n); if (cy) s0p[(*s0size)++] = cy; } else { cy = mpn_add(s0p, u1, u1n, s0p, *s0size); (*s0size) = u1n; if (cy) s0p[(*s0size)++] = cy; } if (swapped ^ negate) (*s0size) = -(*s0size); TMP_FREE; return gn; } }