2008-06-25 03:33:36 -04:00
|
|
|
/* mpz_gcdext(g, s, t, a, b) -- Set G to gcd(a, b), and S and T such that
|
|
|
|
g = as + bt.
|
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
Copyright 1991, 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2005, 2011,
|
|
|
|
2012 Free Software Foundation, Inc.
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
This file is part of the GNU MP Library.
|
|
|
|
|
|
|
|
The GNU MP Library is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
2014-02-27 12:58:43 -05:00
|
|
|
the Free Software Foundation; either version 3 of the License, or (at your
|
2008-06-25 03:33:36 -04:00
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
The GNU MP Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2014-02-27 12:58:43 -05:00
|
|
|
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
|
2008-06-25 03:33:36 -04:00
|
|
|
|
|
|
|
#include <stdio.h> /* for NULL */
|
2014-02-27 16:35:04 -05:00
|
|
|
#include "mpir.h"
|
2008-06-25 03:33:36 -04:00
|
|
|
#include "gmp-impl.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
mpz_gcdext (mpz_ptr g, mpz_ptr s, mpz_ptr t, mpz_srcptr a, mpz_srcptr b)
|
|
|
|
{
|
2014-02-27 12:58:43 -05:00
|
|
|
mp_size_t asize, bsize;
|
|
|
|
mp_ptr tmp_ap, tmp_bp;
|
2008-06-25 03:33:36 -04:00
|
|
|
mp_size_t gsize, ssize, tmp_ssize;
|
2014-02-27 12:58:43 -05:00
|
|
|
mp_ptr gp, tmp_gp, tmp_sp;
|
2008-06-25 03:33:36 -04:00
|
|
|
TMP_DECL;
|
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
/* mpn_gcdext requires that Usize >= Vsize. Therefore, we often
|
|
|
|
have to swap U and V. The computed cofactor will be the
|
|
|
|
"smallest" one, which is faster to produce. The wanted one will
|
|
|
|
be computed here; this is needed anyway when both are requested. */
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
asize = ABSIZ (a);
|
|
|
|
bsize = ABSIZ (b);
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
if (asize < bsize)
|
2008-06-25 03:33:36 -04:00
|
|
|
{
|
2014-02-27 12:58:43 -05:00
|
|
|
MPZ_SRCPTR_SWAP (a, b);
|
|
|
|
MP_SIZE_T_SWAP (asize, bsize);
|
|
|
|
MPZ_PTR_SWAP (s, t);
|
2008-06-25 03:33:36 -04:00
|
|
|
}
|
2014-02-27 12:58:43 -05:00
|
|
|
|
|
|
|
if (bsize == 0)
|
2008-06-25 03:33:36 -04:00
|
|
|
{
|
2014-02-27 12:58:43 -05:00
|
|
|
/* g = |a|, s = sgn(a), t = 0. */
|
|
|
|
ssize = SIZ (a) >= 0 ? (asize != 0) : -1;
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
gp = MPZ_REALLOC (g, asize);
|
|
|
|
MPN_COPY (gp, PTR (a), asize);
|
|
|
|
SIZ (g) = asize;
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
if (t != NULL)
|
|
|
|
SIZ (t) = 0;
|
|
|
|
if (s != NULL)
|
|
|
|
{
|
|
|
|
SIZ (s) = ssize;
|
|
|
|
PTR (s)[0] = 1;
|
|
|
|
}
|
|
|
|
return;
|
2008-06-25 03:33:36 -04:00
|
|
|
}
|
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
TMP_MARK;
|
|
|
|
|
|
|
|
TMP_ALLOC_LIMBS_2 (tmp_ap, asize, tmp_bp, bsize);
|
|
|
|
MPN_COPY (tmp_ap, PTR (a), asize);
|
|
|
|
MPN_COPY (tmp_bp, PTR (b), bsize);
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
TMP_ALLOC_LIMBS_2 (tmp_gp, bsize, tmp_sp, bsize + 1);
|
2008-06-25 03:33:36 -04:00
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
gsize = mpn_gcdext (tmp_gp, tmp_sp, &tmp_ssize, tmp_ap, asize, tmp_bp, bsize);
|
|
|
|
|
|
|
|
ssize = ABS (tmp_ssize);
|
|
|
|
tmp_ssize = SIZ (a) >= 0 ? tmp_ssize : -tmp_ssize;
|
|
|
|
|
|
|
|
if (t != NULL)
|
2008-06-25 03:33:36 -04:00
|
|
|
{
|
2014-02-27 12:58:43 -05:00
|
|
|
mpz_t x;
|
|
|
|
__mpz_struct gtmp, stmp;
|
|
|
|
|
|
|
|
PTR (>mp) = tmp_gp;
|
|
|
|
SIZ (>mp) = gsize;
|
|
|
|
|
|
|
|
PTR (&stmp) = tmp_sp;
|
|
|
|
SIZ (&stmp) = tmp_ssize;
|
|
|
|
|
|
|
|
MPZ_TMP_INIT (x, ssize + asize + 1);
|
|
|
|
mpz_mul (x, &stmp, a);
|
|
|
|
mpz_sub (x, >mp, x);
|
|
|
|
mpz_divexact (t, x, b);
|
2008-06-25 03:33:36 -04:00
|
|
|
}
|
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
if (s != NULL)
|
2008-06-25 03:33:36 -04:00
|
|
|
{
|
2014-02-27 12:58:43 -05:00
|
|
|
mp_ptr sp;
|
|
|
|
|
|
|
|
sp = MPZ_REALLOC (s, ssize);
|
2008-06-25 03:33:36 -04:00
|
|
|
MPN_COPY (sp, tmp_sp, ssize);
|
2014-02-27 12:58:43 -05:00
|
|
|
SIZ (s) = tmp_ssize;
|
2008-06-25 03:33:36 -04:00
|
|
|
}
|
|
|
|
|
2014-02-27 12:58:43 -05:00
|
|
|
gp = MPZ_REALLOC (g, gsize);
|
2008-06-25 03:33:36 -04:00
|
|
|
MPN_COPY (gp, tmp_gp, gsize);
|
|
|
|
SIZ (g) = gsize;
|
|
|
|
|
|
|
|
TMP_FREE;
|
|
|
|
}
|