[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / gmp / mpz_sub.c
1 /* mpz_sub -- Subtract two integers.
2
3 Copyright (C) 1991 Free Software Foundation, Inc.
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
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)
10 any later version.
11
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.
16
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.  */
20
21 #include "gmp.h"
22 #include "gmp-impl.h"
23
24 #ifndef BERKELEY_MP
25 void
26 #ifdef __STDC__
27 mpz_sub (MP_INT *w, const MP_INT *u, const MP_INT *v)
28 #else
29 mpz_sub (w, u, v)
30      MP_INT *w;
31      const MP_INT *u;
32      const MP_INT *v;
33 #endif
34 #else /* BERKELEY_MP */
35 void
36 #ifdef __STDC__
37 msub (const MP_INT *u, const MP_INT *v, MP_INT *w)
38 #else
39 msub (u, v, w)
40      const MP_INT *u;
41      const MP_INT *v;
42      MP_INT *w;
43 #endif
44 #endif /* BERKELEY_MP */
45 {
46   mp_srcptr up, vp;
47   mp_ptr wp;
48   mp_size usize, vsize, wsize;
49   mp_size abs_usize;
50   mp_size abs_vsize;
51
52   usize = u->size;
53   vsize = -v->size;             /* The "-" makes the difference from mpz_add */
54   abs_usize = ABS (usize);
55   abs_vsize = ABS (vsize);
56
57   if (abs_usize < abs_vsize)
58     {
59       /* Swap U and V. */
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;}
63     }
64
65   /* True: abs(USIZE) >= abs(VSIZE) */
66
67   /* If not space for sum (and possible carry), increase space.  */
68   wsize = abs_usize + 1;
69   if (w->alloc < wsize)
70     _mpz_realloc (w, wsize);
71
72   /* These must be after realloc (u or v may be the same as w).  */
73   up = u->d;
74   vp = v->d;
75   wp = w->d;
76
77   if (usize >= 0)
78     {
79       if (vsize >= 0)
80         {
81           wsize = mpn_add (wp, up, abs_usize, vp, abs_vsize);
82           if (wsize != 0)
83             wp[abs_usize] = 1;
84           wsize = wsize + abs_usize;
85         }
86       else
87         {
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));
92           else
93             wsize = abs_usize + mpn_sub (wp, up, abs_usize, vp, abs_vsize);
94         }
95     }
96   else
97     {
98       if (vsize >= 0)
99         {
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);
104           else
105             wsize = -(abs_usize + mpn_sub (wp, up, abs_usize, vp, abs_vsize));
106         }
107       else
108         {
109           wsize = mpn_add (wp, up, abs_usize, vp, abs_vsize);
110           if (wsize != 0)
111             wp[abs_usize] = 1;
112           wsize = -(wsize + abs_usize);
113         }
114     }
115
116   w->size = wsize;
117 }