forked from flintlib/flint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaors_ui.c
338 lines (312 loc) · 10.6 KB
/
aors_ui.c
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Copyright (C) 2009 William Hart
Copyright (C) 2022 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 2.1 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include <gmp.h>
#include "fmpz.h"
static void
_fmpz_add_mpn_1(fmpz_t f, const mp_limb_t * glimbs, mp_size_t gsz, mp_limb_t x);
static void
_fmpz_sub_mpn_1(fmpz_t f, const mp_limb_t * glimbs, mp_size_t gsz, mp_limb_t x);
void
fmpz_add_ui(fmpz_t f, const fmpz_t g, ulong x)
{
__mpz_struct * mf;
slong g1 = *g;
slong f1 = *f;
if (!COEFF_IS_MPZ(g1)) /* g is small */
{
mp_size_t sz = 2;
if (g1 >= 0)
{
{ /* add with jump if carry */
ulong tmp = g1;
g1 += x;
if (((ulong) g1) < tmp)
goto carry;
}
if (((ulong) g1) <= COEFF_MAX)
{
if (COEFF_IS_MPZ(f1))
_fmpz_clear_mpz(f1);
*f = g1;
return;
}
nocarry: sz = 1; /* No carry, but result does not fit in small fmpz */
carry: if (COEFF_IS_MPZ(f1))
mf = COEFF_TO_PTR(f1);
else
{
mf = _fmpz_new_mpz();
*f = PTR_TO_COEFF(mf);
}
mf->_mp_size = sz;
mf->_mp_d[0] = g1;
mf->_mp_d[1] = 1; /* Set carry (not used if sz = 1) */
}
else /* g < 0 */
{
g1 += x;
if (((slong) x) >= 0 && g1 <= COEFF_MAX)
{
/* If x > 0 does not have its top bit set
* and COEFF_MIN <= g < 0, we can interpret x + g as a slong.
* So if the result in g1 is smaller than COEFF_MAX, it is a
* small fmpz. */
if (COEFF_IS_MPZ(f1))
_fmpz_clear_mpz(f1);
*f = g1;
return;
}
else
{
/* 1) If top bit is set in x, the result is going to be positive
* but will be larger than COEFF_MAX since
*
* x + g >= (2^63) - (2^62 - 1) = 2^62 + 1.
*
* However, it will be contained in one limb since g < 0.
*
* 2) If top bit is not set, then result is larger than
* COEFF_MAX, and so it cannot be a small fmpz. However, it
* must fit in one limb since
*
* x + g <= (2^63 - 1) + (-1) = 2^63 - 2,
*
* which is contained in one limb. */
goto nocarry;
}
}
}
else
{
__mpz_struct * mg = COEFF_TO_PTR(g1);
mp_size_t gsz = mg->_mp_size;
mp_limb_t * glimbs = mg->_mp_d;
if (gsz > 0)
_fmpz_add_mpn_1(f, glimbs, gsz, x);
else
_fmpz_sub_mpn_1(f, glimbs, gsz, x);
}
}
/* "Add" two number with same sign. Decide sign from g. */
static void
_fmpz_add_mpn_1(fmpz_t f, const mp_limb_t * glimbs, mp_size_t gsz, mp_limb_t x)
{
__mpz_struct * mf;
mp_limb_t * flimbs;
mp_size_t gabssz = FLINT_ABS(gsz);
/* Promote f as it is guaranteed to be large */
if (COEFF_IS_MPZ(*f))
mf = COEFF_TO_PTR(*f);
else
{
mf = _fmpz_new_mpz();
*f = PTR_TO_COEFF(mf);
}
flimbs = mf->_mp_d;
if (mf->_mp_alloc < (gabssz + 1)) /* Ensure result fits */
{
mf->_mp_d = flint_realloc(mf->_mp_d, sizeof(mp_limb_t) * (gabssz + 1));
mf->_mp_alloc = gabssz + 1;
/* If f and g are aliased, then we need to change glimbs as well. */
if (flimbs == glimbs)
glimbs = mf->_mp_d;
flimbs = mf->_mp_d;
}
/* Use GMP to calculate result */
flimbs[gabssz] = mpn_add_1(flimbs, glimbs, gabssz, x);
/* flimbs[gabssz] is the carry from mpn_add_1,
* and so gabssz + flimbs[gabssz] is valid to determine the size of f */
mf->_mp_size = gabssz + flimbs[gabssz];
if (gsz < 0)
{
/* g and x has same sign. If g is negative, we negate the result */
mf->_mp_size = -mf->_mp_size;
}
}
/* Subtract two limbs (they have different sign) and decide the sign via g. */
static void
_fmpz_sub_mpn_1(fmpz_t f, const mp_limb_t * glimbs, mp_size_t gsz, mp_limb_t x)
{
__mpz_struct * mf;
mp_limb_t * flimbs;
mp_size_t gabssz = FLINT_ABS(gsz);
/* If size of g is 1, we have a higher probability of the result being
* small. */
if (gabssz == 1)
{
if (x <= glimbs[0]) /* Result is zero or has the same sign as g */
{
x = glimbs[0] - x;
L1: if (x <= COEFF_MAX) /* Fits in small fmpz */
{
if (COEFF_IS_MPZ(*f))
_fmpz_clear_mpz(*f);
*f = (gsz > 0) ? x : -x; /* With consideration of sign */
}
else /* Does not fit in small fmpz */
{
if (COEFF_IS_MPZ(*f))
mf = COEFF_TO_PTR(*f);
else
{
mf = _fmpz_new_mpz();
*f = PTR_TO_COEFF(mf);
}
mf->_mp_d[0] = x;
mf->_mp_size = gsz; /* Sign of f is the same as for g */
}
}
else /* |x| > |g|, which implies f has opposite sign of g */
{
/* Set x to the absolute value of |g - x|. By switching sign of
* gsz, we can reuse the code above. */
x -= glimbs[0];
gsz = -gsz;
goto L1;
}
return;
}
/* As g has more than one limb, it is a very high probability that result
* does not fit inside small fmpz. */
if (COEFF_IS_MPZ(*f))
mf = COEFF_TO_PTR(*f);
else
{
mf = _fmpz_new_mpz();
*f = PTR_TO_COEFF(mf);
}
flimbs = mf->_mp_d;
if (gabssz == 2)
{
/* Special case. Can result in a small fmpz, but as |g| > |x| the sign
* cannot change. */
sub_ddmmss(flimbs[1], flimbs[0], glimbs[1], glimbs[0], 0, x);
if (flimbs[1] != 0)
{
/* Most likely: upper limb not zero, so we just have set the sign
* of f to g's. */
mf->_mp_size = gsz;
}
else if (flimbs[0] > COEFF_MAX)
{
/* Still very likely: Upper limb is zero but lower limb does not
* fit inside a small fmpz. Sign is the same as for g, but the
* absolute value of the size is one. */
mf->_mp_size = (gsz > 0) ? 1 : -1;
}
else
{
/* Upper limb is zero and lower limb fits inside a small fmpz.
* Therefore we set f to +/- flimbs[0] and clear the mpz associated
* to f. */
slong tmp = flimbs[0]; /* We will clear this mpz, so save first. */
_fmpz_clear_mpz(*f);
*f = (gsz > 0) ? tmp : -tmp;
}
}
else
{
/* As the absolute value of g's size is larger than 2, the result won't
* fit inside a small fmpz. */
if (mf->_mp_alloc < gabssz) /* Ensure result fits */
{
/* The allocation size of g is always larger than the absolute value
* of g. Therefore, if f's allocation size is smaller than g's
* size, they cannot be aliased. */
mf->_mp_d = flimbs = flint_realloc(mf->_mp_d, sizeof(mp_limb_t) * gabssz);
mf->_mp_alloc = gabssz;
}
mpn_sub_1(flimbs, glimbs, gabssz, x); /* Subtract via GMP */
/* If last limb is zero, we have to set f's absolute size to one less
* than g's. */
mf->_mp_size = gabssz - (flimbs[gabssz - 1] == 0);
if (gsz < 0) /* If g is negative, then f is negative as well. */
mf->_mp_size = -mf->_mp_size;
}
}
void
fmpz_sub_ui(fmpz_t f, const fmpz_t g, ulong x)
{
__mpz_struct * mf;
slong g1 = *g;
slong f1 = *f;
if (!COEFF_IS_MPZ(g1)) /* g is small */
{
mp_size_t sz = -2;
if (g1 <= 0)
{
/* "add" with jump if carry */
g1 = x - g1; /* g1 = x + |g| */
if (((ulong) g1) < x)
goto carry;
if (((ulong) g1) <= COEFF_MAX)
{
if (COEFF_IS_MPZ(f1))
_fmpz_clear_mpz(f1);
*f = -g1;
return;
}
nocarry: sz = -1; /* No carry, but result is not a small fmpz */
carry: if (COEFF_IS_MPZ(f1))
mf = COEFF_TO_PTR(f1);
else
{
mf = _fmpz_new_mpz();
*f = PTR_TO_COEFF(mf);
}
mf->_mp_size = sz;
mf->_mp_d[0] = g1;
mf->_mp_d[1] = 1; /* Set carry (not used if sz = -1) */
}
else
{
g1 = x - g1; /* -(g - x) */
if (((slong) x) >= 0 && g1 <= COEFF_MAX)
{
/* If x > 0 does not have its top bit set
* and 0 < g <= COEFF_MAX, we can interpret x - g as a slong.
* So if the result in g1 is smaller than COEFF_MAX, it is a
* small fmpz. */
if (COEFF_IS_MPZ(f1))
_fmpz_clear_mpz(f1);
*f = -g1; /* g - x = -(x - g) */
return;
}
else
{
/* 1) If top bit is set in x, the result is going to be negative
* but will be larger than COEFF_MAX since
*
* x - g >= 2^63 - (2^62 - 1) = 2^62 + 1.
*
* However, it will be contained in one limb since g > 0.
*
* 2) If top bit is not set, then result is smaller than
* COEFF_MIN, and so it cannot be a small fmpz. However, it
* must fit in one limb since
*
* x - g <= (2^63 - 1) - 1 = 2^63 - 2,
*
* which is contained in one limb. */
goto nocarry;
}
}
}
else
{
__mpz_struct * mg = COEFF_TO_PTR(g1);
mp_size_t gsz = mg->_mp_size;
mp_limb_t * glimbs = mg->_mp_d;
if (gsz > 0)
_fmpz_sub_mpn_1(f, glimbs, gsz, x);
else
_fmpz_add_mpn_1(f, glimbs, gsz, x);
}
}