Annotation of OpenXM_contrib/gmp/mpz/sub.c, Revision 1.1.1.2
1.1 maekawa 1: /* mpz_sub -- Subtract two integers.
2:
1.1.1.2 ! maekawa 3: Copyright (C) 1991, 1993, 1994, 1996, 2000 Free Software Foundation, Inc.
1.1 maekawa 4:
5: This file is part of the GNU MP Library.
6:
7: The GNU MP Library is free software; you can redistribute it and/or modify
1.1.1.2 ! maekawa 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
1.1 maekawa 10: option) any later version.
11:
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
1.1.1.2 ! maekawa 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1.1 maekawa 15: License for more details.
16:
1.1.1.2 ! maekawa 17: You should have received a copy of the GNU Lesser General Public License
1.1 maekawa 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. */
21:
22: #include "gmp.h"
23: #include "gmp-impl.h"
1.1.1.2 ! maekawa 24: #ifdef BERKELEY_MP
! 25: #include "mp.h"
! 26: #endif
1.1 maekawa 27:
28: #ifndef BERKELEY_MP
29: void
30: #if __STDC__
31: mpz_sub (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
32: #else
33: mpz_sub (w, u, v)
34: mpz_ptr w;
35: mpz_srcptr u;
36: mpz_srcptr v;
37: #endif
38: #else /* BERKELEY_MP */
39: void
40: #if __STDC__
41: msub (mpz_srcptr u, mpz_srcptr v, mpz_ptr w)
42: #else
43: msub (u, v, w)
44: mpz_srcptr u;
45: mpz_srcptr v;
46: mpz_ptr w;
47: #endif
48: #endif /* BERKELEY_MP */
49: {
50: mp_srcptr up, vp;
51: mp_ptr wp;
52: mp_size_t usize, vsize, wsize;
53: mp_size_t abs_usize;
54: mp_size_t abs_vsize;
55:
56: usize = u->_mp_size;
57: vsize = -v->_mp_size; /* The "-" makes the difference from mpz_add */
58: abs_usize = ABS (usize);
59: abs_vsize = ABS (vsize);
60:
61: if (abs_usize < abs_vsize)
62: {
63: /* Swap U and V. */
1.1.1.2 ! maekawa 64: MPZ_SRCPTR_SWAP (u, v);
! 65: MP_SIZE_T_SWAP (usize, vsize);
! 66: MP_SIZE_T_SWAP (abs_usize, abs_vsize);
1.1 maekawa 67: }
68:
69: /* True: ABS_USIZE >= ABS_VSIZE. */
70:
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);
75:
76: /* These must be after realloc (u or v may be the same as w). */
77: up = u->_mp_d;
78: vp = v->_mp_d;
79: wp = w->_mp_d;
80:
81: if ((usize ^ vsize) < 0)
82: {
83: /* U and V have different sign. Need to compare them to determine
84: which operand to subtract from which. */
85:
86: /* This test is right since ABS_USIZE >= ABS_VSIZE. */
87: if (abs_usize != abs_vsize)
88: {
89: mpn_sub (wp, up, abs_usize, vp, abs_vsize);
90: wsize = abs_usize;
91: MPN_NORMALIZE (wp, wsize);
92: if (usize < 0)
93: wsize = -wsize;
94: }
95: else if (mpn_cmp (up, vp, abs_usize) < 0)
96: {
97: mpn_sub_n (wp, vp, up, abs_usize);
98: wsize = abs_usize;
99: MPN_NORMALIZE (wp, wsize);
100: if (usize >= 0)
101: wsize = -wsize;
102: }
103: else
104: {
105: mpn_sub_n (wp, up, vp, abs_usize);
106: wsize = abs_usize;
107: MPN_NORMALIZE (wp, wsize);
108: if (usize < 0)
109: wsize = -wsize;
110: }
111: }
112: else
113: {
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;
118: if (usize < 0)
119: wsize = -wsize;
120: }
121:
122: w->_mp_size = wsize;
123: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>