1 /* mpz_add -- Add two integers.
3 Copyright (C) 1991 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 The GNU MP Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the GNU MP Library; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 mpz_add (MP_INT *sum, const MP_INT *u, const MP_INT *v)
34 #else /* BERKELEY_MP */
37 madd (const MP_INT *u, const MP_INT *v, MP_INT *sum)
44 #endif /* BERKELEY_MP */
48 mp_size usize, vsize, sumsize;
54 abs_usize = ABS (usize);
55 abs_vsize = ABS (vsize);
57 if (abs_usize < abs_vsize)
60 {const MP_INT *t = u; u = v; v = t;}
61 {mp_size t = usize; usize = vsize; vsize = t;}
62 {mp_size t = abs_usize; abs_usize = abs_vsize; abs_vsize = t;}
65 /* True: abs(USIZE) >= abs(VSIZE) */
67 /* If not space for sum (and possible carry), increase space. */
68 sumsize = abs_usize + 1;
69 if (sum->alloc < sumsize)
70 _mpz_realloc (sum, sumsize);
72 /* These must be after realloc (u or v may be the same as sum). */
81 sumsize = mpn_add (sump, up, abs_usize, vp, abs_vsize);
84 sumsize = sumsize + abs_usize;
88 /* The signs are different. Need exact comparision to determine
89 which operand to subtract from which. */
90 if (abs_usize == abs_vsize && mpn_cmp (up, vp, abs_usize) < 0)
92 + mpn_sub (sump, vp, abs_usize, up, abs_usize));
95 + mpn_sub (sump, up, abs_usize, vp, abs_vsize));
102 /* The signs are different. Need exact comparision to determine
103 which operand to subtract from which. */
104 if (abs_usize == abs_vsize && mpn_cmp (up, vp, abs_usize) < 0)
106 + mpn_sub (sump, vp, abs_usize, up, abs_usize));
108 sumsize = -(abs_usize
109 + mpn_sub (sump, up, abs_usize, vp, abs_vsize));
113 sumsize = mpn_add (sump, up, abs_usize, vp, abs_vsize);
116 sumsize = -(sumsize + abs_usize);