xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/aors.h (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /* mpz_add, mpz_sub -- add or subtract integers.
2 
3 Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2011, 2012 Free Software
4 Foundation, Inc.
5 
6 This file is part of the GNU MP Library.
7 
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
20 
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 
24 
25 #ifdef OPERATION_add
26 #define FUNCTION     mpz_add
27 #define VARIATION
28 #endif
29 #ifdef OPERATION_sub
30 #define FUNCTION     mpz_sub
31 #define VARIATION    -
32 #endif
33 
34 #ifndef FUNCTION
35 Error, need OPERATION_add or OPERATION_sub
36 #endif
37 
38 
39 void
40 FUNCTION (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
41 {
42   mp_srcptr up, vp;
43   mp_ptr wp;
44   mp_size_t usize, vsize, wsize;
45   mp_size_t abs_usize;
46   mp_size_t abs_vsize;
47 
48   usize = SIZ(u);
49   vsize = VARIATION SIZ(v);
50   abs_usize = ABS (usize);
51   abs_vsize = ABS (vsize);
52 
53   if (abs_usize < abs_vsize)
54     {
55       /* Swap U and V. */
56       MPZ_SRCPTR_SWAP (u, v);
57       MP_SIZE_T_SWAP (usize, vsize);
58       MP_SIZE_T_SWAP (abs_usize, abs_vsize);
59     }
60 
61   /* True: ABS_USIZE >= ABS_VSIZE.  */
62 
63   /* If not space for w (and possible carry), increase space.  */
64   wsize = abs_usize + 1;
65   wp = MPZ_REALLOC (w, wsize);
66 
67   /* These must be after realloc (u or v may be the same as w).  */
68   up = PTR(u);
69   vp = PTR(v);
70 
71   if ((usize ^ vsize) < 0)
72     {
73       /* U and V have different sign.  Need to compare them to determine
74 	 which operand to subtract from which.  */
75 
76       /* This test is right since ABS_USIZE >= ABS_VSIZE.  */
77       if (abs_usize != abs_vsize)
78 	{
79 	  mpn_sub (wp, up, abs_usize, vp, abs_vsize);
80 	  wsize = abs_usize;
81 	  MPN_NORMALIZE (wp, wsize);
82 	  if (usize < 0)
83 	    wsize = -wsize;
84 	}
85       else if (mpn_cmp (up, vp, abs_usize) < 0)
86 	{
87 	  mpn_sub_n (wp, vp, up, abs_usize);
88 	  wsize = abs_usize;
89 	  MPN_NORMALIZE (wp, wsize);
90 	  if (usize >= 0)
91 	    wsize = -wsize;
92 	}
93       else
94 	{
95 	  mpn_sub_n (wp, up, vp, abs_usize);
96 	  wsize = abs_usize;
97 	  MPN_NORMALIZE (wp, wsize);
98 	  if (usize < 0)
99 	    wsize = -wsize;
100 	}
101     }
102   else
103     {
104       /* U and V have same sign.  Add them.  */
105       mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
106       wp[abs_usize] = cy_limb;
107       wsize = abs_usize + cy_limb;
108       if (usize < 0)
109 	wsize = -wsize;
110     }
111 
112   SIZ(w) = wsize;
113 }
114