1 /* mpz_add -- Add two integers.
3 Copyright (C) 1991, 1993, 1994, 1996 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 Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
28 mpz_add (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
35 #else /* BERKELEY_MP */
38 madd (mpz_srcptr u, mpz_srcptr v, mpz_ptr w)
45 #endif /* BERKELEY_MP */
49 mp_size_t usize, vsize, wsize;
55 abs_usize = ABS (usize);
56 abs_vsize = ABS (vsize);
58 if (abs_usize < abs_vsize)
61 {const __mpz_struct *t = u; u = v; v = t;}
62 {mp_size_t t = usize; usize = vsize; vsize = t;}
63 {mp_size_t t = abs_usize; abs_usize = abs_vsize; abs_vsize = t;}
66 /* True: ABS_USIZE >= ABS_VSIZE. */
68 /* If not space for w (and possible carry), increase space. */
69 wsize = abs_usize + 1;
70 if (w->_mp_alloc < wsize)
71 _mpz_realloc (w, wsize);
73 /* These must be after realloc (u or v may be the same as w). */
78 if ((usize ^ vsize) < 0)
80 /* U and V have different sign. Need to compare them to determine
81 which operand to subtract from which. */
83 /* This test is right since ABS_USIZE >= ABS_VSIZE. */
84 if (abs_usize != abs_vsize)
86 mpn_sub (wp, up, abs_usize, vp, abs_vsize);
88 MPN_NORMALIZE (wp, wsize);
92 else if (mpn_cmp (up, vp, abs_usize) < 0)
94 mpn_sub_n (wp, vp, up, abs_usize);
96 MPN_NORMALIZE (wp, wsize);
102 mpn_sub_n (wp, up, vp, abs_usize);
104 MPN_NORMALIZE (wp, wsize);
111 /* U and V have same sign. Add them. */
112 mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
113 wp[abs_usize] = cy_limb;
114 wsize = abs_usize + cy_limb;