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 4163989174 : icopy_sign(GEN x, long sx)
18 : {
19 4163989174 : GEN y=icopy(x);
20 4163989174 : setsigne(y,sx);
21 4163989174 : return y;
22 : }
23 :
24 : GEN
25 327905148 : addsi_sign(long x, GEN y, long sy)
26 : {
27 : long sx,ly;
28 : GEN z;
29 :
30 327905148 : if (!x) return icopy_sign(y, sy);
31 311438958 : if (!sy) return stoi(x);
32 181155350 : if (x<0) { sx=-1; x=-x; } else sx=1;
33 181155350 : if (sx==sy)
34 : {
35 51114861 : z = adduispec(x,y+2, lgefint(y)-2);
36 51114861 : setsigne(z,sy); return z;
37 : }
38 130040489 : ly=lgefint(y);
39 130040489 : if (ly==3)
40 : {
41 18137678 : const long d = (long)(uel(y,2) - (ulong)x);
42 18137678 : if (!d) return gen_0;
43 17115475 : z=cgeti(3);
44 17115475 : if (y[2] < 0 || d > 0) {
45 14640288 : z[1] = evalsigne(sy) | evallgefint(3);
46 14640288 : z[2] = d;
47 : }
48 : else {
49 2475187 : z[1] = evalsigne(-sy) | evallgefint(3);
50 2475187 : z[2] =-d;
51 : }
52 17115475 : return z;
53 : }
54 111902811 : z = subiuspec(y+2,x, ly-2);
55 111902811 : setsigne(z,sy); return z;
56 : }
57 : GEN
58 246341636 : addui_sign(ulong x, GEN y, long sy)
59 : {
60 : long ly;
61 : GEN z;
62 :
63 246341636 : if (!x) return icopy_sign(y, sy);
64 243388275 : if (!sy) return utoipos(x);
65 234739892 : if (sy == 1) return adduispec(x,y+2, lgefint(y)-2);
66 116235327 : ly=lgefint(y);
67 116235327 : if (ly==3)
68 : {
69 29274373 : const ulong t = y[2];
70 29274373 : if (x == t) return gen_0;
71 26841231 : z=cgeti(3);
72 26841231 : if (x < t) {
73 25109580 : z[1] = evalsigne(-1) | evallgefint(3);
74 25109580 : z[2] = t - x;
75 : }
76 : else {
77 1731651 : z[1] = evalsigne(1) | evallgefint(3);
78 1731651 : z[2] = x - t;
79 : }
80 26841231 : return z;
81 : }
82 86960954 : z = subiuspec(y+2,x, ly-2);
83 86960954 : setsigne(z,-1); return z;
84 : }
85 :
86 : /* return gen_0 when the sign is 0 */
87 : GEN
88 19764923082 : addii_sign(GEN x, long sx, GEN y, long sy)
89 : {
90 : long lx,ly;
91 : GEN z;
92 :
93 19764923082 : if (!sx) return sy? icopy_sign(y, sy): gen_0;
94 15607258751 : if (!sy) return icopy_sign(x, sx);
95 12711673015 : lx = lgefint(x);
96 12711673015 : ly = lgefint(y);
97 12711673015 : if (sx==sy)
98 6706062757 : z = addiispec(x+2,y+2,lx-2,ly-2);
99 : else
100 : { /* sx != sy */
101 6005610258 : long i = cmpiispec(x+2,y+2,lx-2,ly-2);
102 6005610258 : if (!i) return gen_0;
103 : /* we must ensure |x| > |y| for subiispec */
104 5703003689 : if (i < 0) {
105 2320828311 : sx = sy;
106 2320828311 : z = subiispec(y+2,x+2,ly-2,lx-2);
107 : }
108 : else
109 3382175378 : z = subiispec(x+2,y+2,lx-2,ly-2);
110 : }
111 12409066438 : setsigne(z,sx); return z;
112 : }
113 :
114 : INLINE GEN
115 401196786 : rcopy_sign(GEN x, long sx) { GEN y = rcopy(x); setsigne(y,sx); return y; }
116 :
117 : GEN
118 494909849 : addir_sign(GEN x, long sx, GEN y, long sy)
119 : {
120 : long e, ly;
121 : GEN z;
122 :
123 494909849 : if (!sx) return rcopy_sign(y, sy);
124 148812818 : e = expo(y) - expi(x);
125 148812818 : if (!sy)
126 : {
127 704411 : if (e >= 0) return rcopy_sign(y, sy);
128 704363 : z = itor(x, nbits2prec(-e));
129 704363 : setsigne(z, sx); return z;
130 : }
131 :
132 148108407 : ly = lg(y);
133 148108407 : if (e > 0)
134 : {
135 43232658 : ly -= divsBIL(e);
136 43232658 : if (ly < 3) return rcopy_sign(y, sy);
137 : }
138 : else
139 104875749 : ly += nbits2nlong(-e);
140 147608430 : z = (GEN)avma;
141 147608430 : y = addrr_sign(itor_lg(x, ly), sx, y, sy);
142 1463469101 : ly = lg(y); while (ly--) *--z = y[ly];
143 147608423 : set_avma((pari_sp)z); return z;
144 : }
145 :
146 : static GEN
147 508346781 : addsr_sign(long x, GEN y, long sy)
148 : {
149 : long e, ly, sx;
150 : GEN z;
151 :
152 508346781 : if (!x) return rcopy_sign(y, sy);
153 508333558 : if (x < 0) { sx = -1; x = -x; } else sx = 1;
154 508333558 : e = expo(y) - expu(x);
155 508333558 : if (!sy)
156 : {
157 504895 : if (e >= 0) return rcopy_sign(y, sy);
158 504867 : if (sx == -1) x = -x;
159 504867 : return stor(x, nbits2prec(-e));
160 : }
161 :
162 507828663 : ly = lg(y);
163 507828663 : if (e > 0)
164 : {
165 5070673 : ly -= divsBIL(e);
166 5070673 : if (ly < 3) return rcopy_sign(y, sy);
167 : }
168 : else
169 502757990 : ly += nbits2nlong(-e);
170 506912377 : z = (GEN)avma;
171 506912377 : y = addrr_sign(stor_lg(x, ly), sx, y, sy);
172 6580567889 : ly = lg(y); while (ly--) *--z = y[ly];
173 506912377 : set_avma((pari_sp)z); return z;
174 : }
175 :
176 : GEN
177 499204938 : addsr(long x, GEN y) { return addsr_sign(x, y, signe(y)); }
178 :
179 : GEN
180 9141843 : subsr(long x, GEN y) { return addsr_sign(x, y, -signe(y)); }
181 :
182 : GEN
183 5333553489 : addrr_sign(GEN x, long sx, GEN y, long sy)
184 : {
185 5333553489 : long lx, ex = expo(x);
186 5333553489 : 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 5333553489 : if (!sy)
193 : {
194 332481697 : if (!sx)
195 : {
196 107551081 : if (e > 0) ex = ey;
197 107551081 : return real_0_bit(ex);
198 : }
199 224930616 : if (e >= 0) return real_0_bit(ey);
200 223757657 : lz = nbits2lg(-e);
201 223757657 : lx = lg(x); if (lz > lx) lz = lx;
202 1696494248 : z = cgetg(lz, t_REAL); while(--lz) z[lz] = x[lz];
203 223757657 : setsigne(z,sx); return z;
204 : }
205 5001071792 : if (!sx)
206 : {
207 28803137 : if (e <= 0) return real_0_bit(ex);
208 27199299 : lz = nbits2lg(e);
209 27199299 : ly = lg(y); if (lz > ly) lz = ly;
210 138312536 : z = cgetg(lz, t_REAL); while (--lz) z[lz] = y[lz];
211 27199299 : setsigne(z,sy); return z;
212 : }
213 :
214 4972268655 : if (e < 0) { swap(x,y); lswap(sx,sy); ey=ex; e=-e; }
215 : /* now ey >= ex */
216 4972268655 : lx = lg(x);
217 4972268655 : 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 4972268655 : extend = 0;
222 4972268655 : if (e)
223 : {
224 4302314648 : long d = dvmdsBIL(e, &m), l = ly-d;
225 4302314648 : if (l <= 2) return rcopy_sign(y, sy);
226 4248644455 : if (l > lx) { lz = lx + d + 1; extend = 1; }
227 3416781590 : else { lz = ly; lx = l; }
228 4248644455 : if (m)
229 : { /* shift x right m bits */
230 4239797519 : const pari_sp av = avma;
231 4239797519 : const ulong sh = BITS_IN_LONG-m;
232 4239797519 : GEN p1 = x; x = new_chunk(lx + lz + 1);
233 4239797512 : shift_right(x,p1,2,lx, 0,m);
234 4239797512 : if (extend) uel(x,lx) = uel(p1,lx-1) << sh;
235 4239797512 : set_avma(av); /* HACK: cgetg(lz, t_REAL) will not overwrite x */
236 : }
237 : }
238 : else
239 : { /* d = 0 */
240 669954007 : m = 0;
241 669954007 : if (lx > ly) lx = ly;
242 669954007 : lz = lx;
243 : }
244 :
245 4918598455 : if (sx == sy)
246 : { /* addition */
247 2576875803 : i = lz-1;
248 2576875803 : j = lx-1;
249 2576875803 : if (extend) {
250 509909905 : ulong garde = addll(x[lx], y[i]);
251 509909905 : if (m < 4) /* don't extend for few correct bits */
252 57562932 : z = cgetg(--lz, t_REAL);
253 : else
254 : {
255 452346973 : z = cgetg(lz, t_REAL);
256 452346973 : z[i] = garde;
257 : }
258 : }
259 : else
260 : {
261 2066965898 : z = cgetg(lz, t_REAL);
262 2066965898 : z[i] = addll(x[j], y[i]); j--;
263 : }
264 2576875803 : i--;
265 13993181914 : for (; j>=2; i--,j--) z[i] = addllx(x[j],y[i]);
266 2576875803 : if (overflow)
267 : {
268 606458263 : z[1] = 1; /* stops since z[1] != 0 */
269 607726613 : for (;;) { z[i] = uel(y,i)+1; if (z[i--]) break; }
270 606458263 : if (i <= 0)
271 : {
272 602379588 : shift_right(z,z, 2,lz, 1,1);
273 602379588 : z[1] = evalsigne(sx) | evalexpo(ey+1); return z;
274 : }
275 : }
276 2495865590 : for (; i>=2; i--) z[i] = y[i];
277 1974496215 : z[1] = evalsigne(sx) | evalexpo(ey); return z;
278 : }
279 :
280 : /* subtraction */
281 2341722652 : if (e) f2 = 1;
282 : else
283 : {
284 612781561 : i = 2; while (i < lx && x[i] == y[i]) i++;
285 374135887 : if (i==lx) return real_0_bit(ey+1 - lg2prec(lx));
286 348962960 : f2 = (uel(y,i) > uel(x,i));
287 : }
288 : /* result is nonzero. f2 = (y > x) */
289 2316549725 : i = lz-1; z = cgetg(lz, t_REAL);
290 2316549725 : if (f2)
291 : {
292 2159570708 : j = lx-1;
293 2159570708 : if (extend) z[i] = subll(y[i], x[lx]);
294 1837617755 : else z[i] = subll(y[i], x[j--]);
295 13282006559 : for (i--; j>=2; i--) z[i] = subllx(y[i], x[j--]);
296 2159570708 : if (overflow) /* stops since y[1] != 0 */
297 63630235 : for (;;) { z[i] = uel(y,i)-1; if (y[i--]) break; }
298 2536071017 : for (; i>=2; i--) z[i] = y[i];
299 2159570708 : sx = sy;
300 : }
301 : else
302 : {
303 156979017 : if (extend) z[i] = subll(x[lx], y[i]);
304 156979017 : else z[i] = subll(x[i], y[i]);
305 824502896 : for (i--; i>=2; i--) z[i] = subllx(x[i], y[i]);
306 : }
307 :
308 2472267417 : x = z+2; i = 0; while (!x[i]) i++;
309 2316549725 : lz -= i; z += i;
310 2316549725 : j = bfffo(z[2]); /* need to shift left by j bits to normalize mantissa */
311 2316549725 : ez = ey - (j | (i * BITS_IN_LONG));
312 2316549725 : 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 321952953 : if (m - j < 5 && lz > 3)
316 172028011 : { /* shorten z */
317 172028011 : 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 172028011 : if (j == 0)
322 : /* stackdummy((pari_sp)(z + lz+1), (pari_sp)(z + lz)); */
323 14589431 : z[lz] = evaltyp(t_VECSMALL) | _evallg(1);
324 : else
325 : {
326 157438580 : GEN t = z;
327 157438580 : z++; shift_left(z,t,2,lz-1, last,j);
328 : }
329 172028011 : if ((last<<j) & HIGHBIT)
330 : { /* round up */
331 62391750 : i = lz-1;
332 62853984 : while (++((ulong*)z)[i] == 0 && i > 1) i--;
333 62391750 : if (i == 1) { ez++; z[2] = (long)HIGHBIT; }
334 : }
335 : }
336 149924942 : else if (j) shift_left(z,z,2,lz-1, 0,j);
337 : }
338 1994596772 : else if (j) shift_left(z,z,2,lz-1, 0,j);
339 2316549725 : z[1] = evalsigne(sx) | evalexpo(ez);
340 2316549725 : z[0] = evaltyp(t_REAL) | evallg(lz);
341 2316549725 : set_avma((pari_sp)z); return z;
342 : }
|