1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright (C) 2009 William Hart
Copyright (C) 2021 Fredrik Johansson
Copyright (C) 2021 Albin Ahlbäck
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "mpn_extras.h"
#include "fmpz.h"
void
fmpz_gcd(fmpz_t f, const fmpz_t g, const fmpz_t h)
{
fmpz c1 = *g;
fmpz c2 = *h;
if (!COEFF_IS_MPZ(c1)) /* g is small */
{
ulong u1;
if (c1 == 0)
{
fmpz_abs(f, h);
return;
}
u1 = FLINT_ABS(c1);
if (!COEFF_IS_MPZ(c2)) /* and h is also small */
{
ulong u2;
if (c2 == 0)
{
fmpz_abs(f, g);
return;
}
u2 = FLINT_ABS(c2);
fmpz_set_ui(f, mpn_gcd_1((nn_srcptr) &u2, (slong) 1, u1));
}
else /* but h is large */
{
mpz_ptr mpzc2 = COEFF_TO_PTR(c2);
slong size = mpzc2->_mp_size;
/* The sign is stored in the size of an mpz, and gcd_1 only takes
* positive integers. */
fmpz_set_ui(f, mpn_gcd_1(mpzc2->_mp_d, FLINT_ABS(size), u1));
}
}
else /* g is large */
{
if (!COEFF_IS_MPZ(c2)) /* but h is small */
{
ulong u2;
mpz_ptr mpzc1;
slong size;
if (c2 == 0)
{
fmpz_abs(f, g);
return;
}
u2 = FLINT_ABS(c2);
mpzc1 = COEFF_TO_PTR(c1);
size = mpzc1->_mp_size;
fmpz_set_ui(f, mpn_gcd_1(mpzc1->_mp_d, FLINT_ABS(size), u2));
}
else
{
/* TODO: Change to mpn_gcd in order to save some calculations that
* have already been already made. */
mpz_ptr mf = _fmpz_promote(f);
mpz_gcd(mf, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2));
_fmpz_demote_val(f);
}
}
}
void
fmpz_gcd_ui(fmpz_t res, const fmpz_t a, ulong b)
{
if (b == 0)
fmpz_abs(res, a);
else if (!COEFF_IS_MPZ(*a))
{
if (*a != 0)
{
_fmpz_demote(res);
*res = mpn_gcd_1(&b, 1, FLINT_ABS(*a));
}
else
fmpz_set_ui(res, b);
}
else
{
mpz_ptr ma = COEFF_TO_PTR(*a);
fmpz_set_ui(res, mpn_gcd_1(ma->_mp_d, FLINT_ABS(ma->_mp_size), b));
}
}
/* Assumes that c fits in fmpz */
static void
_fmpz_gcd3_small(fmpz_t res, const fmpz_t a, const fmpz_t b, ulong c)
{
if (c <= 1)
{
if (c == 1)
fmpz_one(res);
else
fmpz_gcd(res, a, b);
}
else
{
if (!COEFF_IS_MPZ(*a))
{
if (*a != 0)
c = mpn_gcd_1(&c, 1, FLINT_ABS(*a));
if (c != 1)
{
if (!COEFF_IS_MPZ(*b))
{
if (*b != 0)
c = mpn_gcd_1(&c, 1, FLINT_ABS(*b));
}
else
{
mpz_ptr mb = COEFF_TO_PTR(*b);
c = mpn_gcd_1(mb->_mp_d, FLINT_ABS(mb->_mp_size), c);
}
}
}
else
{
mpz_ptr ma = COEFF_TO_PTR(*a);
if (!COEFF_IS_MPZ(*b))
{
if (*b != 0)
c = mpn_gcd_1(&c, 1, FLINT_ABS(*b));
if (c != 1)
c = mpn_gcd_1(ma->_mp_d, FLINT_ABS(ma->_mp_size), c);
}
else
{
c = mpn_gcd_1(ma->_mp_d, FLINT_ABS(ma->_mp_size), c);
if (c != 1)
{
ma = COEFF_TO_PTR(*b);
c = mpn_gcd_1(ma->_mp_d, FLINT_ABS(ma->_mp_size), c);
}
}
}
if (COEFF_IS_MPZ(*res))
_fmpz_demote(res);
*res = c;
}
}
void
fmpz_gcd3(fmpz_t res, const fmpz_t a, const fmpz_t b, const fmpz_t c)
{
if (!COEFF_IS_MPZ(*a))
{
_fmpz_gcd3_small(res, b, c, FLINT_ABS(*a));
}
else if (!COEFF_IS_MPZ(*b))
{
_fmpz_gcd3_small(res, a, c, FLINT_ABS(*b));
}
else if (!COEFF_IS_MPZ(*c))
{
_fmpz_gcd3_small(res, a, b, FLINT_ABS(*c));
}
else
{
/* Three-way mpz_gcd. */
mpz_ptr rp, ap, bp, cp, tp;
slong an, bn, cn, mn;
/* If res is small, it cannot be aliased with a, b, c, so promoting is fine. */
rp = _fmpz_promote(res);
ap = COEFF_TO_PTR(*a);
bp = COEFF_TO_PTR(*b);
cp = COEFF_TO_PTR(*c);
an = FLINT_ABS(ap->_mp_size);
bn = FLINT_ABS(bp->_mp_size);
cn = FLINT_ABS(cp->_mp_size);
/* Select c to be the largest operand; we do the smaller gcd first. */
mn = FLINT_MAX(FLINT_MAX(an, bn), cn);
tp = cp;
if (mn != cn)
{
if (mn == an)
{
cp = ap;
ap = tp;
}
else
{
cp = bp;
bp = tp;
}
cn = mn;
}
/* Handle aliasing */
if (rp == cp)
{
mpz_t t;
TMP_INIT;
TMP_START;
/* It would be more efficient to allocate temporary space for
gcd(a, b), but we can't be sure that mpz_gcd never attempts
to reallocate the output. */
t->_mp_d = TMP_ALLOC(sizeof(ulong) * cn);
t->_mp_size = t->_mp_alloc = cn;
flint_mpn_copyi(t->_mp_d, cp->_mp_d, cn);
mpz_gcd(rp, ap, bp);
if (mpz_cmpabs_ui(rp, 1) != 0)
mpz_gcd(rp, rp, t);
TMP_END;
}
else
{
mpz_gcd(rp, ap, bp);
if (mpz_cmpabs_ui(rp, 1) != 0)
mpz_gcd(rp, rp, cp);
}
/* The result may be small */
_fmpz_demote_val(res);
}
}