/src/gmp-6.2.1/mpz/gcdext.c
Line  | Count  | Source (jump to first uncovered line)  | 
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 1991, 1993-1997, 2000, 2001, 2005, 2011, 2012, 2015 Free  | 
5  |  | Software 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 either:  | 
11  |  |  | 
12  |  |   * the GNU Lesser General Public License as published by the Free  | 
13  |  |     Software Foundation; either version 3 of the License, or (at your  | 
14  |  |     option) any later version.  | 
15  |  |  | 
16  |  | or  | 
17  |  |  | 
18  |  |   * the GNU General Public License as published by the Free Software  | 
19  |  |     Foundation; either version 2 of the License, or (at your option) any  | 
20  |  |     later version.  | 
21  |  |  | 
22  |  | or both in parallel, as here.  | 
23  |  |  | 
24  |  | The GNU MP Library is distributed in the hope that it will be useful, but  | 
25  |  | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  | 
26  |  | or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License  | 
27  |  | for more details.  | 
28  |  |  | 
29  |  | You should have received copies of the GNU General Public License and the  | 
30  |  | GNU Lesser General Public License along with the GNU MP Library.  If not,  | 
31  |  | see https://www.gnu.org/licenses/.  */  | 
32  |  |  | 
33  |  | #include <stdio.h> /* for NULL */  | 
34  |  | #include "gmp-impl.h"  | 
35  |  |  | 
36  |  | void  | 
37  |  | mpz_gcdext (mpz_ptr g, mpz_ptr s, mpz_ptr t, mpz_srcptr a, mpz_srcptr b)  | 
38  | 1.69k  | { | 
39  | 1.69k  |   mp_size_t asize, bsize;  | 
40  | 1.69k  |   mp_ptr tmp_ap, tmp_bp;  | 
41  | 1.69k  |   mp_size_t gsize, ssize, tmp_ssize;  | 
42  | 1.69k  |   mp_ptr gp, tmp_gp, tmp_sp;  | 
43  | 1.69k  |   TMP_DECL;  | 
44  |  |  | 
45  |  |   /* mpn_gcdext requires that Usize >= Vsize.  Therefore, we often  | 
46  |  |      have to swap U and V.  The computed cofactor will be the  | 
47  |  |      "smallest" one, which is faster to produce.  The wanted one will  | 
48  |  |      be computed here; this is needed anyway when both are requested.  */  | 
49  |  |  | 
50  | 1.69k  |   asize = ABSIZ (a);  | 
51  | 1.69k  |   bsize = ABSIZ (b);  | 
52  |  |  | 
53  | 1.69k  |   ASSERT (s != NULL);  | 
54  |  |  | 
55  | 1.69k  |   if (asize < bsize)  | 
56  | 1.21k  |     { | 
57  | 1.21k  |       MPZ_SRCPTR_SWAP (a, b);  | 
58  | 1.21k  |       MP_SIZE_T_SWAP (asize, bsize);  | 
59  | 1.21k  |       MPZ_PTR_SWAP (s, t);  | 
60  | 1.21k  |     }  | 
61  |  |  | 
62  | 1.69k  |   if (bsize == 0)  | 
63  | 13  |     { | 
64  |  |       /* g = |a|, s = sgn(a), t = 0. */  | 
65  | 13  |       ssize = SIZ (a) >= 0 ? (asize != 0) : -1;  | 
66  |  |  | 
67  | 13  |       if (g != NULL)  | 
68  | 13  |   { | 
69  |  |     /* If g == a, then ALLOC(g) == ALLOC(a) >= asize, i.e.  | 
70  |  |        the next MPZ_NEWALLOC returns the old PTR(a) .*/  | 
71  | 13  |     gp = MPZ_NEWALLOC (g, asize);  | 
72  | 13  |     MPN_COPY (gp, PTR (a), asize);  | 
73  | 13  |     SIZ (g) = asize;  | 
74  | 13  |   }  | 
75  | 13  |       if (t != NULL)  | 
76  | 13  |   SIZ (t) = 0;  | 
77  | 13  |       if (s != NULL)  | 
78  | 7  |   { | 
79  | 7  |     SIZ (s) = ssize;  | 
80  | 7  |     MPZ_NEWALLOC (s, 1)[0] = 1;  | 
81  | 7  |   }  | 
82  | 13  |       return;  | 
83  | 13  |     }  | 
84  |  |  | 
85  | 1.68k  |   TMP_MARK;  | 
86  |  |  | 
87  | 1.68k  |   TMP_ALLOC_LIMBS_2 (tmp_gp, bsize, tmp_sp, asize + bsize + bsize + 1);  | 
88  | 1.68k  |   tmp_bp = tmp_sp + bsize + 1;  | 
89  | 1.68k  |   tmp_ap = tmp_bp + bsize;  | 
90  | 1.68k  |   MPN_COPY (tmp_ap, PTR (a), asize);  | 
91  | 1.68k  |   MPN_COPY (tmp_bp, PTR (b), bsize);  | 
92  |  |  | 
93  | 1.68k  |   gsize = mpn_gcdext (tmp_gp, tmp_sp, &tmp_ssize, tmp_ap, asize, tmp_bp, bsize);  | 
94  |  |  | 
95  | 1.68k  |   ssize = ABS (tmp_ssize);  | 
96  | 1.68k  |   tmp_ssize = SIZ (a) >= 0 ? tmp_ssize : -tmp_ssize;  | 
97  |  |  | 
98  | 1.68k  |   if (t != NULL)  | 
99  | 1.34k  |     { | 
100  | 1.34k  |       mpz_t x;  | 
101  | 1.34k  |       mpz_t gtmp, stmp;  | 
102  |  |  | 
103  | 1.34k  |       PTR (gtmp) = tmp_gp;  | 
104  | 1.34k  |       SIZ (gtmp) = gsize;  | 
105  |  |  | 
106  | 1.34k  |       PTR (stmp) = tmp_sp;  | 
107  | 1.34k  |       SIZ (stmp) = tmp_ssize;  | 
108  |  |  | 
109  | 1.34k  |       ASSERT (ssize <= bsize); /* ssize*2 + asize + 1 <= asize + bsize*2 + 1 */  | 
110  | 1.34k  |       PTR (x) = tmp_sp + ssize;  | 
111  | 1.34k  |       ALLOC (x) = ssize + asize + 1;  | 
112  |  |  | 
113  | 1.34k  |       mpz_mul (x, stmp, a);  | 
114  | 1.34k  |       mpz_sub (x, gtmp, x);  | 
115  | 1.34k  |       mpz_divexact (t, x, b);  | 
116  | 1.34k  |     }  | 
117  |  |  | 
118  | 1.68k  |   if (s != NULL)  | 
119  | 589  |     { | 
120  | 589  |       mp_ptr sp;  | 
121  |  |  | 
122  | 589  |       sp = MPZ_NEWALLOC (s, ssize);  | 
123  | 589  |       MPN_COPY (sp, tmp_sp, ssize);  | 
124  | 589  |       SIZ (s) = tmp_ssize;  | 
125  | 589  |     }  | 
126  |  |  | 
127  | 1.68k  |   if (g != NULL)  | 
128  | 1.68k  |     { | 
129  | 1.68k  |       gp = MPZ_NEWALLOC (g, gsize);  | 
130  | 1.68k  |       MPN_COPY (gp, tmp_gp, gsize);  | 
131  | 1.68k  |       SIZ (g) = gsize;  | 
132  | 1.68k  |     }  | 
133  |  |  | 
134  | 1.68k  |   TMP_FREE;  | 
135  | 1.68k  | }  |