Line data Source code
1 : #line 2 "../src/kernel/none/add.c"
2 : /* Copyright (C) 2002-2003 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 : INLINE GEN
17 3980595547 : icopy_sign(GEN x, long sx)
18 : {
19 3980595547 : GEN y=icopy(x);
20 3988824026 : setsigne(y,sx);
21 3988824026 : return y;
22 : }
23 :
24 : GEN
25 313758550 : addsi_sign(long x, GEN y, long sy)
26 : {
27 : long sx,ly;
28 : GEN z;
29 :
30 313758550 : if (!x) return icopy_sign(y, sy);
31 297382884 : if (!sy) return stoi(x);
32 168983915 : if (x<0) { sx=-1; x=-x; } else sx=1;
33 168983915 : if (sx==sy)
34 : {
35 47181012 : z = adduispec(x,y+2, lgefint(y)-2);
36 47175049 : setsigne(z,sy); return z;
37 : }
38 121802903 : ly=lgefint(y);
39 121802903 : if (ly==3)
40 : {
41 18108982 : const long d = (long)(uel(y,2) - (ulong)x);
42 18108982 : if (!d) return gen_0;
43 17089104 : z=cgeti(3);
44 17086701 : if (y[2] < 0 || d > 0) {
45 14610227 : z[1] = evalsigne(sy) | evallgefint(3);
46 14610227 : z[2] = d;
47 : }
48 : else {
49 2476474 : z[1] = evalsigne(-sy) | evallgefint(3);
50 2476474 : z[2] =-d;
51 : }
52 17086701 : return z;
53 : }
54 103693921 : z = subiuspec(y+2,x, ly-2);
55 103805453 : setsigne(z,sy); return z;
56 : }
57 : GEN
58 235179716 : addui_sign(ulong x, GEN y, long sy)
59 : {
60 : long ly;
61 : GEN z;
62 :
63 235179716 : if (!x) return icopy_sign(y, sy);
64 232234700 : if (!sy) return utoipos(x);
65 223606013 : if (sy == 1) return adduispec(x,y+2, lgefint(y)-2);
66 115666201 : ly=lgefint(y);
67 115666201 : if (ly==3)
68 : {
69 28806324 : const ulong t = y[2];
70 28806324 : if (x == t) return gen_0;
71 26684096 : z=cgeti(3);
72 26683827 : if (x < t) {
73 24952212 : z[1] = evalsigne(-1) | evallgefint(3);
74 24952212 : z[2] = t - x;
75 : }
76 : else {
77 1731615 : z[1] = evalsigne(1) | evallgefint(3);
78 1731615 : z[2] = x - t;
79 : }
80 26683827 : return z;
81 : }
82 86859877 : z = subiuspec(y+2,x, ly-2);
83 86862472 : setsigne(z,-1); return z;
84 : }
85 :
86 : /* return gen_0 when the sign is 0 */
87 : GEN
88 18676492511 : addii_sign(GEN x, long sx, GEN y, long sy)
89 : {
90 : long lx,ly;
91 : GEN z;
92 :
93 18676492511 : if (!sx) return sy? icopy_sign(y, sy): gen_0;
94 14813924876 : if (!sy) return icopy_sign(x, sx);
95 12066888780 : lx = lgefint(x);
96 12066888780 : ly = lgefint(y);
97 12066888780 : if (sx==sy)
98 6485776645 : z = addiispec(x+2,y+2,lx-2,ly-2);
99 : else
100 : { /* sx != sy */
101 5581112135 : long i = cmpiispec(x+2,y+2,lx-2,ly-2);
102 5659357594 : if (!i) return gen_0;
103 : /* we must ensure |x| > |y| for subiispec */
104 5364130393 : if (i < 0) {
105 2209126948 : sx = sy;
106 2209126948 : z = subiispec(y+2,x+2,ly-2,lx-2);
107 : }
108 : else
109 3155003445 : z = subiispec(x+2,y+2,lx-2,ly-2);
110 : }
111 11824664687 : setsigne(z,sx); return z;
112 : }
113 :
114 : INLINE GEN
115 392739142 : rcopy_sign(GEN x, long sx) { GEN y = rcopy(x); setsigne(y,sx); return y; }
116 :
117 : GEN
118 488954717 : addir_sign(GEN x, long sx, GEN y, long sy)
119 : {
120 : long e, l, ly;
121 : GEN z;
122 :
123 488954717 : if (!sx) return rcopy_sign(y, sy);
124 147390052 : e = expo(y) - expi(x);
125 147438305 : if (!sy)
126 : {
127 700235 : if (e >= 0) return rcopy_sign(y, sy);
128 700187 : z = itor(x, nbits2prec(-e));
129 700163 : setsigne(z, sx); return z;
130 : }
131 :
132 146738070 : ly = lg(y);
133 146738070 : if (e > 0)
134 : {
135 42005985 : l = lg2prec(ly - divsBIL(e));
136 42005086 : if (l < LOWDEFAULTPREC) return rcopy_sign(y, sy);
137 : }
138 104732085 : else l = lg2prec(ly) + nbits2extraprec(-e);
139 146251800 : z = (GEN)avma;
140 146251800 : y = addrr_sign(itor(x,l), sx, y, sy);
141 1441469300 : ly = lg(y); while (ly--) *--z = y[ly];
142 146216053 : set_avma((pari_sp)z); return z;
143 : }
144 :
145 : static GEN
146 495394901 : addsr_sign(long x, GEN y, long sy)
147 : {
148 : long e, l, ly, sx;
149 : GEN z;
150 :
151 495394901 : if (!x) return rcopy_sign(y, sy);
152 495382154 : if (x < 0) { sx = -1; x = -x; } else sx = 1;
153 495382154 : e = expo(y) - expu(x);
154 495321525 : if (!sy)
155 : {
156 503769 : if (e >= 0) return rcopy_sign(y, sy);
157 503741 : if (sx == -1) x = -x;
158 503741 : return stor(x, nbits2prec(-e));
159 : }
160 :
161 494817756 : ly = lg(y);
162 494817756 : if (e > 0)
163 : {
164 5042753 : l = lg2prec(ly - divsBIL(e));
165 5042730 : if (l < LOWDEFAULTPREC) return rcopy_sign(y, sy);
166 : }
167 489775003 : else l = lg2prec(ly) + nbits2extraprec(-e);
168 493733995 : z = (GEN)avma;
169 493733995 : y = addrr_sign(stor(x,l), sx, y, sy);
170 6494270238 : ly = lg(y); while (ly--) *--z = y[ly];
171 493845614 : set_avma((pari_sp)z); return z;
172 : }
173 :
174 : GEN
175 485927669 : addsr(long x, GEN y) { return addsr_sign(x, y, signe(y)); }
176 :
177 : GEN
178 9457810 : subsr(long x, GEN y) { return addsr_sign(x, y, -signe(y)); }
179 :
180 : GEN
181 5081552463 : addrr_sign(GEN x, long sx, GEN y, long sy)
182 : {
183 5081552463 : long lx, ex = expo(x);
184 5081552463 : long ly, ey = expo(y), e = ey - ex;
185 : long i, j, lz, ez, m;
186 : int extend, f2;
187 : GEN z;
188 : LOCAL_OVERFLOW;
189 :
190 5081552463 : if (!sy)
191 : {
192 318988641 : if (!sx)
193 : {
194 102000945 : if (e > 0) ex = ey;
195 102000945 : return real_0_bit(ex);
196 : }
197 216987696 : if (e >= 0) return real_0_bit(ey);
198 215866755 : lz = nbits2lg(-e);
199 216021729 : lx = lg(x); if (lz > lx) lz = lx;
200 1673745095 : z = cgetg(lz, t_REAL); while(--lz) z[lz] = x[lz];
201 216032305 : setsigne(z,sx); return z;
202 : }
203 4762563822 : if (!sx)
204 : {
205 28349617 : if (e <= 0) return real_0_bit(ex);
206 26810181 : lz = nbits2lg(e);
207 26810014 : ly = lg(y); if (lz > ly) lz = ly;
208 136706935 : z = cgetg(lz, t_REAL); while (--lz) z[lz] = y[lz];
209 26807881 : setsigne(z,sy); return z;
210 : }
211 :
212 4734214205 : if (e < 0) { swap(x,y); lswap(sx,sy); ey=ex; e=-e; }
213 : /* now ey >= ex */
214 4734214205 : lx = lg(x);
215 4734214205 : ly = lg(y);
216 : /* If exponents differ, need to shift one argument, here x. If
217 : * extend = 1: extension of x,z by m < BIL bits (round to 1 word) */
218 : /* in this case, lz = lx + d + 1, otherwise lx + d */
219 4734214205 : extend = 0;
220 4734214205 : if (e)
221 : {
222 4090797211 : long d = dvmdsBIL(e, &m), l = ly-d;
223 4088366306 : if (l <= 2) return rcopy_sign(y, sy);
224 4043766971 : if (l > lx) { lz = lx + d + 1; extend = 1; }
225 3231783384 : else { lz = ly; lx = l; }
226 4043766971 : if (m)
227 : { /* shift x right m bits */
228 4030750815 : const pari_sp av = avma;
229 4030750815 : const ulong sh = BITS_IN_LONG-m;
230 4030750815 : GEN p1 = x; x = new_chunk(lx + lz + 1);
231 4026176933 : shift_right(x,p1,2,lx, 0,m);
232 4028501098 : if (extend) uel(x,lx) = uel(p1,lx-1) << sh;
233 4028501098 : set_avma(av); /* HACK: cgetg(lz, t_REAL) will not overwrite x */
234 : }
235 : }
236 : else
237 : { /* d = 0 */
238 643416994 : m = 0;
239 643416994 : if (lx > ly) lx = ly;
240 643416994 : lz = lx;
241 : }
242 :
243 4691420675 : if (sx == sy)
244 : { /* addition */
245 2433525115 : i = lz-1;
246 2433525115 : j = lx-1;
247 2433525115 : if (extend) {
248 488094616 : ulong garde = addll(x[lx], y[i]);
249 488094616 : if (m < 4) /* don't extend for few correct bits */
250 55923329 : z = cgetg(--lz, t_REAL);
251 : else
252 : {
253 432171287 : z = cgetg(lz, t_REAL);
254 432067850 : z[i] = garde;
255 : }
256 : }
257 : else
258 : {
259 1945430499 : z = cgetg(lz, t_REAL);
260 1945260927 : z[i] = addll(x[j], y[i]); j--;
261 : }
262 2429620656 : i--;
263 13478226755 : for (; j>=2; i--,j--) z[i] = addllx(x[j],y[i]);
264 2429620656 : if (overflow)
265 : {
266 581558422 : z[1] = 1; /* stops since z[1] != 0 */
267 582821100 : for (;;) { z[i] = uel(y,i)+1; if (z[i--]) break; }
268 581558422 : if (i <= 0)
269 : {
270 577921825 : shift_right(z,z, 2,lz, 1,1);
271 578208657 : z[1] = evalsigne(sx) | evalexpo(ey+1); return z;
272 : }
273 : }
274 2353802378 : for (; i>=2; i--) z[i] = y[i];
275 1851698831 : z[1] = evalsigne(sx) | evalexpo(ey); return z;
276 : }
277 :
278 : /* subtraction */
279 2257895560 : if (e) f2 = 1;
280 : else
281 : {
282 605569511 : i = 2; while (i < lx && x[i] == y[i]) i++;
283 365024884 : if (i==lx) return real_0_bit(ey+1 - lg2prec(lx));
284 340095102 : f2 = (uel(y,i) > uel(x,i));
285 : }
286 : /* result is nonzero. f2 = (y > x) */
287 2232965778 : i = lz-1; z = cgetg(lz, t_REAL);
288 2228360737 : if (f2)
289 : {
290 2080587377 : j = lx-1;
291 2080587377 : if (extend) z[i] = subll(y[i], x[lx]);
292 1757239443 : else z[i] = subll(y[i], x[j--]);
293 13017501150 : for (i--; j>=2; i--) z[i] = subllx(y[i], x[j--]);
294 2080587377 : if (overflow) /* stops since y[1] != 0 */
295 68420916 : for (;;) { z[i] = uel(y,i)-1; if (y[i--]) break; }
296 2441704282 : for (; i>=2; i--) z[i] = y[i];
297 2080587377 : sx = sy;
298 : }
299 : else
300 : {
301 147773360 : if (extend) z[i] = subll(x[lx], y[i]);
302 147773360 : else z[i] = subll(x[i], y[i]);
303 800285508 : for (i--; i>=2; i--) z[i] = subllx(x[i], y[i]);
304 : }
305 :
306 2388345870 : x = z+2; i = 0; while (!x[i]) i++;
307 2228360737 : lz -= i; z += i;
308 2228360737 : j = bfffo(z[2]); /* need to shift left by j bits to normalize mantissa */
309 2228360737 : ez = ey - (j | (i * BITS_IN_LONG));
310 2228360737 : if (extend)
311 : { /* z was extended by d+1 words [should be e bits = d words + m bits] */
312 : /* not worth keeping extra word if less than 5 significant bits in there */
313 323740801 : if (m - j < 5 && lz > 3)
314 171694574 : { /* shorten z */
315 171688564 : ulong last = (ulong)z[--lz]; /* cancelled word */
316 :
317 : /* if we need to shift anyway, shorten from left
318 : * If not, shorten from right, neutralizing last word of z */
319 171688564 : if (j == 0)
320 : /* stackdummy((pari_sp)(z + lz+1), (pari_sp)(z + lz)); */
321 14103335 : z[lz] = evaltyp(t_VECSMALL) | _evallg(1);
322 : else
323 : {
324 157585229 : GEN t = z;
325 157585229 : z++; shift_left(z,t,2,lz-1, last,j);
326 : }
327 171694574 : if ((last<<j) & HIGHBIT)
328 : { /* round up */
329 62363140 : i = lz-1;
330 62824040 : while (++((ulong*)z)[i] == 0 && i > 1) i--;
331 62363140 : if (i == 1) { ez++; z[2] = (long)HIGHBIT; }
332 : }
333 : }
334 152052237 : else if (j) shift_left(z,z,2,lz-1, 0,j);
335 : }
336 1904619936 : else if (j) shift_left(z,z,2,lz-1, 0,j);
337 2229527632 : z[1] = evalsigne(sx) | evalexpo(ez);
338 2236569645 : z[0] = evaltyp(t_REAL) | evallg(lz);
339 2235329749 : set_avma((pari_sp)z); return z;
340 : }
|