xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/mul_2exp.c (revision 49d8c9ecf4abd21261269266ef64939f71b3cd09)
1 /* mpz_mul_2exp -- Multiply a bignum by 2**CNT
2 
3 Copyright 1991, 1993, 1994, 1996, 2001, 2002, 2012 Free Software Foundation,
4 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 void
25 mpz_mul_2exp (mpz_ptr r, mpz_srcptr u, mp_bitcnt_t cnt)
26 {
27   mp_size_t un, rn;
28   mp_size_t limb_cnt;
29   mp_ptr rp;
30   mp_srcptr up;
31   mp_limb_t rlimb;
32 
33   un = ABSIZ (u);
34   limb_cnt = cnt / GMP_NUMB_BITS;
35   rn = un + limb_cnt;
36 
37   if (un == 0)
38     rn = 0;
39   else
40     {
41       rp = MPZ_REALLOC (r, rn + 1);
42       up = PTR(u);
43 
44       cnt %= GMP_NUMB_BITS;
45       if (cnt != 0)
46 	{
47 	  rlimb = mpn_lshift (rp + limb_cnt, up, un, cnt);
48 	  rp[rn] = rlimb;
49 	  rn += (rlimb != 0);
50 	}
51       else
52 	{
53 	  MPN_COPY_DECR (rp + limb_cnt, up, un);
54 	}
55 
56       /* Zero all whole limbs at low end.  Do it here and not before calling
57 	 mpn_lshift, not to lose for U == R.  */
58       MPN_ZERO (rp, limb_cnt);
59     }
60 
61   SIZ(r) = SIZ(u) >= 0 ? rn : -rn;
62 }
63