remove empty dir
[ghc-hetmet.git] / rts / gmp / mpz / gcdext.c
1 /* mpz_gcdext(g, s, t, a, b) -- Set G to gcd(a, b), and S and T such that
2    g = as + bt.
3
4 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 2000 Free Software
5 Foundation, Inc.
6
7 This file is part of the GNU MP Library.
8
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or (at your
12 option) any later version.
13
14 The GNU MP Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 MA 02111-1307, USA. */
23
24 #include <stdio.h> /* for NULL */
25 #include "gmp.h"
26 #include "gmp-impl.h"
27
28 void
29 #if __STDC__
30 mpz_gcdext (mpz_ptr g, mpz_ptr s, mpz_ptr t, mpz_srcptr a, mpz_srcptr b)
31 #else
32 mpz_gcdext (g, s, t, a, b)
33      mpz_ptr g;
34      mpz_ptr s;
35      mpz_ptr t;
36      mpz_srcptr a;
37      mpz_srcptr b;
38 #endif
39 {
40   mp_size_t asize, bsize, usize, vsize;
41   mp_srcptr ap, bp;
42   mp_ptr up, vp;
43   mp_size_t gsize, ssize, tmp_ssize;
44   mp_ptr gp, sp, tmp_gp, tmp_sp;
45   mpz_srcptr u, v;
46   mpz_ptr ss, tt;
47   __mpz_struct stmp, gtmp;
48   TMP_DECL (marker);
49
50   TMP_MARK (marker);
51
52   /* mpn_gcdext requires that U >= V.  Therefore, we often have to swap U and
53      V.  This in turn leads to a lot of complications.  The computed cofactor
54      will be the wrong one, so we have to fix that up at the end.  */
55
56   asize = ABS (SIZ (a));
57   bsize = ABS (SIZ (b));
58   ap = PTR (a);
59   bp = PTR (b);
60   if (asize > bsize || (asize == bsize && mpn_cmp (ap, bp, asize) > 0))
61     {
62       usize = asize;
63       vsize = bsize;
64       up = (mp_ptr) TMP_ALLOC ((usize + 1) * BYTES_PER_MP_LIMB);
65       vp = (mp_ptr) TMP_ALLOC ((vsize + 1) * BYTES_PER_MP_LIMB);
66       MPN_COPY (up, ap, usize);
67       MPN_COPY (vp, bp, vsize);
68       u = a;
69       v = b;
70       ss = s;
71       tt = t;
72     }
73   else
74     {
75       usize = bsize;
76       vsize = asize;
77       up = (mp_ptr) TMP_ALLOC ((usize + 1) * BYTES_PER_MP_LIMB);
78       vp = (mp_ptr) TMP_ALLOC ((vsize + 1) * BYTES_PER_MP_LIMB);
79       MPN_COPY (up, bp, usize);
80       MPN_COPY (vp, ap, vsize);
81       u = b;
82       v = a;
83       ss = t;
84       tt = s;
85     }
86
87   tmp_gp = (mp_ptr) TMP_ALLOC ((usize + 1) * BYTES_PER_MP_LIMB);
88   tmp_sp = (mp_ptr) TMP_ALLOC ((usize + 1) * BYTES_PER_MP_LIMB);
89
90   if (vsize == 0)
91     {
92       tmp_sp[0] = 1;
93       tmp_ssize = 1;
94       MPN_COPY (tmp_gp, up, usize);
95       gsize = usize;
96     }
97   else
98     gsize = mpn_gcdext (tmp_gp, tmp_sp, &tmp_ssize, up, usize, vp, vsize);
99   ssize = ABS (tmp_ssize);
100
101   PTR (&gtmp) = tmp_gp;
102   SIZ (&gtmp) = gsize;
103
104   PTR (&stmp) = tmp_sp;
105   SIZ (&stmp) = (tmp_ssize ^ SIZ (u)) >= 0 ? ssize : -ssize;
106
107   if (tt != NULL)
108     {
109       if (SIZ (v) == 0)
110         SIZ (tt) = 0;
111       else
112         {
113           mpz_t x;
114           MPZ_TMP_INIT (x, ssize + usize + 1);
115           mpz_mul (x, &stmp, u);
116           mpz_sub (x, &gtmp, x);
117           mpz_tdiv_q (tt, x, v);
118         }
119     }
120
121   if (ss != NULL)
122     {
123       if (ALLOC (ss) < ssize)
124         _mpz_realloc (ss, ssize);
125       sp = PTR (ss);
126       MPN_COPY (sp, tmp_sp, ssize);
127       SIZ (ss) = SIZ (&stmp);
128     }
129
130   if (ALLOC (g) < gsize)
131     _mpz_realloc (g, gsize);
132   gp = PTR (g);
133   MPN_COPY (gp, tmp_gp, gsize);
134   SIZ (g) = gsize;
135
136   TMP_FREE (marker);
137 }