Line data Source code
1 : #line 2 "../src/kernel/none/level1.h"
2 : /* Copyright (C) 2000 The PARI group.
3 :
4 : This file is part of the PARI/GP package.
5 :
6 : PARI/GP is free software; you can redistribute it and/or modify it under the
7 : terms of the GNU General Public License as published by the Free Software
8 : Foundation; either version 2 of the License, or (at your option) any later
9 : version. It is distributed in the hope that it will be useful, but WITHOUT
10 : ANY WARRANTY WHATSOEVER.
11 :
12 : Check the License for details. You should have received a copy of it, along
13 : with the package; see the file 'COPYING'. If not, write to the Free Software
14 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
15 :
16 : /* This file defines "level 1" kernel functions.
17 : * These functions can be inline; they are also defined externally in
18 : * mpinl.c, which includes this file and never needs to be changed */
19 :
20 : INLINE long
21 2517630855 : nbits2lg(long x) {
22 2517630855 : return (long)(((ulong)x+3*BITS_IN_LONG-1) >> TWOPOTBITS_IN_LONG);
23 : }
24 : INLINE long
25 838571836 : lg2prec(long x){ return (x-2) * BITS_IN_LONG; }
26 :
27 : INLINE long
28 >10003*10^7 : evallg(long x)
29 : {
30 >10003*10^7 : if (x & ~LGBITS) pari_err_OVERFLOW("lg()");
31 >10003*10^7 : return _evallg(x);
32 : }
33 : INLINE long
34 81660137 : evalvalp(long x)
35 : {
36 81660137 : long v = _evalvalp(x);
37 81660137 : if (v & ~VALPBITS) pari_err_OVERFLOW("valp()");
38 81660137 : return v;
39 : }
40 : INLINE long
41 25309350 : evalvalser(long x)
42 : {
43 25309350 : long v = _evalvalser(x);
44 25309350 : if (v & ~VALSERBITS) pari_err_OVERFLOW("valser()");
45 25309350 : return v;
46 : }
47 : INLINE long
48 13975626271 : evalexpo(long x)
49 : {
50 13975626271 : long v = _evalexpo(x);
51 13975626271 : if (v & ~EXPOBITS) pari_err_OVERFLOW("expo()");
52 13975626264 : return v;
53 : }
54 : INLINE long
55 78703534 : evalprecp(long x)
56 : {
57 78703534 : long v = _evalprecp(x);
58 78703534 : if (x & ~((1UL<<(BITS_IN_LONG-VALPnumBITS))-1)) pari_err_OVERFLOW("precp()");
59 78703534 : return v;
60 : }
61 :
62 : INLINE int
63 218513252 : varncmp(long x, long y)
64 : {
65 218513252 : if (varpriority[x] < varpriority[y]) return 1;
66 153653628 : if (varpriority[x] > varpriority[y]) return -1;
67 74173942 : return 0;
68 : }
69 : INLINE long
70 26180 : varnmin(long x, long y)
71 26180 : { return (varpriority[x] <= varpriority[y])? x: y; }
72 : INLINE long
73 203 : varnmax(long x, long y)
74 203 : { return (varpriority[x] >= varpriority[y])? x: y; }
75 :
76 : /* Inhibit some area GC-wise: declare it to be a non recursive
77 : * type, of length l. Thus gc_stack_update won't inspect the zone, just copy it.
78 : * For the following situation:
79 : * z = cgetg(t,a); av = avma; garbage(); ltop = avma;
80 : * for (i=1; i<HUGE; i++) gel(z,i) = blah();
81 : * stackdummy(av,ltop);
82 : * loses (av-ltop) words but saves a GC. */
83 : INLINE void
84 3746935314 : stackdummy(pari_sp av, pari_sp ltop) {
85 3746935314 : long l = ((GEN)av) - ((GEN)ltop);
86 3746935314 : if (l > 0) {
87 1250179134 : GEN z = (GEN)ltop;
88 1250179134 : z[0] = evaltyp(t_VECSMALL) | evallg(l);
89 : #ifdef DEBUG
90 : { long i; for (i = 1; i < l; i++) z[i] = 0; }
91 : #endif
92 : }
93 3746935314 : }
94 : INLINE void
95 106536664 : fixlg(GEN x, long ly) {
96 106536664 : long lx = lg(x), l = lx - ly;
97 106536664 : if (l > 0)
98 : { /* stackdummy(x+lx, x+ly) */
99 49850610 : GEN z = x + ly;
100 49850610 : z[0] = evaltyp(t_VECSMALL) | evallg(l);
101 49850610 : setlg(x, ly);
102 : #ifdef DEBUG
103 : { long i; for (i = 1; i < l; i++) z[i] = 0; }
104 : #endif
105 : }
106 106536664 : }
107 : /* update lg(z) before affrr(y, z) [ to cater for precision loss ]*/
108 : INLINE void
109 56699245 : affrr_fixlg(GEN y, GEN z) { fixlg(z, lg(y)); affrr(y, z); }
110 :
111 : /*******************************************************************/
112 : /* */
113 : /* ALLOCATE ON STACK */
114 : /* */
115 : /*******************************************************************/
116 : INLINE ulong
117 0 : get_avma(void) { return avma; }
118 : INLINE void
119 >13597*10^7 : set_avma(ulong av) { avma = av; }
120 :
121 : INLINE double
122 198967846 : gc_double(pari_sp av, double d) { set_avma(av); return d; }
123 : INLINE long
124 243058842 : gc_long(pari_sp av, long s) { set_avma(av); return s; }
125 : INLINE ulong
126 52321794 : gc_ulong(pari_sp av, ulong s) { set_avma(av); return s; }
127 : INLINE int
128 55258072 : gc_bool(pari_sp av, int s) { set_avma(av); return s; }
129 : INLINE int
130 2818124 : gc_int(pari_sp av, int s) { set_avma(av); return s; }
131 : INLINE GEN
132 7910117 : gc_NULL(pari_sp av) { set_avma(av); return NULL; }
133 : INLINE GEN
134 15921262032 : gc_const(pari_sp av, GEN x) { set_avma(av); return x; }
135 : #define retgc_const(av, x) \
136 : do { set_avma(av); return x; } while(0)
137 :
138 : INLINE GEN
139 155900 : gc_stoi(pari_sp av, long x) { set_avma(av); return stoi(x); }
140 : INLINE GEN
141 467839 : gc_utoi(pari_sp av, ulong x) { set_avma(av); return utoi(x); }
142 : INLINE GEN
143 1155333 : gc_utoipos(pari_sp av, ulong x) { set_avma(av); return utoipos(x); }
144 :
145 : INLINE GEN
146 98597108646 : new_chunk(size_t x) /* x is a number of longs */
147 : {
148 98597108646 : GEN z = ((GEN) avma) - x;
149 : CHECK_CTRLC
150 98597108646 : if (x > (avma-pari_mainstack->bot) / sizeof(long))
151 18 : new_chunk_resize(x);
152 98597108628 : set_avma((pari_sp)z);
153 : #ifdef MEMSTEP
154 : if (DEBUGMEM>1 && pari_mainstack->memused != DISABLE_MEMUSED) {
155 : long d = (long)pari_mainstack->memused - (long)z;
156 : if (labs(d) > 4*MEMSTEP)
157 : {
158 : pari_mainstack->memused = (pari_sp)z;
159 : err_printf("...%4.0lf Mbytes used\n",
160 : (pari_mainstack->top-pari_mainstack->memused)/1048576.);
161 : }
162 : }
163 : #endif
164 98597108624 : return z;
165 : }
166 :
167 : INLINE char *
168 46711247 : stack_malloc(size_t N)
169 : {
170 46711247 : long n = nchar2nlong(N);
171 46711247 : return (char*)new_chunk(n);
172 : }
173 :
174 : INLINE char *
175 55669671 : stack_malloc_align(size_t N, long k)
176 : {
177 55669671 : ulong d = ((ulong)avma) % k, e = ((ulong)N) % k;
178 55669671 : if (d) (void)new_chunk(d/sizeof(long));
179 55669671 : if (e) N += k-e;
180 55669671 : return (char*) new_chunk(nchar2nlong(N));
181 : }
182 :
183 : INLINE char *
184 110625 : stack_calloc(size_t N)
185 : {
186 110625 : char *p = stack_malloc(N);
187 110625 : memset(p, 0, N); return p;
188 : }
189 :
190 : INLINE char *
191 3325 : stack_calloc_align(size_t N, long k)
192 : {
193 3325 : ulong d = ((ulong)avma) % k, e = ((ulong)N) % k;
194 3325 : if (d) (void)new_chunk(d/sizeof(long));
195 3325 : if (e) N += k-e;
196 3325 : return stack_calloc(N);
197 : }
198 :
199 : INLINE GEN
200 857553 : newblock(size_t n)
201 857553 : { return newblock_t(n, 0); }
202 :
203 : /* cgetg(lg(x), typ(x)), set *lx. Implicit unsetisclone() */
204 : INLINE GEN
205 1490607980 : cgetg_copy(GEN x, long *plx) {
206 : GEN y;
207 1490607980 : *plx = lg(x); y = new_chunk((size_t)*plx);
208 1490607980 : y[0] = x[0] & (TYPBITS|LGBITS); return y;
209 : }
210 : INLINE GEN
211 430325 : cgetg_block(long x, long y)
212 : {
213 430325 : GEN z = newblock((size_t)x);
214 430325 : z[0] = CLONEBIT | evaltyp(y) | evallg(x);
215 430325 : return z;
216 : }
217 : INLINE GEN
218 26283401726 : cgetg(long x, long y)
219 : {
220 26283401726 : GEN z = new_chunk((size_t)x);
221 26283401726 : z[0] = evaltyp(y) | evallg(x);
222 26283401726 : return z;
223 : }
224 : INLINE GEN
225 28624063276 : cgeti(long x)
226 : {
227 28624063276 : GEN z = new_chunk((size_t)x);
228 28624063268 : z[0] = evaltyp(t_INT) | evallg(x);
229 28624063262 : return z;
230 : }
231 : INLINE GEN
232 16897425731 : cgetipos(long x)
233 : {
234 16897425731 : GEN z = cgeti(x);
235 16897425731 : z[1] = evalsigne(1) | evallgefint(x);
236 16897425731 : return z;
237 : }
238 : INLINE GEN
239 301062564 : cgetineg(long x)
240 : {
241 301062564 : GEN z = cgeti(x);
242 301062564 : z[1] = evalsigne(-1) | evallgefint(x);
243 301062564 : return z;
244 : }
245 : INLINE GEN
246 44735 : cgetr_block(long x)
247 : {
248 44735 : long l = nbits2lg(x);
249 44735 : GEN z = newblock((size_t)l);
250 44735 : z[0] = CLONEBIT | evaltyp(t_REAL) | evallg(l);
251 44735 : return z;
252 : }
253 : INLINE GEN
254 1945311635 : cgetr(long x)
255 : {
256 1945311635 : long l = nbits2lg(x);
257 1945311635 : GEN z = new_chunk((size_t)l);
258 1945311635 : z[0] = evaltyp(t_REAL) | evallg(l);
259 1945311635 : return z;
260 : }
261 :
262 : /*******************************************************************/
263 : /* */
264 : /* COPY, NEGATION, ABSOLUTE VALUE */
265 : /* */
266 : /*******************************************************************/
267 : /* cannot do memcpy because sometimes x and y overlap */
268 : INLINE GEN
269 5281770645 : leafcopy(GEN x)
270 : {
271 5281770645 : long lx = lg(x);
272 5281770645 : GEN y = new_chunk(lx); /* can't use cgetg_copy, in case x,y overlap */
273 26999296351 : while (--lx > 0) y[lx] = x[lx];
274 5281770645 : y[0] = x[0] & (TYPBITS|LGBITS); return y;
275 : }
276 : INLINE GEN
277 9368317609 : icopy(GEN x)
278 : {
279 9368317609 : long i = lgefint(x), lx = i;
280 9368317609 : GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
281 42162176920 : while (--i > 0) y[i] = x[i];
282 9368317602 : y[0] = evaltyp(t_INT) | evallg(lx);
283 9368317601 : return y;
284 : }
285 : INLINE GEN
286 119694333 : icopyspec(GEN x, long nx)
287 : {
288 119694333 : long i = nx+2, lx = i;
289 119694333 : GEN y = new_chunk(lx); /* can't use cgeti, in case x,y overlap */
290 3141683940 : x -= 2; while (--i >= 2) y[i] = x[i];
291 119694333 : y[1] = evalsigne(1) | evallgefint(lx);
292 119694333 : y[0] = evaltyp(t_INT) | evallg(lx);
293 119694333 : return y;
294 : }
295 1105106326 : INLINE GEN rcopy(GEN x) { return leafcopy(x); }
296 665 : INLINE GEN mpcopy(GEN x) { return leafcopy(x); }
297 :
298 : INLINE GEN
299 2118312134 : mpabs(GEN x) { GEN y = leafcopy(x); setabssign(y); return y; }
300 : INLINE GEN
301 13428280 : mpabs_shallow(GEN x) { return signe(x) < 0? mpabs(x): x; }
302 2076241516 : INLINE GEN absi(GEN x) { return mpabs(x); }
303 61952791 : INLINE GEN absi_shallow(GEN x) { return signe(x) < 0? negi(x): x; }
304 20510 : INLINE GEN absr(GEN x) { return mpabs(x); }
305 :
306 : INLINE GEN
307 951747160 : mpneg(GEN x) { GEN y = leafcopy(x); togglesign(y); return y; }
308 661325894 : INLINE GEN negi(GEN x) { return mpneg(x); }
309 3961206 : INLINE GEN negr(GEN x) { return mpneg(x); }
310 :
311 : /* negate in place */
312 : INLINE void
313 1995816461 : togglesign(GEN x) { if (x[1] & SIGNBITS) { x[1] ^= HIGHBIT; } }
314 : INLINE void
315 2186886869 : setabssign(GEN x) { x[1] &= ~HIGHBIT; }
316 : /* negate in place, except universal constants */
317 : INLINE void
318 157521618 : togglesign_safe(GEN *px)
319 : {
320 157521618 : switch(*px - gen_1) /* gen_1, gen_2, gen_m1, gen_m2 */
321 : {
322 3670922 : case 0: *px = gen_m1; break;
323 4 : case 3: *px = gen_m2; break;
324 845673 : case 6: *px = gen_1; break;
325 0 : case 9: *px = gen_2; break;
326 153005019 : default: togglesign(*px);
327 : }
328 157521618 : }
329 : /* setsigne(y, signe(x)) */
330 : INLINE void
331 0 : affectsign(GEN x, GEN y)
332 : {
333 0 : y[1] = (x[1] & SIGNBITS) | (y[1] & ~SIGNBITS);
334 0 : }
335 : /* copies sign in place, except for universal constants */
336 : INLINE void
337 10754964 : affectsign_safe(GEN x, GEN *py)
338 : {
339 10754964 : if (((*py)[1] ^ x[1]) & HIGHBIT) togglesign_safe(py);
340 10754964 : }
341 : /*******************************************************************/
342 : /* */
343 : /* GEN -> LONG, LONG -> GEN */
344 : /* */
345 : /*******************************************************************/
346 : /* assume x != 0, return -x as a t_INT */
347 : INLINE GEN
348 300139804 : utoineg(ulong x) { GEN y = cgetineg(3); y[2] = x; return y; }
349 : /* assume x != 0, return utoi(x) */
350 : INLINE GEN
351 14661350980 : utoipos(ulong x) { GEN y = cgetipos(3); y[2] = x; return y; }
352 : INLINE GEN
353 12428771340 : utoi(ulong x) { return x? utoipos(x): gen_0; }
354 : INLINE GEN
355 775980848 : stoi(long x)
356 : {
357 775980848 : if (!x) return gen_0;
358 552887368 : return x > 0? utoipos((ulong)x): utoineg((ulong)-x);
359 : }
360 :
361 : /* x 2^BIL + y */
362 : INLINE GEN
363 9224566833 : uutoi(ulong x, ulong y)
364 : {
365 : GEN z;
366 9224566833 : if (!x) return utoi(y);
367 884189948 : z = cgetipos(4);
368 884189948 : *int_W_lg(z, 1, 4) = x;
369 884189948 : *int_W_lg(z, 0, 4) = y; return z;
370 : }
371 : /* - (x 2^BIL + y) */
372 : INLINE GEN
373 365874 : uutoineg(ulong x, ulong y)
374 : {
375 : GEN z;
376 365874 : if (!x) return y? utoineg(y): gen_0;
377 16534 : z = cgetineg(4);
378 16534 : *int_W_lg(z, 1, 4) = x;
379 16534 : *int_W_lg(z, 0, 4) = y; return z;
380 : }
381 :
382 : INLINE long
383 477689084 : itos(GEN x)
384 : {
385 477689084 : long s = signe(x);
386 : long u;
387 :
388 477689084 : if (!s) return 0;
389 445788049 : u = x[2];
390 445788049 : if (lgefint(x) > 3 || u < 0)
391 30 : pari_err_OVERFLOW("t_INT-->long assignment");
392 445788019 : return (s>0) ? u : -u;
393 : }
394 : /* as itos, but return 0 if too large. Cf is_bigint */
395 : INLINE long
396 44914153 : itos_or_0(GEN x) {
397 : long n;
398 44914153 : if (lgefint(x) != 3 || (n = x[2]) & HIGHBIT) return 0;
399 38505932 : return signe(x) > 0? n: -n;
400 : }
401 : INLINE ulong
402 180892281 : itou(GEN x)
403 : {
404 180892281 : switch(lgefint(x)) {
405 13844215 : case 2: return 0;
406 167048066 : case 3: return x[2];
407 0 : default:
408 0 : pari_err_OVERFLOW("t_INT-->ulong assignment");
409 : return 0; /* LCOV_EXCL_LINE */
410 : }
411 : }
412 :
413 : /* as itou, but return 0 if too large. Cf is_bigint */
414 : INLINE ulong
415 4918365 : itou_or_0(GEN x) {
416 4918365 : if (lgefint(x) != 3) return 0;
417 4897286 : return (ulong)x[2];
418 : }
419 :
420 : INLINE ulong
421 6346931 : umuluu_or_0(ulong x, ulong y)
422 : {
423 : ulong z;
424 : LOCAL_HIREMAINDER;
425 6346931 : z = mulll(x, y);
426 6346931 : return hiremainder? 0: z;
427 : }
428 : /* return x*y if <= n, else 0. Beware overflow */
429 : INLINE ulong
430 7618608 : umuluu_le(ulong x, ulong y, ulong n)
431 : {
432 : ulong z;
433 : LOCAL_HIREMAINDER;
434 7618608 : z = mulll(x, y);
435 7618608 : return (hiremainder || z > n)? 0: z;
436 : }
437 :
438 : INLINE GEN
439 501115704 : real_0_bit(long bitprec) { GEN x=cgetg(2, t_REAL); x[1]=evalexpo(bitprec); return x; }
440 : INLINE GEN
441 1160772 : real_0(long prec) { return real_0_bit(-prec); }
442 : INLINE GEN
443 4699334 : real_1_bit(long bit) { return real_1(nbits2prec(bit)); }
444 : INLINE GEN
445 143848036 : real_1(long prec) {
446 143848036 : GEN x = cgetr(prec);
447 143848036 : long i, l = lg(x);
448 143848036 : x[1] = evalsigne(1) | _evalexpo(0);
449 709257670 : x[2] = (long)HIGHBIT; for (i=3; i<l; i++) x[i] = 0;
450 143848036 : return x;
451 : }
452 : INLINE GEN
453 455 : real_m1(long prec) {
454 455 : GEN x = cgetr(prec);
455 455 : long i, l = lg(x);
456 455 : x[1] = evalsigne(-1) | _evalexpo(0);
457 1761 : x[2] = (long)HIGHBIT; for (i=3; i<l; i++) x[i] = 0;
458 455 : return x;
459 : }
460 :
461 : /* 2.^n */
462 : INLINE GEN
463 1070081 : real2n(long n, long prec) { GEN z = real_1(prec); setexpo(z, n); return z; }
464 : INLINE GEN
465 126 : real_m2n(long n, long prec) { GEN z = real_m1(prec); setexpo(z, n); return z; }
466 : INLINE GEN
467 507929587 : stor(long s, long prec) { GEN z = cgetr(prec); affsr(s,z); return z; }
468 : INLINE GEN
469 13344603 : utor(ulong s, long prec){ GEN z = cgetr(prec); affur(s,z); return z; }
470 : INLINE GEN
471 752865426 : itor(GEN x, long prec) { GEN z = cgetr(prec); affir(x,z); return z; }
472 : INLINE GEN
473 308815146 : rtor(GEN x, long prec) { GEN z = cgetr(prec); affrr(x,z); return z; }
474 :
475 : INLINE ulong
476 21021018 : int_bit(GEN x, long n)
477 : {
478 21021018 : long r, q = dvmdsBIL(n, &r);
479 21021018 : return q < lgefint(x)-2?((ulong)*int_W(x,q) >> r) & 1UL:0;
480 : }
481 :
482 : /*******************************************************************/
483 : /* */
484 : /* COMPARISON */
485 : /* */
486 : /*******************************************************************/
487 : INLINE int
488 1416201 : cmpss(long a, long b)
489 1416201 : { return a>b? 1: (a<b? -1: 0); }
490 :
491 : INLINE int
492 318022331 : cmpuu(ulong a, ulong b)
493 318022331 : { return a>b? 1: (a<b? -1: 0); }
494 :
495 : INLINE int
496 1730062 : cmpir(GEN x, GEN y)
497 : {
498 : pari_sp av;
499 : GEN z;
500 :
501 1730062 : if (!signe(x)) return -signe(y);
502 421476 : if (!signe(y))
503 : {
504 6182 : if (expo(y) >= expi(x)) return 0;
505 6154 : return signe(x);
506 : }
507 415294 : av=avma; z = itor(x, realprec(y)); set_avma(av);
508 415294 : return cmprr(z,y); /* cmprr does no memory adjustment */
509 : }
510 : INLINE int
511 303853 : cmpri(GEN x, GEN y) { return -cmpir(y,x); }
512 : INLINE int
513 796228 : cmpsr(long x, GEN y)
514 : {
515 : pari_sp av;
516 : GEN z;
517 :
518 796228 : if (!x) return -signe(y);
519 796228 : av=avma; z = stor(x, LOWDEFAULTPREC); set_avma(av);
520 796228 : return cmprr(z,y);
521 : }
522 : INLINE int
523 40996 : cmprs(GEN x, long y) { return -cmpsr(y,x); }
524 : /* compare x and y */
525 : INLINE int
526 12997112 : cmpui(ulong x, GEN y)
527 : {
528 : ulong p;
529 12997112 : if (!x) return -signe(y);
530 12997112 : if (signe(y) <= 0) return 1;
531 12868942 : if (lgefint(y) > 3) return -1;
532 12595885 : p = y[2]; if (p == x) return 0;
533 12492010 : return p < x ? 1 : -1;
534 : }
535 : INLINE int
536 12997112 : cmpiu(GEN x, ulong y) { return -cmpui(y,x); }
537 : /* compare x and |y| */
538 : INLINE int
539 34003339 : abscmpui(ulong x, GEN y)
540 : {
541 34003339 : long l = lgefint(y);
542 : ulong p;
543 :
544 34003339 : if (!x) return (l > 2)? -1: 0;
545 34003318 : if (l == 2) return 1;
546 33824784 : if (l > 3) return -1;
547 33803175 : p = y[2]; if (p == x) return 0;
548 32871597 : return p < x ? 1 : -1;
549 : }
550 : INLINE int
551 34003339 : abscmpiu(GEN x, ulong y) { return -abscmpui(y,x); }
552 : INLINE int
553 6225282 : cmpsi(long x, GEN y)
554 : {
555 : ulong p;
556 :
557 6225282 : if (!x) return -signe(y);
558 :
559 6221069 : if (x > 0)
560 : {
561 6220033 : if (signe(y)<=0) return 1;
562 6209092 : if (lgefint(y)>3) return -1;
563 6203190 : p = y[2]; if (p == (ulong)x) return 0;
564 6133035 : return p < (ulong)x ? 1 : -1;
565 : }
566 :
567 1036 : if (signe(y)>=0) return -1;
568 119 : if (lgefint(y)>3) return 1;
569 119 : p = y[2]; if (p == (ulong)-x) return 0;
570 14 : return p < (ulong)(-x) ? -1 : 1;
571 : }
572 : INLINE int
573 5993283 : cmpis(GEN x, long y) { return -cmpsi(y,x); }
574 : INLINE int
575 2164580 : mpcmp(GEN x, GEN y)
576 : {
577 2164580 : if (typ(x)==t_INT)
578 70459 : return (typ(y)==t_INT) ? cmpii(x,y) : cmpir(x,y);
579 2094121 : return (typ(y)==t_INT) ? -cmpir(y,x) : cmprr(x,y);
580 : }
581 :
582 : /* x == y ? */
583 : INLINE int
584 3042312 : equalui(ulong x, GEN y)
585 : {
586 3042312 : if (!x) return !signe(y);
587 3041619 : if (signe(y) <= 0 || lgefint(y) != 3) return 0;
588 3030056 : return ((ulong)y[2] == (ulong)x);
589 : }
590 : /* x == y ? */
591 : INLINE int
592 1150030 : equalsi(long x, GEN y)
593 : {
594 1150030 : if (!x) return !signe(y);
595 1150030 : if (x > 0)
596 : {
597 1142694 : if (signe(y) <= 0 || lgefint(y) != 3) return 0;
598 1065072 : return ((ulong)y[2] == (ulong)x);
599 : }
600 7336 : if (signe(y) >= 0 || lgefint(y) != 3) return 0;
601 4885 : return ((ulong)y[2] == (ulong)-x);
602 : }
603 : /* x == |y| ? */
604 : INLINE int
605 47340909 : absequalui(ulong x, GEN y)
606 : {
607 47340909 : if (!x) return !signe(y);
608 47340909 : return (lgefint(y) == 3 && (ulong)y[2] == x);
609 : }
610 : INLINE int
611 45591178 : absequaliu(GEN x, ulong y) { return absequalui(y,x); }
612 : INLINE int
613 1149848 : equalis(GEN x, long y) { return equalsi(y,x); }
614 : INLINE int
615 3042312 : equaliu(GEN x, ulong y) { return equalui(y,x); }
616 :
617 : /* assume x != 0, is |x| == 2^n ? */
618 : INLINE int
619 1580660 : absrnz_equal2n(GEN x) {
620 1580660 : if ((ulong)x[2]==HIGHBIT)
621 : {
622 206721 : long i, lx = lg(x);
623 615846 : for (i = 3; i < lx; i++)
624 424774 : if (x[i]) return 0;
625 191072 : return 1;
626 : }
627 1373939 : return 0;
628 : }
629 : /* assume x != 0, is |x| == 1 ? */
630 : INLINE int
631 4559159 : absrnz_equal1(GEN x) { return !expo(x) && absrnz_equal2n(x); }
632 :
633 : INLINE long
634 7511191534 : maxss(long x, long y) { return x>y?x:y; }
635 : INLINE long
636 2060413898 : minss(long x, long y) { return x<y?x:y; }
637 : INLINE long
638 57543266 : minuu(ulong x, ulong y) { return x<y?x:y; }
639 : INLINE long
640 6603109 : maxuu(ulong x, ulong y) { return x>y?x:y; }
641 : INLINE double
642 3218320 : maxdd(double x, double y) { return x>y?x:y; }
643 : INLINE double
644 265748 : mindd(double x, double y) { return x<y?x:y; }
645 :
646 : /*******************************************************************/
647 : /* */
648 : /* ADD / SUB */
649 : /* */
650 : /*******************************************************************/
651 : INLINE GEN
652 25081 : subuu(ulong x, ulong y)
653 : {
654 : ulong z;
655 : LOCAL_OVERFLOW;
656 25081 : z = subll(x, y);
657 25081 : return overflow? utoineg(-z): utoi(z);
658 : }
659 : INLINE GEN
660 3542291387 : adduu(ulong x, ulong y) { ulong t = x+y; return uutoi((t < x), t); }
661 :
662 : INLINE GEN
663 25081 : addss(long x, long y)
664 : {
665 25081 : if (!x) return stoi(y);
666 25081 : if (!y) return stoi(x);
667 25081 : if (x > 0) return y > 0? adduu(x,y): subuu(x, -y);
668 :
669 25081 : if (y > 0) return subuu(y, -x);
670 : else { /* - adduu(-x, -y) */
671 0 : ulong t = (-x)+(-y); return uutoineg((t < (ulong)(-x)), t);
672 : }
673 : }
674 25081 : INLINE GEN subss(long x, long y) { return addss(-y,x); }
675 :
676 : INLINE GEN
677 7999376559 : subii(GEN x, GEN y)
678 : {
679 7999376559 : if (x==y) return gen_0; /* frequent with x = y = gen_0 */
680 6636425047 : return addii_sign(x, signe(x), y, -signe(y));
681 : }
682 : INLINE GEN
683 13117039973 : addii(GEN x, GEN y) { return addii_sign(x, signe(x), y, signe(y)); }
684 : INLINE GEN
685 3029487995 : addrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, signe(y)); }
686 : INLINE GEN
687 1062219745 : subrr(GEN x, GEN y) { return addrr_sign(x, signe(x), y, -signe(y)); }
688 : INLINE GEN
689 490346441 : addir(GEN x, GEN y) { return addir_sign(x, signe(x), y, signe(y)); }
690 : INLINE GEN
691 3030767 : subir(GEN x, GEN y) { return addir_sign(x, signe(x), y, -signe(y)); }
692 : INLINE GEN
693 14336798 : subri(GEN x, GEN y) { return addir_sign(y, -signe(y), x, signe(x)); }
694 : INLINE GEN
695 320387213 : addsi(long x, GEN y) { return addsi_sign(x, y, signe(y)); }
696 : INLINE GEN
697 108580179 : addui(ulong x, GEN y) { return addui_sign(x, y, signe(y)); }
698 : INLINE GEN
699 6244765 : subsi(long x, GEN y) { return addsi_sign(x, y, -signe(y)); }
700 : INLINE GEN
701 138448940 : subui(ulong x, GEN y) { return addui_sign(x, y, -signe(y)); }
702 :
703 : /*******************************************************************/
704 : /* */
705 : /* MOD, REM, DIV */
706 : /* */
707 : /*******************************************************************/
708 113283948 : INLINE ulong mod2BIL(GEN x) { return *int_LSW(x); }
709 0 : INLINE long mod64(GEN x) { return mod2BIL(x) & 63; }
710 259 : INLINE long mod32(GEN x) { return mod2BIL(x) & 31; }
711 308664 : INLINE long mod16(GEN x) { return mod2BIL(x) & 15; }
712 15671974 : INLINE long mod8(GEN x) { return mod2BIL(x) & 7; }
713 7470821 : INLINE long mod4(GEN x) { return mod2BIL(x) & 3; }
714 65776550 : INLINE long mod2(GEN x) { return mod2BIL(x) & 1; }
715 : INLINE int
716 118264473 : mpodd(GEN x) { return signe(x) && mod2(x); }
717 : /* x mod 2^n, n < BITS_IN_LONG */
718 : INLINE ulong
719 57880754 : umodi2n(GEN x, long n)
720 : {
721 57880754 : long s = signe(x);
722 57880754 : const ulong _2n = 1UL << n;
723 : ulong m;
724 57880754 : if (!s) return 0;
725 56151089 : m = *int_LSW(x) & (_2n - 1);
726 56151089 : if (s < 0 && m) m = _2n - m;
727 56151089 : return m;
728 : }
729 0 : INLINE ulong Mod64(GEN x){ return umodi2n(x,6); }
730 311157 : INLINE ulong Mod32(GEN x){ return umodi2n(x,5); }
731 389789 : INLINE ulong Mod16(GEN x){ return umodi2n(x,4); }
732 3247314 : INLINE ulong Mod8(GEN x) { return umodi2n(x,3); }
733 50398915 : INLINE ulong Mod4(GEN x) { return umodi2n(x,2); }
734 3533481 : INLINE ulong Mod2(GEN x) { return umodi2n(x,1); }
735 :
736 : INLINE GEN
737 54895821 : truedivii(GEN a,GEN b) { return truedvmdii(a,b,NULL); }
738 : INLINE GEN
739 268434 : truedivis(GEN a, long b) { return truedvmdis(a,b,NULL); }
740 : INLINE GEN
741 6202318 : truedivsi(long a, GEN b) { return truedvmdsi(a,b,NULL); }
742 :
743 : INLINE GEN
744 15867899 : divii(GEN a, GEN b) { return dvmdii(a,b,NULL); }
745 : INLINE GEN
746 3102954963 : remii(GEN a, GEN b) { return dvmdii(a,b,ONLY_REM); }
747 :
748 : INLINE GEN
749 0 : divss(long x, long y) { return stoi(x / y); }
750 : INLINE GEN
751 0 : modss(long x, long y) { return utoi(smodss(x, y)); }
752 : INLINE GEN
753 0 : remss(long x, long y) { return stoi(x % y); }
754 : INLINE long
755 13998082 : smodss(long x, long y)
756 : {
757 13998082 : long r = x%y;
758 13998082 : return (r >= 0)? r: labs(y) + r;
759 : }
760 : INLINE ulong
761 734449783 : umodsu(long x, ulong y)
762 : {
763 734449783 : return x>=0 ? x%y: Fl_neg((-x)%y, y);
764 : }
765 :
766 : INLINE long
767 0 : sdivss_rem(long x, long y, long *r)
768 : {
769 : long q;
770 : LOCAL_HIREMAINDER;
771 0 : if (!y) pari_err_INV("sdivss_rem",gen_0);
772 0 : hiremainder = 0; q = divll((ulong)labs(x),(ulong)labs(y));
773 0 : if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
774 0 : if (y < 0) q = -q;
775 0 : *r = hiremainder; return q;
776 : }
777 : INLINE GEN
778 0 : divss_rem(long x, long y, long *r) { return stoi(sdivss_rem(x,y,r)); }
779 : INLINE ulong
780 2418435 : udivuu_rem(ulong x, ulong y, ulong *r)
781 : {
782 2418435 : if (!y) pari_err_INV("udivuu_rem",gen_0);
783 2418435 : *r = x % y; return x / y;
784 : }
785 : INLINE ulong
786 3917040 : ceildivuu(ulong a, ulong b)
787 : {
788 : ulong c;
789 3917040 : if (!a) return 0;
790 3733038 : c = a / b; return (a % b)? c+1: c;
791 : }
792 :
793 : INLINE ulong
794 128889 : uabsdivui_rem(ulong x, GEN y, ulong *r)
795 : {
796 128889 : long q, s = signe(y);
797 : LOCAL_HIREMAINDER;
798 :
799 128889 : if (!s) pari_err_INV("uabsdivui_rem",gen_0);
800 128889 : if (!x || lgefint(y)>3) { *r = x; return 0; }
801 128182 : hiremainder=0; q = (long)divll(x, (ulong)y[2]);
802 128182 : if (s < 0) q = -q;
803 128182 : *r = hiremainder; return q;
804 : }
805 :
806 : /* assume d != 0 and |n| / d can be represented as an ulong.
807 : * Return |n|/d, set *r = |n| % d */
808 : INLINE ulong
809 25949953 : uabsdiviu_rem(GEN n, ulong d, ulong *r)
810 : {
811 25949953 : switch(lgefint(n))
812 : {
813 0 : case 2: *r = 0; return 0;
814 25949953 : case 3:
815 : {
816 25949953 : ulong nn = n[2];
817 25949953 : *r = nn % d; return nn / d;
818 : }
819 0 : default: /* 4 */
820 : {
821 : ulong n1, n0, q;
822 : LOCAL_HIREMAINDER;
823 0 : n0 = *int_W(n,0);
824 0 : n1 = *int_W(n,1);
825 0 : hiremainder = n1;
826 0 : q = divll(n0, d);
827 0 : *r = hiremainder; return q;
828 : }
829 : }
830 : }
831 :
832 : INLINE long
833 51446796 : sdivsi_rem(long x, GEN y, long *r)
834 : {
835 51446796 : long q, s = signe(y);
836 : LOCAL_HIREMAINDER;
837 :
838 51446796 : if (!s) pari_err_INV("sdivsi_rem",gen_0);
839 51446796 : if (!x || lgefint(y)>3 || ((long)y[2]) < 0) { *r = x; return 0; }
840 49489317 : hiremainder=0; q = (long)divll(labs(x), (ulong)y[2]);
841 49489317 : if (x < 0) { hiremainder = -((long)hiremainder); q = -q; }
842 49489317 : if (s < 0) q = -q;
843 49489317 : *r = hiremainder; return q;
844 : }
845 : INLINE GEN
846 0 : divsi_rem(long s, GEN y, long *r) { return stoi(sdivsi_rem(s,y,r)); }
847 :
848 : INLINE long
849 102165 : sdivsi(long x, GEN y)
850 : {
851 102165 : long q, s = signe(y);
852 :
853 102165 : if (!s) pari_err_INV("sdivsi",gen_0);
854 102165 : if (!x || lgefint(y)>3 || ((long)y[2]) < 0) return 0;
855 102060 : q = labs(x) / y[2];
856 102060 : if (x < 0) q = -q;
857 102060 : if (s < 0) q = -q;
858 102060 : return q;
859 : }
860 :
861 : INLINE GEN
862 0 : dvmdss(long x, long y, GEN *z)
863 : {
864 : long r;
865 0 : GEN q = divss_rem(x,y, &r);
866 0 : *z = stoi(r); return q;
867 : }
868 : INLINE long
869 7366157198 : dvmdsBIL(long n, long *r) { *r = remsBIL(n); return divsBIL(n); }
870 : INLINE ulong
871 170572975 : dvmduBIL(ulong n, ulong *r) { *r = remsBIL(n); return divsBIL(n); }
872 : INLINE GEN
873 0 : dvmdsi(long x, GEN y, GEN *z)
874 : {
875 : long r;
876 0 : GEN q = divsi_rem(x,y, &r);
877 0 : *z = stoi(r); return q;
878 : }
879 : INLINE GEN
880 0 : dvmdis(GEN x, long y, GEN *z)
881 : {
882 : long r;
883 0 : GEN q = divis_rem(x,y, &r);
884 0 : *z = stoi(r); return q;
885 : }
886 :
887 : INLINE long
888 21176116 : smodis(GEN x, long y)
889 : {
890 21176116 : pari_sp av = avma;
891 21176116 : long r; (void)divis_rem(x,y, &r);
892 21176116 : return gc_long(av, (r >= 0)? r: labs(y) + r);
893 : }
894 : INLINE GEN
895 19638526 : modis(GEN x, long y) { return stoi(smodis(x,y)); }
896 : INLINE GEN
897 45242182 : modsi(long x, GEN y) {
898 45242182 : long r; (void)sdivsi_rem(x, y, &r);
899 45242182 : return (r >= 0)? stoi(r): addsi_sign(r, y, 1);
900 : }
901 :
902 : INLINE ulong
903 710986 : umodui(ulong x, GEN y)
904 : {
905 710986 : if (!signe(y)) pari_err_INV("umodui",gen_0);
906 710986 : if (!x || lgefint(y) > 3) return x;
907 414849 : return x % (ulong)y[2];
908 : }
909 :
910 : INLINE ulong
911 10312787 : ugcdiu(GEN x, ulong y) { return ugcd(umodiu(x,y), y); }
912 : INLINE ulong
913 2737 : ugcdui(ulong y, GEN x) { return ugcd(umodiu(x,y), y); }
914 :
915 : INLINE GEN
916 0 : remsi(long x, GEN y)
917 0 : { long r; (void)sdivsi_rem(x,y, &r); return stoi(r); }
918 : INLINE GEN
919 0 : remis(GEN x, long y)
920 : {
921 0 : pari_sp av = avma;
922 : long r;
923 0 : (void)divis_rem(x,y, &r); return gc_stoi(av, r);
924 : }
925 :
926 : INLINE GEN
927 0 : rdivis(GEN x, long y, long prec)
928 : {
929 0 : GEN z = cgetr(prec);
930 0 : pari_sp av = avma;
931 0 : affrr(divrs(itor(x,prec), y),z);
932 0 : set_avma(av); return z;
933 : }
934 : INLINE GEN
935 0 : rdivsi(long x, GEN y, long prec)
936 : {
937 0 : GEN z = cgetr(prec);
938 0 : pari_sp av = avma;
939 0 : affrr(divsr(x, itor(y,prec)), z);
940 0 : set_avma(av); return z;
941 : }
942 : INLINE GEN
943 839647 : rdivss(long x, long y, long prec)
944 : {
945 839647 : GEN z = cgetr(prec);
946 839647 : pari_sp av = avma;
947 839647 : affrr(divrs(stor(x, prec), y), z);
948 839647 : set_avma(av); return z;
949 : }
950 :
951 : INLINE void
952 13173966 : rdiviiz(GEN x, GEN y, GEN z)
953 : {
954 13173966 : long lz = lg(z), lx = lgefint(x), ly = lgefint(y);
955 13173966 : if (lx == 2) { affur(0, z); return; }
956 13173966 : if (ly == 3)
957 : {
958 2256076 : affir(x, z); if (signe(y) < 0) togglesign(z);
959 2256076 : affrr(divru(z, y[2]), z);
960 : }
961 10917890 : else if (lx > lz + 1 || ly > lz + 1)
962 : {
963 5962512 : affir(x,z); affrr(divri(z, y), z);
964 : }
965 : else
966 : {
967 4955378 : long b = lg2prec(lz) + expi(y) - expi(x) + 1;
968 4955378 : GEN q = divii(b > 0? shifti(x, b): x, y);
969 4955378 : affir(q, z); if (b > 0) shiftr_inplace(z, -b);
970 : }
971 13173966 : set_avma((ulong)z);
972 : }
973 : INLINE GEN
974 13134339 : rdivii(GEN x, GEN y, long prec)
975 13134339 : { GEN z = cgetr(prec); rdiviiz(x, y, z); return z; }
976 : INLINE GEN
977 7389437 : fractor(GEN x, long prec)
978 7389437 : { return rdivii(gel(x,1), gel(x,2), prec); }
979 :
980 : INLINE int
981 17072415 : dvdii(GEN x, GEN y)
982 : {
983 17072415 : pari_sp av = avma;
984 : GEN r;
985 17072415 : if (!signe(x)) return 1;
986 15635616 : if (!signe(y)) return 0;
987 15635616 : r = remii(x,y);
988 15635616 : return gc_bool(av, r == gen_0);
989 : }
990 : INLINE int
991 371 : dvdsi(long x, GEN y)
992 : {
993 371 : if (x == 0) return 1;
994 266 : if (!signe(y)) return 0;
995 266 : if (lgefint(y) != 3) return 0;
996 259 : return x % y[2] == 0;
997 : }
998 : INLINE int
999 167545 : dvdui(ulong x, GEN y)
1000 : {
1001 167545 : if (x == 0) return 1;
1002 167545 : if (!signe(y)) return 0;
1003 167545 : if (lgefint(y) != 3) return 0;
1004 156924 : return x % y[2] == 0;
1005 : }
1006 : INLINE int
1007 33765 : dvdis(GEN x, long y)
1008 33765 : { return y? smodis(x, y) == 0: signe(x) == 0; }
1009 : INLINE int
1010 576287 : dvdiu(GEN x, ulong y)
1011 576287 : { return y? umodiu(x, y) == 0: signe(x) == 0; }
1012 :
1013 : INLINE int
1014 0 : dvdisz(GEN x, long y, GEN z)
1015 : {
1016 0 : const pari_sp av = avma;
1017 : long r;
1018 0 : GEN p1 = divis_rem(x,y, &r);
1019 0 : set_avma(av); if (r) return 0;
1020 0 : affii(p1,z); return 1;
1021 : }
1022 : INLINE int
1023 0 : dvdiuz(GEN x, ulong y, GEN z)
1024 : {
1025 0 : const pari_sp av = avma;
1026 : ulong r;
1027 0 : GEN p1 = absdiviu_rem(x,y, &r);
1028 0 : set_avma(av); if (r) return 0;
1029 0 : affii(p1,z); return 1;
1030 : }
1031 : INLINE int
1032 0 : dvdiiz(GEN x, GEN y, GEN z)
1033 : {
1034 0 : const pari_sp av=avma;
1035 0 : GEN p2, p1 = dvmdii(x,y,&p2);
1036 0 : if (signe(p2)) return gc_bool(av,0);
1037 0 : affii(p1,z); return gc_bool(av,1);
1038 : }
1039 :
1040 : INLINE ulong
1041 79167995 : remlll_pre(ulong u2, ulong u1, ulong u0, ulong n, ulong ninv)
1042 : {
1043 79167995 : u1 = remll_pre(u2, u1, n, ninv);
1044 79167995 : return remll_pre(u1, u0, n, ninv);
1045 : }
1046 :
1047 : INLINE ulong
1048 2563944591 : Fl_sqr_pre(ulong a, ulong p, ulong pi)
1049 : {
1050 : ulong x;
1051 : LOCAL_HIREMAINDER;
1052 2563944591 : x = mulll(a,a);
1053 2563944591 : return remll_pre(hiremainder, x, p, pi);
1054 : }
1055 :
1056 : INLINE ulong
1057 4446009244 : Fl_mul_pre(ulong a, ulong b, ulong p, ulong pi)
1058 : {
1059 : ulong x;
1060 : LOCAL_HIREMAINDER;
1061 4446009244 : x = mulll(a,b);
1062 4446009244 : return remll_pre(hiremainder, x, p, pi);
1063 : }
1064 :
1065 : INLINE ulong
1066 8205633402 : Fl_addmul_pre(ulong y0, ulong x0, ulong x1, ulong p, ulong pi)
1067 : {
1068 : ulong l0, h0;
1069 : LOCAL_HIREMAINDER;
1070 8205633402 : hiremainder = y0;
1071 8205633402 : l0 = addmul(x0, x1); h0 = hiremainder;
1072 8205633402 : return remll_pre(h0, l0, p, pi);
1073 : }
1074 :
1075 : INLINE ulong
1076 74215850 : Fl_addmulmul_pre(ulong x0, ulong y0, ulong x1, ulong y1, ulong p, ulong pi)
1077 : {
1078 : ulong l0, l1, h0, h1;
1079 : LOCAL_OVERFLOW;
1080 : LOCAL_HIREMAINDER;
1081 74215850 : l0 = mulll(x0, y0); h0 = hiremainder;
1082 74215850 : l1 = mulll(x1, y1); h1 = hiremainder;
1083 74215850 : l0 = addll(l0, l1); h0 = addllx(h0, h1);
1084 74215850 : return overflow ? remlll_pre(1, h0, l0, p, pi): remll_pre(h0, l0, p, pi);
1085 : }
1086 :
1087 : INLINE ulong
1088 242732 : Fl_ellj_pre(ulong a4, ulong a6, ulong p, ulong pi)
1089 : {
1090 : /* a43 = 4 a4^3 */
1091 242732 : ulong a43 = Fl_double(Fl_double(
1092 : Fl_mul_pre(a4, Fl_sqr_pre(a4, p, pi), p, pi), p), p);
1093 : /* a62 = 27 a6^2 */
1094 242732 : ulong a62 = Fl_mul_pre(Fl_sqr_pre(a6, p, pi), 27 % p, p, pi);
1095 242732 : ulong z1 = Fl_mul_pre(a43, 1728 % p, p, pi);
1096 242732 : ulong z2 = Fl_add(a43, a62, p);
1097 242732 : return Fl_div(z1, z2, p);
1098 : }
1099 :
1100 : /*******************************************************************/
1101 : /* */
1102 : /* MP (INT OR REAL) */
1103 : /* */
1104 : /*******************************************************************/
1105 : INLINE GEN
1106 49 : mptrunc(GEN x) { return typ(x)==t_INT? icopy(x): truncr(x); }
1107 : INLINE GEN
1108 0 : mpfloor(GEN x) { return typ(x)==t_INT? icopy(x): floorr(x); }
1109 : INLINE GEN
1110 0 : mpceil(GEN x) { return typ(x)==t_INT? icopy(x): ceilr(x); }
1111 : INLINE GEN
1112 1216174 : mpround(GEN x) { return typ(x) == t_INT? icopy(x): roundr(x); }
1113 :
1114 : INLINE long
1115 37844963 : mpexpo(GEN x) { return typ(x) == t_INT? expi(x): expo(x); }
1116 :
1117 : INLINE GEN
1118 599765229 : mpadd(GEN x, GEN y)
1119 : {
1120 599765229 : if (typ(x)==t_INT)
1121 16464184 : return (typ(y)==t_INT) ? addii(x,y) : addir(x,y);
1122 583301045 : return (typ(y)==t_INT) ? addir(y,x) : addrr(x,y);
1123 : }
1124 : INLINE GEN
1125 274014845 : mpsub(GEN x, GEN y)
1126 : {
1127 274014845 : if (typ(x)==t_INT)
1128 519555 : return (typ(y)==t_INT) ? subii(x,y) : subir(x,y);
1129 273495290 : return (typ(y)==t_INT) ? subri(x,y) : subrr(x,y);
1130 : }
1131 : INLINE GEN
1132 891368605 : mpmul(GEN x, GEN y)
1133 : {
1134 891368605 : if (typ(x)==t_INT)
1135 38303349 : return (typ(y)==t_INT) ? mulii(x,y) : mulir(x,y);
1136 853065256 : return (typ(y)==t_INT) ? mulir(y,x) : mulrr(x,y);
1137 : }
1138 : INLINE GEN
1139 91859474 : mpsqr(GEN x) { return (typ(x)==t_INT) ? sqri(x) : sqrr(x); }
1140 :
1141 : /* obsolete */
1142 : INLINE GEN
1143 0 : mpdiv(GEN x, GEN y)
1144 : {
1145 0 : if (typ(x) == t_REAL) return divrmp(x,y);
1146 0 : if (typ(y) == t_REAL) return divmpr(x,y);
1147 0 : pari_err_TYPE2("mpdiv",x,y); return NULL;
1148 : }
1149 : INLINE GEN
1150 1202981 : divrmp(GEN x, GEN y) { return (typ(y)==t_INT) ? divri(x,y) : divrr(x,y); }
1151 : INLINE GEN
1152 3759 : divmpr(GEN x, GEN y) { return (typ(x)==t_INT) ? divir(x,y) : divrr(x,y); }
1153 :
1154 : /*******************************************************************/
1155 : /* */
1156 : /* Z/nZ, n ULONG */
1157 : /* */
1158 : /*******************************************************************/
1159 : INLINE ulong
1160 520885887 : Fl_double(ulong a, ulong p)
1161 : {
1162 520885887 : ulong res = a << 1;
1163 520885887 : return (res >= p || res < a) ? res - p : res;
1164 : }
1165 : INLINE ulong
1166 99973669 : Fl_triple(ulong a, ulong p)
1167 : {
1168 99973669 : ulong res = a << 1;
1169 99973669 : if (res >= p || res < a) res -= p;
1170 99973669 : res += a;
1171 99973669 : return (res >= p || res < a)? res - p: res;
1172 : }
1173 : INLINE ulong
1174 20602786 : Fl_halve(ulong a, ulong p)
1175 : {
1176 : ulong ap, ap2;
1177 20602786 : if ((a&1UL)==0) return a>>1;
1178 10392238 : ap = a + p; ap2 = ap>>1;
1179 10392238 : return ap>=a ? ap2: (ap2|HIGHBIT);
1180 : }
1181 :
1182 : INLINE ulong
1183 6561057333 : Fl_add(ulong a, ulong b, ulong p)
1184 : {
1185 6561057333 : ulong res = a + b;
1186 6561057333 : return (res >= p || res < a) ? res - p : res;
1187 : }
1188 : INLINE ulong
1189 741556794 : Fl_neg(ulong x, ulong p) { return x ? p - x: 0; }
1190 :
1191 : INLINE ulong
1192 7717561211 : Fl_sub(ulong a, ulong b, ulong p)
1193 : {
1194 7717561211 : ulong res = a - b;
1195 7717561211 : return (res > a) ? res + p: res;
1196 : }
1197 :
1198 : /* centerlift(u mod p) */
1199 : INLINE long
1200 4490059 : Fl_center(ulong u, ulong p, ulong ps2) { return (long) (u > ps2)? u - p: u; }
1201 :
1202 : INLINE ulong
1203 2552179413 : Fl_mul(ulong a, ulong b, ulong p)
1204 : {
1205 : ulong x;
1206 : LOCAL_HIREMAINDER;
1207 2552179413 : x = mulll(a,b);
1208 2552179413 : if (!hiremainder) return x % p;
1209 431761898 : (void)divll(x,p); return hiremainder;
1210 : }
1211 : INLINE ulong
1212 108100969 : Fl_sqr(ulong a, ulong p)
1213 : {
1214 : ulong x;
1215 : LOCAL_HIREMAINDER;
1216 108100969 : x = mulll(a,a);
1217 108100969 : if (!hiremainder) return x % p;
1218 26959892 : (void)divll(x,p); return hiremainder;
1219 : }
1220 : /* don't assume that p is prime: can't special case a = 0 */
1221 : INLINE ulong
1222 49575838 : Fl_div(ulong a, ulong b, ulong p)
1223 49575838 : { return Fl_mul(a, Fl_inv(b, p), p); }
1224 :
1225 : /*******************************************************************/
1226 : /* */
1227 : /* DEFINED FROM EXISTING ONE EXPLOITING COMMUTATIVITY */
1228 : /* */
1229 : /*******************************************************************/
1230 : INLINE GEN
1231 1113219 : addri(GEN x, GEN y) { return addir(y,x); }
1232 : INLINE GEN
1233 195198495 : addis(GEN x, long s) { return addsi(s,x); }
1234 : INLINE GEN
1235 104505615 : addiu(GEN x, ulong s) { return addui(s,x); }
1236 : INLINE GEN
1237 12387589 : addrs(GEN x, long s) { return addsr(s,x); }
1238 :
1239 : INLINE GEN
1240 133453647 : subiu(GEN x, long y) { GEN z = subui(y, x); togglesign(z); return z; }
1241 : INLINE GEN
1242 171754 : subis(GEN x, long y) { return addsi(-y,x); }
1243 : INLINE GEN
1244 16573238 : subrs(GEN x, long y) { return addsr(-y,x); }
1245 :
1246 : INLINE GEN
1247 470667592 : mulis(GEN x, long s) { return mulsi(s,x); }
1248 : INLINE GEN
1249 379140436 : muliu(GEN x, ulong s) { return mului(s,x); }
1250 : INLINE GEN
1251 2765059 : mulru(GEN x, ulong s) { return mulur(s,x); }
1252 : INLINE GEN
1253 53048205 : mulri(GEN x, GEN s) { return mulir(s,x); }
1254 : INLINE GEN
1255 7181596 : mulrs(GEN x, long s) { return mulsr(s,x); }
1256 :
1257 : /*******************************************************************/
1258 : /* */
1259 : /* VALUATION, EXPONENT, SHIFTS */
1260 : /* */
1261 : /*******************************************************************/
1262 : INLINE long
1263 193226542 : vali(GEN x)
1264 : {
1265 : long i;
1266 : GEN xp;
1267 :
1268 193226542 : if (!signe(x)) return -1;
1269 193141691 : xp=int_LSW(x);
1270 201980970 : for (i=0; !*xp; i++) xp=int_nextW(xp);
1271 193141691 : return vals(*xp) + i * BITS_IN_LONG;
1272 : }
1273 :
1274 : /* assume x > 0 */
1275 : INLINE long
1276 979973901 : expu(ulong x) { return (BITS_IN_LONG-1) - (long)bfffo(x); }
1277 :
1278 : INLINE long
1279 2487430467 : expi(GEN x)
1280 : {
1281 2487430467 : const long lx=lgefint(x);
1282 2487430467 : return lx==2? -(long)HIGHEXPOBIT: bit_accuracy(lx)-(long)bfffo(*int_MSW(x))-1;
1283 : }
1284 :
1285 : INLINE GEN
1286 363693708 : shiftr(GEN x, long n)
1287 : {
1288 363693708 : const long e = evalexpo(expo(x)+n);
1289 363693708 : const GEN y = rcopy(x);
1290 :
1291 363693708 : if (e & ~EXPOBITS) pari_err_OVERFLOW("expo()");
1292 363693708 : y[1] = (y[1]&~EXPOBITS) | e; return y;
1293 : }
1294 : INLINE GEN
1295 153965869 : mpshift(GEN x,long s) { return (typ(x)==t_INT)?shifti(x,s):shiftr(x,s); }
1296 :
1297 : /* FIXME: adapt/use mpn_[lr]shift instead */
1298 : /* z2[imin..imax] := z1[imin..imax].f shifted left sh bits
1299 : * (feeding f from the right). Assume sh > 0 */
1300 : INLINE void
1301 7840711244 : shift_left(GEN z2, GEN z1, long imin, long imax, ulong f, ulong sh)
1302 : {
1303 7840711244 : GEN sb = z1 + imin, se = z1 + imax, te = z2 + imax;
1304 7840711244 : ulong l, m = BITS_IN_LONG - sh, k = f >> m;
1305 50072995940 : while (se > sb) {
1306 42232284696 : l = *se--;
1307 42232284696 : *te-- = (l << sh) | k;
1308 42232284696 : k = l >> m;
1309 : }
1310 7840711244 : *te = (((ulong)*se) << sh) | k;
1311 7840711244 : }
1312 : /* z2[imin..imax] := f.z1[imin..imax-1] shifted right sh bits
1313 : * (feeding f from the left). Assume sh > 0 */
1314 : INLINE void
1315 5941143438 : shift_right(GEN z2, GEN z1, long imin, long imax, ulong f, ulong sh)
1316 : {
1317 5941143438 : GEN sb = z1 + imin, se = z1 + imax, tb = z2 + imin;
1318 5941143438 : ulong k, l = *sb++, m = BITS_IN_LONG - sh;
1319 5941143438 : *tb++ = (l >> sh) | (f << m);
1320 31021147943 : while (sb < se) {
1321 25080004505 : k = l << m;
1322 25080004505 : l = *sb++;
1323 25080004505 : *tb++ = (l >> sh) | k;
1324 : }
1325 5941143438 : }
1326 :
1327 : /* Backward compatibility. Inefficient && unused */
1328 : extern ulong hiremainder;
1329 : INLINE ulong
1330 0 : shiftl(ulong x, ulong y)
1331 0 : { hiremainder = x>>(BITS_IN_LONG-y); return (x<<y); }
1332 :
1333 : INLINE ulong
1334 0 : shiftlr(ulong x, ulong y)
1335 0 : { hiremainder = x<<(BITS_IN_LONG-y); return (x>>y); }
1336 :
1337 : INLINE void
1338 472339438 : shiftr_inplace(GEN z, long d)
1339 : {
1340 472339438 : setexpo(z, expo(z)+d);
1341 472339431 : }
1342 :
1343 : /*******************************************************************/
1344 : /* */
1345 : /* ASSIGNMENT */
1346 : /* */
1347 : /*******************************************************************/
1348 : INLINE void
1349 1002916783 : affii(GEN x, GEN y)
1350 : {
1351 1002916783 : long lx = lgefint(x);
1352 1002916783 : if (lg(y)<lx) pari_err_OVERFLOW("t_INT-->t_INT assignment");
1353 38516185021 : while (--lx) y[lx] = x[lx];
1354 1002916783 : }
1355 : INLINE void
1356 6484730 : affsi(long s, GEN x)
1357 : {
1358 6484730 : if (!s) x[1] = evalsigne(0) | evallgefint(2);
1359 : else
1360 : {
1361 6208260 : if (s > 0) { x[1] = evalsigne( 1) | evallgefint(3); x[2] = s; }
1362 2082143 : else { x[1] = evalsigne(-1) | evallgefint(3); x[2] = -s; }
1363 : }
1364 6484730 : }
1365 : INLINE void
1366 47850644 : affui(ulong u, GEN x)
1367 : {
1368 47850644 : if (!u) x[1] = evalsigne(0) | evallgefint(2);
1369 47811346 : else { x[1] = evalsigne(1) | evallgefint(3); x[2] = u; }
1370 47850644 : }
1371 :
1372 : INLINE void
1373 507929587 : affsr(long x, GEN y)
1374 : {
1375 507929587 : long sh, i, ly = lg(y);
1376 :
1377 507929587 : if (!x)
1378 : {
1379 0 : y[1] = evalexpo(-bit_accuracy(ly));
1380 0 : return;
1381 : }
1382 507929587 : if (x < 0) {
1383 13805 : x = -x; sh = bfffo(x);
1384 13805 : y[1] = evalsigne(-1) | _evalexpo((BITS_IN_LONG-1)-sh);
1385 : }
1386 : else
1387 : {
1388 507915782 : sh = bfffo(x);
1389 507915782 : y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
1390 : }
1391 5181487132 : y[2] = ((ulong)x)<<sh; for (i=3; i<ly; i++) y[i]=0;
1392 : }
1393 :
1394 : INLINE void
1395 13344743 : affur(ulong x, GEN y)
1396 : {
1397 13344743 : long sh, i, ly = lg(y);
1398 :
1399 13344743 : if (!x)
1400 : {
1401 1368994 : y[1] = evalexpo(-bit_accuracy(ly));
1402 1368994 : return;
1403 : }
1404 11975749 : sh = bfffo(x);
1405 11975749 : y[1] = evalsigne(1) | _evalexpo((BITS_IN_LONG-1)-sh);
1406 43194137 : y[2] = x<<sh; for (i=3; i<ly; i++) y[i] = 0;
1407 : }
1408 :
1409 : INLINE long
1410 24078741 : hammingu(ulong x)
1411 : {
1412 : #ifdef LONG_IS_64BIT
1413 20521539 : x = (x & 0x5555555555555555UL) + ((x >> 1) & 0x5555555555555555UL);
1414 20521539 : x = (x & 0x3333333333333333UL) + ((x >> 2) & 0x3333333333333333UL);
1415 20521539 : x = (x & 0x0F0F0F0F0F0F0F0FUL) + ((x >> 4) & 0x0F0F0F0F0F0F0F0FUL);
1416 20521539 : return (x * 0x0101010101010101UL) >> 56;
1417 : #else
1418 3557202 : x = (x & 0x55555555UL) + ((x >> 1) & 0x55555555UL);
1419 3557202 : x = (x & 0x33333333UL) + ((x >> 2) & 0x33333333UL);
1420 3557202 : x = (x & 0x0F0F0F0FUL) + ((x >> 4) & 0x0F0F0F0FUL);
1421 3557202 : return (x * 0x01010101UL) >> 24;
1422 : #endif
1423 : }
1424 :
1425 : INLINE ulong
1426 3313 : thuemorseu(ulong c)
1427 : {
1428 : #ifdef LONG_IS_64BIT
1429 2838 : c ^= c >> 32;
1430 : #endif
1431 3313 : c ^= c >> 16;
1432 3313 : c ^= c >> 8;
1433 3313 : c ^= c >> 4;
1434 3313 : c ^= c >> 2;
1435 3313 : c ^= c >> 1;
1436 3313 : return c & 1;
1437 : }
|