1 /* mpz_mod_2exp -- divide a MP_INT by 2**n and produce a remainder.
3 Copyright (C) 1991 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 the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 The GNU MP Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the GNU MP Library; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 mpz_mod_2exp (MP_INT *res, const MP_INT *in, unsigned long int cnt)
28 mpz_mod_2exp (res, in, cnt)
31 unsigned long int cnt;
34 mp_size in_size = ABS (in->size);
36 mp_size limb_cnt = cnt / BITS_PER_MP_LIMB;
37 mp_srcptr in_ptr = in->d;
39 if (in_size > limb_cnt)
41 /* The input operand is (probably) greater than 2**CNT. */
44 x = in_ptr[limb_cnt] & (((mp_limb) 1 << cnt % BITS_PER_MP_LIMB) - 1);
47 res_size = limb_cnt + 1;
48 if (res->alloc < res_size)
49 _mpz_realloc (res, res_size);
57 for (i = limb_cnt - 1; i >= 0; i--)
62 if (res->alloc < res_size)
63 _mpz_realloc (res, res_size);
70 /* The input operand is smaller than 2**CNT. We perform a no-op,
71 apart from that we might need to copy IN to RES. */
73 if (res->alloc < res_size)
74 _mpz_realloc (res, res_size);
80 MPN_COPY (res->d, in->d, limb_cnt);
81 res->size = (in->size >= 0) ? res_size : -res_size;