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 4164078644 : icopy_sign(GEN x, long sx)
18 : {
19 4164078644 : GEN y=icopy(x);
20 4164078644 : setsigne(y,sx);
21 4164078644 : return y;
22 : }
23 :
24 : GEN
25 327910191 : addsi_sign(long x, GEN y, long sy)
26 : {
27 : long sx,ly;
28 : GEN z;
29 :
30 327910191 : if (!x) return icopy_sign(y, sy);
31 311443387 : if (!sy) return stoi(x);
32 181158400 : if (x<0) { sx=-1; x=-x; } else sx=1;
33 181158400 : if (sx==sy)
34 : {
35 51117228 : z = adduispec(x,y+2, lgefint(y)-2);
36 51117228 : setsigne(z,sy); return z;
37 : }
38 130041172 : ly=lgefint(y);
39 130041172 : if (ly==3)
40 : {
41 18138361 : const long d = (long)(uel(y,2) - (ulong)x);
42 18138361 : if (!d) return gen_0;
43 17116136 : z=cgeti(3);
44 17116136 : if (y[2] < 0 || d > 0) {
45 14640910 : z[1] = evalsigne(sy) | evallgefint(3);
46 14640910 : z[2] = d;
47 : }
48 : else {
49 2475226 : z[1] = evalsigne(-sy) | evallgefint(3);
50 2475226 : z[2] =-d;
51 : }
52 17116136 : return z;
53 : }
54 111902811 : z = subiuspec(y+2,x, ly-2);
55 111902811 : setsigne(z,sy); return z;
56 : }
57 : GEN
58 243039142 : addui_sign(ulong x, GEN y, long sy)
59 : {
60 : long ly;
61 : GEN z;
62 :
63 243039142 : if (!x) return icopy_sign(y, sy);
64 240085781 : if (!sy) return utoipos(x);
65 231437094 : if (sy == 1) return adduispec(x,y+2, lgefint(y)-2);
66 116233815 : ly=lgefint(y);
67 116233815 : if (ly==3)
68 : {
69 29274396 : const ulong t = y[2];
70 29274396 : if (x == t) return gen_0;
71 26841225 : z=cgeti(3);
72 26841225 : if (x < t) {
73 25109574 : z[1] = evalsigne(-1) | evallgefint(3);
74 25109574 : z[2] = t - x;
75 : }
76 : else {
77 1731651 : z[1] = evalsigne(1) | evallgefint(3);
78 1731651 : z[2] = x - t;
79 : }
80 26841225 : return z;
81 : }
82 86959419 : z = subiuspec(y+2,x, ly-2);
83 86959419 : setsigne(z,-1); return z;
84 : }
85 :
86 : /* return gen_0 when the sign is 0 */
87 : GEN
88 19767202057 : addii_sign(GEN x, long sx, GEN y, long sy)
89 : {
90 : long lx,ly;
91 : GEN z;
92 :
93 19767202057 : if (!sx) return sy? icopy_sign(y, sy): gen_0;
94 15609376496 : if (!sy) return icopy_sign(x, sx);
95 12713743621 : lx = lgefint(x);
96 12713743621 : ly = lgefint(y);
97 12713743621 : if (sx==sy)
98 6706721788 : z = addiispec(x+2,y+2,lx-2,ly-2);
99 : else
100 : { /* sx != sy */
101 6007021833 : long i = cmpiispec(x+2,y+2,lx-2,ly-2);
102 6007021833 : if (!i) return gen_0;
103 : /* we must ensure |x| > |y| for subiispec */
104 5704405744 : if (i < 0) {
105 2321530071 : sx = sy;
106 2321530071 : z = subiispec(y+2,x+2,ly-2,lx-2);
107 : }
108 : else
109 3382875673 : z = subiispec(x+2,y+2,lx-2,ly-2);
110 : }
111 12411127529 : setsigne(z,sx); return z;
112 : }
113 :
114 : INLINE GEN
115 401223540 : rcopy_sign(GEN x, long sx) { GEN y = rcopy(x); setsigne(y,sx); return y; }
116 :
117 : GEN
118 494935298 : addir_sign(GEN x, long sx, GEN y, long sy)
119 : {
120 : long e, ly;
121 : GEN z;
122 :
123 494935298 : if (!sx) return rcopy_sign(y, sy);
124 148820257 : e = expo(y) - expi(x);
125 148820257 : if (!sy)
126 : {
127 704235 : if (e >= 0) return rcopy_sign(y, sy);
128 704187 : z = itor(x, nbits2prec(-e));
129 704187 : setsigne(z, sx); return z;
130 : }
131 :
132 148116022 : ly = lg(y);
133 148116022 : if (e > 0)
134 : {
135 43232938 : ly -= divsBIL(e);
136 43232938 : if (ly < 3) return rcopy_sign(y, sy);
137 : }
138 : else
139 104883084 : ly += nbits2nlong(-e);
140 147616054 : z = (GEN)avma;
141 147616054 : y = addrr_sign(itor_lg(x, ly), sx, y, sy);
142 1463516592 : ly = lg(y); while (ly--) *--z = y[ly];
143 147616047 : set_avma((pari_sp)z); return z;
144 : }
145 :
146 : static GEN
147 508348753 : addsr_sign(long x, GEN y, long sy)
148 : {
149 : long e, ly, sx;
150 : GEN z;
151 :
152 508348753 : if (!x) return rcopy_sign(y, sy);
153 508335530 : if (x < 0) { sx = -1; x = -x; } else sx = 1;
154 508335530 : e = expo(y) - expu(x);
155 508335530 : if (!sy)
156 : {
157 504894 : if (e >= 0) return rcopy_sign(y, sy);
158 504866 : if (sx == -1) x = -x;
159 504866 : return stor(x, nbits2prec(-e));
160 : }
161 :
162 507830636 : ly = lg(y);
163 507830636 : if (e > 0)
164 : {
165 5070792 : ly -= divsBIL(e);
166 5070792 : if (ly < 3) return rcopy_sign(y, sy);
167 : }
168 : else
169 502759844 : ly += nbits2nlong(-e);
170 506914341 : z = (GEN)avma;
171 506914341 : y = addrr_sign(stor_lg(x, ly), sx, y, sy);
172 6580589006 : ly = lg(y); while (ly--) *--z = y[ly];
173 506914341 : set_avma((pari_sp)z); return z;
174 : }
175 :
176 : GEN
177 499206749 : addsr(long x, GEN y) { return addsr_sign(x, y, signe(y)); }
178 :
179 : GEN
180 9142004 : subsr(long x, GEN y) { return addsr_sign(x, y, -signe(y)); }
181 :
182 : GEN
183 5331963469 : addrr_sign(GEN x, long sx, GEN y, long sy)
184 : {
185 5331963469 : long lx, ex = expo(x);
186 5331963469 : long ly, ey = expo(y), e = ey - ex;
187 : long i, j, lz, ez, m;
188 : int extend, f2;
189 : GEN z;
190 : LOCAL_OVERFLOW;
191 :
192 5331963469 : if (!sy)
193 : {
194 330693529 : if (!sx)
195 : {
196 107363660 : if (e > 0) ex = ey;
197 107363660 : return real_0_bit(ex);
198 : }
199 223329869 : if (e >= 0) return real_0_bit(ey);
200 222156608 : lz = nbits2lg(-e);
201 222156608 : lx = lg(x); if (lz > lx) lz = lx;
202 1678642705 : z = cgetg(lz, t_REAL); while(--lz) z[lz] = x[lz];
203 222156608 : setsigne(z,sx); return z;
204 : }
205 5001269940 : if (!sx)
206 : {
207 28791825 : if (e <= 0) return real_0_bit(ex);
208 27187588 : lz = nbits2lg(e);
209 27187588 : ly = lg(y); if (lz > ly) lz = ly;
210 138167560 : z = cgetg(lz, t_REAL); while (--lz) z[lz] = y[lz];
211 27187588 : setsigne(z,sy); return z;
212 : }
213 :
214 4972478115 : if (e < 0) { swap(x,y); lswap(sx,sy); ey=ex; e=-e; }
215 : /* now ey >= ex */
216 4972478115 : lx = lg(x);
217 4972478115 : ly = lg(y);
218 : /* If exponents differ, need to shift one argument, here x. If
219 : * extend = 1: extension of x,z by m < BIL bits (round to 1 word) */
220 : /* in this case, lz = lx + d + 1, otherwise lx + d */
221 4972478115 : extend = 0;
222 4972478115 : if (e)
223 : {
224 4302481548 : long d = dvmdsBIL(e, &m), l = ly-d;
225 4302481548 : if (l <= 2) return rcopy_sign(y, sy);
226 4248802611 : if (l > lx) { lz = lx + d + 1; extend = 1; }
227 3416934347 : else { lz = ly; lx = l; }
228 4248802611 : if (m)
229 : { /* shift x right m bits */
230 4239955597 : const pari_sp av = avma;
231 4239955597 : const ulong sh = BITS_IN_LONG-m;
232 4239955597 : GEN p1 = x; x = new_chunk(lx + lz + 1);
233 4239955590 : shift_right(x,p1,2,lx, 0,m);
234 4239955590 : if (extend) uel(x,lx) = uel(p1,lx-1) << sh;
235 4239955590 : set_avma(av); /* HACK: cgetg(lz, t_REAL) will not overwrite x */
236 : }
237 : }
238 : else
239 : { /* d = 0 */
240 669996567 : m = 0;
241 669996567 : if (lx > ly) lx = ly;
242 669996567 : lz = lx;
243 : }
244 :
245 4918799171 : if (sx == sy)
246 : { /* addition */
247 2576974366 : i = lz-1;
248 2576974366 : j = lx-1;
249 2576974366 : if (extend) {
250 509912820 : ulong garde = addll(x[lx], y[i]);
251 509912820 : if (m < 4) /* don't extend for few correct bits */
252 57563673 : z = cgetg(--lz, t_REAL);
253 : else
254 : {
255 452349147 : z = cgetg(lz, t_REAL);
256 452349147 : z[i] = garde;
257 : }
258 : }
259 : else
260 : {
261 2067061546 : z = cgetg(lz, t_REAL);
262 2067061546 : z[i] = addll(x[j], y[i]); j--;
263 : }
264 2576974366 : i--;
265 13993355088 : for (; j>=2; i--,j--) z[i] = addllx(x[j],y[i]);
266 2576974366 : if (overflow)
267 : {
268 606498178 : z[1] = 1; /* stops since z[1] != 0 */
269 607766529 : for (;;) { z[i] = uel(y,i)+1; if (z[i--]) break; }
270 606498178 : if (i <= 0)
271 : {
272 602419498 : shift_right(z,z, 2,lz, 1,1);
273 602419498 : z[1] = evalsigne(sx) | evalexpo(ey+1); return z;
274 : }
275 : }
276 2495936684 : for (; i>=2; i--) z[i] = y[i];
277 1974554868 : z[1] = evalsigne(sx) | evalexpo(ey); return z;
278 : }
279 :
280 : /* subtraction */
281 2341824805 : if (e) f2 = 1;
282 : else
283 : {
284 612836534 : i = 2; while (i < lx && x[i] == y[i]) i++;
285 374162536 : if (i==lx) return real_0_bit(ey+1 - lg2prec(lx));
286 348989969 : f2 = (uel(y,i) > uel(x,i));
287 : }
288 : /* result is nonzero. f2 = (y > x) */
289 2316652238 : i = lz-1; z = cgetg(lz, t_REAL);
290 2316652238 : if (f2)
291 : {
292 2159660497 : j = lx-1;
293 2159660497 : if (extend) z[i] = subll(y[i], x[lx]);
294 1837705060 : else z[i] = subll(y[i], x[j--]);
295 13282102002 : for (i--; j>=2; i--) z[i] = subllx(y[i], x[j--]);
296 2159660497 : if (overflow) /* stops since y[1] != 0 */
297 63630482 : for (;;) { z[i] = uel(y,i)-1; if (y[i--]) break; }
298 2536172955 : for (; i>=2; i--) z[i] = y[i];
299 2159660497 : sx = sy;
300 : }
301 : else
302 : {
303 156991741 : if (extend) z[i] = subll(x[lx], y[i]);
304 156991741 : else z[i] = subll(x[i], y[i]);
305 824551698 : for (i--; i>=2; i--) z[i] = subllx(x[i], y[i]);
306 : }
307 :
308 2472425591 : x = z+2; i = 0; while (!x[i]) i++;
309 2316652238 : lz -= i; z += i;
310 2316652238 : j = bfffo(z[2]); /* need to shift left by j bits to normalize mantissa */
311 2316652238 : ez = ey - (j | (i * BITS_IN_LONG));
312 2316652238 : if (extend)
313 : { /* z was extended by d+1 words [should be e bits = d words + m bits] */
314 : /* not worth keeping extra word if less than 5 significant bits in there */
315 321955437 : if (m - j < 5 && lz > 3)
316 172029632 : { /* shorten z */
317 172029632 : ulong last = (ulong)z[--lz]; /* cancelled word */
318 :
319 : /* if we need to shift anyway, shorten from left
320 : * If not, shorten from right, neutralizing last word of z */
321 172029632 : if (j == 0)
322 : /* stackdummy((pari_sp)(z + lz+1), (pari_sp)(z + lz)); */
323 14590018 : z[lz] = evaltyp(t_VECSMALL) | _evallg(1);
324 : else
325 : {
326 157439614 : GEN t = z;
327 157439614 : z++; shift_left(z,t,2,lz-1, last,j);
328 : }
329 172029632 : if ((last<<j) & HIGHBIT)
330 : { /* round up */
331 62392353 : i = lz-1;
332 62854590 : while (++((ulong*)z)[i] == 0 && i > 1) i--;
333 62392353 : if (i == 1) { ez++; z[2] = (long)HIGHBIT; }
334 : }
335 : }
336 149925805 : else if (j) shift_left(z,z,2,lz-1, 0,j);
337 : }
338 1994696801 : else if (j) shift_left(z,z,2,lz-1, 0,j);
339 2316652238 : z[1] = evalsigne(sx) | evalexpo(ez);
340 2316652238 : z[0] = evaltyp(t_REAL) | evallg(lz);
341 2316652238 : set_avma((pari_sp)z); return z;
342 : }
|