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
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
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/. */
35 /* expecting x and y both with non-zero high limbs */
36 #define mpn_cmp_twosizes_lt(xp,xsize, yp,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
;
59 /* w unaffected if x==0 or y==0 */
62 if (xsize
== 0 || ysize
== 0)
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
);
75 /* use mpn_addmul_1/mpn_submul_1 if possible */
78 mpz_aorsmul_1 (w
, x
, PTR(y
)[0], sub
);
85 wsize_signed
= SIZ(w
);
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
);
98 SIZ(w
) = (sub
>= 0 ? tsize
: -tsize
);
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);
111 mp_size_t usize
= wsize
;
123 c
= mpn_add (wp
, up
,usize
, tp
,tsize
);
130 mp_size_t usize
= wsize
;
132 if (mpn_cmp_twosizes_lt (up
,usize
, tp
,tsize
))
140 wsize_signed
= -wsize_signed
;
143 ASSERT_NOCARRY (mpn_sub (wp
, up
,usize
, tp
,tsize
));
145 MPN_NORMALIZE (wp
, wsize
);
148 SIZ(w
) = (wsize_signed
>= 0 ? wsize
: -wsize
);
155 mpz_addmul (mpz_ptr w
, mpz_srcptr u
, mpz_srcptr v
)
157 mpz_aorsmul (w
, u
, v
, (mp_size_t
) 0);
161 mpz_submul (mpz_ptr w
, mpz_srcptr u
, mpz_srcptr v
)
163 mpz_aorsmul (w
, u
, v
, (mp_size_t
) -1);