1 /* mpz_fdiv_q_2exp -- Divide an integer by 2**CNT. Round the quotient
4 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
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 Library General Public
16 License for more details.
18 You should have received a copy of the GNU Library General Public License
19 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
28 mpz_fdiv_q_2exp (mpz_ptr w, mpz_srcptr u, unsigned long int cnt)
30 mpz_fdiv_q_2exp (w, u, cnt)
33 unsigned long int cnt;
36 mp_size_t usize = u->_mp_size;
38 mp_size_t abs_usize = ABS (usize);
43 limb_cnt = cnt / BITS_PER_MP_LIMB;
44 wsize = abs_usize - limb_cnt;
49 /* Set ROUND since we know we skip some non-zero words in this case.
50 Well, if U is zero, we don't, but then this will be taken care of
51 below, since rounding only really takes place for negative U. */
54 w->_mp_size = -(usize < 0);
62 /* Make sure there is enough space. We make an extra limb
63 here to account for possible rounding at the end. */
64 if (w->_mp_alloc < wsize + 1)
65 _mpz_realloc (w, wsize + 1);
70 /* Set ROUND if we are about skip some non-zero limbs. */
71 for (i = 0; i < limb_cnt && round == 0; i++)
74 cnt %= BITS_PER_MP_LIMB;
77 round |= mpn_rshift (wp, up + limb_cnt, wsize, cnt);
78 wsize -= wp[wsize - 1] == 0;
82 MPN_COPY_INCR (wp, up + limb_cnt, wsize);
86 if (usize < 0 && round != 0)
89 cy = mpn_add_1 (wp, wp, wsize, 1);
93 w->_mp_size = usize >= 0 ? wsize : -wsize;