~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/mpz/gcd.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpz/gcd.c:   Calculate the greatest common divisor of two integers.
 
2
 
 
3
Copyright 1991, 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
 
4
 
 
5
This file is part of the GNU MP Library.
 
6
 
 
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.
 
11
 
 
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.
 
16
 
 
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. */
 
21
 
 
22
#include "gmp.h"
 
23
#include "gmp-impl.h"
 
24
#include "longlong.h"
 
25
#ifdef BERKELEY_MP
 
26
#include "mp.h"
 
27
#endif
 
28
 
 
29
 
 
30
void
 
31
#ifndef BERKELEY_MP
 
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 */
 
36
{
 
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;
 
39
  mp_ptr tp;
 
40
  mp_ptr up = u->_mp_d;
 
41
  mp_size_t usize = ABS (u->_mp_size);
 
42
  mp_ptr vp = v->_mp_d;
 
43
  mp_size_t vsize = ABS (v->_mp_size);
 
44
  mp_size_t gsize;
 
45
  TMP_DECL (marker);
 
46
 
 
47
  /* GCD(0, V) == V.  */
 
48
  if (usize == 0)
 
49
    {
 
50
      g->_mp_size = vsize;
 
51
      if (g == v)
 
52
        return;
 
53
      if (g->_mp_alloc < vsize)
 
54
        _mpz_realloc (g, vsize);
 
55
      MPN_COPY (g->_mp_d, vp, vsize);
 
56
      return;
 
57
    }
 
58
 
 
59
  /* GCD(U, 0) == U.  */
 
60
  if (vsize == 0)
 
61
    {
 
62
      g->_mp_size = usize;
 
63
      if (g == u)
 
64
        return;
 
65
      if (g->_mp_alloc < usize)
 
66
        _mpz_realloc (g, usize);
 
67
      MPN_COPY (g->_mp_d, up, usize);
 
68
      return;
 
69
    }
 
70
 
 
71
  if (usize == 1)
 
72
    {
 
73
      g->_mp_size = 1;
 
74
      g->_mp_d[0] = mpn_gcd_1 (vp, vsize, up[0]);
 
75
      return;
 
76
    }
 
77
 
 
78
  if (vsize == 1)
 
79
    {
 
80
      g->_mp_size = 1;
 
81
      g->_mp_d[0] = mpn_gcd_1 (up, usize, vp[0]);
 
82
      return;
 
83
    }
 
84
 
 
85
  TMP_MARK (marker);
 
86
 
 
87
  /*  Eliminate low zero bits from U and V and move to temporary storage.  */
 
88
  while (*up == 0)
 
89
    up++;
 
90
  u_zero_limbs = up - u->_mp_d;
 
91
  usize -= u_zero_limbs;
 
92
  count_trailing_zeros (u_zero_bits, *up);
 
93
  tp = up;
 
94
  up = (mp_ptr) TMP_ALLOC (usize * BYTES_PER_MP_LIMB);
 
95
  if (u_zero_bits != 0)
 
96
    {
 
97
      mpn_rshift (up, tp, usize, u_zero_bits);
 
98
      usize -= up[usize - 1] == 0;
 
99
    }
 
100
  else
 
101
    MPN_COPY (up, tp, usize);
 
102
 
 
103
  while (*vp == 0)
 
104
    vp++;
 
105
  v_zero_limbs = vp - v->_mp_d;
 
106
  vsize -= v_zero_limbs;
 
107
  count_trailing_zeros (v_zero_bits, *vp);
 
108
  tp = vp;
 
109
  vp = (mp_ptr) TMP_ALLOC (vsize * BYTES_PER_MP_LIMB);
 
110
  if (v_zero_bits != 0)
 
111
    {
 
112
      mpn_rshift (vp, tp, vsize, v_zero_bits);
 
113
      vsize -= vp[vsize - 1] == 0;
 
114
    }
 
115
  else
 
116
    MPN_COPY (vp, tp, vsize);
 
117
 
 
118
  if (u_zero_limbs > v_zero_limbs)
 
119
    {
 
120
      g_zero_limbs = v_zero_limbs;
 
121
      g_zero_bits = v_zero_bits;
 
122
    }
 
123
  else if (u_zero_limbs < v_zero_limbs)
 
124
    {
 
125
      g_zero_limbs = u_zero_limbs;
 
126
      g_zero_bits = u_zero_bits;
 
127
    }
 
128
  else  /*  Equal.  */
 
129
    {
 
130
      g_zero_limbs = u_zero_limbs;
 
131
      g_zero_bits = MIN (u_zero_bits, v_zero_bits);
 
132
    }
 
133
 
 
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);
 
138
 
 
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)
 
142
    {
 
143
      mp_limb_t cy_limb;
 
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);
 
148
 
 
149
      tp = g->_mp_d + g_zero_limbs;
 
150
      cy_limb = mpn_lshift (tp, vp, vsize, g_zero_bits);
 
151
      if (cy_limb != 0)
 
152
        tp[vsize] = cy_limb;
 
153
    }
 
154
  else
 
155
    {
 
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);
 
160
    }
 
161
 
 
162
  g->_mp_size = gsize;
 
163
  TMP_FREE (marker);
 
164
}