1 /* mpz/gcd.c: Calculate the greatest common divisor of two integers.
3 Copyright (C) 1991, 1993, 1994, 1996 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 Library General Public License as published by
9 the Free Software Foundation; either version 2 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 Library General Public
15 License for more details.
17 You should have received a copy of the GNU Library 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. */
26 void *_mpz_realloc ();
31 mpz_gcd (mpz_ptr g, mpz_srcptr u, mpz_srcptr v)
38 #else /* BERKELEY_MP */
41 gcd (mpz_srcptr u, mpz_srcptr v, mpz_ptr g)
48 #endif /* BERKELEY_MP */
51 unsigned long int g_zero_bits, u_zero_bits, v_zero_bits;
52 mp_size_t g_zero_limbs, u_zero_limbs, v_zero_limbs;
55 mp_size_t usize = ABS (u->_mp_size);
57 mp_size_t vsize = ABS (v->_mp_size);
67 if (g->_mp_alloc < vsize)
68 _mpz_realloc (g, vsize);
69 MPN_COPY (g->_mp_d, vp, vsize);
79 if (g->_mp_alloc < usize)
80 _mpz_realloc (g, usize);
81 MPN_COPY (g->_mp_d, up, usize);
88 g->_mp_d[0] = mpn_gcd_1 (vp, vsize, up[0]);
95 g->_mp_d[0] = mpn_gcd_1 (up, usize, vp[0]);
101 /* Eliminate low zero bits from U and V and move to temporary storage. */
104 u_zero_limbs = up - u->_mp_d;
105 usize -= u_zero_limbs;
106 count_trailing_zeros (u_zero_bits, *up);
108 up = (mp_ptr) TMP_ALLOC (usize * BYTES_PER_MP_LIMB);
109 if (u_zero_bits != 0)
111 mpn_rshift (up, tp, usize, u_zero_bits);
112 usize -= up[usize - 1] == 0;
115 MPN_COPY (up, tp, usize);
119 v_zero_limbs = vp - v->_mp_d;
120 vsize -= v_zero_limbs;
121 count_trailing_zeros (v_zero_bits, *vp);
123 vp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
124 if (v_zero_bits != 0)
126 mpn_rshift (vp, tp, vsize, v_zero_bits);
127 vsize -= vp[vsize - 1] == 0;
130 MPN_COPY (vp, tp, vsize);
132 if (u_zero_limbs > v_zero_limbs)
134 g_zero_limbs = v_zero_limbs;
135 g_zero_bits = v_zero_bits;
137 else if (u_zero_limbs < v_zero_limbs)
139 g_zero_limbs = u_zero_limbs;
140 g_zero_bits = u_zero_bits;
144 g_zero_limbs = u_zero_limbs;
145 g_zero_bits = MIN (u_zero_bits, v_zero_bits);
148 /* Call mpn_gcd. The 1st argument must not have more bits than the 2nd. */
149 vsize = (usize < vsize || (usize == vsize && up[usize-1] < vp[vsize-1]))
150 ? mpn_gcd (vp, up, usize, vp, vsize)
151 : mpn_gcd (vp, vp, vsize, up, usize);
153 /* Here G <-- V << (g_zero_limbs*BITS_PER_MP_LIMB + g_zero_bits). */
154 gsize = vsize + g_zero_limbs;
155 if (g_zero_bits != 0)
158 gsize += (vp[vsize - 1] >> (BITS_PER_MP_LIMB - g_zero_bits)) != 0;
159 if (g->_mp_alloc < gsize)
160 _mpz_realloc (g, gsize);
161 MPN_ZERO (g->_mp_d, g_zero_limbs);
163 tp = g->_mp_d + g_zero_limbs;
164 cy_limb = mpn_lshift (tp, vp, vsize, g_zero_bits);
170 if (g->_mp_alloc < gsize)
171 _mpz_realloc (g, gsize);
172 MPN_ZERO (g->_mp_d, g_zero_limbs);
173 MPN_COPY (g->_mp_d + g_zero_limbs, vp, vsize);