1 /* mpz_sub -- Subtract two integers.
3 Copyright (C) 1991, 1993, 1994, 1996, 2000 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 2.1 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; 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. */
31 mpz_sub (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
38 #else /* BERKELEY_MP */
41 msub (mpz_srcptr u, mpz_srcptr v, mpz_ptr w)
48 #endif /* BERKELEY_MP */
52 mp_size_t usize, vsize, wsize;
57 vsize = -v->_mp_size; /* The "-" makes the difference from mpz_add */
58 abs_usize = ABS (usize);
59 abs_vsize = ABS (vsize);
61 if (abs_usize < abs_vsize)
64 MPZ_SRCPTR_SWAP (u, v);
65 MP_SIZE_T_SWAP (usize, vsize);
66 MP_SIZE_T_SWAP (abs_usize, abs_vsize);
69 /* True: ABS_USIZE >= ABS_VSIZE. */
71 /* If not space for w (and possible carry), increase space. */
72 wsize = abs_usize + 1;
73 if (w->_mp_alloc < wsize)
74 _mpz_realloc (w, wsize);
76 /* These must be after realloc (u or v may be the same as w). */
81 if ((usize ^ vsize) < 0)
83 /* U and V have different sign. Need to compare them to determine
84 which operand to subtract from which. */
86 /* This test is right since ABS_USIZE >= ABS_VSIZE. */
87 if (abs_usize != abs_vsize)
89 mpn_sub (wp, up, abs_usize, vp, abs_vsize);
91 MPN_NORMALIZE (wp, wsize);
95 else if (mpn_cmp (up, vp, abs_usize) < 0)
97 mpn_sub_n (wp, vp, up, abs_usize);
99 MPN_NORMALIZE (wp, wsize);
105 mpn_sub_n (wp, up, vp, abs_usize);
107 MPN_NORMALIZE (wp, wsize);
114 /* U and V have same sign. Add them. */
115 mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
116 wp[abs_usize] = cy_limb;
117 wsize = abs_usize + cy_limb;