1
/* mpz/gcd.c: Calculate the greatest common divisor of two integers.
3
Copyright 1991, 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
5
This file is part of the GNU MP Library.
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The GNU MP Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
32
mpz_gcd (mpz_ptr g, mpz_srcptr u, mpz_srcptr v)
33
#else /* BERKELEY_MP */
34
gcd (mpz_srcptr u, mpz_srcptr v, mpz_ptr g)
35
#endif /* BERKELEY_MP */
37
unsigned long int g_zero_bits, u_zero_bits, v_zero_bits;
38
mp_size_t g_zero_limbs, u_zero_limbs, v_zero_limbs;
41
mp_size_t usize = ABS (u->_mp_size);
43
mp_size_t vsize = ABS (v->_mp_size);
53
if (g->_mp_alloc < vsize)
54
_mpz_realloc (g, vsize);
55
MPN_COPY (g->_mp_d, vp, vsize);
65
if (g->_mp_alloc < usize)
66
_mpz_realloc (g, usize);
67
MPN_COPY (g->_mp_d, up, usize);
74
g->_mp_d[0] = mpn_gcd_1 (vp, vsize, up[0]);
81
g->_mp_d[0] = mpn_gcd_1 (up, usize, vp[0]);
87
/* Eliminate low zero bits from U and V and move to temporary storage. */
90
u_zero_limbs = up - u->_mp_d;
91
usize -= u_zero_limbs;
92
count_trailing_zeros (u_zero_bits, *up);
94
up = (mp_ptr) TMP_ALLOC (usize * BYTES_PER_MP_LIMB);
97
mpn_rshift (up, tp, usize, u_zero_bits);
98
usize -= up[usize - 1] == 0;
101
MPN_COPY (up, tp, usize);
105
v_zero_limbs = vp - v->_mp_d;
106
vsize -= v_zero_limbs;
107
count_trailing_zeros (v_zero_bits, *vp);
109
vp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
110
if (v_zero_bits != 0)
112
mpn_rshift (vp, tp, vsize, v_zero_bits);
113
vsize -= vp[vsize - 1] == 0;
116
MPN_COPY (vp, tp, vsize);
118
if (u_zero_limbs > v_zero_limbs)
120
g_zero_limbs = v_zero_limbs;
121
g_zero_bits = v_zero_bits;
123
else if (u_zero_limbs < v_zero_limbs)
125
g_zero_limbs = u_zero_limbs;
126
g_zero_bits = u_zero_bits;
130
g_zero_limbs = u_zero_limbs;
131
g_zero_bits = MIN (u_zero_bits, v_zero_bits);
134
/* Call mpn_gcd. The 2nd argument must not have more bits than the 1st. */
135
vsize = (usize < vsize || (usize == vsize && up[usize-1] < vp[vsize-1]))
136
? mpn_gcd (vp, vp, vsize, up, usize)
137
: mpn_gcd (vp, up, usize, vp, vsize);
139
/* Here G <-- V << (g_zero_limbs*BITS_PER_MP_LIMB + g_zero_bits). */
140
gsize = vsize + g_zero_limbs;
141
if (g_zero_bits != 0)
144
gsize += (vp[vsize - 1] >> (BITS_PER_MP_LIMB - g_zero_bits)) != 0;
145
if (g->_mp_alloc < gsize)
146
_mpz_realloc (g, gsize);
147
MPN_ZERO (g->_mp_d, g_zero_limbs);
149
tp = g->_mp_d + g_zero_limbs;
150
cy_limb = mpn_lshift (tp, vp, vsize, g_zero_bits);
156
if (g->_mp_alloc < gsize)
157
_mpz_realloc (g, gsize);
158
MPN_ZERO (g->_mp_d, g_zero_limbs);
159
MPN_COPY (g->_mp_d + g_zero_limbs, vp, vsize);