beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / aorsmul.c
blobbf012c965ce71c557024cd278a4d43a5ef845098
1 /* mpz_addmul, mpz_submul -- add or subtract multiple.
3 Copyright 2001, 2004, 2005, 2012 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 either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
35 /* expecting x and y both with non-zero high limbs */
36 #define mpn_cmp_twosizes_lt(xp,xsize, yp,ysize) \
37 ((xsize) < (ysize) \
38 || ((xsize) == (ysize) && mpn_cmp (xp, yp, xsize) < 0))
41 /* sub>=0 means an addmul w += x*y, sub<0 means a submul w -= x*y.
43 The signs of w, x and y are fully accounted for by each flipping "sub".
45 The sign of w is retained for the result, unless the absolute value
46 submul underflows, in which case it flips. */
48 static void __gmpz_aorsmul (REGPARM_3_1 (mpz_ptr w, mpz_srcptr x, mpz_srcptr y, mp_size_t sub)) REGPARM_ATTR (1);
49 #define mpz_aorsmul(w,x,y,sub) __gmpz_aorsmul (REGPARM_3_1 (w, x, y, sub))
51 REGPARM_ATTR (1) static void
52 mpz_aorsmul (mpz_ptr w, mpz_srcptr x, mpz_srcptr y, mp_size_t sub)
54 mp_size_t xsize, ysize, tsize, wsize, wsize_signed;
55 mp_ptr wp, tp;
56 mp_limb_t c, high;
57 TMP_DECL;
59 /* w unaffected if x==0 or y==0 */
60 xsize = SIZ(x);
61 ysize = SIZ(y);
62 if (xsize == 0 || ysize == 0)
63 return;
65 /* make x the bigger of the two */
66 if (ABS(ysize) > ABS(xsize))
68 MPZ_SRCPTR_SWAP (x, y);
69 MP_SIZE_T_SWAP (xsize, ysize);
72 sub ^= ysize;
73 ysize = ABS(ysize);
75 /* use mpn_addmul_1/mpn_submul_1 if possible */
76 if (ysize == 1)
78 mpz_aorsmul_1 (w, x, PTR(y)[0], sub);
79 return;
82 sub ^= xsize;
83 xsize = ABS(xsize);
85 wsize_signed = SIZ(w);
86 sub ^= wsize_signed;
87 wsize = ABS(wsize_signed);
89 tsize = xsize + ysize;
90 wp = MPZ_REALLOC (w, MAX (wsize, tsize) + 1);
92 if (wsize_signed == 0)
94 /* Nothing to add to, just set w=x*y. No w==x or w==y overlap here,
95 since we know x,y!=0 but w==0. */
96 high = mpn_mul (wp, PTR(x),xsize, PTR(y),ysize);
97 tsize -= (high == 0);
98 SIZ(w) = (sub >= 0 ? tsize : -tsize);
99 return;
102 TMP_MARK;
103 tp = TMP_ALLOC_LIMBS (tsize);
105 high = mpn_mul (tp, PTR(x),xsize, PTR(y),ysize);
106 tsize -= (high == 0);
107 ASSERT (tp[tsize-1] != 0);
108 if (sub >= 0)
110 mp_srcptr up = wp;
111 mp_size_t usize = wsize;
113 if (usize < tsize)
115 up = tp;
116 usize = tsize;
117 tp = wp;
118 tsize = wsize;
120 wsize = usize;
123 c = mpn_add (wp, up,usize, tp,tsize);
124 wp[wsize] = c;
125 wsize += (c != 0);
127 else
129 mp_srcptr up = wp;
130 mp_size_t usize = wsize;
132 if (mpn_cmp_twosizes_lt (up,usize, tp,tsize))
134 up = tp;
135 usize = tsize;
136 tp = wp;
137 tsize = wsize;
139 wsize = usize;
140 wsize_signed = -wsize_signed;
143 ASSERT_NOCARRY (mpn_sub (wp, up,usize, tp,tsize));
144 wsize = usize;
145 MPN_NORMALIZE (wp, wsize);
148 SIZ(w) = (wsize_signed >= 0 ? wsize : -wsize);
150 TMP_FREE;
154 void
155 mpz_addmul (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
157 mpz_aorsmul (w, u, v, (mp_size_t) 0);
160 void
161 mpz_submul (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
163 mpz_aorsmul (w, u, v, (mp_size_t) -1);