1 /* mpz_sub -- Subtract 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_sub (MP_INT *w, const MP_INT *u, const MP_INT *v)
34 #else /* BERKELEY_MP */
37 msub (const MP_INT *u, const MP_INT *v, MP_INT *w)
44 #endif /* BERKELEY_MP */
48 mp_size usize, vsize, wsize;
53 vsize = -v->size; /* The "-" makes the difference from mpz_add */
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 wsize = abs_usize + 1;
70 _mpz_realloc (w, wsize);
72 /* These must be after realloc (u or v may be the same as w). */
81 wsize = mpn_add (wp, up, abs_usize, vp, abs_vsize);
84 wsize = wsize + 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)
91 wsize = -(abs_usize + mpn_sub (wp, vp, abs_usize, up, abs_usize));
93 wsize = abs_usize + mpn_sub (wp, up, abs_usize, vp, abs_vsize);
100 /* The signs are different. Need exact comparision to determine
101 which operand to subtract from which. */
102 if (abs_usize == abs_vsize && mpn_cmp (up, vp, abs_usize) < 0)
103 wsize = abs_usize + mpn_sub (wp, vp, abs_usize, up, abs_usize);
105 wsize = -(abs_usize + mpn_sub (wp, up, abs_usize, vp, abs_vsize));
109 wsize = mpn_add (wp, up, abs_usize, vp, abs_vsize);
112 wsize = -(wsize + abs_usize);