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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
Copyright (C) 2009 William Hart
Copyright (C) 2010 Sebastian Pancratz
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2020 Daniel Schultz
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 "gmpcompat.h"
#include "fmpz.h"
void fmpz_cdiv_q_2exp(fmpz_t f, const fmpz_t g, ulong exp)
{
fmpz d = *g;
if (!COEFF_IS_MPZ(d)) /* g is small */
{
fmpz_set_si(f, -((-d) >> FLINT_MIN(exp, SMALL_FMPZ_BITCOUNT_MAX)));
}
else /*g is large */
{
mpz_ptr mf = _fmpz_promote(f); /* g is already large */
mpz_cdiv_q_2exp(mf, COEFF_TO_PTR(d), exp);
_fmpz_demote_val(f); /* division may make value small */
}
}
void
fmpz_cdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h)
{
fmpz c1 = *g;
fmpz c2 = *h;
if (fmpz_is_zero(h))
{
flint_throw(FLINT_DIVZERO, "Exception (fmpz_cdiv_q). Division by zero.\n");
}
if (!COEFF_IS_MPZ(c1)) /* g is small */
{
if (!COEFF_IS_MPZ(c2)) /* h is also small */
{
fmpz q = c1 / c2; /* compute C quotient */
fmpz r = c1 - c2 * q; /* compute remainder */
if (r && ((c2 ^ r) > WORD(0))) /* r != 0, c2 and r same sign */
++q;
fmpz_set_si(f, q);
}
else /* h is large and g is small */
{
if ((c1 < WORD(0) && fmpz_sgn(h) < 0) || (c1 > WORD(0) && fmpz_sgn(h) > 0)) /* signs are the same */
fmpz_one(f); /* quotient is positive, round up to one */
else
fmpz_zero(f); /* quotient is zero, or negative, round up to zero */
}
}
else /* g is large */
{
mpz_ptr mf;
if (!COEFF_IS_MPZ(c2)) /* h is small */
{
mf = _fmpz_promote(f);
if (c2 > 0) /* h > 0 */
{
flint_mpz_cdiv_q_ui(mf, COEFF_TO_PTR(c1), c2);
}
else
{
flint_mpz_fdiv_q_ui(mf, COEFF_TO_PTR(c1), -c2);
mpz_neg(mf, mf);
}
_fmpz_demote_val(f); /* division by h may result in small value */
}
else /* both are large */
{
if (MPZ_WANT_FLINT_DIVISION(COEFF_TO_PTR(c1), COEFF_TO_PTR(c2)))
{
_fmpz_cdiv_q_newton(f, g, h);
}
else
{
mf = _fmpz_promote(f);
mpz_cdiv_q(mf, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2));
_fmpz_demote_val(f); /* division by h may result in small value */
}
}
}
}
void fmpz_cdiv_qr(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h)
{
fmpz c1 = *g;
fmpz c2 = *h;
if (fmpz_is_zero(h))
{
flint_throw(FLINT_DIVZERO, "Exception (fmpz_cdiv_q). Division by zero.\n");
}
if (!COEFF_IS_MPZ(c1)) /* g is small */
{
if (!COEFF_IS_MPZ(c2)) /* h is also small */
{
fmpz q = c1 / c2; /* compute C quotient */
fmpz r = c1 - c2 * q; /* compute remainder */
/* rounding of division is undefined, but it should satisfy: */
FLINT_ASSERT(FLINT_ABS(r) < FLINT_ABS(c2));
if ((c2 > WORD(0) && r > WORD(0)) || (c2 < WORD(0) && r < WORD(0)))
{
q += 1; /* q cannot overflow as remainder implies |c2| != 1 */
r -= c2;
}
fmpz_set_si(f, q);
fmpz_set_si(s, r);
}
else /* h is large and g is small */
{
int sgn_h = fmpz_sgn(h);
if ((c1 < WORD(0) && sgn_h < 0) || (c1 > WORD(0) && sgn_h > 0)) /* signs are the same */
{
fmpz_sub(s, g, h);
fmpz_one(f); /* quotient > 0, round up to 1 */
}
else
{
fmpz_set_si(s, c1);
fmpz_zero(f); /* quotient <= 0, round up to 0 */
}
}
}
else /* g is large */
{
mpz_ptr mf, ms;
if (!COEFF_IS_MPZ(c2)) /* h is small */
{
_fmpz_promote(f); /* must not hang on to ptr whilst promoting s */
ms = _fmpz_promote(s);
mf = COEFF_TO_PTR(*f);
if (c2 > 0) /* h > 0 */
{
flint_mpz_cdiv_qr_ui(mf, ms, COEFF_TO_PTR(c1), c2);
}
else
{
flint_mpz_fdiv_qr_ui(mf, ms, COEFF_TO_PTR(c1), -c2);
mpz_neg(mf, mf);
}
_fmpz_demote_val(f); /* division by h may result in small value */
_fmpz_demote_val(s); /* division by h may result in small value */
}
else /* both are large */
{
if (MPZ_WANT_FLINT_DIVISION(COEFF_TO_PTR(c1), COEFF_TO_PTR(c2)))
{
_fmpz_cdiv_qr_newton(f, s, g, h);
}
else
{
_fmpz_promote(f); /* must not hang on to ptr whilst promoting s */
ms = _fmpz_promote(s);
mf = COEFF_TO_PTR(*f);
mpz_cdiv_qr(mf, ms, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2));
_fmpz_demote_val(f); /* division by h may result in small value */
_fmpz_demote_val(s); /* division by h may result in small value */
}
}
}
}
void
fmpz_cdiv_q_si(fmpz_t f, const fmpz_t g, slong h)
{
fmpz c1 = *g;
slong c2 = h;
if (h == 0)
{
flint_throw(FLINT_DIVZERO, "Exception (fmpz_cdiv_q_si). Division by zero.\n");
}
if (!COEFF_IS_MPZ(c1)) /* g is small */
{
fmpz q = c1 / c2; /* compute C quotient */
fmpz r = c1 - c2 * q; /* compute remainder */
if (r && ((c1 ^ c2) > WORD(0)))
++q;
fmpz_set_si(f, q);
}
else /* g is large */
{
mpz_ptr mf = _fmpz_promote(f);
if (c2 > 0)
{
flint_mpz_cdiv_q_ui(mf, COEFF_TO_PTR(c1), c2);
}
else
{
flint_mpz_fdiv_q_ui(mf, COEFF_TO_PTR(c1), -(ulong) c2);
mpz_neg(mf, mf);
}
_fmpz_demote_val(f); /* division by h may result in small value */
}
}
void
fmpz_cdiv_q_ui(fmpz_t f, const fmpz_t g, ulong h)
{
fmpz c1 = *g;
ulong c2 = h;
if (h == 0)
{
flint_throw(FLINT_DIVZERO, "Exception: division by zero in fmpz_cdiv_q_ui\n");
}
if (!COEFF_IS_MPZ(c1)) /* g is small */
{
if (c1 > 0)
{
ulong q = c1 / c2;
ulong r = c1 - c2 * q;
if (r)
++q;
fmpz_set_ui(f, q);
}
else
{
fmpz_set_si(f, - (((ulong) -c1) / c2));
}
}
else /* g is large */
{
mpz_ptr mf = _fmpz_promote(f);
flint_mpz_cdiv_q_ui(mf, COEFF_TO_PTR(c1), c2);
_fmpz_demote_val(f); /* division by h may result in small value */
}
}
void fmpz_cdiv_r_2exp(fmpz_t f, const fmpz_t g, ulong exp)
{
fmpz d = *g;
if (!COEFF_IS_MPZ(d)) /* g is small */
{
if (d <= 0)
{
d = -d;
fmpz_neg_ui(f, exp < (SMALL_FMPZ_BITCOUNT_MAX) ? d & ((WORD(1) << exp) - 1) : d);
}
else
{
if (exp <= SMALL_FMPZ_BITCOUNT_MAX)
{
fmpz_neg_ui(f, (-d) & ((WORD(1) << exp) - 1));
}
else
{
mpz_ptr mf = _fmpz_promote(f);
flint_mpz_set_ui(mf, 1);
mpz_mul_2exp(mf, mf, exp);
flint_mpz_sub_ui(mf, mf, d);
mpz_neg(mf, mf);
}
}
}
else /*g is large */
{
mpz_ptr mf = _fmpz_promote(f); /* g is already large */
mpz_cdiv_r_2exp(mf, COEFF_TO_PTR(d), exp);
_fmpz_demote_val(f); /* division may make value small */
}
}
ulong
fmpz_cdiv_ui(const fmpz_t g, ulong h)
{
fmpz c1 = *g;
ulong r;
if (h == UWORD(0))
{
flint_throw(FLINT_ERROR, "Exception (fmpz_cdiv_ui). Division by 0.\n");
}
if (!COEFF_IS_MPZ(c1)) /* g is small */
{
if (c1 >= WORD(1))
r = h - 1 - ((c1 - WORD(1)) % h);
else
r = (-c1) % h;
return r;
}
else /* g is large */
{
return flint_mpz_cdiv_ui(COEFF_TO_PTR(c1), h);
}
}