1 /* mpz_add, mpz_sub -- add or subtract integers.
3 Copyright 1991, 1993, 1994, 1996, 2000, 2001 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 Lesser General Public License as published by
9 the Free Software Foundation; either version 3 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 Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
35 #define ARGUMENTS mpz_srcptr u, mpz_srcptr v, mpz_ptr w
37 #else /* normal GMP */
40 #define FUNCTION mpz_add
44 #define FUNCTION mpz_sub
47 #define ARGUMENTS mpz_ptr w, mpz_srcptr u, mpz_srcptr v
52 Error, need OPERATION_add or OPERATION_sub
61 mp_size_t usize, vsize, wsize;
66 vsize = VARIATION v->_mp_size;
67 abs_usize = ABS (usize);
68 abs_vsize = ABS (vsize);
70 if (abs_usize < abs_vsize)
73 MPZ_SRCPTR_SWAP (u, v);
74 MP_SIZE_T_SWAP (usize, vsize);
75 MP_SIZE_T_SWAP (abs_usize, abs_vsize);
78 /* True: ABS_USIZE >= ABS_VSIZE. */
80 /* If not space for w (and possible carry), increase space. */
81 wsize = abs_usize + 1;
82 if (w->_mp_alloc < wsize)
83 _mpz_realloc (w, wsize);
85 /* These must be after realloc (u or v may be the same as w). */
90 if ((usize ^ vsize) < 0)
92 /* U and V have different sign. Need to compare them to determine
93 which operand to subtract from which. */
95 /* This test is right since ABS_USIZE >= ABS_VSIZE. */
96 if (abs_usize != abs_vsize)
98 mpn_sub (wp, up, abs_usize, vp, abs_vsize);
100 MPN_NORMALIZE (wp, wsize);
104 else if (mpn_cmp (up, vp, abs_usize) < 0)
106 mpn_sub_n (wp, vp, up, abs_usize);
108 MPN_NORMALIZE (wp, wsize);
114 mpn_sub_n (wp, up, vp, abs_usize);
116 MPN_NORMALIZE (wp, wsize);
123 /* U and V have same sign. Add them. */
124 mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
125 wp[abs_usize] = cy_limb;
126 wsize = abs_usize + cy_limb;