xref: /dragonfly/contrib/gmp/mpz/mul_2exp.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /* mpz_mul_2exp -- Multiply a bignum by 2**CNT
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino Copyright 1991, 1993, 1994, 1996, 2001, 2002 Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino 
5*86d7f5d3SJohn Marino This file is part of the GNU MP Library.
6*86d7f5d3SJohn Marino 
7*86d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
8*86d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
9*86d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
10*86d7f5d3SJohn Marino option) any later version.
11*86d7f5d3SJohn Marino 
12*86d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
13*86d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14*86d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15*86d7f5d3SJohn Marino License for more details.
16*86d7f5d3SJohn Marino 
17*86d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
18*86d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19*86d7f5d3SJohn Marino 
20*86d7f5d3SJohn Marino #include "gmp.h"
21*86d7f5d3SJohn Marino #include "gmp-impl.h"
22*86d7f5d3SJohn Marino 
23*86d7f5d3SJohn Marino void
mpz_mul_2exp(mpz_ptr w,mpz_srcptr u,mp_bitcnt_t cnt)24*86d7f5d3SJohn Marino mpz_mul_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
25*86d7f5d3SJohn Marino {
26*86d7f5d3SJohn Marino   mp_size_t usize = u->_mp_size;
27*86d7f5d3SJohn Marino   mp_size_t abs_usize = ABS (usize);
28*86d7f5d3SJohn Marino   mp_size_t wsize;
29*86d7f5d3SJohn Marino   mp_size_t limb_cnt;
30*86d7f5d3SJohn Marino   mp_ptr wp;
31*86d7f5d3SJohn Marino   mp_limb_t wlimb;
32*86d7f5d3SJohn Marino 
33*86d7f5d3SJohn Marino   if (usize == 0)
34*86d7f5d3SJohn Marino     {
35*86d7f5d3SJohn Marino       w->_mp_size = 0;
36*86d7f5d3SJohn Marino       return;
37*86d7f5d3SJohn Marino     }
38*86d7f5d3SJohn Marino 
39*86d7f5d3SJohn Marino   limb_cnt = cnt / GMP_NUMB_BITS;
40*86d7f5d3SJohn Marino   wsize = abs_usize + limb_cnt + 1;
41*86d7f5d3SJohn Marino   if (w->_mp_alloc < wsize)
42*86d7f5d3SJohn Marino     _mpz_realloc (w, wsize);
43*86d7f5d3SJohn Marino 
44*86d7f5d3SJohn Marino   wp = w->_mp_d;
45*86d7f5d3SJohn Marino   wsize = abs_usize + limb_cnt;
46*86d7f5d3SJohn Marino 
47*86d7f5d3SJohn Marino   cnt %= GMP_NUMB_BITS;
48*86d7f5d3SJohn Marino   if (cnt != 0)
49*86d7f5d3SJohn Marino     {
50*86d7f5d3SJohn Marino       wlimb = mpn_lshift (wp + limb_cnt, u->_mp_d, abs_usize, cnt);
51*86d7f5d3SJohn Marino       if (wlimb != 0)
52*86d7f5d3SJohn Marino 	{
53*86d7f5d3SJohn Marino 	  wp[wsize] = wlimb;
54*86d7f5d3SJohn Marino 	  wsize++;
55*86d7f5d3SJohn Marino 	}
56*86d7f5d3SJohn Marino     }
57*86d7f5d3SJohn Marino   else
58*86d7f5d3SJohn Marino     {
59*86d7f5d3SJohn Marino       MPN_COPY_DECR (wp + limb_cnt, u->_mp_d, abs_usize);
60*86d7f5d3SJohn Marino     }
61*86d7f5d3SJohn Marino 
62*86d7f5d3SJohn Marino   /* Zero all whole limbs at low end.  Do it here and not before calling
63*86d7f5d3SJohn Marino      mpn_lshift, not to lose for U == W.  */
64*86d7f5d3SJohn Marino   MPN_ZERO (wp, limb_cnt);
65*86d7f5d3SJohn Marino 
66*86d7f5d3SJohn Marino   w->_mp_size = usize >= 0 ? wsize : -wsize;
67*86d7f5d3SJohn Marino }
68